diff --git a/lib/cucumber/core/report/summary.rb b/lib/cucumber/core/report/summary.rb index a218812c..59f245c0 100644 --- a/lib/cucumber/core/report/summary.rb +++ b/lib/cucumber/core/report/summary.rb @@ -12,7 +12,7 @@ def initialize(event_bus) subscribe_to(event_bus) end - def ok?(be_strict = false) + def ok?(be_strict = Test::Result::StrictConfiguration.new) test_cases.ok?(be_strict) end diff --git a/lib/cucumber/core/test/result.rb b/lib/cucumber/core/test/result.rb index c9f088dc..136002ad 100644 --- a/lib/cucumber/core/test/result.rb +++ b/lib/cucumber/core/test/result.rb @@ -6,11 +6,12 @@ module Core module Test module Result TYPES = [:failed, :flaky, :skipped, :undefined, :pending, :passed, :unknown].freeze + STRICT_AFFECTED_TYPES = [:flaky, :undefined, :pending].freeze - def self.ok?(type, be_strict = false) + def self.ok?(type, be_strict = StrictConfiguration.new) private class_name = type.to_s.slice(0, 1).capitalize + type.to_s.slice(1..-1) - const_get(class_name).ok?(be_strict) + const_get(class_name).ok?(be_strict.strict?(type)) end # Defines to_sym on a result class for the given result type @@ -67,8 +68,8 @@ def to_s "✓" end - def ok?(be_strict = false) - self.class.ok?(be_strict) + def ok?(be_strict = nil) + self.class.ok? end def with_appended_backtrace(step) @@ -106,8 +107,8 @@ def to_s "✗" end - def ok?(be_strict = false) - self.class.ok?(be_strict) + def ok?(be_strict = nil) + self.class.ok? end def with_duration(new_duration) @@ -164,8 +165,8 @@ def with_filtered_backtrace(filter) filter.new(dup).exception end - def ok?(be_strict = false) - self.class.ok?(be_strict) + def ok?(be_strict = StrictConfiguration.new) + self.class.ok?(be_strict.strict?(to_sym)) end end @@ -223,6 +224,54 @@ def to_s end end + # Handles the strict settings for the result types that are + # affected by the strict options (that is the STRICT_AFFECTED_TYPES). + class StrictConfiguration + attr_accessor :settings + private :settings + + def initialize(strict_types = []) + @settings = Hash[STRICT_AFFECTED_TYPES.map { |t| [t, :default] }] + strict_types.each do |type| + set_strict(true, type) + end + end + + def strict?(type = nil) + if type.nil? + settings.each do |_key, value| + return true if value == true + end + false + else + return false unless settings.key?(type) + return false unless set?(type) + settings[type] + end + end + + def set_strict(setting, type = nil) + if type.nil? + STRICT_AFFECTED_TYPES.each do |t| + set_strict(setting, t) + end + else + settings[type] = setting + end + end + + def merge!(other) + settings.keys.each do |type| + set_strict(other.strict?(type), type) if other.set?(type) + end + self + end + + def set?(type) + settings[type] != :default + end + end + # # An object that responds to the description protocol from the results # and collects summary information. @@ -250,7 +299,7 @@ def method_missing(name, *args) end end - def ok?(be_strict = false) + def ok?(be_strict = StrictConfiguration.new) TYPES.each do |type| if get_total(type) > 0 return false unless Result.ok?(type, be_strict) diff --git a/spec/cucumber/core/report/summary_spec.rb b/spec/cucumber/core/report/summary_spec.rb index bf45847b..1bb6b2db 100644 --- a/spec/cucumber/core/report/summary_spec.rb +++ b/spec/cucumber/core/report/summary_spec.rb @@ -159,14 +159,16 @@ module Cucumber::Core::Report event_bus.send(:test_case_finished, test_case, pending_result) expect( @summary.ok? ).to eq true - expect( @summary.ok?(true) ).to eq false + be_strict = ::Cucumber::Core::Test::Result::StrictConfiguration.new([:pending]) + expect( @summary.ok?(be_strict) ).to eq false end it "undefined test case is ok if not strict" do event_bus.send(:test_case_finished, test_case, undefined_result) expect( @summary.ok? ).to eq true - expect( @summary.ok?(true) ).to eq false + be_strict = ::Cucumber::Core::Test::Result::StrictConfiguration.new([:undefined]) + expect( @summary.ok?(be_strict) ).to eq false end end end diff --git a/spec/cucumber/core/test/result_spec.rb b/spec/cucumber/core/test/result_spec.rb index 42964877..1fced17e 100644 --- a/spec/cucumber/core/test/result_spec.rb +++ b/spec/cucumber/core/test/result_spec.rb @@ -49,8 +49,7 @@ module Cucumber::Core::Test specify { expect( result ).not_to be_flaky } specify { expect( result ).to be_ok } - specify { expect( result.ok?(false) ).to be_truthy } - specify { expect( result.ok?(true) ).to be_truthy } + specify { expect( result.ok? ).to be_truthy } end describe Result::Failed do @@ -109,8 +108,7 @@ module Cucumber::Core::Test specify { expect( result ).not_to be_flaky } specify { expect( result ).to_not be_ok } - specify { expect( result.ok?(false) ).to be_falsey } - specify { expect( result.ok?(true) ).to be_falsey } + specify { expect( result.ok? ).to be_falsey } end describe Result::Unknown do @@ -202,8 +200,9 @@ module Cucumber::Core::Test specify { expect( result ).not_to be_flaky } specify { expect( result ).to be_ok } - specify { expect( result.ok?(false) ).to be_truthy } - specify { expect( result.ok?(true) ).to be_falsey } + specify { expect( result.ok? ).to be_truthy } + be_strict = Result::StrictConfiguration.new([:undefined]) + specify { expect( result.ok?(be_strict) ).to be_falsey } end describe Result::Skipped do @@ -225,8 +224,7 @@ module Cucumber::Core::Test specify { expect( result ).not_to be_flaky } specify { expect( result ).to be_ok } - specify { expect( result.ok?(false) ).to be_truthy } - specify { expect( result.ok?(true) ).to be_truthy } + specify { expect( result.ok? ).to be_truthy } end describe Result::Pending do @@ -249,8 +247,9 @@ module Cucumber::Core::Test specify { expect( result ).to be_pending } specify { expect( result ).to be_ok } - specify { expect( result.ok?(false) ).to be_truthy } - specify { expect( result.ok?(true) ).to be_falsey } + specify { expect( result.ok? ).to be_truthy } + be_strict = Result::StrictConfiguration.new([:pending]) + specify { expect( result.ok?(be_strict) ).to be_falsey } end describe Result::Flaky do @@ -258,6 +257,77 @@ module Cucumber::Core::Test specify { expect( Result::Flaky.ok?(true) ).to be_falsey } end + describe Result::StrictConfiguration do + subject(:strict_configuration) { Result::StrictConfiguration.new} + + describe '#set_strict' do + context 'no type argument' do + it 'sets all result types to the setting argument' do + strict_configuration.set_strict(true) + expect( strict_configuration.strict?(:undefined) ).to be_truthy + expect( strict_configuration.strict?(:pending) ).to be_truthy + expect( strict_configuration.strict?(:flaky) ).to be_truthy + + strict_configuration.set_strict(false) + expect( strict_configuration.strict?(:undefined) ).to be_falsey + expect( strict_configuration.strict?(:pending) ).to be_falsey + expect( strict_configuration.strict?(:flaky) ).to be_falsey + end + end + context 'with type argument' do + it 'sets the specified result type to the setting argument' do + strict_configuration.set_strict(true, :undefined) + expect( strict_configuration.strict?(:undefined) ).to be_truthy + expect( strict_configuration.set?(:pending) ).to be_falsey + expect( strict_configuration.set?(:flaky) ).to be_falsey + + strict_configuration.set_strict(false, :undefined) + expect( strict_configuration.strict?(:undefined) ).to be_falsey + expect( strict_configuration.set?(:pending) ).to be_falsey + expect( strict_configuration.set?(:flaky) ).to be_falsey + end + end + end + + describe '#strict?' do + context 'no type argument' do + it 'returns true if any result type is set to strict' do + strict_configuration.set_strict(false, :pending) + expect( strict_configuration.strict? ).to be_falsey + + strict_configuration.set_strict(true, :flaky) + expect( strict_configuration.strict? ).to be_truthy + end + end + context 'with type argument' do + it 'returns true if the specified result type is set to strict' do + strict_configuration.set_strict(false, :pending) + strict_configuration.set_strict(true, :flaky) + + expect( strict_configuration.strict?(:undefined) ).to be_falsey + expect( strict_configuration.strict?(:pending) ).to be_falsey + expect( strict_configuration.strict?(:flaky) ).to be_truthy + end + end + end + + describe '#merge!' do + let(:merged_configuration) { Result::StrictConfiguration.new } + it 'sets the not default values from the argument accordingly' do + strict_configuration.set_strict(false, :undefined) + strict_configuration.set_strict(false, :pending) + strict_configuration.set_strict(true, :flaky) + merged_configuration.set_strict(true, :pending) + merged_configuration.set_strict(false, :flaky) + strict_configuration.merge!(merged_configuration) + + expect( strict_configuration.strict?(:undefined) ).to be_falsey + expect( strict_configuration.strict?(:pending) ).to be_truthy + expect( strict_configuration.strict?(:flaky) ).to be_falsey + end + end + end + describe Result::Summary do let(:summary) { Result::Summary.new } let(:failed) { Result::Failed.new(Result::Duration.new(10), exception) } @@ -360,19 +430,22 @@ def describe_to(visitor, *args) it "pending result is ok if not strict" do pending.describe_to summary expect( summary.ok? ).to be true - expect( summary.ok?(true) ).to be false + be_strict = Result::StrictConfiguration.new([:pending]) + expect( summary.ok?(be_strict) ).to be false end it "undefined result is ok if not strict" do undefined.describe_to summary expect( summary.ok? ).to be true - expect( summary.ok?(true) ).to be false + be_strict = Result::StrictConfiguration.new([:undefined]) + expect( summary.ok?(be_strict) ).to be false end it "flaky result is ok if not strict" do summary.flaky expect( summary.ok? ).to be true - expect( summary.ok?(true) ).to be false + be_strict = Result::StrictConfiguration.new([:flaky]) + expect( summary.ok?(be_strict) ).to be false end end end