pacemaker 3.0.1-16e74fc4da
Scalable High-Availability cluster resource manager
Loading...
Searching...
No Matches
pcmk__replace_submatches_test.c
Go to the documentation of this file.
1/*
2 * Copyright 2024 the Pacemaker project contributors
3 *
4 * The version control history for this file may have further details.
5 *
6 * This source code is licensed under the GNU General Public License version 2
7 * or later (GPLv2+) WITHOUT ANY WARRANTY.
8 */
9
10#include <crm_internal.h>
11
12#include <regex.h> // regmatch_t
13
16
17// An example matched string with submatches
18static const char *match = "this is a string";
19static const regmatch_t submatches[] = {
20 { .rm_so = 0, .rm_eo = 16 }, // %0 = entire string
21 { .rm_so = 5, .rm_eo = 7 }, // %1 = "is"
22 { .rm_so = 9, .rm_eo = 9 }, // %2 = empty match
23};
24static const int nmatches = 3;
25
26static void
27assert_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
41static void
42no_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
49static void
50source_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
60static void
61without_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
69static void
70with_matches(void **state)
71{
72 assert_submatch("%0", match); // %0 matches entire string
73 assert_submatch("this %1", "this is");
74 assert_submatch("%1 this %ok", "is this %ok");
75}
76
77PCMK__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))
char * pcmk__replace_submatches(const char *string, const char *match, const regmatch_t submatches[], int nmatches)
Definition rules.c:614
#define PCMK__UNIT_TEST(group_setup, group_teardown,...)