Class: Object

Inherits:
BasicObject
Defined in:
lib/libis/tools/assert.rb

Instance Method Summary collapse

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:

require 'libis/tools/assert'
assert(value > 0, 'value should be positive number')

# using a code block:
require 'libis/tools/assert'
assert 'database is not idle' do
  db = get_database
  db.status == :IDLE
end

# using $DEBUG:
$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

Parameters:

  • test_expression (Object)

    the expression that will be evaluated; the message if a code block is present

  • message (String) (defaults to: 'assertion failure')

    exception message is no code block is present



43
44
45
46
47
48
49
50
51
# File 'lib/libis/tools/assert.rb', line 43

def assert(test_expression, message = 'assertion failure')
  if $DEBUG
    if block_given?
      message = test_expression
      test_expression = yield
    end
    raise AssertionFailure.new(message) unless test_expression
  end
end