File: pcmk__replace_submatches_test.c

package info (click to toggle)
pacemaker 3.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 68,576 kB
  • sloc: xml: 160,564; ansic: 143,744; python: 5,670; sh: 2,969; makefile: 2,426
file content (81 lines) | stat: -rw-r--r-- 2,470 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright 2024 the Pacemaker project contributors
 *
 * The version control history for this file may have further details.
 *
 * This source code is licensed under the GNU General Public License version 2
 * or later (GPLv2+) WITHOUT ANY WARRANTY.
 */

#include <crm_internal.h>

#include <regex.h>  // regmatch_t

#include <crm/common/rules_internal.h>
#include <crm/common/unittest_internal.h>

// An example matched string with submatches
static const char *match = "this is a string";
static const regmatch_t submatches[] = {
    { .rm_so = 0, .rm_eo = 16 },    // %0 = entire string
    { .rm_so = 5, .rm_eo = 7 },     // %1 = "is"
    { .rm_so = 9, .rm_eo = 9 },     // %2 = empty match
};
static const int nmatches = 3;

static void
assert_submatch(const char *string, const char *reference)
{
    char *expanded = NULL;

    expanded = pcmk__replace_submatches(string, match, submatches, nmatches);
    if ((expanded == NULL) || (reference == NULL)) {
        assert_null(expanded);
        assert_null(reference);
    } else {
        assert_int_equal(strcmp(expanded, reference), 0);
    }
    free(expanded);
}

static void
no_source(void **state)
{
    assert_null(pcmk__replace_submatches(NULL, NULL, NULL, 0));
    assert_submatch(NULL, NULL);
    assert_submatch("", NULL);
}

static void
source_has_no_variables(void **state)
{
    assert_null(pcmk__replace_submatches("this has no submatch variables",
                                         match, submatches, nmatches));
    assert_null(pcmk__replace_submatches("this ends in a %",
                                         match, submatches, nmatches));
    assert_null(pcmk__replace_submatches("%this starts with one",
                                         match, submatches, nmatches));
}

static void
without_matches(void **state)
{
    assert_submatch("this has an empty submatch %2",
                    "this has an empty submatch ");
    assert_submatch("this has a nonexistent submatch %3",
                    "this has a nonexistent submatch ");
}

static void
with_matches(void **state)
{
    assert_submatch("%0", match); // %0 matches entire string
    assert_submatch("this %1", "this is");
    assert_submatch("%1 this %ok", "is this %ok");
}

PCMK__UNIT_TEST(NULL, NULL,
                cmocka_unit_test(no_source),
                cmocka_unit_test(source_has_no_variables),
                cmocka_unit_test(without_matches),
                cmocka_unit_test(with_matches))