More work hooking up expectations

This commit is contained in:
Michael Miller 2021-01-21 00:03:57 -07:00
parent b7ed4ec14c
commit a8840351d5
No known key found for this signature in database
GPG Key ID: F9A0C5C65B162436
2 changed files with 48 additions and 5 deletions

View File

@ -11,6 +11,42 @@ module Spectator
# for instance using the *should* syntax or dynamically created expectations.
getter source : Source?
# Indicates whether the expectation was met.
def satisfied?
@match_data.matched?
end
# Indicates whether the expectation was not met.
def failed?
!satisfied?
end
# If nil, then the match was successful.
def failure_message?
@match_data.as?(Matchers::FailedMatchData).try(&.failure_message)
end
# Description of why the match failed.
def failure_message
failure_message?.not_nil!
end
# Additional information about the match, useful for debug.
# If nil, then the match was successful.
def values?
@match_data.as?(Matchers::FailedMatchData).try(&.values)
end
# Additional information about the match, useful for debug.
def values
values?.not_nil!
end
def description
@match_data.description
end
# Creates the expectation.
# The *match_data* comes from the result of calling `Matcher#match`.
# The *source* is the location of the expectation in source code, if available.

View File

@ -1,4 +1,5 @@
require "./error_result"
require "./expectation"
require "./pass_result"
require "./result"
@ -29,6 +30,8 @@ module Spectator
# Instead, methods the test calls can access it.
# For instance, an expectation reporting a result.
class Harness
Log = ::Spectator::Log.for(self)
# Retrieves the harness for the current running example.
class_getter! current : self
@ -56,8 +59,9 @@ module Spectator
translate(*outcome)
end
def report(expectation)
# TODO
def report(expectation : Expectation) : Nil
Log.debug { "Reporting expectation #{expectation}" }
raise ExpectationFailed.new(expectation) if expectation.failed?
end
# Stores a block of code to be executed later.
@ -84,10 +88,13 @@ module Spectator
# Takes the *elapsed* time and a possible *error* from the test.
# Returns a type of `Result`.
private def translate(elapsed, error) : Result
if error
ErrorResult.new(elapsed, error)
else
case error
when nil
PassResult.new(elapsed)
when ExpectationFailed
FailResult.new(elapsed, error)
else
ErrorResult.new(elapsed, error)
end
end