How to use parse method of Gherkin Package

Best Gherkin-ruby code snippet using Gherkin.parse

bench.rake

Source:bench.rake Github

copy

Full Screen

...68 69 def report_all 70 Benchmark.bmbm do |x|71 x.report("native_gherkin:") { run_native_gherkin }72 x.report("native_gherkin_no_parser:") { run_native_gherkin_no_parser }73 x.report("rb_gherkin:") { run_rb_gherkin }74 x.report("cucumber:") { run_cucumber }75 x.report("tt:") { run_tt }76 end77 end78 79 def run_cucumber80 require 'cucumber'81 require 'logger'82 step_mother = Cucumber::StepMother.new83 logger = Logger.new(STDOUT)84 logger.level = Logger::INFO85 step_mother.log = logger86 step_mother.load_plain_text_features(@features)87 end88 89 def run_tt90 require 'cucumber'91 # Using Cucumber's Treetop lexer, but never calling #build to build the AST92 lexer = Cucumber::Parser::NaturalLanguage.new(nil, 'en').parser93 @features.each do |file|94 source = IO.read(file)95 parse_tree = lexer.parse(source)96 if parse_tree.nil?97 raise Cucumber::Parser::SyntaxError.new(lexer, file, 0)98 end99 end100 end101 def run_rb_gherkin 102 require 'gherkin'103 require 'null_formatter'104 parser = Gherkin::Parser::Parser.new(NullFormatter.new, true, "root", true)105 @features.each do |feature|106 parser.parse(File.read(feature), feature, 0)107 end108 end109 def run_native_gherkin110 require 'gherkin'111 require 'null_listener'112 parser = Gherkin::Parser::Parser.new(NullFormatter.new, true, "root", false)113 @features.each do |feature|114 parser.parse(File.read(feature), feature, 0)115 end116 end117 def run_native_gherkin_no_parser118 require 'gherkin'119 require 'gherkin/lexer/i18n_lexer'120 require 'null_listener'121 lexer = Gherkin::Lexer::I18nLexer.new(NullListener.new, false)122 @features.each do |feature|123 lexer.scan(File.read(feature), feature, 0)124 end125 end126end127desc "Generate 500 random features and benchmark Cucumber, Treetop and Gherkin with them"128task :bench => ["bench:clean", "bench:gen"] do129 benchmarker = Benchmarker.new130 benchmarker.report_all131end132namespace :bench do133 desc "Generate [number] features with random content, or 500 features if number is not provided"134 task :gen, :number do |t, args|135 args.with_defaults(:number => 500)136 generator = RandomFeatureGenerator.new(args.number.to_i)137 generator.generate 138 end139 desc "Benchmark Cucumber AST building from the features in tasks/bench/generated"140 task :cucumber do141 benchmarker = Benchmarker.new142 benchmarker.report("cucumber")143 end144 145 desc "Benchmark the Treetop parser with the features in tasks/bench/generated"146 task :tt do147 benchmarker = Benchmarker.new148 benchmarker.report("tt")149 end150 desc "Benchmark the Ruby Gherkin lexer+parser with the features in tasks/bench/generated"151 task :rb_gherkin do152 benchmarker = Benchmarker.new153 benchmarker.report("rb_gherkin")154 end155 desc "Benchmark the ntive Gherkin lexer+parser with the features in tasks/bench/generated"156 task :native_gherkin do157 benchmarker = Benchmarker.new158 benchmarker.report("native_gherkin")159 end160 desc "Benchmark the native Gherkin lexer (no parser) with the features in tasks/bench/generated"161 task :native_gherkin_no_parser do162 benchmarker = Benchmarker.new163 benchmarker.report("native_gherkin_no_parser")164 end165 desc "Remove all generated features in tasks/bench/generated"166 task :clean do167 rm_f FileList[GENERATED_FEATURES + "/**/*feature"]168 end169end...

Full Screen

Full Screen

parser_spec.rb

Source:parser_spec.rb Github

copy

Full Screen

