Rename ExpectationResults to ExampleExpectations

Update to use new Expectation type.
This commit is contained in:
Michael Miller 2018-11-13 15:59:51 -07:00
parent b625cb69cf
commit a66262d197
6 changed files with 69 additions and 68 deletions

View File

@ -3,14 +3,13 @@ require "./example_failed"
module Spectator
# Exception that indicates a required expectation was not met in an example.
class ExpectationFailed < ExampleFailed
# Outcome of the expectation.
# Additional information can be retrieved through this.
getter result : Expectations::Expectation::Result
# Expectation that failed.
getter expectation : Expectations::Expectation
# Creates the exception.
# The exception string is generated from the expecation message.
def initialize(@result)
super(@result.actual_message)
def initialize(@expectation)
super(@expectation.actual_message)
end
end
end

View File

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

View File

@ -2,11 +2,11 @@ module Spectator::Expectations
# Tracks the expectations and their outcomes in an example.
# A single instance of this class should be associated with one example.
class ExpectationReporter
# All results are stored in this array.
# All expectations are stored in this array.
# The initial capacity is set to one,
# as that is the typical (and recommended)
# number of expectations per example.
@results = Array(Expectation::Result).new(1)
@expectations = Array(Expectation).new(1)
# Creates the reporter.
# 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,
# then this method will raise an exception
# when a failing result is given.
def report(result : Expectation::Result) : Nil
@results << result
raise ExpectationFailed.new(result) if result.failure? && @raise_on_failure
def report(expectation : Expectation) : Nil
@expectations << expectation
raise ExpectationFailed.new(expectation) if !expectation.satisfied? && @raise_on_failure
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.
def results : ExpectationResults
ExpectationResults.new(@results.dup)
def expectations : ExampleExpectations
ExampleExpectations.new(@expectations)
end
end
end

View File

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

View File

@ -3,7 +3,7 @@ require "./result"
module Spectator
class FailedResult < Result
getter error : Exception
getter expectations : Expectations::ExpectationResults
getter expectations : Expectations::ExampleExpectations
def initialize(example, elapsed, @expectations, @error)
super(example, elapsed)

View File

@ -2,7 +2,7 @@ require "./result"
module Spectator
class SuccessfulResult < Result
getter expectations : Expectations::ExpectationResults
getter expectations : Expectations::ExampleExpectations
def initialize(example, elapsed, @expectations)
super(example, elapsed)