Class: Sus::Expect

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(assertions, subject, inverted: false, distinct: false) ⇒ Expect

Returns a new instance of Expect.



8
9
10
11
12
13
# File 'lib/sus/expect.rb', line 8

def initialize(assertions, subject, inverted: false, distinct: false)
	@assertions = assertions
	@subject = subject
	@inverted = inverted
	@distinct = true
end

Instance Attribute Details

#invertedObject (readonly)

Returns the value of attribute inverted.



16
17
18
# File 'lib/sus/expect.rb', line 16

def inverted
  @inverted
end

#subjectObject (readonly)

Returns the value of attribute subject.



15
16
17
# File 'lib/sus/expect.rb', line 15

def subject
  @subject
end

Instance Method Details

#and(predicate) ⇒ Object



45
46
47
# File 'lib/sus/expect.rb', line 45

def and(predicate)
	return to(predicate)
end

#notObject



18
19
20
21
22
# File 'lib/sus/expect.rb', line 18

def not
	self.dup.tap do |expect|
		expect.instance_variable_set(:@inverted, !@inverted)
	end
end


24
25
26
27
28
29
30
31
32
# File 'lib/sus/expect.rb', line 24

def print(output)
	output.write("expect ", :variable, @subject.inspect, :reset, " ")
	
	if @inverted
		output.write("to not", :reset)
	else
		output.write("to", :reset)
	end
end

#to(predicate) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/sus/expect.rb', line 34

def to(predicate)
	# This gets the identity scoped to the current call stack, which ensures that any failures are logged at this point in the code.
	identity = @assertions.identity&.scoped
	
	@assertions.nested(self, inverted: @inverted, identity: identity, distinct: @distinct) do |assertions|
		predicate.call(assertions, @subject)
	end
	
	return self
end