Assertions: Difference between revisions

Add Ecstasy example
m (→‎{{header|Wren}}: Changed to Wren S/H)
(Add Ecstasy example)
Line 578:
<syntaxhighlight lang="text">
ASSERT(a = 42,'A is not 42!',FAIL);</syntaxhighlight>
 
=={{header|Ecstasy}}==
Ecstasy assertions raise an exception on failure. The default <code>assert</code> statement raises an <code>IllegalState</code>, but there are a few varieties:
 
{| class="wikitable"
! statement !! exception class
|-
| <code>assert</code> || <code>IllegalState</code>
|-
| <code>assert:arg</code> || <code>IllegalArgument</code>
|-
| <code>assert:bounds</code> || <code>OutOfBounds</code>
|-
| <code>assert:TODO</code> || <code>NotImplemented</code>
|}
 
The above assertions are always evaluated when they are encountered in the code; in other words, assertions are always enabled. There are three additional forms that allow developers to alter this behavior:
 
{| class="wikitable"
! statement !! exception class
|-
| <code>assert:test</code> || Evaluate when in "test" mode, but never evaluate in production mode
|-
| <code>assert:once</code> || Evaluate the assertion only the first time it is encountered
|-
| <code>assert:rnd(</code><i>n</i><code>)</code> || Statistically sample, such that the assertion is evaluated 1 out of every <code>n</code> times that the assertion is encountered
|}
 
This example will always evalaute (and fail) the assertion:
 
<syntaxhighlight lang="ecstasy">
module test {
void run() {
Int x = 7;
assert x == 42;
}
}
</syntaxhighlight>
 
Note that the text of the assertion expression and the relevant values are both included in the exception message:
 
{{out}}
<pre>
x$ xec test
 
2024-04-24 17:29:23.427 Service "test" (id=1) at ^test (CallLaterRequest: native), fiber 4: Unhandled exception: IllegalState: "x == 42": x=7
at run() (test.x:4)
at ^test (CallLaterRequest: native)
</pre>
 
=={{header|Eiffel}}==
162

edits