nx_test_harness_test.nx source
↩ module page · 61 lines · 2090 B
1// nx_test_harness_test.nx -- self-test for the assertion DSL.
2//
3// All expected-PASS assertions are run normally; expected-FAIL
4// assertions are run with arguments that should fail, and we verify
5// by directly reading NX_TEST_FAILS counter. Final exit code is 0 iff
6// counters match expectations.
7
8import "nx_test_harness.nx"
9
10func _ths_strlen(s: *u8) -> i64 {
11 var n: i64 = 0
12 while s[n] != 0 { n = n + 1 }
13 return n
14}
15
16func main() -> i64 {
17 nx_test_init()
18
19 // T1: expect_eq passes for equal values.
20 nx_test_expect_eq("equal ints PASS" as *u8, 42 as i64, 42 as i64)
21
22 // T2: expect_contains passes for present substring.
23 let html: *u8 = "<div class=\"chk-shell\"><button data-action=\"2\">New game</button></div>" as *u8
24 let n: i64 = _ths_strlen(html)
25 nx_test_expect_contains("substring present PASS" as *u8, html, n, "New game" as *u8)
26
27 // T3: expect_not_contains passes for absent substring.
28 nx_test_expect_not_contains("substring absent PASS" as *u8, html, n, "Old game" as *u8)
29
30 // T4: expect_class_count = 1 for chk-shell.
31 nx_test_expect_class_count("class chk-shell = 1" as *u8, html, n,
32 "chk-shell" as *u8, 1 as i64)
33
34 // T5: expect_tag_count = 1 for div.
35 nx_test_expect_tag_count("div tag = 1" as *u8, html, n, "div" as *u8, 1 as i64)
36
37 // After 5 successful expectations, counters should be (5, 0).
38 let p_after: i64 = NX_TEST_PASSES
39 let f_after: i64 = NX_TEST_FAILS
40 var ok: i64 = 0
41 if p_after == 5 {
42 if f_after == 0 { ok = 1 }
43 }
44
45 // Now deliberately trigger ONE failure (mismatched expect_eq) to verify
46 // the fail-path increments the counter.
47 nx_test_expect_eq("mismatched ints EXPECTED FAIL" as *u8, 1 as i64, 2 as i64)
48 let p2: i64 = NX_TEST_PASSES
49 let f2: i64 = NX_TEST_FAILS
50 var ok2: i64 = 0
51 if p2 == 5 {
52 if f2 == 1 { ok2 = 1 }
53 }
54
55 // Summary (will print, then we return based on our own check, not its rc).
56 nx_test_summary()
57
58 if ok != 1 { return 10 }
59 if ok2 != 1 { return 11 }
60 return 0
61}