summaryrefslogtreecommitdiff
path: root/lib/testr/testr.myr
blob: 1f7660ca2bdd86f7bd4513c2ce59a90bf40f489b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use std

pkg testr =
	type ctx = struct
		ok	: bool
		reason	: byte[:]
		jmpbuf	: std.jmpbuf#
	;;

	type spec = struct
		name	: byte[:]
		fn	: (ctx : ctx# -> void)
	;;

	const run	: (specs : spec[:] -> void)
	const ok	: (ctx : ctx# -> void)
	const fail	: (ctx : ctx#, msg : byte[:] -> void)
	const check	: (ctx : ctx#, cond : bool, msg : byte[:] -> void)

	const softfail	: (ctx : ctx#, msg : byte[:] -> void)
;;

const run = {specs
	std.put("MTEST {}\n", specs.len)
	for s in specs
		runspec(&s)
	;;
}

const ok = {ctx
	/* nothing to do here */
}

const check = {ctx, cond, msg
	if !cond
		fail(ctx, msg)
	;;
}

const fail = {ctx, msg
	softfail(ctx, msg)
	std.longjmp(ctx.jmpbuf)
}

const softfail = {ctx, msg
	ctx.ok = false
	ctx.reason = msg
}

const runspec = {ts
	var ctx : ctx
	var status, reason
	var jmpbuf

	ctx.ok = true
	ctx.reason = ""
	ctx.jmpbuf = &jmpbuf

	std.put("test {} <<{{!\n", ts.name)
	if !std.setjmp(&jmpbuf)
		ts.fn(&ctx)
	;;

	if ctx.ok
		status = "ok"
		reason = ""
	else
		status = "fail"
		reason = ctx.reason
	;;
	std.put("!}}>> {} {}\n", status, reason)
}