Skip to content

Commit 1cd5969

Browse files
committed
Update to use gherkin version 6.
1 parent 12d04cd commit 1cd5969

File tree

6 files changed

+54
-71
lines changed

6 files changed

+54
-71
lines changed

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
source "https://rubygems.org"
22
gemspec
3+
4+
gem 'gherkin', path: ENV['GHERKIN_RUBY'] if ENV['GHERKIN_RUBY']
5+
6+
gem 'cucumber-messages', path: ENV['CUCUMBER_MESSAGES_RUBY'] if ENV['CUCUMBER_MESSAGES_RUBY']

cucumber-core.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
1414
s.license = "MIT"
1515
s.required_ruby_version = '>= 2.2' # Keep in sync with .travis.yml
1616

17-
s.add_dependency 'gherkin', '~> 5.1'
17+
s.add_dependency 'gherkin', '~> 6.0'
1818
s.add_dependency 'cucumber-tag_expressions', '~> 1.1.0'
1919
s.add_dependency 'backports', '>= 3.8.0'
2020

lib/cucumber/core/compiler.rb

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ def initialize(receiver)
1818
@receiver = receiver
1919
end
2020

21-
def pickles(pickles, uri)
22-
pickles.each do |pickle|
23-
test_case = create_test_case(pickle, uri)
24-
test_case.describe_to(receiver)
25-
end
21+
def pickle(pickle)
22+
test_case = create_test_case(pickle)
23+
test_case.describe_to(receiver)
2624
end
2725

2826
def done
@@ -32,37 +30,37 @@ def done
3230

3331
private
3432

35-
def create_test_case(pickle, uri)
33+
def create_test_case(pickle)
34+
uri = pickle[:uri]
3635
test_steps = pickle[:steps].map { |step| create_test_step(step, uri) }
37-
lines = pickle[:locations].map { |location| location[:line] }
36+
lines = pickle[:locations].map { |location| location[:line] }.sort.reverse
3837
tags = pickle[:tags].map { |tag| Test::Tag.new(Test::Location.new(uri, tag[:location][:line]), tag[:name]) }
3938
Test::Case.new(pickle[:name], test_steps, Test::Location.new(uri, lines), tags, pickle[:language])
4039
end
4140

4241
def create_test_step(pickle_step, uri)
43-
lines = pickle_step[:locations].map { |location| location[:line] }
44-
multiline_arg = create_multiline_arg(pickle_step[:arguments], uri)
42+
lines = pickle_step[:locations].map { |location| location[:line] }.sort.reverse
43+
multiline_arg = create_multiline_arg(pickle_step, uri)
4544
Test::Step.new(pickle_step[:text], Test::Location.new(uri, lines), multiline_arg)
4645
end
4746

48-
def create_multiline_arg(pickle_step_arguments, uri)
49-
if pickle_step_arguments.empty?
50-
Test::EmptyMultilineArgument.new
47+
def create_multiline_arg(pickle_step, uri)
48+
if !pickle_step[:doc_string].nil?
49+
argument = pickle_step[:doc_string]
50+
Test::DocString.new(
51+
argument[:content],
52+
argument[:content_type],
53+
Test::Location.new(uri, argument[:location][:line])
54+
)
55+
elsif !pickle_step[:data_table].nil?
56+
argument = pickle_step[:data_table]
57+
first_cell = argument[:rows].first[:cells].first
58+
Test::DataTable.new(
59+
argument[:rows].map { |row| row[:cells].map { |cell| cell[:value] } },
60+
Test::Location.new(uri, first_cell[:location][:line])
61+
)
5162
else
52-
argument = pickle_step_arguments.first
53-
if argument[:content]
54-
Test::DocString.new(
55-
argument[:content],
56-
argument[:content_type],
57-
Test::Location.new(uri, argument[:location][:line])
58-
)
59-
else
60-
first_cell = argument[:rows].first[:cells].first
61-
Test::DataTable.new(
62-
argument[:rows].map { |row| row[:cells].map { |cell| cell[:value] } },
63-
Test::Location.new(uri, first_cell[:location][:line])
64-
)
65-
end
63+
Test::EmptyMultilineArgument.new
6664
end
6765
end
6866
end

lib/cucumber/core/events.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ module Core
66
module Events
77

88
# Signals that a gherkin source has been parsed
9-
class GherkinSourceParsed < Event.new(:uri, :gherkin_document)
10-
# The uri of the file
11-
attr_reader :uri
12-
9+
class GherkinSourceParsed < Event.new(:gherkin_document)
1310
# @return [GherkinDocument] the GherkinDocument Ast Node
1411
attr_reader :gherkin_document
1512

