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
14
15
16
17
# File 'lib/sus/expect.rb', line 8

def initialize(assertions, subject, inverted: false, distinct: false)
	@assertions = assertions
	@subject = subject
	
	# We capture this here, as changes to state may cause the inspect output to change, affecting the output produced by #print.
	@inspect = @subject.inspect
	
	@inverted = inverted
	@distinct = true
end

Instance Attribute Details

#invertedObject (readonly)

Returns the value of attribute inverted.



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

def inverted
  @inverted
end

#subjectObject (readonly)

Returns the value of attribute subject.



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

def subject
  @subject
end

Instance Method Details

#and(predicate) ⇒ Object



49
50
51
# File 'lib/sus/expect.rb', line 49

def and(predicate)
	return to(predicate)
end

#notObject



22
23
24
25
26
# File 'lib/sus/expect.rb', line 22

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


28
29
30
31
32
33
34
35
36
# File 'lib/sus/expect.rb', line 28

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

#to(predicate) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/sus/expect.rb', line 38

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