diff --git a/src/spectator/expectation_failed.cr b/src/spectator/expectation_failed.cr index cd5e1c2..35d7d5f 100644 --- a/src/spectator/expectation_failed.cr +++ b/src/spectator/expectation_failed.cr @@ -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 diff --git a/src/spectator/expectations/example_expectations.cr b/src/spectator/expectations/example_expectations.cr new file mode 100644 index 0000000..28a9ef8 --- /dev/null +++ b/src/spectator/expectations/example_expectations.cr @@ -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 diff --git a/src/spectator/expectations/expectation_reporter.cr b/src/spectator/expectations/expectation_reporter.cr index 936e6f6..7a2108b 100644 --- a/src/spectator/expectations/expectation_reporter.cr +++ b/src/spectator/expectations/expectation_reporter.cr @@ -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 diff --git a/src/spectator/expectations/expectation_results.cr b/src/spectator/expectations/expectation_results.cr deleted file mode 100644 index 17195d7..0000000 --- a/src/spectator/expectations/expectation_results.cr +++ /dev/null @@ -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 diff --git a/src/spectator/failed_result.cr b/src/spectator/failed_result.cr index 20cf7d3..eb2e93c 100644 --- a/src/spectator/failed_result.cr +++ b/src/spectator/failed_result.cr @@ -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) diff --git a/src/spectator/successful_result.cr b/src/spectator/successful_result.cr index 714430d..bb1827b 100644 --- a/src/spectator/successful_result.cr +++ b/src/spectator/successful_result.cr @@ -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)