Skip to main content

basic_bdd/
basic_bdd.rs

1//! Basic BDD usage — describe/it/expect with assertions.
2//!
3//! Demonstrates the core testing workflow: write Lua tests,
4//! run them from Rust, inspect structured results.
5
6fn main() {
7    let summary = mlua_lspec::run_tests(
8        r#"
9        local describe, it, expect = lust.describe, lust.it, lust.expect
10
11        describe('string utilities', function()
12            describe('upper', function()
13                it('converts lowercase to uppercase', function()
14                    expect(string.upper("hello")).to.equal("HELLO")
15                end)
16
17                it('leaves uppercase unchanged', function()
18                    expect(string.upper("HELLO")).to.equal("HELLO")
19                end)
20
21                it('handles empty string', function()
22                    expect(string.upper("")).to.equal("")
23                end)
24            end)
25
26            describe('rep', function()
27                it('repeats string n times', function()
28                    expect(string.rep("ab", 3)).to.equal("ababab")
29                end)
30
31                it('returns empty for zero repeats', function()
32                    expect(string.rep("x", 0)).to.equal("")
33                end)
34            end)
35
36            describe('find', function()
37                it('returns start index', function()
38                    local start = string.find("hello world", "world")
39                    expect(start).to.equal(7)
40                end)
41
42                it('returns nil for no match', function()
43                    local result = string.find("hello", "xyz")
44                    expect(result).to_not.exist()
45                end)
46            end)
47        end)
48    "#,
49        "@basic_bdd.lua",
50    )
51    .expect("test execution failed");
52
53    println!(
54        "Results: {} passed, {} failed",
55        summary.passed, summary.failed
56    );
57    for test in &summary.tests {
58        let icon = if test.passed { "PASS" } else { "FAIL" };
59        println!("  [{icon}] {}: {}", test.suite, test.name);
60        if let Some(ref err) = test.error {
61            println!("         {err}");
62        }
63    }
64
65    assert_eq!(summary.failed, 0, "all tests should pass");
66}