From 4ccc27321e9d48ba38e7ad62bd4772c8efa1e8ab Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Wed, 23 Jan 2019 20:31:28 -0700 Subject: [PATCH] Add less-than matcher --- src/spectator/matchers/be_comparison.cr | 4 ++-- src/spectator/matchers/less_than_matcher.cr | 25 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/spectator/matchers/less_than_matcher.cr diff --git a/src/spectator/matchers/be_comparison.cr b/src/spectator/matchers/be_comparison.cr index a8d7f0e..ea12985 100644 --- a/src/spectator/matchers/be_comparison.cr +++ b/src/spectator/matchers/be_comparison.cr @@ -10,8 +10,8 @@ module Spectator::Matchers # ``` # expect(0).to be < 1 # ``` - def <(other : ExpectedType) forall ExpectedType - raise NotImplementedError.new("be <") + def <(expected : ExpectedType) forall ExpectedType + LessThanMatcher.new(expected) end # Creates a matcher that checks if a value is less than or equal to an expected value. diff --git a/src/spectator/matchers/less_than_matcher.cr b/src/spectator/matchers/less_than_matcher.cr new file mode 100644 index 0000000..363b564 --- /dev/null +++ b/src/spectator/matchers/less_than_matcher.cr @@ -0,0 +1,25 @@ +require "./value_matcher" + +module Spectator::Matchers + # Matcher that tests whether one value is less than another. + # The values are compared with the `<` operator. + struct LessThanMatcher(ExpectedType) < ValueMatcher(ExpectedType) + # Determines whether the matcher is satisfied with the value given to it. + # True is returned if the match was successful, false otherwise. + def match?(partial : Expectations::ValueExpectationPartial(ActualType)) : Bool forall ActualType + partial.actual < expected + end + + # Describes the condition that satisfies the matcher. + # This is informational and displayed to the end-user. + def message(partial : Expectations::ValueExpectationPartial(ActualType)) : String forall ActualType + "Expected #{partial.label} to be less than #{label} (using <)" + end + + # Describes the condition that won't satsify the matcher. + # This is informational and displayed to the end-user. + def negated_message(partial : Expectations::ValueExpectationPartial(ActualType)) : String forall ActualType + "Expected #{partial.label} to not be less than #{label} (using <)" + end + end +end