summaryrefslogtreecommitdiffstats
path: root/test/glob.c
blob: cbb97c8b8bc41978139d5cc1298a0e4efd8ba7fa (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
// What a glob matches, since nothing here uses fnmatch or regex.
#include "test.h"

static void test_glob(void)
{
    CHECK(glob_match("*", ""));
    CHECK(glob_match("*", "anything/at/all"));
    CHECK(!glob_match("", "x"));
    CHECK(glob_match("", ""));
    CHECK(glob_match("refs/heads/*", "refs/heads/main"));
    CHECK(glob_match("refs/heads/*", "refs/heads/a/b/c"));   // '*' crosses '/'
    CHECK(!glob_match("refs/heads/*", "refs/tags/v1"));
    CHECK(glob_match("refs/heads/feature/*", "refs/heads/feature/login"));
    CHECK(!glob_match("refs/heads/feature/*", "refs/heads/featurey"));
    CHECK(glob_match("refs/tags/v?", "refs/tags/v1"));
    CHECK(!glob_match("refs/tags/v?", "refs/tags/v12"));
    CHECK(glob_match("refs/heads/[ab]*", "refs/heads/beta"));
    CHECK(glob_match("refs/heads/[ab]*", "refs/heads/alpha"));
    CHECK(!glob_match("refs/heads/[ab]*", "refs/heads/delta"));
    CHECK(glob_match("refs/heads/[!ab]*", "refs/heads/delta"));
    CHECK(!glob_match("refs/heads/[!ab]*", "refs/heads/alpha"));
    CHECK(glob_match("refs/heads/[a-c]1", "refs/heads/b1"));
    CHECK(!glob_match("refs/heads/[a-c]1", "refs/heads/d1"));
    CHECK(glob_match("refs/heads/]", "refs/heads/]"));        // ']' first is a member
    CHECK(glob_match("refs/heads/a[-]z", "refs/heads/a-z"));  // trailing '-' literal
    CHECK(glob_match("refs/heads/[", "refs/heads/["));        // unclosed is literal
    CHECK(glob_match("a*b*c", "aXXbYYc"));
    CHECK(!glob_match("a*b*c", "aXXbYY"));
    CHECK(glob_match("*.git", "team.git"));
    CHECK(glob_match("team*", "team"));
    CHECK(glob_match("refs/heads/ma?n", "refs/heads/main"));
    CHECK(!glob_match("refs/heads/main", "refs/heads/MAIN")); // case matters
    CHECK(glob_match("refs/*/x", "refs/a/b/x"));
    CHECK(glob_match("**", "x"));                             // redundant stars
    CHECK(!glob_match("refs/heads/*", ""));
}

int main(void)
{
    test_init("");

    test_glob();

    return test_report("glob");
}