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 boolean result.
  • expressionMessage is an expression that returns a value that becomes a detailed message.

For example:

assert i > 0 : i

By 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, the assert statement has no effect.
  • If the expression returns false, Gosu throws an AssertionError exception. 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.