summaryrefslogtreecommitdiff
path: root/lib/testr/testr.myr
blob: 22a271cc25feb363d2d5ec215ab346e97e191d41 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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[:], args : ... -> void)
	const check	: (ctx : ctx#, cond : bool, msg : byte[:], args : ... -> void)

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

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

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

const check = {ctx, cond, msg, args
	var ap
	
	if !cond
		ap = std.vastart(&args)
		failv(ctx, msg, &ap)
	;;
}

const fail = {ctx, msg, args
	var ap
	
	ap = std.vastart(&args)
	failv(ctx, msg, &ap)
}

const failv = {ctx, msg, ap
	softfailv(ctx, msg, ap)
	std.longjmp(ctx.jmpbuf)
}

const softfail = {ctx, msg, args
	var ap
	
	ap = std.vastart(&args)
	softfailv(ctx, msg, &ap)
}

const softfailv = {ctx, msg, ap
	ctx.ok = false
	ctx.reason = std.fmtv(msg, ap)
}

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)
	std.slfree(reason)
}