[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / Support / GlobPatternTest.cpp
blob7acd311b0bb923f6329d8c80bb4c7ae956b07234
1 //===- llvm/unittest/Support/GlobPatternTest.cpp --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/GlobPattern.h"
10 #include "gtest/gtest.h"
12 using namespace llvm;
13 namespace {
15 class GlobPatternTest : public ::testing::Test {};
17 TEST_F(GlobPatternTest, Empty) {
18 Expected<GlobPattern> Pat1 = GlobPattern::create("");
19 EXPECT_TRUE((bool)Pat1);
20 EXPECT_TRUE(Pat1->match(""));
21 EXPECT_FALSE(Pat1->match("a"));
24 TEST_F(GlobPatternTest, Glob) {
25 Expected<GlobPattern> Pat1 = GlobPattern::create("ab*c*def");
26 EXPECT_TRUE((bool)Pat1);
27 EXPECT_TRUE(Pat1->match("abcdef"));
28 EXPECT_TRUE(Pat1->match("abxcxdef"));
29 EXPECT_FALSE(Pat1->match(""));
30 EXPECT_FALSE(Pat1->match("xabcdef"));
31 EXPECT_FALSE(Pat1->match("abcdefx"));
34 TEST_F(GlobPatternTest, Wildcard) {
35 Expected<GlobPattern> Pat1 = GlobPattern::create("a??c");
36 EXPECT_TRUE((bool)Pat1);
37 EXPECT_TRUE(Pat1->match("axxc"));
38 EXPECT_FALSE(Pat1->match("axxx"));
39 EXPECT_FALSE(Pat1->match(""));
42 TEST_F(GlobPatternTest, Escape) {
43 Expected<GlobPattern> Pat1 = GlobPattern::create("\\*");
44 EXPECT_TRUE((bool)Pat1);
45 EXPECT_TRUE(Pat1->match("*"));
46 EXPECT_FALSE(Pat1->match("\\*"));
47 EXPECT_FALSE(Pat1->match("a"));
49 Expected<GlobPattern> Pat2 = GlobPattern::create("a?\\?c");
50 EXPECT_TRUE((bool)Pat2);
51 EXPECT_TRUE(Pat2->match("ax?c"));
52 EXPECT_FALSE(Pat2->match("axxc"));
53 EXPECT_FALSE(Pat2->match(""));
56 TEST_F(GlobPatternTest, BasicCharacterClass) {
57 Expected<GlobPattern> Pat1 = GlobPattern::create("[abc-fy-z]");
58 EXPECT_TRUE((bool)Pat1);
59 EXPECT_TRUE(Pat1->match("a"));
60 EXPECT_TRUE(Pat1->match("b"));
61 EXPECT_TRUE(Pat1->match("c"));
62 EXPECT_TRUE(Pat1->match("d"));
63 EXPECT_TRUE(Pat1->match("e"));
64 EXPECT_TRUE(Pat1->match("f"));
65 EXPECT_TRUE(Pat1->match("y"));
66 EXPECT_TRUE(Pat1->match("z"));
67 EXPECT_FALSE(Pat1->match("g"));
68 EXPECT_FALSE(Pat1->match(""));
71 TEST_F(GlobPatternTest, NegatedCharacterClass) {
72 Expected<GlobPattern> Pat1 = GlobPattern::create("[^abc-fy-z]");
73 EXPECT_TRUE((bool)Pat1);
74 EXPECT_TRUE(Pat1->match("g"));
75 EXPECT_FALSE(Pat1->match("a"));
76 EXPECT_FALSE(Pat1->match("b"));
77 EXPECT_FALSE(Pat1->match("c"));
78 EXPECT_FALSE(Pat1->match("d"));
79 EXPECT_FALSE(Pat1->match("e"));
80 EXPECT_FALSE(Pat1->match("f"));
81 EXPECT_FALSE(Pat1->match("y"));
82 EXPECT_FALSE(Pat1->match("z"));
83 EXPECT_FALSE(Pat1->match(""));
85 Expected<GlobPattern> Pat2 = GlobPattern::create("[!abc-fy-z]");
86 EXPECT_TRUE((bool)Pat2);
87 EXPECT_TRUE(Pat2->match("g"));
88 EXPECT_FALSE(Pat2->match("a"));
89 EXPECT_FALSE(Pat2->match("b"));
90 EXPECT_FALSE(Pat2->match("c"));
91 EXPECT_FALSE(Pat2->match("d"));
92 EXPECT_FALSE(Pat2->match("e"));
93 EXPECT_FALSE(Pat2->match("f"));
94 EXPECT_FALSE(Pat2->match("y"));
95 EXPECT_FALSE(Pat2->match("z"));
96 EXPECT_FALSE(Pat2->match(""));
99 TEST_F(GlobPatternTest, BracketFrontOfCharacterClass) {
100 Expected<GlobPattern> Pat1 = GlobPattern::create("[]a]x");
101 EXPECT_TRUE((bool)Pat1);
102 EXPECT_TRUE(Pat1->match("]x"));
103 EXPECT_TRUE(Pat1->match("ax"));
104 EXPECT_FALSE(Pat1->match("a]x"));
105 EXPECT_FALSE(Pat1->match(""));
108 TEST_F(GlobPatternTest, SpecialCharsInCharacterClass) {
109 Expected<GlobPattern> Pat1 = GlobPattern::create("[*?^]");
110 EXPECT_TRUE((bool)Pat1);
111 EXPECT_TRUE(Pat1->match("*"));
112 EXPECT_TRUE(Pat1->match("?"));
113 EXPECT_TRUE(Pat1->match("^"));
114 EXPECT_FALSE(Pat1->match("*?^"));
115 EXPECT_FALSE(Pat1->match(""));
118 TEST_F(GlobPatternTest, Invalid) {
119 Expected<GlobPattern> Pat1 = GlobPattern::create("[");
120 EXPECT_FALSE((bool)Pat1);
121 handleAllErrors(Pat1.takeError(), [&](ErrorInfoBase &EIB) {});
123 Expected<GlobPattern> Pat2 = GlobPattern::create("[]");
124 EXPECT_FALSE((bool)Pat2);
125 handleAllErrors(Pat2.takeError(), [&](ErrorInfoBase &EIB) {});
128 TEST_F(GlobPatternTest, ExtSym) {
129 Expected<GlobPattern> Pat1 = GlobPattern::create("a*\xFF");
130 EXPECT_TRUE((bool)Pat1);
131 EXPECT_TRUE(Pat1->match("axxx\xFF"));
132 Expected<GlobPattern> Pat2 = GlobPattern::create("[\xFF-\xFF]");
133 EXPECT_TRUE((bool)Pat2);
134 EXPECT_TRUE(Pat2->match("\xFF"));
137 TEST_F(GlobPatternTest, IsTrivialMatchAll) {
138 Expected<GlobPattern> Pat1 = GlobPattern::create("*");
139 EXPECT_TRUE((bool)Pat1);
140 EXPECT_TRUE(Pat1->isTrivialMatchAll());
142 const char *NegativeCases[] = {"a*", "*a", "?*", "*?", "**", "\\*"};
143 for (auto *P : NegativeCases) {
144 Expected<GlobPattern> Pat2 = GlobPattern::create(P);
145 EXPECT_TRUE((bool)Pat2);
146 EXPECT_FALSE(Pat2->isTrivialMatchAll());