summaryrefslogtreecommitdiff
path: root/lib/regex/test/testmatch.myr
blob: 460041084e8e8dc36de51f1c92a16c6d944911c4 (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
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)
}