SusSourceSusHaveKey

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 name Object

The key name to check for.

parameter predicate Object, 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 output Output

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 assertions Assertions

The assertions instance to use.

parameter subject Object

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