Assertions: Difference between revisions
Add Ecstasy example
(Add Ecstasy example) |
|||
(5 intermediate revisions by 5 users not shown) | |||
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}}==
Line 835 ⟶ 884:
=={{header|Java}}==
In Java, the <code>assert</code> keyword is used to place an assertive statement within the program.<br />
A 'false' assertion will stop the program, as opposed to pausing, as with some other languages.<br />
It's worth noting that assertions were created specifically for the development and debugging of the program, and are not intended to be part of the control-flow.<br />
Some JVM implementations will have assertions disabled by default, so you'll have to enable them at the command line, switch '<tt>-ea</tt>' or '<tt>-enableassertions</tt>'.<br />
Inversely, to disable them, use '<tt>-da</tt>', or '<tt>-disableassertions</tt>'.<br />
For more information see [https://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html Oracle - Programming With Assertions].<br /><br />
The <code>assert</code> syntax is as follows.
<syntaxhighlight lang="java">
assert valueA == valueB;
</syntaxhighlight>
It is essentially a boolean expression, which if not met, will throw an <code>AssertionError</code> exception.<br />
It is effectively shorthand for the following code.
<syntaxhighlight lang="java">
if (valueA != valueB)
throw new AssertionError();
You can also specify a <code>String</code> with the assertion, which will be used as the exception's detail-message, which is displayed with the stack-trace upon error.
<syntaxhighlight lang="java">
assert valueA == valueB : "valueA is not 42";
</syntaxhighlight>
<pre>
Exception in thread "main" java.lang.AssertionError: valueA is not 42
at Example.main(Example.java:5)
</pre>
You can also specify any other Object or primitive data type as a message.<br />
If it's an object, the <code>toString</code> method will be used as the message.
<syntaxhighlight lang="java">
assert valueA == valueB : valueA;
</syntaxhighlight>
=={{header|JavaScript}}==
Line 1,278 ⟶ 1,338:
doAssert(a == 42, "Not 42!")</syntaxhighlight>
This second kind of assertion cannot be disabled.
=={{header|Nutt}}==
<syntaxhighlight lang="Nutt">
module main
demand 5==42
end
</syntaxhighlight>
=={{header|Oberon-2}}==
Line 1,788 ⟶ 1,857:
}
</syntaxhighlight>
=={{header|RPL}}==
There is no build-in assertion feature in RPL, but it can be easily programmed
≪ '''IF''' SWAP '''THEN''' ABORT '''ELSE''' DROP '''END''' ≫ ''''ASSRT'''' STO ''( condition message -- message )''
≪ 43 → a
≪ a 42 == "Not good" '''ASSRT'''
"This won't happen"
≫ ≫ EVAL
{{out}}
<pre>
1: "Not good"
</pre>
=={{header|Ruby}}==
Line 2,039 ⟶ 2,121:
=={{header|Wren}}==
Wren does not have assertions as such though we can write something similar.
<syntaxhighlight lang="
var assert = Fn.new { |cond|
Line 2,062 ⟶ 2,144:
{{libheader|Wren-debug}}
The above module also provides limited support for assertions.
<syntaxhighlight lang="
var x = 42
Line 2,107 ⟶ 2,189:
pub fn main() void {
const n: i64 = 43;
assert(n == 42); // On failure, an `unreachable` is reached
}</syntaxhighlight>
|