This is a simple example about using asserts by Christopher Oliver, 24-May-2007 on Orangevolt that works in JFXPad.
To do something with an assertion you use "assert" triggers:
trigger on not assert assertion {
throw "Assertion failed: {assertion}";
}
trigger on assert assertion {
println("Assertion passed: {assertion}");
}
var a = [1,2];
assert sizeof a == 2;
assert sizeof a == 1;
compile thread: Thread[AWT-EventQueue-0,6,main]
compile 0.0060
Assertion passed: Assertion {description: [] sourceURL: 'file:/Users/coliver/javafx/trunk/assert.fx' lineNumber: 10 assertion: 'sizeof a == 2' result: true}
uncaught FX exception: 'Assertion failed: Assertion {description: [] sourceURL: 'file:/Users/coliver/javafx/trunk/assert.fx' lineNumber: 11 assertion: 'sizeof a == 1' result: false}'
at throw "Assertion failed: {assertion}"; ("file:/Users/coliver/javafx/trunk/assert.fx", Line 2)
You can have multiple such triggers. Also you can pass a description of the assertion like this:
assert sizeof a == 1 : "This should fail: size = {sizeof a}";
The object passed to your trigger is an instance of the Assertion class which looks like this
public class Assertion {
public attribute description: String*;
public attribute sourceURL: String?;
public attribute lineNumber: Number;
public attribute assertion: String;
public attribute result: Boolean;
}
The value after the ":" in the assert statement (which may be a list of trings) will be assigned to the description attribute of the assertion bject passed to your triggers.