Class: Sus::Have::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/sus/have.rb

Overview

Represents a predicate that checks if a hash has a specific key.

Instance Method Summary collapse

Constructor Details

#initialize(name, predicate = nil) ⇒ Key

Initialize a new Key predicate.



17
18
19
20
# File 'lib/sus/have.rb', line 17

def initialize(name, predicate = nil)
	@name = name
	@predicate = predicate
end

Instance Method Details

#call(assertions, subject) ⇒ Object

Evaluate this predicate against a subject.



31
32
33
34
35
36
37
38
39
# File 'lib/sus/have.rb', line 31

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

Print a representation of this predicate.



24
25
26
# File 'lib/sus/have.rb', line 24

def print(output)
	output.write("key ", :variable, @name.inspect, :reset, " ", @predicate, :reset)
end