SusSourceSusRaiseException

class RaiseException

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

Definitions

def initialize(exception_class = Exception, message: nil)

Initialize a new RaiseException predicate.

Signature

parameter exception_class Class

The exception class to expect.

parameter message String | Regexp | Object | Nil

Optional message matcher.

Implementation

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

def and(predicate)

Add an additional predicate to check on the exception.

Signature

parameter predicate Object

The predicate to apply to the exception.

returns RaiseException

Returns self for method chaining.

Implementation

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

def call(assertions, subject)

Evaluate this predicate against a subject (block).

Signature

parameter assertions Assertions

The assertions instance to use.

parameter subject Proc

The block to evaluate.

Implementation

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

def print(output)

Print a representation of this predicate.

Signature

parameter output Output

The output target.

Implementation

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