Module ammtest

AMM test library.

Index

Function

assertBoolean()

Assert that the geven value is a boolean.

assertClose()

Assert that two floats are equal, within the given tolerance.

assertDeepEq()

Assert that two values are deep-equal.

assertEq()

Assert that g == e.

assertError()

Assert that the given function throws an error when called, and that the error message matches the given pattern.

assertFalse()

Assert that the given value is false.

assertGt()

Assert that g > e.

assertGte()

Assert that g >= e.

assertLen()

Assert that two arrays contain the same number of elements.

assertLt()

Assert that g < e.

assertLte()

Assert that g <= e.

assertMatch()

Assert that the given string matches a pattern.

assertNil()

Assert that the geven value is a nil.

assertNotBoolean()

Assert that the geven value is not a boolean.

assertNotClose()

Assert that two floats are not equal, within the given tolerance.

assertNotDeepEq()

Assert that two values are not deep-equal.

assertNotEq()

Assert that g ~= e.

assertNotLen()

Assert that two arrays contain different number of elements.

assertNotMatch()

Assert that the given string does not match a pattern.

assertNotNil()

Assert that the geven value is not a nil.

assertNotNumber()

Assert that the geven value is not a number.

assertNotString()

Assert that the geven value is not a string.

assertNotTable()

Assert that the geven value is not a table.

assertNumber()

Assert that the geven value is a number.

assertString()

Assert that the geven value is a string.

assertTable()

Assert that the geven value is a table.

assertTrue()

Assert that the given value is true.

fail()

Mark test as failed and immediately stop it.

getLog()

Get log lines that were printed during the test.

getLogStr()

Get log lines that were printed during the test, concatenated into a single string.

isInTest()

Check if we're currently in test mode.

loadTests()

Search and load all tests in all dev packages.

main()

Main function that will collect and run all the tests.

param()

Test parameter, used with ammtest.Suite.caseParams.

patch()

Temporarily replace value of a variable.

pprint()

Pretty print a table/variable.

pprintVa()

Pretty print function arguments.

run()

Run all collected tests and return a result.

skip()

Mark test as skipped and immediately stop it.

suite()

Create a new test suite.

Class

CaseResult

Results of a single test case.

Param

Container for test parameters and additional debug values. Used with ammtest.Suite.caseParams.

Result

Results of a single test suite.

Suite

Base class for test suites.

Alias

Status

Result of a test run.

Function

ammtest.assertBoolean(g: any, msg?: string)

Assert that the geven value is a boolean.

ammtest.assertClose(g: number, e: number, tol?: number, msg?: string)

Assert that two floats are equal, within the given tolerance.

ammtest.assertDeepEq(g: <T>, e: <T>, msg?: string)

Assert that two values are deep-equal.

That is, if two values are tables, they should contain the same set of keys, and their values should themselves be deep-equal; if two values are not tables, they should be equal when compared by the == operator.

ammtest.assertEq(g: <T>, e: <T>, msg?: string)

Assert that g == e.

ammtest.assertError(fn: fun(...any): ...unknown, args: any[], pat: string, msg?: string)

Assert that the given function throws an error when called, and that the error message matches the given pattern.

If the given function doesn’t throw an error, and returns a value instead, the value will be displayed in a test failure message.

Example:

test.assertError(
    function (a, b) return a + b end,
    { nil, 1 },
    "attempt to perform arithmetic on a nil value",
)
ammtest.assertFalse(g: any, msg?: string)

Assert that the given value is false.

ammtest.assertGt(g: <T>, e: <T>, msg?: string)

Assert that g > e.

ammtest.assertGte(g: <T>, e: <T>, msg?: string)

Assert that g >= e.

ammtest.assertLen(g: any[], e: integer, msg?: string)

Assert that two arrays contain the same number of elements.

ammtest.assertLt(g: <T>, e: <T>, msg?: string)

Assert that g < e.

ammtest.assertLte(g: <T>, e: <T>, msg?: string)

Assert that g <= e.

ammtest.assertMatch(g: string, pat: string, msg?: string)

Assert that the given string matches a pattern.

ammtest.assertNil(g: any, msg?: string)

Assert that the geven value is a nil.

ammtest.assertNotBoolean(g: any, msg?: string)

Assert that the geven value is not a boolean.

ammtest.assertNotClose(g: number, e: number, tol?: number, msg?: string)

Assert that two floats are not equal, within the given tolerance.

ammtest.assertNotDeepEq(g: <T>, e: <T>, msg?: string)

Assert that two values are not deep-equal.

ammtest.assertNotEq(g: <T>, e: <T>, msg?: string)

Assert that g ~= e.

ammtest.assertNotLen(g: any[], e: integer, msg?: string)

Assert that two arrays contain different number of elements.

ammtest.assertNotMatch(g: string, pat: string, msg?: string)

Assert that the given string does not match a pattern.

ammtest.assertNotNil(g: any, msg?: string)

Assert that the geven value is not a nil.

ammtest.assertNotNumber(g: any, msg?: string)

Assert that the geven value is not a number.

ammtest.assertNotString(g: any, msg?: string)

Assert that the geven value is not a string.

ammtest.assertNotTable(g: any, msg?: string)

