Class: Sus::Have::Key
- Inherits:
-
Object
- Object
- Sus::Have::Key
- Defined in:
- lib/sus/have.rb
Overview
Represents a predicate that checks if a hash has a specific key.
Instance Method Summary collapse
-
#call(assertions, subject) ⇒ Object
Evaluate this predicate against a subject.
-
#initialize(name, predicate = nil) ⇒ Key
constructor
Initialize a new Key predicate.
-
#print(output) ⇒ Object
Print a representation of this predicate.
Constructor Details
#initialize(name, predicate = nil) ⇒ Key
Initialize a new Key predicate.
18 19 20 21 |
# File 'lib/sus/have.rb', line 18 def initialize(name, predicate = nil) @name = name @predicate = predicate end |
Instance Method Details
#call(assertions, subject) ⇒ Object
Evaluate this predicate against a subject.
34 35 36 37 38 39 40 41 42 |
# File 'lib/sus/have.rb', line 34 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(output) ⇒ Object
Print a representation of this predicate.
25 26 27 28 29 |
# File 'lib/sus/have.rb', line 25 def print(output) output.write("key ") output.variable(@name) output.write(" ", @predicate, :reset) end |