shithub: mc

ref: f90519332a6d24444a54c2b0943f705e8579550f
dir: /libregex/test/testmatch.myr/

View raw version
use std
use regex

pkg =
	const testmatch	: (pat : byte[:], text : byte[:], expected : std.option(byte[:][:]) -> void)
	const dbgmatch	: (pat : byte[:], text : byte[:], expected : std.option(byte[:][:]) -> void)
;;

const testmatch = {pat, text, expected
	run(regex.compile(pat), pat, text, expected)
}

const dbgmatch = {pat, text, expected
	run(regex.dbgcompile(pat), pat, text, expected)
}

const run = {regex, pat, text, expected
	var i, re

	re = std.try(regex)
	match regex.exec(re, text)
	| `std.Some res:
		match expected
		| `std.None:
			std.fatal("expected no match, got:")
			for i = 0; i < res.len; i++
				std.put("\t{}: {}\n", i, res[i])
			;;
		| `std.Some exp:
			if !std.sleq(res[0], text)
				std.put("whole match does not match text!\n")
				std.fatal("failed matching {} over {}\n", pat, text)
			;;
			res = res[1:]
			if res.len != exp.len
				std.put("mismatch: expected {} matches, got {}\n",  exp.len, res.len)
				std.fatal("failed matching {} over {}\n", pat, text)
			;;
			for i = 0; i < exp.len; i++
				if !std.sleq(res[i], exp[i])
					std.put("mismatch on {}: expected {}, got {}\n", i, exp[i], res[i])
					std.fatal("failed matching {} over {}\n", pat, text)
				;;
			;;
		;;
	| `std.None:
		match expected
		| `std.None:	/* : expected failure */
		| `std.Some matches:
			std.put("expected matches:\n")
			for i = 0; i < matches.len; i++
				std.put("\t{}: {}\n", i, matches[i])
			;;
			std.fatal("no match found\n")
		;;
	;;
	regex.free(re)
}