Assert that the geven value is not a table.

ammtest.assertNumber(g: any, msg?: string)

Assert that the geven value is a number.

ammtest.assertString(g: any, msg?: string)

Assert that the geven value is a string.

ammtest.assertTable(g: any, msg?: string)

Assert that the geven value is a table.

ammtest.assertTrue(g: any, msg?: string)

Assert that the given value is true.

ammtest.fail(msg?: string)

Mark test as failed and immediately stop it.

ammtest.getLog() {level: integer, msg: string}[]

Get log lines that were printed during the test.

This function only works in tests, no in setup/teardown functions.

ammtest.getLogStr() string

Get log lines that were printed during the test, concatenated into a single string.

This function only works in tests, no in setup/teardown functions.

ammtest.isInTest() boolean

Check if we’re currently in test mode.

ammtest.loadTests(name?: string)

Search and load all tests in all dev packages.

ammtest.main(name?: string)

Main function that will collect and run all the tests.

ammtest.param(...: any) ammtest.Param

Test parameter, used with ammtest.Suite:caseParams().

ammtest.patch(env?: table<string, any>, name: string, value: any)

Temporarily replace value of a variable.

Calling ammtest.patch(a.b, "c", x) is equivalent to executing a.b.c = x, and then restoring the old value. To replace global variable, set env to nil.

Depending on where this function was called from, the effects are restored after test case, test teardown, or suite teardown.

Parameters:
  • env? (table<string, any>) – table that contains the replaced value.

  • name (string) – name of the replaced value.

  • value (any) – new value.

ammtest.pprint(x: any, long: boolean) string

Pretty print a table/variable.

Parameters:
  • x (any) – value to be pretty printed.

  • long (boolean) – whether to shorten the value or not.

ammtest.pprintVa(params: any[], long: boolean) string

Pretty print function arguments.

Parameters:
  • params (any[]) – array of function arguments.

  • long (boolean) – whether to shorten the value or not.

ammtest.run() ammtest.Result[]

Run all collected tests and return a result.

ammtest.skip(msg?: string)

Mark test as skipped and immediately stop it.

ammtest.suite(name?: string) ammtest.Suite

Create a new test suite.

Class

class ammtest.CaseResult : ammcore.class.Base

Results of a single test case.

New(self: <T: ammtest.CaseResult>, name: string, status?: "FAIL" | "OK" | "SKIP", msg?: string, testLoc?: string, loc?: string) <T: ammtest.CaseResult>
const name: string

Name of the test case.

const status: "FAIL" | "OK" | "SKIP"

Status of the test case.

const msg: string?

Message with which the test case failed.

const testLoc: string?

Location of the test function.

const loc: string?

Location of the error that failed the test.

class ammtest.Param : ammcore.class.Base

Container for test parameters and additional debug values. Used with ammtest.Suite:caseParams().

New(self: <T: ammtest.Param>, values: any[], loc?: string) <T: ammtest.Param>

Note

Use ammtest.param() to properly construct test parameters.

const values: any[]

Values that will be passed to the test function.

const loc: string

Location where this parameter was created.

class ammtest.Result : ammcore.class.Base

Results of a single test suite.

New(self: <T: ammtest.Result>, name: string, status?: "FAIL" | "OK" | "SKIP", msg?: string, loc?: string) <T: ammtest.Result>
const name: string

Name of the test suite.

const status: "FAIL" | "OK" | "SKIP"

Status of the test suite.

const msg: string?

Message with which the test suite failed.

const loc: string?

Location of the error that failed the suite. Only set if the failure occurred during suite setup/teardown.

const cases: ammtest.CaseResult[]

Array of test cases executed within this suite.

class ammtest.Suite : ammcore.class.Base

Base class for test suites.

New(self: <T: ammtest.Suite>, name?: string) <T: ammtest.Suite>

Note

Use ammtest.suite() to properly construct test suites.

Parameters:

name? (string) – name of the test suite.

const name: string

Name of the suite, used for error messages.

virtual setupTest(self: ammtest.Suite)

Runs before every test.

virtual teardownTest(self: ammtest.Suite)

Runs after every test.

virtual setupSuite(self: ammtest.Suite)

Runs before every suite.

virtual teardownSuite(self: ammtest.Suite)

Runs after every suite.

case(self: ammtest.Suite, name: string, fn: fun())

Add a test to the suite.

Parameters:
  • name (string) – test name, used for error messages.

  • fn (fun()) – test implementation.

caseParams(self: ammtest.Suite, name: string, params: ammtest.Param[], fn: fun(...any))

Add a parametrized test to the suite.

Example:

local suite = test.suite("math")
suite:caseParams(
    "multiply",
    {
        test.param(2, 2, 4),
        test.param(3, 3, 9),
    },
    function (a, b, c)
        test.assertEq(a * b, c)
    end
)
Parameters:
  • name (string) – test name, used for error messages.

  • params (ammtest.Param[]) – array of test parameters.

  • fn (fun(...any)) – test implementation, must accept parameters as its arguments.

Alias

alias ammtest.Status = "FAIL" | "OK" | "SKIP"

Result of a test run.

OK

Test finished successfully.

SKIP

Test was skipped.

FAIL

Test failed.