Skip to content

Commit eb7aaf6

Browse files
committed
add test for issue 69
I've added a test for kokke#69. In order to do this I enhanced the new test3.c file to support patterns that match in the middle of text.
1 parent 8311dc8 commit eb7aaf6

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

tests/test3.c

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,65 @@
55
#include "re.h"
66

77
#ifdef RE_CSTRINGS
8+
#define RE_MATCH re_match_cstr
89
#define RE_MATCH_START_ONLY re_match_start_only_cstr
910
#define text_args(t) t
1011
#else
12+
#define RE_MATCH re_match
1113
#define RE_MATCH_START_ONLY re_match_start_only
1214
#define text_args(t) t, t + strlen(t)
1315
#endif
1416

15-
void test(const char *regex, const char *text, int expected_match_len)
17+
static void checkMatch(struct re_context *context, int match_result, re_length_t match_start, re_length_t expected_match_start, int expected_match_len)
1618
{
17-
printf("Pattern '%s' Text '%s'\n", regex, text);
18-
struct re_context context = {0};
19-
20-
if (RE_MATCH_START_ONLY(&context, regex, text_args(text))) {
21-
if (expected_match_len == -1) {
22-
printf("fail: expected not to match\n");
23-
exit(1);
24-
}
25-
if ((re_length_t)expected_match_len != context.match_length) {
26-
printf("fail: expected match length %d but got %llu\n", expected_match_len, (unsigned long long)context.match_length);
27-
exit(1);
19+
if (match_result) {
20+
if (expected_match_start != match_start) {
21+
printf("fail: expected not to match to start at %llu but started at %llu\n",
22+
(unsigned long long)expected_match_start, (unsigned long long)match_start);
23+
exit(1);
24+
}
25+
if (expected_match_len == -1) {
26+
printf("fail: expected not to match\n");
27+
exit(1);
28+
}
29+
if ((re_length_t)expected_match_len != context->match_length) {
30+
printf("fail: expected match length %d but got %llu\n", expected_match_len, (unsigned long long)context->match_length);
31+
exit(1);
32+
}
33+
} else {
34+
if (expected_match_len != -1) {
35+
printf("fail: expected a match\n");
36+
exit(1);
37+
}
2838
}
29-
} else {
30-
if (expected_match_len != -1) {
31-
printf("fail: expected a match\n");
39+
if (context->error) {
40+
printf("fail: unexpected error %s\n", re_error_name_table[context->error]);
3241
exit(1);
3342
}
43+
}
44+
45+
void testExt(const char *regex, const char *text, re_length_t expected_match_start, int expected_match_len)
46+
{
47+
printf("Pattern '%s' Text '%s'\n", regex, text);
48+
{
49+
struct re_context context = {0};
50+
re_length_t match_start;
51+
int match_result = RE_MATCH(&context, regex, text_args(text), &match_start);
52+
checkMatch(&context, match_result, match_start, expected_match_start, expected_match_len);
3453
}
35-
if (context.error) {
36-
printf("fail: unexpected error %s\n", re_error_name_table[context.error]);
37-
exit(1);
54+
55+
if (expected_match_start == 0) {
56+
struct re_context context = {0};
57+
int match_result = RE_MATCH_START_ONLY(&context, regex, text_args(text));
58+
checkMatch(&context, match_result, 0, 0, expected_match_len);
3859
}
3960
}
4061

62+
void test(const char *regex, const char *text, int expected_match_len)
63+
{
64+
testExt(regex, text, 0, expected_match_len);
65+
}
66+
4167
void testbad(const char *regex, const char *text, enum re_error error)
4268
{
4369
printf("BadPattern '%s' Text '%s'\n", regex, text);
@@ -194,4 +220,7 @@ int main()
194220

195221
// https://github.com/kokke/tiny-regex-c/issues/53
196222
test(".*a.*a", "aa", 2);
223+
224+
// https://github.com/kokke/tiny-regex-c/issues/69
225+
testExt("{\\w+}", "{{FW}_TEST", 1, 4);
197226
}

0 commit comments

Comments
 (0)