Add have_value matcher

This commit is contained in:
Michael Miller 2019-02-05 19:37:25 -07:00
parent 7698724241
commit e14e86a5f3
2 changed files with 42 additions and 0 deletions

View file

@ -412,6 +412,23 @@ module Spectator::DSL
have_key({{expected}})
end
# Indicates that some set, such as a `Hash`, has a given value.
# The `has_value?` method is used for this check.
#
# Examples:
# ```
# expect({foo: "bar"}).to have_value("bar")
# expect({"lucky" => 7}).to have_value(7)
# ```
macro have_value(expected)
::Spectator::Matchers::HaveValueMatcher.new({{expected.stringify}}, {{expected}})
end
# ditto
macro has_value(expected)
have_value({{expected}})
end
# Indicates that some value should have a set of attributes matching some conditions.
# A list of named arguments are expected.
# The names correspond to the attributes in the instance to check.

View file

@ -0,0 +1,25 @@
require "./value_matcher"
module Spectator::Matchers
# Matcher that tests whether a `Hash` (or similar type) has a given value.
# The set is checked with the `has_value?` method.
struct HaveValueMatcher(ExpectedType) < ValueMatcher(ExpectedType)
# Determines whether the matcher is satisfied with the value given to it.
# True is returned if the match was successful, false otherwise.
def match?(partial)
partial.actual.has_value?(expected)
end
# Describes the condition that satisfies the matcher.
# This is informational and displayed to the end-user.
def message(partial)
"Expected #{partial.label} to have value #{label}"
end
# Describes the condition that won't satsify the matcher.
# This is informational and displayed to the end-user.
def negated_message(partial)
"Expected #{partial.label} to not have value #{label}"
end
end
end