<commit_before># encoding: utf-8
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'gherkin/tools/line_filter_listener'
require 'gherkin/tools/pretty_listener'
require 'stringio'

module Gherkin
  module Tools
    describe LineFilterListener do
      
      class LineListener
        attr_reader :lines
        
        def method_missing(*sexp_args)
          @lines ||= []
          @lines << Sexp.new(sexp_args).line
        end
      end
      
      def verify_lines(expected_lines, lines)
        line_listener = LineListener.new
        scan(line_listener, lines)
        line_listener.lines.should == expected_lines
      end

      def verify_output(expected_output, lines)
        io = StringIO.new
        scan(PrettyListener.new(io, true), lines)
        io.rewind
        io.read.should == expected_output
      end

      def scan(listener, lines)
        line_filter_listener = LineFilterListener.new(listener, lines)
        parser = Gherkin::Parser.new(line_filter_listener, true, "root")
        lexer  = Gherkin::I18nLexer.new(parser, true)
        lexer.scan(@input)
      end

      context "Scenario" do
        before do
          @input = %{Feature: 1
  Scenario: 2
    Given 3

  Scenario: 5
    Given 6
}
        end

        it "should not replay anything if no lines match" do
          verify_lines([:eof], [90])
        end

        it "should replay identically (except newlines) when there is no filter" do
          verify_lines([1,2,3,5,6,:eof], [])
        end

        it "should filter on step line of first scenario" do
          verify_lines([1,2,3,:eof], [3])
        end

        it "should filter on step line of second scenario" do
          verify_lines([1,5,6,:eof], [6])
        end

        it "should replay identically (except newlines) when the filter matches both scenarios" do
          verify_lines([1,2,3,5,6,:eof], [3,6])
        end
      end
      
      context "Scenario with Table and Comment and Tag" do
        before do
          @input = %{#language:en
Feature: 2
  # 3
  Scenario: 4
    Given 5
    When 6

  @tag8
  Scenario: 9
    Given 10
    When 11
     | 12 | 12 |
     | 13 | 13 |
}
        end

        it "should replay identically when there is no filter" do
          verify_lines([1,2,3,4,5,6,8,9,10,11,12,13,:eof], [])
        end

        it "should filter on step line of first scenario" do
          verify_lines([1,2,3,4,5,6,:eof], [5])
        end

        it "should filter on scenario line of second scenario" do
          verify_lines([1,2,8,9,10,11,12,13,:eof], [9])
        end
        
        it "should return everything when a line is given in each scenario" do
          verify_lines([1,2,3,4,5,6,8,9,10,11,12,13,:eof], [6,9])
        end
      end
      
      context "Scenario with Background and Comment" do
        before do
          @input = %{#language:en
Feature: 2<commit_msg>Pending specs so I don't forget. Refactoring comes next.<commit_after># encoding: utf-8
require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper')
require 'gherkin/tools/line_filter_listener'
require 'gherkin/tools/pretty_listener'
require 'stringio'

module Gherkin
  module Tools
    describe LineFilterListener do
      
      class LineListener
        attr_reader :lines
        
        def method_missing(*sexp_args)
          @lines ||= []
          @lines << Sexp.new(sexp_args).line
        end
      end
      
      def verify_lines(expected_lines, lines)
        line_listener = LineListener.new
        scan(line_listener, lines)
        line_listener.lines.should == expected_lines
      end

      def verify_output(expected_output, lines)
        io = StringIO.new
        scan(PrettyListener.new(io, true), lines)
        io.rewind
        io.read.should == expected_output
      end

      def scan(listener, lines)
        line_filter_listener = LineFilterListener.new(listener, lines)
        parser = Gherkin::Parser.new(line_filter_listener, true, "root")
        lexer  = Gherkin::I18nLexer.new(parser, true)
        lexer.scan(@input)
      end

      context "Scenario" do
        before do
          @input = %{Feature: 1
  Scenario: 2
    Given 3

  Scenario: 5
    Given 6
}
        end

        it "should not replay anything if no lines match" do
          verify_lines([:eof], [90])
        end

        it "should replay identically (except newlines) when there is no filter" do
          verify_lines([1,2,3,5,6,:eof], [])
        end

        it "should filter on step line of first scenario" do
          verify_lines([1,2,3,:eof], [3])
        end

        it "should filter on step line of second scenario" do
          verify_lines([1,5,6,:eof], [6])
        end

        it "should replay identically (except newlines) when the filter matches both scenarios" do
          verify_lines([1,2,3,5,6,:eof], [3,6])
        end
      end
      
      context "Scenario with Table and Comment and Tag" do
        before do
          @input = %{#language:en
Feature: 2
  # 3
  Scenario: 4
    Given 5
    When 6

  @tag8
  Scenario: 9
    Given 10
    When 11
     | 12 | 12 |
     | 13 | 13 |
}
        end

        it "should replay identically when there is no filter" do
          verify_lines([1,2,3,4,5,6,8,9,10,11,12,13,:eof], [])
        end

        it "should filter on step line of first scenario" do
          verify_lines([1,2,3,4,5,6,:eof], [5])
        end

        it "should filter on scenario line of second scenario" do
          verify_lines([1,2,8,9,10,11,12,13,:eof], [9])
        end
        
        it "should return everything when a line is given in each scenario" do
          verify_lines([1,2,3,4,5,6,8,9,10,11,12,13,:eof], [6,9])
        end

        it "should return a scenario when a line is given for its tag" do
          pending
          verify_lines([1,2,8,9,10,11,12,13,:eof], [8])
        end

        it "should not return a scenario when a line is given for its comment" do
          pending
          verify_lines([:eof], [3])
        end
      end
      
      context "Scenario with Background and Comment" do
        before do
          @input = %{#language:en
Feature: 2
{'fraction_non_alphanumeric': 0.1009233759922242, 'fraction_numerical': 0.035963064960311035, 'mean_word_length': 2.0993975903614457, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5518856', 'n_tokens_mistral': 2052, 'n_tokens_neox': 1912, 'n_words': 556}