Skip to content

Commit

Permalink
Handle selective strict settings.
Browse files Browse the repository at this point in the history
Let the result types affected the the strict settings be controlled
individually, so only a sub set of the can be set to be handled strict.
  • Loading branch information
brasmusson committed Aug 3, 2017
1 parent 1aa0140 commit 51eee9c
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/cucumber/core/report/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
67 changes: 58 additions & 9 deletions lib/cucumber/core/test/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions spec/cucumber/core/report/summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
99 changes: 86 additions & 13 deletions spec/cucumber/core/test/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -249,15 +247,87 @@ 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
specify { expect( Result::Flaky.ok?(false) ).to be_truthy }
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) }
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 51eee9c

Please sign in to comment.