Skip to content

Commit

Permalink
Make the Summary report able to say if the total result is ok.
Browse files Browse the repository at this point in the history
* Add TYPES constant to Result module
* Make ok? class methods on the Result classes
* Make the Summary result able to way if the total result is ok.
  • Loading branch information
brasmusson committed Jul 14, 2017
1 parent 718b657 commit 022e8dd
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 16 deletions.
4 changes: 4 additions & 0 deletions lib/cucumber/core/report/summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def initialize(event_bus)
subscribe_to(event_bus)
end

def ok?(be_strict = false)
test_cases.ok?(be_strict)
end

private

def subscribe_to(event_bus)
Expand Down
60 changes: 44 additions & 16 deletions lib/cucumber/core/test/result.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
# encoding: UTF-8
# encoding: utf-8
# frozen_string_literal: true

module Cucumber
module Core
module Test
module Result
TYPES = [:failed, :skipped, :undefined, :pending, :passed, :unknown].freeze

def self.ok?(type, be_strict = false)
private
class_name = type.to_s.slice(0, 1).capitalize + type.to_s.slice(1..-1)
const_get(class_name).ok?(be_strict)
end

# Defines to_sym on a result class for the given result type
#
Expand All @@ -16,7 +23,7 @@ def self.query_methods(result_type)
result_type
end

[:passed, :failed, :undefined, :unknown, :skipped, :pending].each do |possible_result_type|
TYPES.each do |possible_result_type|
define_method("#{possible_result_type}?") do
possible_result_type == to_sym
end
Expand All @@ -41,6 +48,10 @@ class Passed
include Result.query_methods :passed
attr_accessor :duration

def self.ok?(be_strict = false)
true
end

def initialize(duration)
raise ArgumentError unless duration
@duration = duration
Expand All @@ -57,7 +68,7 @@ def to_s
end

def ok?(be_strict = false)
true
self.class.ok?(be_strict)
end

def with_appended_backtrace(step)
Expand All @@ -73,6 +84,10 @@ class Failed
include Result.query_methods :failed
attr_reader :duration, :exception

def self.ok?(be_strict = false)
false
end

def initialize(duration, exception)
raise ArgumentError unless duration
raise ArgumentError unless exception
Expand All @@ -92,7 +107,7 @@ def to_s
end

def ok?(be_strict = false)
false
self.class.ok?(be_strict)
end

def with_duration(new_duration)
Expand Down Expand Up @@ -139,11 +154,19 @@ def with_filtered_backtrace(filter)
return self unless backtrace
filter.new(dup).exception
end

def ok?(be_strict = false)
self.class.ok?(be_strict)
end
end

class Undefined < Raisable
include Result.query_methods :undefined

def self.ok?(be_strict = false)
!be_strict
end

def describe_to(visitor, *args)
visitor.undefined(*args)
visitor.duration(duration, *args)
Expand All @@ -153,15 +176,15 @@ def describe_to(visitor, *args)
def to_s
"?"
end

def ok?(be_strict = false)
!be_strict
end
end

class Skipped < Raisable
include Result.query_methods :skipped

def self.ok?(be_strict = false)
true
end

def describe_to(visitor, *args)
visitor.skipped(*args)
visitor.duration(duration, *args)
Expand All @@ -171,15 +194,15 @@ def describe_to(visitor, *args)
def to_s
"-"
end

def ok?(be_strict = false)
true
end
end

class Pending < Raisable
include Result.query_methods :pending

def self.ok?(be_strict = false)
!be_strict
end

def describe_to(visitor, *args)
visitor.pending(self, *args)
visitor.duration(duration, *args)
Expand All @@ -189,10 +212,6 @@ def describe_to(visitor, *args)
def to_s
"P"
end

def ok?(be_strict = false)
!be_strict
end
end

#
Expand Down Expand Up @@ -222,6 +241,15 @@ def method_missing(name, *args)
end
end

def ok?(be_strict = false)
TYPES.each do |type|
if get_total(type) > 0
return false unless Result.ok?(type, be_strict)
end
end
true
end

def exception(exception)
@exceptions << exception
self
Expand Down
36 changes: 36 additions & 0 deletions spec/cucumber/core/report/summary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,41 @@ module Cucumber::Core::Report
end
end
end

context "ok? result" do
let(:test_case) { double }

it "passed test case is ok" do
event_bus.send(:test_case_finished, test_case, passed_result)

expect( @summary.ok? ).to eq true
end

it "skipped test case is ok" do
event_bus.send(:test_case_finished, test_case, skipped_result)

expect( @summary.ok? ).to eq true
end

it "failed test case is not ok" do
event_bus.send(:test_case_finished, test_case, failed_result)

expect( @summary.ok? ).to eq false
end

it "pending test case is ok if not strict" do
event_bus.send(:test_case_finished, test_case, pending_result)

expect( @summary.ok? ).to eq true
expect( @summary.ok?(true) ).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
end
end
end
end
30 changes: 30 additions & 0 deletions spec/cucumber/core/test/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ module Cucumber::Core::Test
let(:passed) { Result::Passed.new(Result::Duration.new(11)) }
let(:skipped) { Result::Skipped.new }
let(:unknown) { Result::Unknown.new }
let(:pending) { Result::Pending.new }
let(:undefined) { Result::Undefined.new }
let(:exception) { StandardError.new }

Expand Down Expand Up @@ -328,6 +329,35 @@ def describe_to(visitor, *args)
[passed, failed].each { |r| r.describe_to summary }
expect( summary.exceptions ).to eq [exception]
end

context "ok? result" do
it "passed result is ok" do
passed.describe_to summary
expect( summary.ok? ).to be true
end

it "skipped result is ok" do
skipped.describe_to summary
expect( summary.ok? ).to be true
end

it "failed result is not ok" do
failed.describe_to summary
expect( summary.ok? ).to be false
end

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
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
end
end
end

describe Result::Duration do
Expand Down

0 comments on commit 022e8dd

Please sign in to comment.