lib/cucumber/core/gherkin/parser.rb

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# frozen_string_literal: true
2-
require 'gherkin/parser'
3-
require 'gherkin/token_scanner'
4-
require 'gherkin/errors'
5-
require 'gherkin/pickles/compiler'
2+
require 'gherkin/gherkin'
63

74
module Cucumber
85
module Core
@@ -19,31 +16,26 @@ def initialize(receiver, event_bus)
1916
end
2017

2118
def document(document)
22-
parser = ::Gherkin::Parser.new
23-
scanner = ::Gherkin::TokenScanner.new(document.body)
24-
token_matcher = ::Gherkin::TokenMatcher.new(document.language)
25-
compiler = ::Gherkin::Pickles::Compiler.new
26-
27-
begin
28-
result = parser.parse(scanner, token_matcher)
29-
event_bus.gherkin_source_parsed(document.uri, result.dup)
30-
pickles = compiler.compile(result)
31-
32-
receiver.pickles(pickles, document.uri)
33-
rescue *PARSER_ERRORS => e
34-
raise Core::Gherkin::ParseError.new("#{document.uri}: #{e.message}")
19+
messages = ::Gherkin::Gherkin.from_source(document.uri, document.body, {default_dialect: document.language, include_source: false})
20+
messages.each do |message|
21+
if !message.gherkinDocument.nil?
22+
event_bus.gherkin_source_parsed(message.gherkinDocument.to_hash)
23+
elsif !message.pickle.nil?
24+
receiver.pickle(message.pickle.to_hash)
25+
elsif !message.attachment.nil?
26+
raise message.attachment.data
27+
else
28+
raise "Unknown message: #{message.to_hash}"
29+
end
3530
end
31+
rescue RuntimeError => e
32+
raise Core::Gherkin::ParseError.new("#{document.uri}: #{e.message}")
3633
end
3734

3835
def done
3936
receiver.done
4037
self
4138
end
42-
43-
private
44-
45-
PARSER_ERRORS = ::Gherkin::ParserError
46-
4739
end
4840
end
4941
end

spec/cucumber/core/gherkin/parser_spec.rb

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,17 @@ def parse
3737
let(:path) { 'path_to/the.feature' }
3838

3939
it "issues a gherkin_source_parsed event" do
40-
allow( receiver ).to receive(:pickles)
4140
expect( event_bus ).to receive(:gherkin_source_parsed)
4241
parse
4342
end
44-
45-
it "passes on the uri" do
46-
expect( receiver ).to receive(:pickles).with(anything, path)
47-
parse
48-
end
4943
end
5044

5145
context "for empty files" do
5246
let(:source) { Gherkin::Document.new(path, '') }
5347
let(:path) { 'path_to/the.feature' }
5448

55-
it "passes on an empty list of pickles" do
56-
expect( receiver ).to receive(:pickles).with([], anything)
49+
it "passes on no pickles" do
50+
expect( receiver ).not_to receive(:pickle)
5751
parse
5852
end
5953
end
@@ -63,25 +57,23 @@ def self.source(&block)
6357
let(:source) { gherkin(&block) }
6458
end
6559

66-
RSpec::Matchers.define :pickles_with_language do |language|
67-
match { |actual| actual.each { |pickle| pickle[:language] == language } }
60+
RSpec::Matchers.define :pickle_with_language do |language|
61+
match { |actual| actual[:language] == language }
6862
end
6963

7064
context "when the Gherkin has a language header" do
7165
source do
72-
feature(language: 'ja', keyword: '機能')
66+
feature(language: 'ja', keyword: '機能') do
67+
scenario(keyword: 'シナリオ')
68+
end
7369
end
7470

7571
it "the pickles have the correct language" do
76-
expect( receiver ).to receive(:pickles).with(pickles_with_language('ja'), anything)
72+
expect( receiver ).to receive(:pickle).with(pickle_with_language('ja'))
7773
parse
7874
end
7975
end
8076

81-
RSpec::Matchers.define :a_list_of_one_pickle do
82-
match { |actual| actual.length == 1 }
83-
end
84-
8577
context "when the Gherkin produces one pickle" do
8678
source do
8779
feature do
@@ -91,8 +83,8 @@ def self.source(&block)
9183
end
9284
end
9385

94-
it "passes on a list with the pickle" do
95-
expect( receiver ).to receive(:pickles).with(a_list_of_one_pickle, anything)
86+
it "passes on the pickle" do
87+
expect( receiver ).to receive(:pickle)
9688
parse
9789
end
9890
end

0 commit comments

Comments
 (0)