Class: Sus::Expect

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

Overview

Represents an expectation that can be used with predicates to make assertions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initialize a new Expect instance.



14
15
16
17
18
19
20
21
22
23
# File 'lib/sus/expect.rb', line 14

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.



29
30
31
# File 'lib/sus/expect.rb', line 29

def inverted
  @inverted
end

#subjectObject (readonly)

Returns the value of attribute subject.



26
27
28
# File 'lib/sus/expect.rb', line 26

def subject
  @subject
end

Instance Method Details

#and(predicate) ⇒ Object

Apply another predicate to this expectation (alias for #to).



68
69
70
# File 'lib/sus/expect.rb', line 68

def and(predicate)
	return to(predicate)
end

#notObject

Invert this expectation (expect not).



33
34
35
36
37
# File 'lib/sus/expect.rb', line 33

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

Print a representation of this expectation.



41
42
43
44
45
46
47
48
49
# File 'lib/sus/expect.rb', line 41

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

#The subject being tested.=(subjectbeingtested. = (value)) ⇒ Object



26
# File 'lib/sus/expect.rb', line 26

attr :subject

#to(predicate) ⇒ Object

Apply a predicate to this expectation.



54
55
56
57
58
59
60
61
62
63
# File 'lib/sus/expect.rb', line 54

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

#Whether the expectation is inverted.=(theexpectationisinverted. = (value)) ⇒ Object



29
# File 'lib/sus/expect.rb', line 29

attr :inverted