| 1 | = Assert = |
| 2 | |
| 3 | The assert statement provides a way to verify the internal state of your code. |
| 4 | To the assertion, you provide a [wiki:ConditionalExpr conditional expression] that is |
| 5 | expected to evaluate true.[[BR]] |
| 6 | If the verification fails (condition is false), an exception (!AssertException) is raised. |
| 7 | |
| 8 | The exception reports the source code of the condition, the file name, the line number, |
| 9 | the current object and information provided as an optional second argument to the assert. |
| 10 | However, it's rarely necessary to provide a second, informational argument because a |
| 11 | failed assertion also reports the value of every subexpression of the condition. |
| 12 | |
| 13 | For example |
| 14 | {{{ assert obj.foo < bar }}} |
| 15 | failing its assertion will report the value of obj.foo, obj and bar. |
| 16 | This makes assertions quick to write and easy to diagnose. |
| 17 | |
| 18 | == Grammar == |
| 19 | {{{ |
| 20 | assert <condition> [, info] |
| 21 | }}} |
| 22 | |
| 23 | == Platform == |
| 24 | |
| 25 | == Examples == |
| 26 | {{{ |
| 27 | assert i > 0 |
| 28 | |
| 29 | assert obj.foo < bar |
| 30 | |
| 31 | assert not badValue # test that a boolean value is false |
| 32 | |
| 33 | assert obj # assert obj not null |
| 34 | assert name.length # assert name is not 0 length |
| 35 | |
| 36 | assert 'result=' in resultString |
| 37 | |
| 38 | # assert with explicit info message |
| 39 | assert _didReset, 'Have not reset. Probably subclass overrides _reset but did not invoke base.' |
| 40 | }}} |
| 41 | |
| 42 | |
| 43 | == Notes == |