From 74905b82bdc1183ee97feb50c2ed57c68c91e648 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Thu, 23 Aug 2018 12:44:40 -0600 Subject: [PATCH] Minimal handling of expectations and failures --- src/spectator/example.cr | 2 ++ src/spectator/expectation.cr | 8 ++++++++ src/spectator/expectation_failed_error.cr | 4 ++++ 3 files changed, 14 insertions(+) create mode 100644 src/spectator/expectation_failed_error.cr diff --git a/src/spectator/example.cr b/src/spectator/example.cr index 4c97fe9..2db98fc 100644 --- a/src/spectator/example.cr +++ b/src/spectator/example.cr @@ -5,6 +5,8 @@ module Spectator def run @block.call + rescue ex : ExpectationFailedError + puts ex end end end diff --git a/src/spectator/expectation.cr b/src/spectator/expectation.cr index 2aae0ac..6a152ea 100644 --- a/src/spectator/expectation.cr +++ b/src/spectator/expectation.cr @@ -1,3 +1,5 @@ +require "./matchers/matcher" + def expect(actual : T) forall T Spectator::Expectation.new(actual) end @@ -8,5 +10,11 @@ module Spectator protected def initialize(@actual : T) end + + def to(matcher : Matchers::Matcher) + unless matcher.match?(self) + raise ExpectationFailedError.new + end + end end end diff --git a/src/spectator/expectation_failed_error.cr b/src/spectator/expectation_failed_error.cr new file mode 100644 index 0000000..71a8e13 --- /dev/null +++ b/src/spectator/expectation_failed_error.cr @@ -0,0 +1,4 @@ +module Spectator + class ExpectationFailedError < Exception + end +end