1# -*- encoding: utf-8 -*-2# frozen_string_literal: true3require 'cucumber/core/gherkin/parser'4require 'cucumber/core/gherkin/writer'5module Cucumber6 module Core7 module Gherkin8 describe Parser do9 let(:receiver) { double }10 let(:event_bus) { double }11 let(:gherkin_query) { double }12 let(:parser) { Parser.new(receiver, event_bus, gherkin_query) }13 let(:visitor) { double }14 before do15 allow( event_bus ).to receive(:gherkin_source_parsed)16 allow( event_bus).to receive(:envelope)17 allow( gherkin_query ).to receive(:update)18 end19 def parse20 parser.document(source)21 end22 context "for invalid gherkin" do23 let(:source) { Gherkin::Document.new(path, "\nnot gherkin\n\nFeature: \n") }24 let(:path) { 'path_to/the.feature' }25 it "raises an error" do26 expect { parse }.to raise_error(ParseError) do |error|27 expect( error.message ).to match(/not gherkin/)28 expect( error.message ).to match(/#{path}/)29 end30 end31 end32 context "for valid gherkin" do33 let(:source) { Gherkin::Document.new(path, 'Feature:') }34 let(:path) { 'path_to/the.feature' }35 it "issues a gherkin_source_parsed event" do36 expect( event_bus ).to receive(:gherkin_source_parsed)37 parse38 end39 it "emits an 'envelope' event for every message produced by Gherkin" do40 # Only one message emited, there's no pickles generated41 expect( event_bus ).to receive(:envelope).once42 parse43 end44 end45 context "for empty files" do46 let(:source) { Gherkin::Document.new(path, '') }47 let(:path) { 'path_to/the.feature' }48 it "passes on no pickles" do49 expect( receiver ).not_to receive(:pickle)50 parse51 end52 end53 include Writer54 def self.source(&block)55 let(:source) { gherkin(&block) }56 end57 RSpec::Matchers.define :pickle_with_language do |language|58 match { |actual| actual.language == language }59 end60 context "when the Gherkin has a language header" do61 source do62 feature(language: 'ja', keyword: '機能') do63 scenario(keyword: 'シナリオ')64 end65 end66 it "the pickles have the correct language" do67 expect( receiver ).to receive(:pickle).with(pickle_with_language('ja'))68 parse69 end70 end71 context "when the Gherkin produces one pickle" do72 source do73 feature do74 scenario do75 step 'text'76 end77 end78 end79 it "passes on the pickle" do80 expect( receiver ).to receive(:pickle)81 parse82 end83 it "emits an 'envelope' event containing the pickle" do84 allow( receiver ).to receive(:pickle)85 # Once for the gherkin document, once with the pickle86 expect( event_bus ).to receive(:envelope).twice87 parse88 end89 end90 context "when scenario is inside a rule" do91 source do92 feature do93 rule do94 scenario name: "My scenario"95 end96 end97 end98 it "passes on the pickle" do99 expect( receiver ).to receive(:pickle)100 parse101 end102 end103 context "when example is inside a rule" do104 source do105 feature do106 rule do107 example name: "My example"108 end109 end110 end111 it "passes on the pickle" do112 expect( receiver ).to receive(:pickle)113 parse114 end115 end116 context "when there are multiple rules and scenarios or examples" do117 source do118 feature do119 rule description: "First rule" do120 scenario name: "Do not talk about the fight club" do121 step 'text'122 end123 end124 rule description: "Second rule" do125 example name: "Do not talk about the fight club" do126 step 'text'127 end128 end129 end130 end131 it "passes on the pickles" do132 expect( receiver ).to receive(:pickle).twice133 parse134 end135 end136 end137 end138 end139end...

Full Screen

Full Screen

parser_message_stream.rb

Source:parser_message_stream.rb Github

copy

Full Screen

