mlua-lspec
BDD test framework for Lua on mlua.
Embeds a forked copy of lust and provides Rust APIs for executing Lua tests with structured result collection. Includes Rust-backed spy/stub/mock test doubles and fixture management.
Quick start
let summary = run_tests.unwrap;
assert_eq!;
assert_eq!;
Assertions
-- Equality
expect..
expect.. -- float tolerance
expect..
-- Identity
expect..
-- Truthiness / existence
expect...
expect..
expect..
-- Type checking
expect...
expect...
-- Comparison
expect... -- greater than
expect... -- greater than or equal
expect... -- less than
expect... -- less than or equal
-- Tables
expect.. -- contains value
expect.. -- has key
expect.. -- #tbl == 3
-- Strings
expect.. -- #str == 5
expect.. -- Lua pattern match
-- Errors
expect..
expect...
-- Negation (all assertions support to_not)
expect..
expect...
expect..
Test doubles
Spy
Records calls while delegating to the original function.
local s = test_doubles.
s
s: -- 1
s: -- {5}
s: -- true
s: -- clear call history
Stub
Returns fixed values without calling any original.
local st = test_doubles.
st:
st -- 42
Spy on table method
Replaces a table method with a spy that calls through. Supports revert().
local spy = test_doubles.
obj.
spy: -- 1
spy: -- restore original
Mock
Extends spy with declarative expectations verified at test end.
local m = test_doubles.
m:
m:
m
m
m: -- passes: called 2 times, once with arg 5
Mock methods:
| Method | Description |
|---|---|
mock(fn?) |
Create mock (optional call-through function) |
expect_call_count(n) |
Expect exactly n calls |
expect_called_with(args...) |
Expect at least one call with these args |
expect_never_called() |
Expect zero calls |
verify() |
Check all expectations (errors if unmet) |
clear_expectations() |
Remove all pending expectations |
call_count(), call_args(n), was_called_with(args...) |
Spy-compatible inspection |
returns(val...), reset() |
Spy-compatible mutation |
Fixture
Managed test data store with temporary directory support.
local fix = test_doubles.
-- Key/value data (any Lua type including tables)
fix:
fix:. -- "alice"
fix: -- true
fix: -- returns the value
fix: -- number of entries
fix: -- list of keys
-- File loading
fix:
fix: -- file contents as string
-- Temporary directories (auto-cleaned on GC or cleanup())
local dir = fix:
-- dir = "/tmp/lspec-work-12345-0"
-- Explicit cleanup (also happens on GC)
fix:
Granular control
For pre-existing Lua VMs (e.g. testing Rust APIs exposed to Lua):
use *;
let lua = new;
// Register your application globals
lua.globals.set.unwrap;
// Register lspec
register.unwrap;
register_doubles.unwrap;
// Run tests
lua.load.exec.unwrap;
let summary = collect_results.unwrap;
assert_eq!;
License
MIT OR Apache-2.0