Skip to content

luxe: assert


import "luxe: assert" for Assert

Simple assertions.

An assertion is a statement in code that is a strict rule. They prevent code from behaving in unexpected ways, by asserting that the code is acting in the way you intended. This can catch a lot of bugs, because it can enforce correct usage of code.

For example, if your function does not allow null for an argument, that is something you can assert. Then the user of your code knows that they’ve used your API incorrectly and can correct the issue.

An assertion calls Fiber.abort(), ending execution (unless handled higher up).

Assert.is_true(condition : Bool) : None

Assert that a particular condition is true.

//In this code, we expect that the player
//should never be here if they are not flying.
Assert.is_true(player.flying)

Assert.is_true(condition : Bool, message : String) : None

Assert that a particular condition is true, and display a message on abort.

//In this code, we expect that the player
//should never be here if they are not flying.
Assert.is_true(player.flying, "Expected player to be in a flying state")

Assert.is_false(condition : Bool) : None

Assert that a particular condition is false.

Assert.is_false(player.flying)

Assert.is_false(condition : Bool, message : String) : None

Assert that a particular condition is false, and display a message on abort.

Assert.is_false(player.flying, "Expected player NOT to be in a flying state")

Assert.not_null(value : Any) : None

Assert that a particular statement is not null.

//We require a valid player in this code
Assert.not_null(player)

Assert.not_null(value : Any, message : String) : None

Assert that a particular statement is not null.

Assert.not_null(player, "A valid player is required")

Assert.is_null(value : Any) : None

Assert that a particular statement is null.

//We assume the player is not holding something.
Assert.is_null(player.item_in_hand)

Assert.is_null(value : Any, message : String) : None

Assert that a particular statement is null, and display a message on abort.

Assert.is_null(player.item_in_hand, "Player must not have an item in hand when calling this")

Assert.equal(one : Any, other : Any) : None

Assert that a two values are the same. (mind that this uses a regular ==, which counts references with the same values as different and can be overridden)

Assert.equal(account.name, player.name)

Assert.equal(one : Any, other : Any, message : String) : None

Assert that a two values are the same, and display a message on abort. (mind that this uses a regular ==, which counts references with the same values as different and can be overridden)

Assert.equal(account.name, player.name, "account and player should always have the same name")