1require 'cucumber/messages'2require_relative '../parser'3require_relative '../token_matcher'4require_relative '../pickles/compiler'5module Gherkin6 module Stream7 class ParserMessageStream8 def initialize(paths, sources, options)9 @paths = paths10 @sources = sources11 @options = options12 id_generator = options[:id_generator] || Cucumber::Messages::IdGenerator::UUID.new13 @parser = Parser.new(AstBuilder.new(id_generator))14 @compiler = Pickles::Compiler.new(id_generator)15 end16 def messages17 enumerated = false18 Enumerator.new do |y|19 raise DoubleIterationException, "Messages have already been enumerated" if enumerated20 enumerated = true21 sources.each do |source|22 y.yield(Cucumber::Messages::Envelope.new(source: source)) if @options[:include_source]23 begin24 gherkin_document = nil25 if @options[:include_gherkin_document]26 gherkin_document = build_gherkin_document(source)27 y.yield(Cucumber::Messages::Envelope.new(gherkin_document: gherkin_document))28 end29 if @options[:include_pickles]30 gherkin_document ||= build_gherkin_document(source)31 pickles = @compiler.compile(gherkin_document, source)32 pickles.each do |pickle|33 y.yield(Cucumber::Messages::Envelope.new(pickle: pickle))34 end35 end36 rescue CompositeParserException => err37 yield_parse_errors(y, err.errors, source.uri)38 rescue ParserException => err39 yield_parse_errors(y, [err], source.uri)40 end41 end42 end43 end44 private45 def yield_parse_errors(y, errors, uri)46 errors.each do |err|47 parse_error = Cucumber::Messages::ParseError.new(48 source: Cucumber::Messages::SourceReference.new(49 uri: uri,50 location: Cucumber::Messages::Location.new(51 line: err.location[:line],52 column: err.location[:column]53 )54 ),55 message: err.message56 )57 y.yield(Cucumber::Messages::Envelope.new(parse_error: parse_error))58 end59 end60 def sources61 Enumerator.new do |y|62 @paths.each do |path|63 source = Cucumber::Messages::Source.new(64 uri: path,65 data: File.open(path, 'r:UTF-8', &:read),66 media_type: 'text/x.cucumber.gherkin+plain'67 )68 y.yield(source)69 end70 @sources.each do |source|71 y.yield(source)72 end73 end74 end75 def build_gherkin_document(source)76 if @options[:default_dialect]77 token_matcher = TokenMatcher.new(@options[:default_dialect])78 gd = @parser.parse(source.data, token_matcher)79 else80 gd = @parser.parse(source.data)81 end82 Cucumber::Messages::GherkinDocument.new(83 uri: source.uri,84 feature: gd.feature,85 comments: gd.comments86 )87 end88 end89 end90end...

Full Screen

Full Screen

pretty_formatter_steps.rb

Source:pretty_formatter_steps.rb Github

copy

Full Screen

2require 'fileutils'3require 'gherkin'4require 'gherkin/formatter/pretty_formatter'5require 'gherkin/formatter/json_formatter'6require 'gherkin/json_parser'7module PrettyPlease8 9 def pretty_machinery(gherkin, feature_path)10 io = StringIO.new11 formatter = Gherkin::Formatter::PrettyFormatter.new(io, true, false)12 parser = Gherkin::Parser::Parser.new(formatter, true)13 parse(parser, gherkin, feature_path)14 io.string15 end16 def json_machinery(gherkin, feature_path)17 json = StringIO.new18 json_formatter = Gherkin::Formatter::JSONFormatter.new(json)19 gherkin_parser = Gherkin::Parser::Parser.new(json_formatter, true)20 parse(gherkin_parser, gherkin, feature_path)21 json_formatter.done22 io = StringIO.new23 pretty_formatter = Gherkin::Formatter::PrettyFormatter.new(io, true, false)24 json_parser = Gherkin::JSONParser.new(pretty_formatter, pretty_formatter)25 json_parser.parse(json.string)26 27 io.string28 end29 30 def parse(parser, gherkin, feature_path)31 begin32 parser.parse(gherkin, feature_path, 0)33 rescue => e34 if e.message =~ /Lexing error/35 FileUtils.mkdir "tmp" unless File.directory?("tmp")36 written_path = "tmp/#{File.basename(feature_path)}"37 File.open(written_path, "w") {|io| io.write(gherkin)}38 e.message << "\nSee #{written_path}"39 end40 raise e41 end42 end43end44World(PrettyPlease)45Given /^I have Cucumber's source code next to Gherkin's$/ do46 @cucumber_home = File.dirname(__FILE__) + '/../../../cucumber'...

