From 09e9c2311539994751c68ac319d17ead589a1585 Mon Sep 17 00:00:00 2001 From: Michael Miller Date: Fri, 3 Apr 2020 11:47:11 -0600 Subject: [PATCH] Additional checks that the actual value satifies needed method --- src/spectator/matchers/contain_matcher.cr | 14 ++++++++++++-- src/spectator/matchers/empty_matcher.cr | 9 ++++++++- src/spectator/matchers/have_key_matcher.cr | 9 ++++++++- src/spectator/matchers/have_value_matcher.cr | 9 ++++++++- src/spectator/matchers/size_matcher.cr | 9 ++++++++- src/spectator/matchers/size_of_matcher.cr | 9 ++++++++- 6 files changed, 52 insertions(+), 7 deletions(-) diff --git a/src/spectator/matchers/contain_matcher.cr b/src/spectator/matchers/contain_matcher.cr index c009851..c28c5b2 100644 --- a/src/spectator/matchers/contain_matcher.cr +++ b/src/spectator/matchers/contain_matcher.cr @@ -13,15 +13,21 @@ module Spectator::Matchers # Checks whether the matcher is satisifed with the expression given to it. private def match?(actual : TestExpression(T)) : Bool forall T + actual_value = actual.value + return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:includes?) + expected.value.all? do |item| - actual.value.includes?(item) + actual_value.includes?(item) end end # If the expectation is negated, then this method is called instead of `#match?`. private def does_not_match?(actual : TestExpression(T)) : Bool forall T + actual_value = actual.value + return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:includes?) + !expected.value.any? do |item| - actual.value.includes?(item) + actual_value.includes?(item) end end @@ -54,5 +60,9 @@ module Spectator::Matchers superset: actual.value.inspect, } end + + private def unexpected(value, label) + raise "#{label} is not a collection (must respond to `#includes?`). #{label}: #{value.inspect}" + end end end diff --git a/src/spectator/matchers/empty_matcher.cr b/src/spectator/matchers/empty_matcher.cr index c64c6d7..e032794 100644 --- a/src/spectator/matchers/empty_matcher.cr +++ b/src/spectator/matchers/empty_matcher.cr @@ -13,7 +13,10 @@ module Spectator::Matchers # Checks whether the matcher is satisifed with the expression given to it. private def match?(actual : TestExpression(T)) : Bool forall T - actual.value.empty? + actual_value = actual.value + return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:empty?) + + actual_value.empty? end # Message displayed when the matcher isn't satisifed. @@ -36,5 +39,9 @@ module Spectator::Matchers private def failure_message_when_negated(actual) : String "#{actual.label} is empty" end + + private def unexpected(value, label) + raise "#{label} is not a collection (must respond to `#empty?`). #{label}: #{value.inspect}" + end end end diff --git a/src/spectator/matchers/have_key_matcher.cr b/src/spectator/matchers/have_key_matcher.cr index c579930..5c07590 100644 --- a/src/spectator/matchers/have_key_matcher.cr +++ b/src/spectator/matchers/have_key_matcher.cr @@ -13,7 +13,10 @@ module Spectator::Matchers # Checks whether the matcher is satisifed with the expression given to it. private def match?(actual : TestExpression(T)) : Bool forall T - actual.value.has_key?(expected.value) + actual_value = actual.value + return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:has_key?) + + actual_value.has_key?(expected.value) end # Message displayed when the matcher isn't satisifed. @@ -47,5 +50,9 @@ module Spectator::Matchers actual: set.inspect, } end + + private def unexpected(value, label) + raise "#{label} is not hash-like (must respond to `#has_key?`). #{label}: #{value.inspect}" + end end end diff --git a/src/spectator/matchers/have_value_matcher.cr b/src/spectator/matchers/have_value_matcher.cr index 1f3e4a9..54d5f40 100644 --- a/src/spectator/matchers/have_value_matcher.cr +++ b/src/spectator/matchers/have_value_matcher.cr @@ -13,7 +13,10 @@ module Spectator::Matchers # Checks whether the matcher is satisifed with the expression given to it. private def match?(actual : TestExpression(T)) : Bool forall T - actual.value.has_value?(expected.value) + actual_value = actual.value + return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:has_value?) + + actual_value.has_value?(expected.value) end # Message displayed when the matcher isn't satisifed. @@ -47,5 +50,9 @@ module Spectator::Matchers actual: set.inspect, } end + + private def unexpected(value, label) + raise "#{label} is not hash-like (must respond to `#has_value?`). #{label}: #{value.inspect}" + end end end diff --git a/src/spectator/matchers/size_matcher.cr b/src/spectator/matchers/size_matcher.cr index d309849..f707159 100644 --- a/src/spectator/matchers/size_matcher.cr +++ b/src/spectator/matchers/size_matcher.cr @@ -13,7 +13,10 @@ module Spectator::Matchers # Checks whether the matcher is satisifed with the expression given to it. private def match?(actual : TestExpression(T)) : Bool forall T - expected.value == actual.value.size + actual_value = actual.value + return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:size?) + + expected.value == actual_value.size end # Message displayed when the matcher isn't satisifed. @@ -54,5 +57,9 @@ module Spectator::Matchers actual: actual.value.size.inspect, } end + + private def unexpected(value, label) + raise "#{label} must respond to `#size`. #{label}: #{value.inspect}" + end end end diff --git a/src/spectator/matchers/size_of_matcher.cr b/src/spectator/matchers/size_of_matcher.cr index 0d7bc30..8784cea 100644 --- a/src/spectator/matchers/size_of_matcher.cr +++ b/src/spectator/matchers/size_of_matcher.cr @@ -13,7 +13,10 @@ module Spectator::Matchers # Checks whether the matcher is satisifed with the expression given to it. private def match?(actual : TestExpression(T)) : Bool forall T - expected.value.size == actual.value.size + actual_value = actual.value + return unexpected(actual_value, actual.label) unless actual_value.responds_to?(:size?) + + expected.value.size == actual_value.size end # Message displayed when the matcher isn't satisifed. @@ -54,5 +57,9 @@ module Spectator::Matchers actual: actual.value.size.inspect, } end + + private def unexpected(value, label) + raise "#{label} must respond to `#size`. #{label}: #{value.inspect}" + end end end