Assert statements
An assert statement provides a concise syntax
for asserting expectations and enforcing a programmatic contract with
calling code. For this purpose, Gosu has an assert statement with the same
semantics and syntax as in Java.
You can use the Gosu assert statement in two different
forms:
assert expressionBoolean
assert expressionBoolean : expressionMessage
- expressionBoolean
is an expression that returns a
booleanresult. - expressionMessage is an expression that returns a value that becomes a detailed message.
For example:
assert i > 0 : iBy default, assert statements have no effect.
Assertions are disabled.
To enable assertions,
you must add the -ea flag
on the JVM that hosts the application or Studio.
If assertions are enabled, Gosu evaluates the initial expression expressionBoolean:
- If the expression returns
true, theassertstatement has no effect. - If the expression returns
false, Gosu throws anAssertionErrorexception. If you use the version of the statement that has a second expression, Gosu uses that value as an exception detail message. Otherwise, there is no exception detail message.
