-
Notifications
You must be signed in to change notification settings - Fork 1
Assert
(introduced with v1.70)
Assert can be used to write tests as you may know them from other environments. The function verifies that ⍺≡⍵ and will signal an event so that the test-fn immediately ends. There are several options to pass a message to the environment about the error:
- the easiest way to document a failing Assert is by adding a comment to the line that contains the
Assertstatement. The error message will show the line with the failing test, so it'll be directly commented:
sink←1 Assert 0.5+0.4 ⍝ this should not fail with value 0.5+0.5!
The result of DTest will then be:
test_assert: Test returned character value = "Assertion failed: ERROR 777
test_assert[6] sink←1 Assert 0.5+0.4 ⍝ this should not fail with value 0.5+0.5!
- if the line would get too long when a comment is added, you can also place the comment in the line immediately preceding or following the test and it will be shown in the result as well:
test_assert: Test returned character value = "Assertion failed: this should not fail with value 0.5+0.5!
test_assert[7] sink←1 Assert 0.5+0.4
- finally, the reason for a failing test may not be "static" as in the examples before, but it could be given in a variable (ie. you call a fn and test its returncode. If it is non-zero, you want to display a msg that was generated by the fn you called). In that case, use
⊢to separate variable and test:
MyMsg⊢0 Assert rc:
Notice that this excerpt from a test that was written as a dfn, features an "empty guard" - they are a valid construct within dfns! (The MyMsg⊢ part obviously has no effect on the result, but is an optional "notational tweak" that enables Assert to create a meaningful message about the reason of a failed assertion!)
DTest will report the failing test as shown:
test_assert: Test returned character value = "Assertion failed: Error msg computed elsewhere...
test_assert[12] MyMsg⊢1 Assert 2×0.4:
(the text "Error msg computed elsewhere..." is the actual content of MyMsg!)
If an assertion fails and the -halt modifier was set when ]DTest was called, the execution of Assert will halt after detecting the difference and you then have an opportunity to inspect the environment.