shithub: mc

ref: eb21d4f1381f73045c81df0840aa50df20515045
dir: /lib/testr/testr.myr/

View raw version
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)
}