Class: Gherkin::Stream::GherkinEvents

Inherits:
Object
  • Object
show all
Defined in:
lib/gherkin/stream/gherkin_events.rb

Instance Method Summary collapse

Constructor Details

#initialize(options, language = 'en') ⇒ GherkinEvents

Returns a new instance of GherkinEvents.



8
9
10
11
12
13
# File 'lib/gherkin/stream/gherkin_events.rb', line 8

def initialize(options, language='en')
  @options = options
  @parser = Gherkin::Parser.new
  @compiler = Gherkin::Pickles::Compiler.new
  @language = language
end

Instance Method Details

#enum(source_event) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gherkin/stream/gherkin_events.rb', line 15

def enum(source_event)
  Enumerator.new do |y|
    uri = source_event[:uri]
    source = source_event[:data]
    begin
      gherkin_document = @parser.parse(source, TokenMatcher.new(@language))

      if (@options[:print_source])
        y.yield source_event
      end
      if (@options[:print_ast])
        y.yield({
          type: 'gherkin-document',
          uri: uri,
          document: gherkin_document
        })
      end
      if (@options[:print_pickles])
        pickles = @compiler.compile(gherkin_document)
        pickles.each do |pickle|
          y.yield({
            type: 'pickle',
            uri: uri,
            pickle: pickle
          })
        end
      end
    rescue Gherkin::CompositeParserException => e
      yield_errors(y, e.errors, uri)
    rescue Gherkin::ParserError => e
      yield_errors(y, [e], uri)
    end
  end
end

#yield_errors(y, errors, uri) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gherkin/stream/gherkin_events.rb', line 50

def yield_errors(y, errors, uri)
  errors.each do |error|
    y.yield({
      type: 'attachment',
      source: {
        uri: uri,
        start: {
          line: error.location[:line],
          column: error.location[:column]
        }
      },
      data: error.message,
      media: {
        encoding: 'utf-8',
        type: 'text/x.cucumber.stacktrace+plain'
      }
    })
  end
end