Skip to content

Commit

Permalink
Add #to_sym to Cucumber::Core::Test::Result classes
Browse files Browse the repository at this point in the history
  • Loading branch information
pdswan committed May 5, 2015
1 parent c72a6db commit 820c973
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 37 deletions.
28 changes: 17 additions & 11 deletions lib/cucumber/core/test/result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,35 @@ module Core
module Test
module Result

# Defines to_sym on a result class for the given status symbol
#
# Defines predicate methods on a result class with only the given one
# returning true
def self.status_queries(status)
def self.query_methods(status_symbol)
Module.new do
define_method :to_sym do
status_symbol
end

[:passed, :failed, :undefined, :unknown, :skipped, :pending].each do |possible_status|
define_method("#{possible_status}?") do
possible_status == status
possible_status == to_sym
end
end
end
end

# Null object for results. Represents the state where we haven't run anything yet
class Unknown
include Result.status_queries :unknown
include Result.query_methods :unknown

def describe_to(visitor, *args)
self
end
end

class Passed
include Result.status_queries(:passed)
include Result.query_methods :passed
attr_accessor :duration

def initialize(duration)
Expand Down Expand Up @@ -55,11 +61,11 @@ def with_filtered_backtrace(filter)
end

class Failed
include Result.status_queries(:failed)
include Result.query_methods :failed
attr_reader :duration, :exception

def initialize(duration, exception)
raise ArgumentError unless duration
raise ArgumentError unless duration
raise ArgumentError unless exception
@duration = duration
@exception = exception
Expand Down Expand Up @@ -90,7 +96,7 @@ def with_filtered_backtrace(filter)
end
end

# Base class for exceptions that can be raised in a step defintion causing
# Base class for exceptions that can be raised in a step defintion causing
# the step to have that result.
class Raisable < StandardError
attr_reader :message, :duration
Expand Down Expand Up @@ -123,7 +129,7 @@ def with_filtered_backtrace(filter)
end

class Undefined < Raisable
include Result.status_queries :undefined
include Result.query_methods :undefined

def describe_to(visitor, *args)
visitor.undefined(*args)
Expand All @@ -138,7 +144,7 @@ def to_s
end

class Skipped < Raisable
include Result.status_queries :skipped
include Result.query_methods :skipped

def describe_to(visitor, *args)
visitor.skipped(*args)
Expand All @@ -153,7 +159,7 @@ def to_s
end

class Pending < Raisable
include Result.status_queries :pending
include Result.query_methods :pending

def describe_to(visitor, *args)
visitor.pending(self, *args)
Expand All @@ -170,7 +176,7 @@ def to_s
# An object that responds to the description protocol from the results
# and collects summary information.
#
# e.g.
# e.g.
# summary = Result::Summary.new
# Result::Passed.new(0).describe_to(summary)
# puts summary.total_passed
Expand Down
62 changes: 36 additions & 26 deletions spec/cucumber/core/test/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ module Cucumber::Core::Test
expect( result.with_filtered_backtrace(double) ).to equal result
end

it { expect( result ).to be_passed }
it { expect( result ).not_to be_failed }
it { expect( result ).not_to be_undefined }
it { expect( result ).not_to be_unknown }
it { expect( result ).not_to be_skipped }
specify { expect( result.to_sym ).to eq :passed }

specify { expect( result ).to be_passed }
specify { expect( result ).not_to be_failed }
specify { expect( result ).not_to be_undefined }
specify { expect( result ).not_to be_unknown }
specify { expect( result ).not_to be_skipped }
end

describe Result::Failed do
Expand Down Expand Up @@ -91,11 +93,13 @@ module Cucumber::Core::Test
expect( result.with_filtered_backtrace(filter_class).exception ).to equal filtered_exception
end

it { expect( result ).not_to be_passed }
it { expect( result ).to be_failed }
it { expect( result ).not_to be_undefined }
it { expect( result ).not_to be_unknown }
it { expect( result ).not_to be_skipped }
specify { expect( result.to_sym ).to eq :failed }

specify { expect( result ).not_to be_passed }
specify { expect( result ).to be_failed }
specify { expect( result ).not_to be_undefined }
specify { expect( result ).not_to be_unknown }
specify { expect( result ).not_to be_skipped }
end

describe Result::Unknown do
Expand All @@ -106,17 +110,19 @@ module Cucumber::Core::Test
result.describe_to(visitor, args)
end

it { expect( result ).not_to be_passed }
it { expect( result ).not_to be_failed }
it { expect( result ).not_to be_undefined }
it { expect( result ).to be_unknown }
it { expect( result ).not_to be_skipped }
specify { expect( result.to_sym ).to eq :unknown }

specify { expect( result ).not_to be_passed }
specify { expect( result ).not_to be_failed }
specify { expect( result ).not_to be_undefined }
specify { expect( result ).to be_unknown }
specify { expect( result ).not_to be_skipped }
end

describe Result::Raisable do
context "with or without backtrace" do
subject(:result) { Result::Raisable.new }

it "does nothing if step has no backtrace line" do
step = "does not respond_to?(:backtrace_line)"

Expand Down Expand Up @@ -170,11 +176,13 @@ module Cucumber::Core::Test
result.describe_to(visitor, args)
end

it { expect( result ).not_to be_passed }
it { expect( result ).not_to be_failed }
it { expect( result ).to be_undefined }
it { expect( result ).not_to be_unknown }
it { expect( result ).not_to be_skipped }
specify { expect( result.to_sym ).to eq :undefined }

specify { expect( result ).not_to be_passed }
specify { expect( result ).not_to be_failed }
specify { expect( result ).to be_undefined }
specify { expect( result ).not_to be_unknown }
specify { expect( result ).not_to be_skipped }
end

describe Result::Skipped do
Expand All @@ -186,11 +194,13 @@ module Cucumber::Core::Test
result.describe_to(visitor, args)
end

it { expect( result ).not_to be_passed }
it { expect( result ).not_to be_failed }
it { expect( result ).not_to be_undefined }
it { expect( result ).not_to be_unknown }
it { expect( result ).to be_skipped }
specify { expect( result.to_sym ).to eq :skipped }

specify { expect( result ).not_to be_passed }
specify { expect( result ).not_to be_failed }
specify { expect( result ).not_to be_undefined }
specify { expect( result ).not_to be_unknown }
specify { expect( result ).to be_skipped }
end

describe Result::Summary do
Expand Down

0 comments on commit 820c973

Please sign in to comment.