Assert
libraryChecks 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
-
thatany Fails with `Condition was not true` unless the value is truthy. -
equalsany Fails with `Expected x but was y` unless both values are equal. -
notNilany Fails with `Expected a non-nil value` when the value is `nil`. -
isNilnil Fails with `Expected nil but was ...` unless the value is `nil`. -
containsany Fails unless the text contains the other text.
that()
Section titled “that()”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
-
conditionany - Condition to check
-
messagestring optional - Text put in front of the failure message
Returns
Example runs in CI
local value = Assert.that(1 + 1 == 2, "math still works")Assert.equals(4, 2 * 2)Assert.contains("hello world", "world")equals()
Section titled “equals()”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
-
expectedany - The value you expect
-
actualany - The value you got
-
messagestring optional - Text put in front of the failure message
Returns
notNil()
Section titled “notNil()”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
-
valueany - Value to check
-
messagestring optional - Text put in front of the failure message
Returns
isNil()
Section titled “isNil()”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
-
valueany - Value to check
-
messagestring optional - Text put in front of the failure message
Returns
contains()
Section titled “contains()”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
-
haystackstring - Text that should contain `needle`
-
needlestring - Text it must contain
-
messagestring optional - Text put in front of the failure message
Returns