This source file includes following definitions.
- assert_submatch
- no_source
- source_has_no_variables
- without_matches
- with_matches
1
2
3
4
5
6
7
8
9
10 #include <crm_internal.h>
11
12 #include <regex.h>
13
14 #include <crm/common/rules_internal.h>
15 #include <crm/common/unittest_internal.h>
16
17
18 static const char *match = "this is a string";
19 static const regmatch_t submatches[] = {
20 { .rm_so = 0, .rm_eo = 16 },
21 { .rm_so = 5, .rm_eo = 7 },
22 { .rm_so = 9, .rm_eo = 9 },
23 };
24 static const int nmatches = 3;
25
26 static void
27 assert_submatch(const char *string, const char *reference)
28 {
29 char *expanded = NULL;
30
31 expanded = pcmk__replace_submatches(string, match, submatches, nmatches);
32 if ((expanded == NULL) || (reference == NULL)) {
33 assert_null(expanded);
34 assert_null(reference);
35 } else {
36 assert_int_equal(strcmp(expanded, reference), 0);
37 }
38 free(expanded);
39 }
40
41 static void
42 no_source(void **state)
43 {
44 assert_null(pcmk__replace_submatches(NULL, NULL, NULL, 0));
45 assert_submatch(NULL, NULL);
46 assert_submatch("", NULL);
47 }
48
49 static void
50 source_has_no_variables(void **state)
51 {
52 assert_null(pcmk__replace_submatches("this has no submatch variables",
53 match, submatches, nmatches));
54 assert_null(pcmk__replace_submatches("this ends in a %",
55 match, submatches, nmatches));
56 assert_null(pcmk__replace_submatches("%this starts with one",
57 match, submatches, nmatches));
58 }
59
60 static void
61 without_matches(void **state)
62 {
63 assert_submatch("this has an empty submatch %2",
64 "this has an empty submatch ");
65 assert_submatch("this has a nonexistent submatch %3",
66 "this has a nonexistent submatch ");
67 }
68
69 static void
70 with_matches(void **state)
71 {
72 assert_submatch("%0", match);
73 assert_submatch("this %1", "this is");
74 assert_submatch("%1 this %ok", "is this %ok");
75 }
76
77 PCMK__UNIT_TEST(NULL, NULL,
78 cmocka_unit_test(no_source),
79 cmocka_unit_test(source_has_no_variables),
80 cmocka_unit_test(without_matches),
81 cmocka_unit_test(with_matches))