diff --git a/src/spectator/matchers/array_matcher.cr b/src/spectator/matchers/array_matcher.cr index 92fb01a..6b2010a 100644 --- a/src/spectator/matchers/array_matcher.cr +++ b/src/spectator/matchers/array_matcher.cr @@ -23,7 +23,10 @@ module Spectator::Matchers # Actually performs the test against the expression. def match(actual : TestExpression(T)) : MatchData forall T - actual_elements = actual.value.to_a + actual_value = actual.value + return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a) + + actual_elements = actual_value.to_a expected_elements = expected.value.to_a missing, extra = compare_arrays(expected_elements, actual_elements) @@ -44,7 +47,10 @@ 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 - actual_elements = actual.value.to_a + actual_value = actual.value + return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a) + + actual_elements = actual_value.to_a expected_elements = expected.value.to_a missing, extra = compare_arrays(expected_elements, actual_elements) @@ -108,5 +114,9 @@ module Spectator::Matchers Array.new(count, element) end.flatten end + + private def unexpected(value, label) + raise "#{label} is not a collection (must respond to `#to_a`). #{label}: #{value.inspect}" + end end end diff --git a/src/spectator/matchers/unordered_array_matcher.cr b/src/spectator/matchers/unordered_array_matcher.cr index 876168b..6c964fc 100644 --- a/src/spectator/matchers/unordered_array_matcher.cr +++ b/src/spectator/matchers/unordered_array_matcher.cr @@ -20,7 +20,10 @@ module Spectator::Matchers # Actually performs the test against the expression. def match(actual : TestExpression(T)) : MatchData forall T - actual_elements = actual.value.to_a + actual_value = actual.value + return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a) + + actual_elements = actual_value.to_a expected_elements = expected.value.to_a missing, extra = array_diff(expected_elements, actual_elements) @@ -39,7 +42,10 @@ 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 - actual_elements = actual.value.to_a + actual_value = actual.value + return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:to_a) + + actual_elements = actual_value.to_a expected_elements = expected.value.to_a missing, extra = array_diff(expected_elements, actual_elements) @@ -71,5 +77,9 @@ module Spectator::Matchers {missing, extra} end + + private def unexpected(value, label) + raise "#{label} is not a collection (must respond to `#to_a`). #{label}: #{value.inspect}" + end end end