Additional checks that the actual value satifies needed method

This commit is contained in:
Michael Miller 2020-04-03 11:47:11 -06:00
parent 7fadd92f62
commit 09e9c23115
6 changed files with 52 additions and 7 deletions

View file

@ -13,15 +13,21 @@ module Spectator::Matchers
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T 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| expected.value.all? do |item|
actual.value.includes?(item) actual_value.includes?(item)
end end
end end
# If the expectation is negated, then this method is called instead of `#match?`. # If the expectation is negated, then this method is called instead of `#match?`.
private def does_not_match?(actual : TestExpression(T)) : Bool forall T 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| !expected.value.any? do |item|
actual.value.includes?(item) actual_value.includes?(item)
end end
end end
@ -54,5 +60,9 @@ module Spectator::Matchers
superset: actual.value.inspect, superset: actual.value.inspect,
} }
end end
private def unexpected(value, label)
raise "#{label} is not a collection (must respond to `#includes?`). #{label}: #{value.inspect}"
end
end end
end end

View file

@ -13,7 +13,10 @@ module Spectator::Matchers
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T 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 end
# Message displayed when the matcher isn't satisifed. # Message displayed when the matcher isn't satisifed.
@ -36,5 +39,9 @@ module Spectator::Matchers
private def failure_message_when_negated(actual) : String private def failure_message_when_negated(actual) : String
"#{actual.label} is empty" "#{actual.label} is empty"
end end
private def unexpected(value, label)
raise "#{label} is not a collection (must respond to `#empty?`). #{label}: #{value.inspect}"
end
end end
end end

View file

@ -13,7 +13,10 @@ module Spectator::Matchers
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T 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 end
# Message displayed when the matcher isn't satisifed. # Message displayed when the matcher isn't satisifed.
@ -47,5 +50,9 @@ module Spectator::Matchers
actual: set.inspect, actual: set.inspect,
} }
end end
private def unexpected(value, label)
raise "#{label} is not hash-like (must respond to `#has_key?`). #{label}: #{value.inspect}"
end
end end
end end

View file

@ -13,7 +13,10 @@ module Spectator::Matchers
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T 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 end
# Message displayed when the matcher isn't satisifed. # Message displayed when the matcher isn't satisifed.
@ -47,5 +50,9 @@ module Spectator::Matchers
actual: set.inspect, actual: set.inspect,
} }
end end
private def unexpected(value, label)
raise "#{label} is not hash-like (must respond to `#has_value?`). #{label}: #{value.inspect}"
end
end end
end end

View file

@ -13,7 +13,10 @@ module Spectator::Matchers
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T 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 end
# Message displayed when the matcher isn't satisifed. # Message displayed when the matcher isn't satisifed.
@ -54,5 +57,9 @@ module Spectator::Matchers
actual: actual.value.size.inspect, actual: actual.value.size.inspect,
} }
end end
private def unexpected(value, label)
raise "#{label} must respond to `#size`. #{label}: #{value.inspect}"
end
end end
end end

View file

@ -13,7 +13,10 @@ module Spectator::Matchers
# Checks whether the matcher is satisifed with the expression given to it. # Checks whether the matcher is satisifed with the expression given to it.
private def match?(actual : TestExpression(T)) : Bool forall T 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 end
# Message displayed when the matcher isn't satisifed. # Message displayed when the matcher isn't satisifed.
@ -54,5 +57,9 @@ module Spectator::Matchers
actual: actual.value.size.inspect, actual: actual.value.size.inspect,
} }
end end
private def unexpected(value, label)
raise "#{label} must respond to `#size`. #{label}: #{value.inspect}"
end
end end
end end