summaryrefslogtreecommitdiff
path: root/lib/regex/test/testmatch.myr
blob: 6957a9bda2a4b736459ea37ef62cf0081ee63652 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
use std
use regex

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

	const testidxmatch	: (\
		pat : byte[:], \
		text : byte[:], \
		expected : std.option((std.size, std.size)[:]) \
		-> void)

	const testsearch	: ( \
		pat : byte[:], \
		text : byte[:], \
		expected : std.option(byte[:][:]) \
		-> void)

	const testsub	: ( \
		pat : byte[:], \
		text : byte[:], \
		sub : byte[:][:], \
		expected : std.option(byte[:]) \
		-> void)

	const testsuball	: ( \
		pat : byte[:], \
		text : byte[:], \
		sub : 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, false)
}

const testidxmatch = {pat, text, expected
	runidx(regex.compile(pat), pat, text, expected, false)
}

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

const testsub = {pat, text, sub, expected
	subst(regex.compile(pat), pat, text, sub, expected, false)
}

const testsuball = {pat, text, sub, expected
	subst(regex.compile(pat), pat, text, sub, expected, true)
}

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

const subst = {restr, pat, text, sub, expected, all
	var re, r

	re = std.try(restr)
	if all
		r = `std.Some regex.suball(re, text, sub)
	else
		r = regex.sub(re, text, sub)
	;;
	match r
	| `std.Some res:
		std.put("res: {}\n", res)
		match expected
		| `std.Some e:
			if !std.sleq(res, e)
				std.fatal("bad subst: expected {}, got {}\n", e, res)
			;;
		| `std.None:
			std.fatal("expected no subst, got {}", res)
		;;
	| `std.None:
		match expected
		| `std.Some e:
			std.fatal("got no sub, expected {}\n", e)
		| `std.None:
		;;
	;;
}

const runidx = {restr, pat, text, expected : std.option((std.size, std.size)[:]), search
	var re, r
	var lo, elo, hi, ehi

	re = std.try(restr)
	if search
		r = regex.isearch(re, text)
	else
		r = regex.iexec(re, text)
	;;
	match r
	| `std.Some res:
		match expected
		| `std.None:
			std.fatal("expected no match, got:")
			for var i = 0; i < res.len; i++
				std.put("\t{}: {}\n", i, res[i])
			;;
		| `std.Some exp:
			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 var i = 0; i < exp.len; i++
				(elo, ehi) = exp[i]
				(lo, hi) = res[i]
				if lo != elo || hi != ehi
					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 var i = 0; i < matches.len; i++
				std.put("\t{}: {}\n", i, matches[i])
			;;
			std.fatal("no match found\n")
		;;
	;;
	regex.free(re)
}
const run = {restr, pat, text, expected, search
	var i, re, r

	re = std.try(restr)
	if search
		r = regex.search(re, text)
	else
		r = regex.exec(re, text)
	;;
	match r
	| `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 !search && !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)
}