Class: Sus::RaiseException

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

Instance Method Summary collapse

Constructor Details

#initialize(exception_class = Exception, message: nil) ⇒ RaiseException

Returns a new instance of RaiseException.



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

def initialize(exception_class = Exception, message: nil)
	@exception_class = exception_class
	@message = message
	@predicate = nil
end

Instance Method Details

#and(predicate) ⇒ Object



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

def and(predicate)
	@predicate = predicate
	return self
end

#call(assertions, subject) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sus/raise_exception.rb', line 19

def call(assertions, subject)
	assertions.nested(self) do |assertions|
		begin
			subject.call
			
			# Didn't throw any exception, so the expectation failed:
			assertions.assert(false, "raised")
		rescue @exception_class => exception
			# Did it have the right message?
			if @message
				Expect.new(assertions, exception.message).to(@message)
			else
				assertions.assert(true, "raised")
			end
			
			@predicate&.call(assertions, exception)
		end
	end
end


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/sus/raise_exception.rb', line 39

def print(output)
	output.write("raise exception")
	
	if @exception_class
		output.write(" ", :variable, @exception_class, :reset)
	end
	
	if @message
		output.write(" with message ", :variable, @message, :reset)
	end
	
	if @predicate
		output.write(" and ", @predicate)
	end
end