class Key
Represents a predicate that checks if a hash has a specific key.
Definitions
def initialize(name, predicate = nil)
Initialize a new Key predicate.
Signature
-
parameter
nameObject The key name to check for.
-
parameter
predicateObject, nil Optional predicate to apply to the key's value.
Implementation
def initialize(name, predicate = nil)
@name = name
@predicate = predicate
end
def print(output)
Print a representation of this predicate.
Signature
-
parameter
outputOutput The output target.
Implementation
def print(output)
output.write("key ")
output.variable(@name)
output.write(" ", @predicate, :reset)
end
def call(assertions, subject)
Evaluate this predicate against a subject.
Signature
-
parameter
assertionsAssertions The assertions instance to use.
-
parameter
subjectObject The subject to evaluate (should be a hash).
Implementation
def call(assertions, subject)
# We want to group all the assertions in to a distinct group:
assertions.nested(self, distinct: true) do |assertions|
assertions.assert(subject.key?(@name), "has key")
if @predicate
Expect.new(assertions, subject[@name]).to(@predicate)
end
end
end