Sha256: 6c0a95edb1adb399ce4992da923257a49fd8087069e71f9603d9f177c6692261

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

require 'gherkin/parser'
require 'gherkin/token_matcher'
require 'gherkin/pickles/compiler'

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

      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

      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
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
honeybadger-4.5.3 vendor/bundle/ruby/2.6.0/gems/gherkin-5.1.0/lib/gherkin/stream/gherkin_events.rb
gherkin-5.1.0 lib/gherkin/stream/gherkin_events.rb
gherkin-5.0.0 lib/gherkin/stream/gherkin_events.rb