Assertions: Difference between revisions

Add Ecstasy example
(→‎{{header|Wren}}: Added a second version using Wren-debug.)
(Add Ecstasy example)
(10 intermediate revisions by 9 users not shown)
Line 174:
<syntaxhighlight lang="axe">A=42??Returnʳ</syntaxhighlight>
 
=={{header|BaConBASIC}}==
==={{header|BaCon}}===
<syntaxhighlight lang="qbasic">' Assertions
answer = assertion(42)
Line 220 ⟶ 221:
41</pre>
 
==={{header|BASIC256}}===
{{works with|BASIC256|2.0.0.11}}
<syntaxhighlight lang="basic256">
Line 231 ⟶ 232:
</syntaxhighlight>
 
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> PROCassert(a% = 42)
END
Line 577 ⟶ 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 834 ⟶ 884:
 
=={{header|Java}}==
In Java, the <code>assert</code> keyword is used to place an assertive statement within the program.<br />
<syntaxhighlight lang="java5">public class Assertions {
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 />
public static void main(String[] args) {
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 />
int a = 13;
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 />
// ... some real code here ...
The <code>assert</code> syntax is as follows.
 
<syntaxhighlight lang="java">
assert a == 42;
assert valueA == valueB;
// Throws an AssertionError when a is not 42.
</syntaxhighlight>
 
It is essentially a boolean expression, which if not met, will throw an <code>AssertionError</code> exception.<br />
assert a == 42 : "Error message";
It is effectively shorthand for the following code.
// Throws an AssertionError when a is not 42,
<syntaxhighlight lang="java">
// with "Error message" for the message.
if (valueA != valueB)
// The error message can be any non-void expression.
throw new AssertionError();
}
}</syntaxhighlight>
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">
Note: assertion checking is disabled by default when you run your program with the <tt>java</tt> command. You must provide the <tt>-ea</tt> (short for <tt>-enableassertions</tt>) flag in order to enable them.
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,097 ⟶ 1,158:
Trapping error may leave current stack of values with values so if we have above try {} a block of Stack New {} then we get old stack after exit of Stack New {} (this statement hold current stack, attach a new stack of value, and at the exit restore old stack). Another way is to use Flush which clear stack. Statement Flush Error clear all level of error information.
 
=====Making own logging for errors=====
 
<syntaxhighlight lang="m2000 interpreter">
Module Assert {
Line 1,120 ⟶ 1,181:
}
Module SaveIt {
.lastfilename$<=replace$("/", "-","Err"+date$(today)+timestr$(now, "-nn-mm")+".err")
Save.Doc .doc$,.lastfilename$
}
Line 1,138 ⟶ 1,199:
Try {
Test
Report "Run this"
Error "Hello"
Line 1,159 ⟶ 1,221:
}
Assert
</syntaxhighlight>
=====Using Assert Statement=====
Assert is a statement from 10th version (the previous example run because we can locally alter a statement using a module with same name).
 
When we run a program by applying a file gsb as argument the escape off statement applied by default. So asserts didn't work for programs. If we open the M2000 environment and then load a program, then the asserts work (or not if we use escape off). So this example can show x, from print x if we have x=10, but can show 0 if we run it with escape off statement.
 
<syntaxhighlight lang="m2000 interpreter">
// escape off // using escape off interpreter skip assert statements
x=random(0, 1)*10
try {
assert x=10
print x
}
</syntaxhighlight>
 
Line 1,263 ⟶ 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,340 ⟶ 1,424:
 
(assert i ===> 42)
; or
(assert (= i 42))
</syntaxhighlight>
{{Out}}
Line 1,350 ⟶ 1,436:
assertion error:
i must be 42
> (assert (= i 42))
>
assertion error:
(= i 42) is not a true
</pre>
 
Line 1,769 ⟶ 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 1,994 ⟶ 2,095:
See [[#C# and Visual Basic .NET]].
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="ecmascript">fn main(){
x := 43
Line 2,020 ⟶ 2,121:
=={{header|Wren}}==
Wren does not have assertions as such though we can write something similar.
<syntaxhighlight lang="ecmascriptwren">var assertEnabled = true
 
var assert = Fn.new { |cond|
Line 2,043 ⟶ 2,144:
{{libheader|Wren-debug}}
The above module also provides limited support for assertions.
<syntaxhighlight lang="ecmascriptwren">import "./debug" for Debug
 
var x = 42
Line 2,088 ⟶ 2,189:
 
pub fn main() void {
const n: i64 = 43;
assert(1 == 0); // On failure, an `unreachable` is reached
assert(n == 42); // On failure, an `unreachable` is reached
}</syntaxhighlight>
 
162

edits