Workaround typing issues

This commit is contained in:
Michael Miller 2020-01-06 22:04:05 -07:00
parent 7868755eee
commit 590d81979e
2 changed files with 17 additions and 13 deletions

View file

@ -23,7 +23,8 @@ module Spectator::Matchers
# Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T
if (value = actual.value).responds_to?(:ends_with?)
value = actual.value
if value.is_a?(String) || value.responds_to?(:ends_with?)
match_ends_with(value, actual.label)
else
match_last(value, actual.label)
@ -33,10 +34,11 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T
if actual.value.responds_to?(:ends_with?)
negated_match_ends_with(actual)
value = actual.value
if value.is_a?(String) || value.responds_to?(:ends_with?)
negated_match_ends_with(value, actual.label)
else
negated_match_last(actual)
negated_match_last(value, actual.label)
end
end
@ -72,11 +74,11 @@ module Spectator::Matchers
# Checks whether the actual value does not end with the expected value.
# This method expects (and uses) the `#ends_with?` method on the value.
private def negated_match_ends_with(actual)
if actual.value.ends_with?(expected.value)
FailedMatchData.new(description, "#{actual.label} ends with #{expected.label} (using #ends_with?)",
private def negated_match_ends_with(actual_value, actual_label)
if actual_value.ends_with?(expected.value)
FailedMatchData.new(description, "#{actual_label} ends with #{expected.label} (using #ends_with?)",
expected: expected.value.inspect,
actual: actual.value.inspect
actual: actual_value.inspect
)
else
SuccessfulMatchData.new(description)
@ -85,12 +87,12 @@ module Spectator::Matchers
# Checks whether the last element of the value is not the expected value.
# This method expects that the actual value is a set (enumerable).
private def negated_match_last(actual)
list = actual.value.to_a
private def negated_match_last(actual_value, actual_label)
list = actual_value.to_a
last = list.last
if expected.value === last
FailedMatchData.new(description, "#{actual.label} ends with #{expected.label} (using expected === last)",
FailedMatchData.new(description, "#{actual_label} ends with #{expected.label} (using expected === last)",
expected: expected.value.inspect,
actual: last.inspect,
list: list.inspect

View file

@ -22,7 +22,8 @@ module Spectator::Matchers
# Actually performs the test against the expression.
def match(actual : TestExpression(T)) : MatchData forall T
if (value = actual.value).responds_to?(:starts_with?)
value = actual.value
if value.is_a?(String) || value.responds_to?(:starts_with?)
match_starts_with(value, actual.label)
else
match_first(value, actual.label)
@ -32,7 +33,8 @@ module Spectator::Matchers
# Performs the test against the expression, but inverted.
# A successful match with `#match` should normally fail for this method, and vice-versa.
def negated_match(actual : TestExpression(T)) : MatchData forall T
if (value = actual.value).responds_to?(:starts_with?)
value = actual.value
if value.is_a?(String) || value.responds_to?(:starts_with?)
negated_match_starts_with(value, actual.label)
else
negated_match_first(value, actual.label)