From 64a233a9e1ea445b61c96fc139f86b111d0c762d Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Sun, 7 Oct 2018 13:46:51 -0600 Subject: [PATCH] Rename ExpectationRegistry to ExpectationReporter Some initial implementation for the class. --- .../expectations/expectation_registry.cr | 26 ----------------- .../expectations/expectation_reporter.cr | 28 +++++++++++++++++++ 2 files changed, 28 insertions(+), 26 deletions(-) delete mode 100644 src/spectator/expectations/expectation_registry.cr create mode 100644 src/spectator/expectations/expectation_reporter.cr diff --git a/src/spectator/expectations/expectation_registry.cr b/src/spectator/expectations/expectation_registry.cr deleted file mode 100644 index eb1216c..0000000 --- a/src/spectator/expectations/expectation_registry.cr +++ /dev/null @@ -1,26 +0,0 @@ -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 ExpectationRegistry - private getter? raise_on_failure : Bool - - private def initialize(@raise_on_failure = true) - end - - def report(result : Expectation::Result) : Nil - raise NotImplementedError.new("ExpectationRegistry#report") - end - - def self.current : ExpectationRegistry - raise NotImplementedError.new("ExpectationRegistry.current") - end - - def self.start(example : Example) : Nil - raise NotImplementedError.new("ExpectationRegistry.start") - end - - def self.finish : ExpectationResults - raise NotImplementedError.new("ExpectationRegistry.finish") - end - end -end diff --git a/src/spectator/expectations/expectation_reporter.cr b/src/spectator/expectations/expectation_reporter.cr new file mode 100644 index 0000000..d0241ea --- /dev/null +++ b/src/spectator/expectations/expectation_reporter.cr @@ -0,0 +1,28 @@ +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. + # 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) + + # Creates the reporter. + # When the `raise_on_failure` flag is set to true, + # which is the default, an exception will be raised + # on the first failure that is reported. + # To store failures and continue, set the flag to false. + def initialize(@raise_on_failure = true) + end + + # Stores the outcome of an expectation. + # 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 + end + end +end