From 7842d428edd6fdf251f360d78c7b2f0e45e70546 Mon Sep 17 00:00:00 2001 From: Luke Hill <20105237+luke-hill@users.noreply.github.com> Date: Fri, 10 Nov 2023 15:00:43 +0000 Subject: [PATCH] Refactor/v13 prep (#273) * Remove random file * Update gemspec * Remove another random file * Fix linting of lines * Unpick some of the JRuby optimisations made * Add documentation for metaprogramming unpicking and re-enable flaky test --- .ruby-gemset | 1 - .yardopts | 6 -- cucumber-core.gemspec | 11 +-- lib/cucumber/core/event.rb | 17 ++-- .../test/filters/locations_filter_spec.rb | 78 ++++++++++--------- spec/cucumber/core_spec.rb | 2 - 6 files changed, 52 insertions(+), 63 deletions(-) delete mode 100644 .ruby-gemset delete mode 100644 .yardopts diff --git a/.ruby-gemset b/.ruby-gemset deleted file mode 100644 index 6e0713a0..00000000 --- a/.ruby-gemset +++ /dev/null @@ -1 +0,0 @@ -cucumber diff --git a/.yardopts b/.yardopts deleted file mode 100644 index e91ce49a..00000000 --- a/.yardopts +++ /dev/null @@ -1,6 +0,0 @@ ---exclude spec ---markup markdown ---no-private -- -HISTORY.md -LICENSE diff --git a/cucumber-core.gemspec b/cucumber-core.gemspec index 6e6f4966..e00159d1 100644 --- a/cucumber-core.gemspec +++ b/cucumber-core.gemspec @@ -1,4 +1,3 @@ -# -*- encoding: utf-8 -*- # frozen_string_literal: true version = File.read(File.expand_path('VERSION', __dir__)).strip @@ -25,7 +24,7 @@ Gem::Specification.new do |s| s.add_dependency 'cucumber-gherkin', '>= 27', '< 28' s.add_dependency 'cucumber-messages', '>= 20', '< 23' - s.add_dependency 'cucumber-tag-expressions', '~> 5.0', '>= 5.0.4' + s.add_dependency 'cucumber-tag-expressions', '> 5', '< 7' s.add_development_dependency 'rake', '~> 13.0', '>= 13.0.6' s.add_development_dependency 'rspec', '~> 3.11', '>= 3.11.0' @@ -35,13 +34,7 @@ Gem::Specification.new do |s| s.add_development_dependency 'rubocop-packaging', '~> 0.5', '>= 0.5.1' s.add_development_dependency 'unindent', '~> 1.0', '>= 1.0' - s.files = Dir[ - 'CHANGELOG.md', - 'CONTRIBUTING.md', - 'README.md', - 'LICENSE', - 'lib/**/*' - ] + s.files = Dir['CHANGELOG.md', 'CONTRIBUTING.md', 'README.md', 'LICENSE', 'lib/**/*'] s.rdoc_options = ['--charset=UTF-8'] s.require_path = 'lib' end diff --git a/lib/cucumber/core/event.rb b/lib/cucumber/core/event.rb index 80f30c3d..bbd1bb49 100644 --- a/lib/cucumber/core/event.rb +++ b/lib/cucumber/core/event.rb @@ -10,6 +10,11 @@ def self.new(*events) return super if ancestors.index(Event) > 0 Class.new(Event) do + # NB: We need to use metaprogramming here instead of direct variable obtainment + # because JRuby does not guarantee the order in which variables are defined is equivalent + # to the order in which they are obtainable + # + # See https://github.com/jruby/jruby/issues/7988 for more info attr_reader(*events) define_method(:initialize) do |*attributes| @@ -18,23 +23,17 @@ def self.new(*events) end end - def attributes - instance_variables.map { |var| instance_variable_get(var) } + define_method(:attributes) do + events.map { |var| instance_variable_get(:"@#{var}") } end - def to_h + define_method(:to_h) do events.zip(attributes).to_h end def event_id self.class.event_id end - - private - - def events - instance_variables.map { |var| (var[1..-1]).to_sym } - end end end diff --git a/spec/cucumber/core/test/filters/locations_filter_spec.rb b/spec/cucumber/core/test/filters/locations_filter_spec.rb index 43355fbf..5c5dca8c 100644 --- a/spec/cucumber/core/test/filters/locations_filter_spec.rb +++ b/spec/cucumber/core/test/filters/locations_filter_spec.rb @@ -40,21 +40,19 @@ module Core end it 'works with wildcard locations' do - locations = [ - Test::Location.new('features/test.feature') - ] + locations = [Test::Location.new('features/test.feature')] filter = described_class.new(locations) compile([doc], receiver, [filter]) - expect(receiver.test_case_locations).to eq([ - Test::Location.new('features/test.feature', 3), - Test::Location.new('features/test.feature', 6) - ]) + expect(receiver.test_case_locations).to eq( + [ + Test::Location.new('features/test.feature', 3), + Test::Location.new('features/test.feature', 6) + ] + ) end it "filters out scenarios that don't match" do - locations = [ - Test::Location.new('features/test.feature', 3) - ] + locations = [Test::Location.new('features/test.feature', 3)] filter = described_class.new(locations) compile([doc], receiver, [filter]) expect(receiver.test_case_locations).to eq(locations) @@ -62,7 +60,6 @@ module Core describe 'matching location' do let(:file) { 'features/path/to/the.feature' } - let(:test_cases) do receiver = double.as_null_object result = [] @@ -81,7 +78,7 @@ module Core Scenario: one Given one a - # comment + # comment @tags Scenario: two Given two a @@ -150,40 +147,48 @@ def test_case_named(name) location = Test::Location.new(file, 29) filter = described_class.new([location]) compile [doc], receiver, [filter] - expect(receiver.test_case_locations).to eq([ - test_case_named('with a rule and background').location, - test_case_named('another with a rule and background').location - ]) + expect(receiver.test_case_locations).to eq( + [ + test_case_named('with a rule and background').location, + test_case_named('another with a rule and background').location + ] + ) end it "matches the rule background location to all of the rule's scenarios" do location = Test::Location.new(file, 30) filter = described_class.new([location]) compile [doc], receiver, [filter] - expect(receiver.test_case_locations).to eq([ - test_case_named('with a rule and background').location, - test_case_named('another with a rule and background').location - ]) + expect(receiver.test_case_locations).to eq( + [ + test_case_named('with a rule and background').location, + test_case_named('another with a rule and background').location + ] + ) end it "matches a rule background step location to all of the rule's scenarios" do location = Test::Location.new(file, 31) filter = described_class.new([location]) compile [doc], receiver, [filter] - expect(receiver.test_case_locations).to eq([ - test_case_named('with a rule and background').location, - test_case_named('another with a rule and background').location - ]) + expect(receiver.test_case_locations).to eq( + [ + test_case_named('with a rule and background').location, + test_case_named('another with a rule and background').location + ] + ) end it "matches a rule location (without a background) to all of the rule's scenarios" do location = Test::Location.new(file, 39) filter = described_class.new([location]) compile [doc], receiver, [filter] - expect(receiver.test_case_locations).to eq([ - test_case_named('with a rule and no background').location, - test_case_named('another with a rule and no background').location - ]) + expect(receiver.test_case_locations).to eq( + [ + test_case_named('with a rule and no background').location, + test_case_named('another with a rule and no background').location + ] + ) end it 'matches a scenario location to the scenario' do @@ -199,10 +204,12 @@ def test_case_named(name) whitespace_location = Test::Location.new(file, 7) filter = described_class.new([scenario_location, another_scenario_location, whitespace_location]) compile([doc], receiver, [filter]) - expect(receiver.test_case_locations).to eq([ - test_case_named('one').location, - test_case_named('two').location - ]) + expect(receiver.test_case_locations).to eq( + [ + test_case_named('one').location, + test_case_named('two').location + ] + ) end it 'matches the first scenario step location to the scenario' do @@ -306,7 +313,7 @@ def test_case_named(name) Given one a # comment on line 6 - @tags-on-line-7 + @tag-on-line-7 Scenario Outline: two Given two a And two @@ -315,7 +322,7 @@ def test_case_named(name) """ # comment on line 15 - @tags-on-line-16 + @tag-on-line-16 Examples: x1 | arg | | b | @@ -421,8 +428,7 @@ def test_case(test_case) test_cases << test_case end - def done - end + def done; end def test_case_locations test_cases.map(&:location) diff --git a/spec/cucumber/core_spec.rb b/spec/cucumber/core_spec.rb index bbbf9838..c88d0664 100644 --- a/spec/cucumber/core_spec.rb +++ b/spec/cucumber/core_spec.rb @@ -99,8 +99,6 @@ end it 'fires events' do - pending 'This spec fails in JRuby. See https://github.com/jruby/jruby/issues/7988, for details' if defined?(JRUBY_VERSION) - observed_events = [] execute [gherkin_document], [Cucumber::Core::Test::Filters::ActivateStepsForSelfTest.new] do |event_bus| event_bus.on(:test_case_started) do |event|