Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/libis/tools/assert.rb
Instance Method Summary collapse
-
#assert(test_expression, message = 'assertion failure') ⇒ Object
Assert functionality as found in other languages (e.g. ‘C’).
Instance Method Details
#assert(test_expression, message = 'assertion failure') ⇒ Object
Assert functionality as found in other languages (e.g. ‘C’).
The assert is enabled/disabled by setting the $DEBUG global variable. If $DEBUG evaluates to true, the assertion is active.
If activated, the first argument will be evaluated and when it evaluates to true, an AssertionFailure exception will be raised with the message given as the second argument.
Alternatively, a code block may be passed to the assert. In that case the test expression is not evaluated, but used as the message for the expression. The assert will yield the code block and it’s result will be evaluated to decide if the exception will be thrown.
Examples:
$DEBUG = nil
assert false, 'assert 1' # nothing happens
$DEBUG = true
assert false, 'assert 2' # AssertionFailure 'assert 2' is raised
assert 'assert 3', 'assert 4' do
false
end # AssertionFailure 'assert 3' is raised
32 33 34 35 36 37 38 39 40 |
# File 'lib/libis/tools/assert.rb', line 32 def assert(test_expression, = 'assertion failure') if $DEBUG if block_given? = test_expression test_expression = yield end raise AssertionFailure.new() unless test_expression end end |