shithub: mc

Download patch

ref: 7f8734b0b4c43f60a3b4a79929fe60725f64fbc2
parent: 44bd7d2ca3dc2b1a06cc0f4f688c0980d52a296e
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Oct 8 15:24:26 EDT 2016

Add vararg messages to failures.

    It's useful to be able to pass back a value in your
    failure reason.

--- a/lib/testr/testr.myr
+++ b/lib/testr/testr.myr
@@ -14,10 +14,10 @@
 
 	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 fail	: (ctx : ctx#, msg : byte[:], args : ... -> void)
+	const check	: (ctx : ctx#, cond : bool, msg : byte[:], args : ... -> void)
 
-	const softfail	: (ctx : ctx#, msg : byte[:] -> void)
+	const softfail	: (ctx : ctx#, msg : byte[:], args : ... -> void)
 ;;
 
 const run = {specs
@@ -31,20 +31,37 @@
 	/* nothing to do here */
 }
 
-const check = {ctx, cond, msg
+const check = {ctx, cond, msg, args
+	var ap
+	
 	if !cond
-		fail(ctx, msg)
+		ap = std.vastart(&args)
+		failv(ctx, msg, &ap)
 	;;
 }
 
-const fail = {ctx, msg
-	softfail(ctx, msg)
+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
+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 = msg
+	ctx.reason = std.fmtv(msg, ap)
 }
 
 const runspec = {ts
@@ -69,4 +86,5 @@
 		reason = ctx.reason
 	;;
 	std.put("!}}>> {} {}\n", status, reason)
+	std.slfree(reason)
 }