From 022e8ddf269a11600b61ef2bd1ec82eb048cb8a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Rasmusson?= Date: Wed, 12 Jul 2017 19:42:59 +0200 Subject: [PATCH] Make the Summary report able to say if the total result is ok. * 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. --- lib/cucumber/core/report/summary.rb | 4 ++ lib/cucumber/core/test/result.rb | 60 +++++++++++++++++------ spec/cucumber/core/report/summary_spec.rb | 36 ++++++++++++++ spec/cucumber/core/test/result_spec.rb | 30 ++++++++++++ 4 files changed, 114 insertions(+), 16 deletions(-) diff --git a/lib/cucumber/core/report/summary.rb b/lib/cucumber/core/report/summary.rb index 48d9c0b1..0520b283 100644 --- a/lib/cucumber/core/report/summary.rb +++ b/lib/cucumber/core/report/summary.rb @@ -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) diff --git a/lib/cucumber/core/test/result.rb b/lib/cucumber/core/test/result.rb index f8dceaf7..5b1122a6 100644 --- a/lib/cucumber/core/test/result.rb +++ b/lib/cucumber/core/test/result.rb @@ -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 # @@ -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 @@ -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 @@ -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) @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -189,10 +212,6 @@ def describe_to(visitor, *args) def to_s "P" end - - def ok?(be_strict = false) - !be_strict - end end # @@ -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 diff --git a/spec/cucumber/core/report/summary_spec.rb b/spec/cucumber/core/report/summary_spec.rb index 24248c52..f2853635 100644 --- a/spec/cucumber/core/report/summary_spec.rb +++ b/spec/cucumber/core/report/summary_spec.rb @@ -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 diff --git a/spec/cucumber/core/test/result_spec.rb b/spec/cucumber/core/test/result_spec.rb index 929f9cb1..426da1f9 100644 --- a/spec/cucumber/core/test/result_spec.rb +++ b/spec/cucumber/core/test/result_spec.rb @@ -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 } @@ -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