|
| 1 | +#{width="96em"} |
| 2 | + |
| 3 | +# `luxe` API (`2021.0.3`) |
| 4 | + |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## `luxe: assert` module |
| 9 | + |
| 10 | +- [Assert](#assert) |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +### Assert |
| 15 | +`:::js import "luxe: assert" for Assert` |
| 16 | +> Simple assertions. |
| 17 | +
|
| 18 | +An assertion is a statement in code that is a strict rule. |
| 19 | +They prevent code from behaving in unexpected ways, by asserting that the code is acting in the way you intended. |
| 20 | +This can catch a lot of bugs, because it can enforce correct usage of code. |
| 21 | + |
| 22 | +For example, if your function does not allow null for an argument, that is something you can assert. |
| 23 | +Then the user of your code knows that they've used your API incorrectly and can correct the issue. |
| 24 | + |
| 25 | +An assertion calls `Fiber.abort()`, ending execution (unless handled higher up). |
| 26 | + |
| 27 | +- [is_true](#Assert.is_true)(**condition**: `Any`) |
| 28 | +- [is_true](#Assert.is_true+2)(**condition**: `Any`, **message**: `Any`) |
| 29 | +- [is_false](#Assert.is_false)(**condition**: `Any`) |
| 30 | +- [is_false](#Assert.is_false+2)(**condition**: `Any`, **message**: `Any`) |
| 31 | +- [not_null](#Assert.not_null)(**value**: `Any`) |
| 32 | +- [not_null](#Assert.not_null+2)(**value**: `Any`, **message**: `Any`) |
| 33 | +- [is_null](#Assert.is_null)(**value**: `Any`) |
| 34 | +- [is_null](#Assert.is_null+2)(**value**: `Any`, **message**: `Any`) |
| 35 | + |
| 36 | +<hr/> |
| 37 | +<endpoint module="luxe: assert" class="Assert" signature="is_true(condition : Any)"></endpoint> |
| 38 | +<signature id="Assert.is_true">Assert.is_true(**condition**: `Any`) |
| 39 | +<a class="headerlink" href="#Assert.is_true" title="Permanent link">¶</a></signature> |
| 40 | +<span class='api_ret'>returns</span> `:::js None` |
| 41 | +> Assert that a particular condition is true. |
| 42 | +```js |
| 43 | +//In this code, we expect that the player |
| 44 | +//should never be here if they are not flying. |
| 45 | +Assert.is_true(player.flying) |
| 46 | +``` |
| 47 | + |
| 48 | +<endpoint module="luxe: assert" class="Assert" signature="is_true(condition : Any, message : Any)"></endpoint> |
| 49 | +<signature id="Assert.is_true+2">Assert.is_true(**condition**: `Any`, **message**: `Any`) |
| 50 | +<a class="headerlink" href="#Assert.is_true+2" title="Permanent link">¶</a></signature> |
| 51 | +<span class='api_ret'>returns</span> `:::js None` |
| 52 | +> Assert that a particular condition is true, and display a message on abort. |
| 53 | +```js |
| 54 | +//In this code, we expect that the player |
| 55 | +//should never be here if they are not flying. |
| 56 | +Assert.is_true(player.flying, "Expected player to be in a flying state") |
| 57 | +``` |
| 58 | + |
| 59 | +<endpoint module="luxe: assert" class="Assert" signature="is_false(condition : Any)"></endpoint> |
| 60 | +<signature id="Assert.is_false">Assert.is_false(**condition**: `Any`) |
| 61 | +<a class="headerlink" href="#Assert.is_false" title="Permanent link">¶</a></signature> |
| 62 | +<span class='api_ret'>returns</span> `:::js None` |
| 63 | +> Assert that a particular condition is false. |
| 64 | +```js |
| 65 | +Assert.is_false(player.flying) |
| 66 | +``` |
| 67 | + |
| 68 | +<endpoint module="luxe: assert" class="Assert" signature="is_false(condition : Any, message : Any)"></endpoint> |
| 69 | +<signature id="Assert.is_false+2">Assert.is_false(**condition**: `Any`, **message**: `Any`) |
| 70 | +<a class="headerlink" href="#Assert.is_false+2" title="Permanent link">¶</a></signature> |
| 71 | +<span class='api_ret'>returns</span> `:::js None` |
| 72 | +> Assert that a particular condition is false, and display a message on abort. |
| 73 | +```js |
| 74 | +Assert.is_false(player.flying, "Expected player NOT to be in a flying state") |
| 75 | +``` |
| 76 | + |
| 77 | +<endpoint module="luxe: assert" class="Assert" signature="not_null(value : Any)"></endpoint> |
| 78 | +<signature id="Assert.not_null">Assert.not_null(**value**: `Any`) |
| 79 | +<a class="headerlink" href="#Assert.not_null" title="Permanent link">¶</a></signature> |
| 80 | +<span class='api_ret'>returns</span> `:::js None` |
| 81 | +> Assert that a particular statement is not null. |
| 82 | +```js |
| 83 | +//We require a valid player in this code |
| 84 | +Assert.not_null(player) |
| 85 | +``` |
| 86 | + |
| 87 | +<endpoint module="luxe: assert" class="Assert" signature="not_null(value : Any, message : Any)"></endpoint> |
| 88 | +<signature id="Assert.not_null+2">Assert.not_null(**value**: `Any`, **message**: `Any`) |
| 89 | +<a class="headerlink" href="#Assert.not_null+2" title="Permanent link">¶</a></signature> |
| 90 | +<span class='api_ret'>returns</span> `:::js None` |
| 91 | +> Assert that a particular statement is not null, and display a message on abort. |
| 92 | +```js |
| 93 | +Assert.not_null(player, "A valid player is required") |
| 94 | +``` |
| 95 | + |
| 96 | +<endpoint module="luxe: assert" class="Assert" signature="is_null(value : Any)"></endpoint> |
| 97 | +<signature id="Assert.is_null">Assert.is_null(**value**: `Any`) |
| 98 | +<a class="headerlink" href="#Assert.is_null" title="Permanent link">¶</a></signature> |
| 99 | +<span class='api_ret'>returns</span> `:::js None` |
| 100 | +> Assert that a particular statement is null. |
| 101 | +```js |
| 102 | +//We assume the player is not holding something. |
| 103 | +Assert.is_null(player.item_in_hand) |
| 104 | +``` |
| 105 | + |
| 106 | +<endpoint module="luxe: assert" class="Assert" signature="is_null(value : Any, message : Any)"></endpoint> |
| 107 | +<signature id="Assert.is_null+2">Assert.is_null(**value**: `Any`, **message**: `Any`) |
| 108 | +<a class="headerlink" href="#Assert.is_null+2" title="Permanent link">¶</a></signature> |
| 109 | +<span class='api_ret'>returns</span> `:::js None` |
| 110 | +> Assert that a particular statement is null, and display a message on abort. |
| 111 | +```js |
| 112 | +Assert.is_null(player.item_in_hand, "Player must not have an item in hand when calling this") |
| 113 | +``` |
| 114 | + |
0 commit comments