Skip to content

Assert

library

Checks conditions in a script and fails with a readable message when they do not hold. A failed check throws an error, which ends the script unless it is caught with `pcall` or the check runs inside `Retry.run`. Every check returns the value it checked, so it can be used inline (`local id = Assert.notNil(response.id)`). The optional last argument is a message that is put in front of the standard text: `my check: Expected 1 but was 2`.

  • Kind Library, a global in every script
  • Category Utilities
  • Functions 5

Functions

  • that any Fails with `Condition was not true` unless the value is truthy.
  • equals any Fails with `Expected x but was y` unless both values are equal.
  • notNil any Fails with `Expected a non-nil value` when the value is `nil`.
  • isNil nil Fails with `Expected nil but was ...` unless the value is `nil`.
  • contains any Fails unless the text contains the other text.
Assert.that(condition: any)→anyAssert.that(condition: any, message: string)→any

Fails with `Condition was not true` unless the value is truthy. In Lua only `false` and `nil` are falsy: `0` and the empty string pass.

Parameters

condition any
Condition to check
message string optional
Text put in front of the failure message

Returns

any The checked value, returned unchanged so the call can be used inline

Example runs in CI

local value = Assert.that(1 + 1 == 2, "math still works")
Assert.equals(4, 2 * 2)
Assert.contains("hello world", "world")
Assert.equals(expected: any, actual: any)→anyAssert.equals(expected: any, actual: any, message: string)→any

Fails with `Expected x but was y` unless both values are equal. Tables are compared by content, including nested tables, regardless of the order of their keys. Numbers are compared as numbers (`1` equals `1.0`) and a number equals text that reads as the same number (`1` equals `"1"`); a boolean never equals a number. Tables in the failure message are shown up to three levels deep.

Parameters

expected any
The value you expect
actual any
The value you got
message string optional
Text put in front of the failure message

Returns

any The actual value, returned unchanged so the call can be used inline
Assert.notNil(value: any)→anyAssert.notNil(value: any, message: string)→any

Fails with `Expected a non-nil value` when the value is `nil`. `false` passes.

Parameters

value any
Value to check
message string optional
Text put in front of the failure message

Returns

any The checked value, returned unchanged so the call can be used inline
Assert.isNil(value: any)→nilAssert.isNil(value: any, message: string)→nil

Fails with `Expected nil but was ...` unless the value is `nil`. `false` fails.

Parameters

value any
Value to check
message string optional
Text put in front of the failure message

Returns

nil Nothing.
Assert.contains(haystack: string, needle: string)→anyAssert.contains(haystack: string, needle: string, message: string)→any

Fails unless the text contains the other text. Both values are converted to text first, so numbers work too; the comparison is case-sensitive. A `nil` haystack fails.

Parameters

haystack string
Text that should contain `needle`
needle string
Text it must contain
message string optional
Text put in front of the failure message

Returns

any The checked value, returned unchanged so the call can be used inline