diff --git a/src/spectator/matchers/end_with_matcher.cr b/src/spectator/matchers/end_with_matcher.cr index 4d77a36..f555153 100644 --- a/src/spectator/matchers/end_with_matcher.cr +++ b/src/spectator/matchers/end_with_matcher.cr @@ -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 diff --git a/src/spectator/matchers/start_with_matcher.cr b/src/spectator/matchers/start_with_matcher.cr index aaa8846..02060e7 100644 --- a/src/spectator/matchers/start_with_matcher.cr +++ b/src/spectator/matchers/start_with_matcher.cr @@ -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)