Move #to and #to_not to base type

Remove some explicit type restrictions.
This commit is contained in:
Michael Miller 2019-01-28 01:09:22 -07:00
parent 14db544441
commit 592d13d0d4
3 changed files with 26 additions and 23 deletions

View File

@ -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)

View File

@ -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

View File

@ -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