Wiki
Version 1 (modified by hopscc, 14 years ago)

--

Assert

The assert statement provides a way to verify the internal state of your code. To the assertion, you provide a conditional expression that is expected to evaluate true.
If the verification fails (condition is false), an exception (AssertException) is raised.

The exception reports the source code of the condition, the file name, the line number, the current object and information provided as an optional second argument to the assert. However, it's rarely necessary to provide a second, informational argument because a failed assertion also reports the value of every subexpression of the condition.

For example assert obj.foo < bar failing its assertion will report the value of obj.foo, obj and bar. This makes assertions quick to write and easy to diagnose.

Grammar

assert <condition> [, info]

Platform

Examples

assert i > 0

assert obj.foo < bar

assert not badValue   # test that a boolean value is false

assert obj   # assert obj not null
assert name.length # assert name is not 0 length  

assert 'result=' in resultString 

# assert with explicit info message
assert _didReset, 'Have not reset. Probably subclass overrides _reset but did not invoke base.'

Notes