Class: Sus::RaiseException

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

Overview

Represents a predicate that checks if a block raises an exception.

Instance Method Summary collapse

Constructor Details

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

Initialize a new RaiseException predicate.



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

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

Instance Method Details

#and(predicate) ⇒ Object

Add an additional predicate to check on the exception.



21
22
23
24
# File 'lib/sus/raise_exception.rb', line 21

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

#call(assertions, subject) ⇒ Object

Evaluate this predicate against a subject (block).



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sus/raise_exception.rb', line 29

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

Print a representation of this predicate.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sus/raise_exception.rb', line 51

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