From a8840351d5f482345a48732a4b2ba6668545912c Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 21 Jan 2021 00:03:57 -0700 Subject: [PATCH] More work hooking up expectations --- src/spectator/expectation.cr | 36 ++++++++++++++++++++++++++++++++++++ src/spectator/harness.cr | 17 ++++++++++++----- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/spectator/expectation.cr b/src/spectator/expectation.cr index 031e41d..a4306f2 100644 --- a/src/spectator/expectation.cr +++ b/src/spectator/expectation.cr @@ -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. diff --git a/src/spectator/harness.cr b/src/spectator/harness.cr index 2b42c8f..6cfd8dc 100644 --- a/src/spectator/harness.cr +++ b/src/spectator/harness.cr @@ -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