Full Screen

Full Screen

parser.rb

Source:parser.rb Github

copy

Full Screen

...17 messages.each do |message|18 event_bus.envelope(message)19 gherkin_query.update(message)20 if !message.gherkin_document.nil?21 event_bus.gherkin_source_parsed(message.gherkin_document)22 elsif !message.pickle.nil?23 receiver.pickle(message.pickle)24 elsif message.parse_error25 raise Core::Gherkin::ParseError.new("#{document.uri}: #{message.parse_error.message}")26 else27 raise "Unknown message: #{message.to_hash}"28 end29 end30 end31 def gherkin_options(document)32 {33 default_dialect: document.language,34 include_source: false,35 include_gherkin_document: true,36 include_pickles: true37 }38 end39 def done...

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new, true, "root", false)2parser.parse("Feature: Hello3parser.parse("Feature: Hello4def capitalize(string)5 string.split(" ").each do |word|

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new, true, "root", false)2parser.parse("Feature: Hello3parser.parse("Feature: Hello4def capitalize(string)5 string.split(" ").each do |word|

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new, true, 'root')2formatter = Gherkin::Formatter::PrettyFormatter.new(STDOUT)3parser.parse(File.read('feature.feature'), formatter, true)

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1parser.parse(File.read('feature.feature'), listener, true)2parser.parse(File.read('feature.feature'), listener, true)3parser.parse(File.read('feature.feature'), listener, true)4parser.parse(File.read('feature.feature'), listener, true)5parser.parse(File.read('feature.feature'), listener, true)6parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new, true, 'root')7formatter = Gherkin::Formatter::PrettyFormatter.new(STDOUT)8parser.parse(File.read('feature.feature'), formatter, true)

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1parser.parse(File.read('feature.feature'), listener, true)2parser.parse(File.read('feature.feature'), listener, true)3parser.parse(File.read('feature.feature'), listener, true)4parser.parse(File.read('feature.feature'), listener, true)5parser.parse(File.read('feature.feature'), listener, true)

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new)2formatter = Gherkin::Formatter::PrettyFormatter.new(Gherkin::Formatter::Pretty.new)3parser.parse(IO.read("features/1.feature"), formatter, "features/1.feature")4parser = Gherkin::Parser::Parser.new(Gherkin::Formatter::ASTBuilder.new)5formatter = Gherkin::Formatter::PrettyFormatter.new(Gherkin::Formatter::Pretty.new)6parser.parse(IO.read("features/2.feature"), formatter, "features/2.feature")

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1feature = File.read('1.feature')2parser.parse(feature, '1.feature', 0)3feature = File.read('1.feature')4parser.parse(feature, :1.feature', 0)5feature = File.read('1.feature')6parser.parse(feature, '1.feature', 0)7feature = File.read('1.feature')8parser.parse(feature, '1.feature', 0)9feature ScFile.read('1.feature')

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1gherkin.parse("Feature: My feature2gherkin.parse("Feature: My feature3gherkin.parse("Feature: My feature4gherkin.parse("Feature: My feature5gherkin.parse("Feature: My feature6gherkin.parse("Feature: My feature7gherkin.parse("Feature: My feature8gherkin.parse("Feature: My feature

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1parser.parse('Feature: Hello2feature = Gherkin::Parser.new.parse('Feature: My feature3Gherkin::Parser.new.parse('Feature: My feature4Gherkin::Parser.new.parse('Feature: My feature5Gherkin::Parser.new.parse('Feature: My feature6Gherkin::Parser.new.parse('Feature: My feature

Full Screen

Full Screen

parse

Using AI Code Generation

copy

Full Screen

1Gherkin::Parser.new.parse(<<-EOF)2Gherkin.parse(<<-EOF)3Gherkin::Parser.new.parse(<<-EOF)4Gherkin::Parser.new.parse(<<-EOF)5Gherkin::Parser.new.parse(<<-EOF)6Gherkin::Parser.new.parse(<<-EOF)

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Gherkin-ruby automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful