From 592d13d0d48f3af8fcb147d084c84128aa1eee06 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Mon, 28 Jan 2019 01:09:22 -0700 Subject: [PATCH] Move #to and #to_not to base type Remove some explicit type restrictions. --- .../expectations/expectation_partial.cr | 25 +++++++++++++++++-- .../expectations/value_expectation.cr | 4 +-- .../expectations/value_expectation_partial.cr | 20 +-------------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/spectator/expectations/expectation_partial.cr b/src/spectator/expectations/expectation_partial.cr index b111fd6..83a0093 100644 --- a/src/spectator/expectations/expectation_partial.cr +++ b/src/spectator/expectations/expectation_partial.cr @@ -4,8 +4,8 @@ module Spectator::Expectations # The part of the expectation this class covers is the actual value. # This can also cover a block's behavior. # Sub-types of this class are returned by the `DSL::ExampleDSL.expect` call. - # Sub-types are expected to implement their own variation - # of the `#to` and `#not_to` methods. + # Sub-types are expected to implement `#eval`, + # which returns a corresponding sub-type of `Expectation`. abstract struct ExpectationPartial # User-friendly string displayed for the actual expression being tested. # For instance, in the expectation: @@ -21,6 +21,27 @@ module Spectator::Expectations private def initialize(@label) end + # Asserts that the `#actual` value matches some criteria. + # The criteria is defined by the matcher passed to this method. + def to(matcher) : Nil + report(eval(matcher)) + end + + # Asserts that the `#actual` value *does not* match some criteria. + # This is effectively the opposite of `#to`. + def to_not(matcher) : Nil + report(eval(matcher, true)) + end + + # ditto + @[AlwaysInline] + def not_to(matcher) : Nil + to_not(matcher) + end + + # Evaluates the expectation and returns it. + private abstract def eval(matcher, negated = false) : Expectation + # Reports an expectation to the current harness. private def report(expectation : Expectation) Internals::Harness.current.report_expectation(expectation) diff --git a/src/spectator/expectations/value_expectation.cr b/src/spectator/expectations/value_expectation.cr index 6477b08..7e05d5e 100644 --- a/src/spectator/expectations/value_expectation.cr +++ b/src/spectator/expectations/value_expectation.cr @@ -5,7 +5,7 @@ module Spectator::Expectations # There are two values - the actual and expected. # The actual value is what the SUT returned. # The expected value is what the test wants to see. - class ValueExpectation(ActualType, ExpectedType) < Expectation + class ValueExpectation(ActualType) < Expectation # Creates the expectation. # The `matched` flag should be true if the matcher is satisfied with the partial. # The `negated` flag should be true if the expectation is inverted. @@ -14,7 +14,7 @@ module Spectator::Expectations # the actual and expected value with matcher respectively. def initialize(matched, negated, @partial : ValueExpectationPartial(ActualType), - @matcher : Matchers::ValueMatcher(ExpectedType)) + @matcher : Matchers::Matcher) super(matched, negated) end diff --git a/src/spectator/expectations/value_expectation_partial.cr b/src/spectator/expectations/value_expectation_partial.cr index 09bf196..f303f45 100644 --- a/src/spectator/expectations/value_expectation_partial.cr +++ b/src/spectator/expectations/value_expectation_partial.cr @@ -21,26 +21,8 @@ module Spectator::Expectations super(@actual.to_s) end - # Asserts that the `#actual` value matches some criteria. - # The criteria is defined by the matcher passed to this method. - def to(matcher : Matchers::ValueMatcher(ExpectedType)) : Nil forall ExpectedType - report(eval(matcher)) - end - - # Asserts that the `#actual` value *does not* match some criteria. - # This is effectively the opposite of `#to`. - def to_not(matcher : Matchers::ValueMatcher(ExpectedType)) : Nil forall ExpectedType - report(eval(matcher, true)) - end - - # ditto - @[AlwaysInline] - def not_to(matcher : Matchers::ValueMatcher(ExpectedType)) : Nil forall ExpectedType - to_not(matcher) - end - # Evaluates the expectation and returns it. - private def eval(matcher : Matchers::ValueMatcher(ExpectedType), negated = false) forall ExpectedType + private def eval(matcher, negated = false) : Expectation matched = matcher.match?(self) ValueExpectation.new(matched, negated, self, matcher) end