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 1, 2015
1 parent 044cca8 commit fec6053
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 36 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 All @@ -47,11 +53,11 @@ def to_s
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 All @@ -74,7 +80,7 @@ def with_duration(new_duration)

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 All @@ -95,7 +101,7 @@ def with_duration(new_duration)
end

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

def describe_to(visitor, *args)
visitor.undefined(*args)
Expand All @@ -110,7 +116,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 @@ -125,7 +131,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 @@ -142,7 +148,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
60 changes: 35 additions & 25 deletions spec/cucumber/core/test/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ module Cucumber::Core::Test
expect { Result::Passed.new }.to raise_error(ArgumentError)
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 All @@ -58,11 +60,13 @@ module Cucumber::Core::Test
expect { Result::Failed.new(duration) }.to raise_error(ArgumentError)
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 @@ -73,11 +77,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 ).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::Undefined do
Expand All @@ -89,11 +95,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 @@ -105,11 +113,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 fec6053

Please sign in to comment.