Rename ExpectationResults to ExampleExpectations
Update to use new Expectation type.
This commit is contained in:
parent
b625cb69cf
commit
a66262d197
|
@ -3,14 +3,13 @@ require "./example_failed"
|
||||||
module Spectator
|
module Spectator
|
||||||
# Exception that indicates a required expectation was not met in an example.
|
# Exception that indicates a required expectation was not met in an example.
|
||||||
class ExpectationFailed < ExampleFailed
|
class ExpectationFailed < ExampleFailed
|
||||||
# Outcome of the expectation.
|
# Expectation that failed.
|
||||||
# Additional information can be retrieved through this.
|
getter expectation : Expectations::Expectation
|
||||||
getter result : Expectations::Expectation::Result
|
|
||||||
|
|
||||||
# Creates the exception.
|
# Creates the exception.
|
||||||
# The exception string is generated from the expecation message.
|
# The exception string is generated from the expecation message.
|
||||||
def initialize(@result)
|
def initialize(@expectation)
|
||||||
super(@result.actual_message)
|
super(@expectation.actual_message)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
require "./expectation"
|
||||||
|
|
||||||
|
module Spectator::Expectations
|
||||||
|
# Collection of expectations from an example.
|
||||||
|
class ExampleExpectations
|
||||||
|
include Enumerable(Expectation)
|
||||||
|
|
||||||
|
# Creates the collection.
|
||||||
|
def initialize(@expectations : Array(Expectation))
|
||||||
|
end
|
||||||
|
|
||||||
|
# Iterates through all expectations.
|
||||||
|
def each
|
||||||
|
@expectations.each do |expectation|
|
||||||
|
yield expectation
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a collection of only the satisfied expectations.
|
||||||
|
def satisfied : Enumerable(Expectation)
|
||||||
|
@expectations.select(&.satisfied?)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Iterates over only the satisfied expectations.
|
||||||
|
def each_satisfied
|
||||||
|
@expectations.each do |expectation|
|
||||||
|
yield expectation if expectation.satisfied?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Returns a collection of only the unsatisfied expectations.
|
||||||
|
def unsatisfied : Enumerable(Expectation)
|
||||||
|
@expectations.reject(&.satisfied?)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Iterates over only the unsatisfied expectations.
|
||||||
|
def each_unsatisfied
|
||||||
|
@expectations.each do |expectation|
|
||||||
|
yield expectation unless expectation.satisfied?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Determines whether the example was successful
|
||||||
|
# based on if all expectations were satisfied.
|
||||||
|
def successful?
|
||||||
|
@expectations.all?(&.satisfied?)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Determines whether the example failed
|
||||||
|
# based on if any expectations were not satisfied.
|
||||||
|
def failed?
|
||||||
|
!successful?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -2,11 +2,11 @@ module Spectator::Expectations
|
||||||
# Tracks the expectations and their outcomes in an example.
|
# Tracks the expectations and their outcomes in an example.
|
||||||
# A single instance of this class should be associated with one example.
|
# A single instance of this class should be associated with one example.
|
||||||
class ExpectationReporter
|
class ExpectationReporter
|
||||||
# All results are stored in this array.
|
# All expectations are stored in this array.
|
||||||
# The initial capacity is set to one,
|
# The initial capacity is set to one,
|
||||||
# as that is the typical (and recommended)
|
# as that is the typical (and recommended)
|
||||||
# number of expectations per example.
|
# number of expectations per example.
|
||||||
@results = Array(Expectation::Result).new(1)
|
@expectations = Array(Expectation).new(1)
|
||||||
|
|
||||||
# Creates the reporter.
|
# Creates the reporter.
|
||||||
# When the `raise_on_failure` flag is set to true,
|
# When the `raise_on_failure` flag is set to true,
|
||||||
|
@ -20,15 +20,15 @@ module Spectator::Expectations
|
||||||
# If the raise on failure flag is set to true,
|
# If the raise on failure flag is set to true,
|
||||||
# then this method will raise an exception
|
# then this method will raise an exception
|
||||||
# when a failing result is given.
|
# when a failing result is given.
|
||||||
def report(result : Expectation::Result) : Nil
|
def report(expectation : Expectation) : Nil
|
||||||
@results << result
|
@expectations << expectation
|
||||||
raise ExpectationFailed.new(result) if result.failure? && @raise_on_failure
|
raise ExpectationFailed.new(expectation) if !expectation.satisfied? && @raise_on_failure
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns the reported expectation results from the example.
|
# Returns the reported expectations from the example.
|
||||||
# This should be run after the example has finished.
|
# This should be run after the example has finished.
|
||||||
def results : ExpectationResults
|
def expectations : ExampleExpectations
|
||||||
ExpectationResults.new(@results.dup)
|
ExampleExpectations.new(@expectations)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,53 +0,0 @@
|
||||||
module Spectator::Expectations
|
|
||||||
# Collection of results of expectations from an example.
|
|
||||||
class ExpectationResults
|
|
||||||
include Enumerable(Expectation::Result)
|
|
||||||
|
|
||||||
# Creates the collection.
|
|
||||||
def initialize(@results : Array(Expectation::Result))
|
|
||||||
end
|
|
||||||
|
|
||||||
# Iterates through all expectation results.
|
|
||||||
def each
|
|
||||||
@results.each do |result|
|
|
||||||
yield result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Returns a collection of only the successful expectation results.
|
|
||||||
def successes : Enumerable(Expectation::Result)
|
|
||||||
@results.select(&.successful?)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Iterates over only the successful expectation results.
|
|
||||||
def each_success
|
|
||||||
@results.each do |result|
|
|
||||||
yield result if result.successful?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Returns a collection of only the failed expectation results.
|
|
||||||
def failures : Enumerable(Expectation::Result)
|
|
||||||
@results.select(&.failure?)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Iterates over only the failed expectation results.
|
|
||||||
def each_failure
|
|
||||||
@results.each do |result|
|
|
||||||
yield result if result.failure?
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Determines whether the example was successful
|
|
||||||
# based on if all expectations were satisfied.
|
|
||||||
def successful?
|
|
||||||
@results.all?(&.successful?)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Determines whether the example failed
|
|
||||||
# based on if any expectations were not satisfied.
|
|
||||||
def failed?
|
|
||||||
@results.any?(&.failure?)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -3,7 +3,7 @@ require "./result"
|
||||||
module Spectator
|
module Spectator
|
||||||
class FailedResult < Result
|
class FailedResult < Result
|
||||||
getter error : Exception
|
getter error : Exception
|
||||||
getter expectations : Expectations::ExpectationResults
|
getter expectations : Expectations::ExampleExpectations
|
||||||
|
|
||||||
def initialize(example, elapsed, @expectations, @error)
|
def initialize(example, elapsed, @expectations, @error)
|
||||||
super(example, elapsed)
|
super(example, elapsed)
|
||||||
|
|
|
@ -2,7 +2,7 @@ require "./result"
|
||||||
|
|
||||||
module Spectator
|
module Spectator
|
||||||
class SuccessfulResult < Result
|
class SuccessfulResult < Result
|
||||||
getter expectations : Expectations::ExpectationResults
|
getter expectations : Expectations::ExampleExpectations
|
||||||
|
|
||||||
def initialize(example, elapsed, @expectations)
|
def initialize(example, elapsed, @expectations)
|
||||||
super(example, elapsed)
|
super(example, elapsed)
|
||||||
|
|
Loading…
Reference in New Issue