File lunatest.lua
Copyright (c) 2009 Scott Vokes
Functions
assert_boolean (val, msg) | Test that val is a boolean. |
assert_equal (exp, got, msg) | exp == got. |
assert_error (f, msg) | Test that the function raises an error when called. |
assert_false (got, msg) | got == false. |
assert_function (val, msg) | Test that val is a function. |
assert_gt (lim, val, msg) | val > lim. |
assert_gte (lim, val, msg) | val >= lim. |
assert_len (len, val, msg) | #val == len. |
assert_lt (lim, val, msg) | val < lim. |
assert_lte (lim, val, msg) | val <= lim. |
assert_match (pat, s, msg) | Test that the string s matches the pattern exp. |
assert_metatable (exp, val, msg) | Test that a value has the expected metatable. |
assert_not_boolean (val, msg) | Test that val is not a boolean. |
assert_not_equal (exp, got, msg) | exp ~= got. |
assert_not_function (val, msg) | Test that val is not a function. |
assert_not_len (len, val, msg) | #val ~= len. |
assert_not_match (pat, s, msg) | Test that the string s doesn't match the pattern exp. |
assert_not_metatable (exp, val, msg) | Test that a value does not have a given metatable. |
assert_not_number (val, msg) | Test that val is not a number. |
assert_not_string (val, msg) | Test that val is not a string. |
assert_not_table (val, msg) | Test that val is not a table. |
assert_not_thread (val, msg) | Test that val is not a thread (coroutine). |
assert_not_userdata (val, msg) | Test that val is not a userdata (light or heavy). |
assert_number (val, msg) | Test that val is a number. |
assert_random (opt, f, ...) | Run a test case with randomly instantiated arguments, running the test function f opt.count (default: 100) times. |
assert_string (val, msg) | Test that val is a string. |
assert_table (val, msg) | Test that val is a table. |
assert_thread (val, msg) | Test that val is a thread (coroutine). |
assert_true (got, msg) | got == true. |
assert_userdata (val, msg) | Test that val is a userdata (light or heavy). |
fail (msg, no_exit) | Fail a test. |
is_test_key (k) | Check if a function name should be considered a test key. |
random_bool () | Get a random bool. |
random_float (low, high) | Get a random float low <= x < high. |
random_int (low, high) | Get a random value low <= x <= high. |
run (hooks, suite_filter) | Run all known test suites, with given configuration hooks. |
set_seed (s) | Set random seed. |
skip (msg) | Skip a test, with a note, e.g. |
suite (modname) | Add a file as a test suite. |
Functions
- assert_boolean (val, msg)
-
Test that val is a boolean.
Parameters
- val:
- msg:
- assert_equal (exp, got, msg)
-
exp == got.
Parameters
- exp:
- got:
- msg:
- assert_error (f, msg)
-
Test that the function raises an error when called.
Parameters
- f:
- msg:
- assert_false (got, msg)
-
got == false.
Parameters
- got:
- msg:
- assert_function (val, msg)
-
Test that val is a function.
Parameters
- val:
- msg:
- assert_gt (lim, val, msg)
-
val > lim.
Parameters
- lim:
- val:
- msg:
- assert_gte (lim, val, msg)
-
val >= lim.
Parameters
- lim:
- val:
- msg:
- assert_len (len, val, msg)
-
#val == len.
Parameters
- len:
- val:
- msg:
- assert_lt (lim, val, msg)
-
val < lim.
Parameters
- lim:
- val:
- msg:
- assert_lte (lim, val, msg)
-
val <= lim.
Parameters
- lim:
- val:
- msg:
- assert_match (pat, s, msg)
-
Test that the string s matches the pattern exp.
Parameters
- pat:
- s:
- msg:
- assert_metatable (exp, val, msg)
-
Test that a value has the expected metatable.
Parameters
- exp:
- val:
- msg:
- assert_not_boolean (val, msg)
-
Test that val is not a boolean.
Parameters
- val:
- msg:
- assert_not_equal (exp, got, msg)
-
exp ~= got.
Parameters
- exp:
- got:
- msg:
- assert_not_function (val, msg)
-
Test that val is not a function.
Parameters
- val:
- msg:
- assert_not_len (len, val, msg)
-
#val ~= len.
Parameters
- len:
- val:
- msg:
- assert_not_match (pat, s, msg)
-
Test that the string s doesn't match the pattern exp.
Parameters
- pat:
- s:
- msg:
- assert_not_metatable (exp, val, msg)
-
Test that a value does not have a given metatable.
Parameters
- exp:
- val:
- msg:
- assert_not_number (val, msg)
-
Test that val is not a number.
Parameters
- val:
- msg:
- assert_not_string (val, msg)
-
Test that val is not a string.
Parameters
- val:
- msg:
- assert_not_table (val, msg)
-
Test that val is not a table.
Parameters
- val:
- msg:
- assert_not_thread (val, msg)
-
Test that val is not a thread (coroutine).
Parameters
- val:
- msg:
- assert_not_userdata (val, msg)
-
Test that val is not a userdata (light or heavy).
Parameters
- val:
- msg:
- assert_number (val, msg)
-
Test that val is a number.
Parameters
- val:
- msg:
- assert_random (opt, f, ...)
-
Run a test case with randomly instantiated arguments, running the test function f opt.count (default: 100) times.
Parameters
-
opt: A table with options, or just a test name string.
opt.count: how many random trials to perform
opt.seed: Start the batch of trials with a specific seed
opt.always: Always test these seeds (for regressions)
opt.show_progress: Whether to print a . after every opt.tick trials.
opt.seed_limit: Max seed to allow.
opt.max_failures, max_errors, max_skips: Give up after X of each.
- f: A test function, run as f(unpack(randomized_args(...)))
-
...: the arg specification. For each argument, creates a random instance of that type.
boolean: return true or false
number n: returns 0 <= x < n, or -n <= x < n if negative. If n has a decimal component, so will the result.
string: Specifiedd as "(len[,maxlen]) (pattern)".
"10 %l" means 10 random lowercase letters.
"10,30 [aeiou]" means between 10-30 vowels.
function: Just call (as f()) and return result.
table or userdata: Call v.__random() and return result.
@usage
-
opt: A table with options, or just a test name string.
- assert_string (val, msg)
-
Test that val is a string.
Parameters
- val:
- msg:
- assert_table (val, msg)
-
Test that val is a table.
Parameters
- val:
- msg:
- assert_thread (val, msg)
-
Test that val is a thread (coroutine).
Parameters
- val:
- msg:
- assert_true (got, msg)
-
got == true. (Named "assert_true" to not conflict with standard assert.)
Parameters
- got:
- msg: Message to display with the result.
- assert_userdata (val, msg)
-
Test that val is a userdata (light or heavy).
Parameters
- val:
- msg:
- fail (msg, no_exit)
-
Fail a test.
Parameters
- msg:
- no_exit: Unless set to true, the presence of any failures causes the test suite to terminate with an exit status of 1.
- is_test_key (k)
-
Check if a function name should be considered a test key. Defaults to functions starting or ending with "test", with leading underscores allowed.
Parameters
- k:
- random_bool ()
- Get a random bool.
- random_float (low, high)
-
Get a random float low <= x < high.
Parameters
- low:
- high:
- random_int (low, high)
-
Get a random value low <= x <= high.
Parameters
- low:
- high:
- run (hooks, suite_filter)
-
Run all known test suites, with given configuration hooks.
Parameters
- hooks: Override the default hooks.
- suite_filter: If set, only run suite(s) with names matching this pattern.
Usage:
If no hooks are provided and arg[1] == "-v", the verbose_hooks will be used. - set_seed (s)
-
Set random seed.
Parameters
- s:
- skip (msg)
-
Skip a test, with a note, e.g. "TODO".
Parameters
- msg:
- suite (modname)
-
Add a file as a test suite.
Parameters
- modname: The module to load as a suite. The file is interpreted in the same manner as require "modname". Which functions are tests is determined by is_test_key(name).