1 //===- SpecialCaseListTest.cpp - Unit tests for SpecialCaseList -----------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Support/SpecialCaseList.h"
10 #include "llvm/Support/FileSystem.h"
11 #include "llvm/Support/MemoryBuffer.h"
12 #include "gtest/gtest.h"
18 class SpecialCaseListTest
: public ::testing::Test
{
20 std::unique_ptr
<SpecialCaseList
> makeSpecialCaseList(StringRef List
,
22 std::unique_ptr
<MemoryBuffer
> MB
= MemoryBuffer::getMemBuffer(List
);
23 return SpecialCaseList::create(MB
.get(), Error
);
26 std::unique_ptr
<SpecialCaseList
> makeSpecialCaseList(StringRef List
) {
28 auto SCL
= makeSpecialCaseList(List
, Error
);
34 std::string
makeSpecialCaseListFile(StringRef Contents
) {
37 sys::fs::createTemporaryFile("SpecialCaseListTest", "temp", FD
, Path
);
38 raw_fd_ostream
OF(FD
, true, true);
45 TEST_F(SpecialCaseListTest
, Basic
) {
46 std::unique_ptr
<SpecialCaseList
> SCL
=
47 makeSpecialCaseList("# This is a comment.\n"
53 EXPECT_TRUE(SCL
->inSection("", "src", "hello"));
54 EXPECT_TRUE(SCL
->inSection("", "src", "bye"));
55 EXPECT_TRUE(SCL
->inSection("", "src", "hi", "category"));
56 EXPECT_TRUE(SCL
->inSection("", "src", "zzzz", "category"));
57 EXPECT_FALSE(SCL
->inSection("", "src", "hi"));
58 EXPECT_FALSE(SCL
->inSection("", "fun", "hello"));
59 EXPECT_FALSE(SCL
->inSection("", "src", "hello", "category"));
61 EXPECT_EQ(3u, SCL
->inSectionBlame("", "src", "hello"));
62 EXPECT_EQ(4u, SCL
->inSectionBlame("", "src", "bye"));
63 EXPECT_EQ(5u, SCL
->inSectionBlame("", "src", "hi", "category"));
64 EXPECT_EQ(6u, SCL
->inSectionBlame("", "src", "zzzz", "category"));
65 EXPECT_EQ(0u, SCL
->inSectionBlame("", "src", "hi"));
66 EXPECT_EQ(0u, SCL
->inSectionBlame("", "fun", "hello"));
67 EXPECT_EQ(0u, SCL
->inSectionBlame("", "src", "hello", "category"));
70 TEST_F(SpecialCaseListTest
, CorrectErrorLineNumberWithBlankLine
) {
72 EXPECT_EQ(nullptr, makeSpecialCaseList("# This is a comment.\n"
77 ((StringRef
)Error
).startswith("malformed section header on line 3:"));
79 EXPECT_EQ(nullptr, makeSpecialCaseList("\n\n\n"
83 ((StringRef
)Error
).startswith("malformed section header on line 4:"));
86 TEST_F(SpecialCaseListTest
, SectionRegexErrorHandling
) {
88 EXPECT_EQ(makeSpecialCaseList("[address", Error
), nullptr);
89 EXPECT_TRUE(((StringRef
)Error
).startswith("malformed section header "));
91 EXPECT_EQ(makeSpecialCaseList("[[]", Error
), nullptr);
92 EXPECT_TRUE(((StringRef
)Error
).startswith("malformed regex for section [: "));
94 EXPECT_EQ(makeSpecialCaseList("src:=", Error
), nullptr);
95 EXPECT_TRUE(((StringRef
)Error
).endswith("Supplied regexp was blank"));
98 TEST_F(SpecialCaseListTest
, Section
) {
99 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("src:global\n"
104 EXPECT_TRUE(SCL
->inSection("arbitrary", "src", "global"));
105 EXPECT_TRUE(SCL
->inSection("", "src", "global"));
106 EXPECT_TRUE(SCL
->inSection("sect1", "src", "test1"));
107 EXPECT_FALSE(SCL
->inSection("sect1-arbitrary", "src", "test1"));
108 EXPECT_FALSE(SCL
->inSection("sect", "src", "test1"));
109 EXPECT_FALSE(SCL
->inSection("sect1", "src", "test2"));
110 EXPECT_TRUE(SCL
->inSection("sect2", "src", "test1"));
111 EXPECT_TRUE(SCL
->inSection("sect3", "src", "test2"));
112 EXPECT_TRUE(SCL
->inSection("sect3-arbitrary", "src", "test2"));
113 EXPECT_FALSE(SCL
->inSection("", "src", "test1"));
114 EXPECT_FALSE(SCL
->inSection("", "src", "test2"));
117 TEST_F(SpecialCaseListTest
, GlobalInit
) {
118 std::unique_ptr
<SpecialCaseList
> SCL
=
119 makeSpecialCaseList("global:foo=init\n");
120 EXPECT_FALSE(SCL
->inSection("", "global", "foo"));
121 EXPECT_FALSE(SCL
->inSection("", "global", "bar"));
122 EXPECT_TRUE(SCL
->inSection("", "global", "foo", "init"));
123 EXPECT_FALSE(SCL
->inSection("", "global", "bar", "init"));
125 SCL
= makeSpecialCaseList("type:t2=init\n");
126 EXPECT_FALSE(SCL
->inSection("", "type", "t1"));
127 EXPECT_FALSE(SCL
->inSection("", "type", "t2"));
128 EXPECT_FALSE(SCL
->inSection("", "type", "t1", "init"));
129 EXPECT_TRUE(SCL
->inSection("", "type", "t2", "init"));
131 SCL
= makeSpecialCaseList("src:hello=init\n");
132 EXPECT_FALSE(SCL
->inSection("", "src", "hello"));
133 EXPECT_FALSE(SCL
->inSection("", "src", "bye"));
134 EXPECT_TRUE(SCL
->inSection("", "src", "hello", "init"));
135 EXPECT_FALSE(SCL
->inSection("", "src", "bye", "init"));
138 TEST_F(SpecialCaseListTest
, Substring
) {
139 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("src:hello\n"
142 EXPECT_FALSE(SCL
->inSection("", "src", "othello"));
143 EXPECT_FALSE(SCL
->inSection("", "fun", "tomfoolery"));
144 EXPECT_FALSE(SCL
->inSection("", "global", "bartender"));
146 SCL
= makeSpecialCaseList("fun:*foo*\n");
147 EXPECT_TRUE(SCL
->inSection("", "fun", "tomfoolery"));
148 EXPECT_TRUE(SCL
->inSection("", "fun", "foobar"));
151 TEST_F(SpecialCaseListTest
, InvalidSpecialCaseList
) {
153 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error
));
154 EXPECT_EQ("malformed line 1: 'badline'", Error
);
155 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error
));
156 EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range",
158 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
161 EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced",
163 std::vector
<std::string
> Files(1, "unexisting");
164 EXPECT_EQ(nullptr, SpecialCaseList::create(Files
, Error
));
165 EXPECT_EQ(0U, Error
.find("can't open file 'unexisting':"));
168 TEST_F(SpecialCaseListTest
, EmptySpecialCaseList
) {
169 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("");
170 EXPECT_FALSE(SCL
->inSection("", "foo", "bar"));
173 TEST_F(SpecialCaseListTest
, MultipleBlacklists
) {
174 std::vector
<std::string
> Files
;
175 Files
.push_back(makeSpecialCaseListFile("src:bar\n"
178 Files
.push_back(makeSpecialCaseListFile("src:baz\n"
180 auto SCL
= SpecialCaseList::createOrDie(Files
);
181 EXPECT_TRUE(SCL
->inSection("", "src", "bar"));
182 EXPECT_TRUE(SCL
->inSection("", "src", "baz"));
183 EXPECT_FALSE(SCL
->inSection("", "src", "ban"));
184 EXPECT_TRUE(SCL
->inSection("", "src", "ban", "init"));
185 EXPECT_TRUE(SCL
->inSection("", "src", "tomfoolery"));
186 EXPECT_TRUE(SCL
->inSection("", "src", "tomfoglery"));
187 for (auto &Path
: Files
)
188 sys::fs::remove(Path
);
191 TEST_F(SpecialCaseListTest
, NoTrigramsInRules
) {
192 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("fun:b.r\n"
194 EXPECT_TRUE(SCL
->inSection("", "fun", "bar"));
195 EXPECT_FALSE(SCL
->inSection("", "fun", "baz"));
196 EXPECT_TRUE(SCL
->inSection("", "fun", "zakaz"));
197 EXPECT_FALSE(SCL
->inSection("", "fun", "zaraza"));
200 TEST_F(SpecialCaseListTest
, NoTrigramsInARule
) {
201 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("fun:*bar*\n"
203 EXPECT_TRUE(SCL
->inSection("", "fun", "abara"));
204 EXPECT_FALSE(SCL
->inSection("", "fun", "bor"));
205 EXPECT_TRUE(SCL
->inSection("", "fun", "zakaz"));
206 EXPECT_FALSE(SCL
->inSection("", "fun", "zaraza"));
209 TEST_F(SpecialCaseListTest
, RepetitiveRule
) {
210 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("fun:*bar*bar*bar*bar*\n"
212 EXPECT_TRUE(SCL
->inSection("", "fun", "bara"));
213 EXPECT_FALSE(SCL
->inSection("", "fun", "abara"));
214 EXPECT_TRUE(SCL
->inSection("", "fun", "barbarbarbar"));
215 EXPECT_TRUE(SCL
->inSection("", "fun", "abarbarbarbar"));
216 EXPECT_FALSE(SCL
->inSection("", "fun", "abarbarbar"));
219 TEST_F(SpecialCaseListTest
, SpecialSymbolRule
) {
220 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("src:*c\\+\\+abi*\n");
221 EXPECT_TRUE(SCL
->inSection("", "src", "c++abi"));
222 EXPECT_FALSE(SCL
->inSection("", "src", "c\\+\\+abi"));
225 TEST_F(SpecialCaseListTest
, PopularTrigram
) {
226 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("fun:*aaaaaa*\n"
230 EXPECT_TRUE(SCL
->inSection("", "fun", "aaa"));
231 EXPECT_TRUE(SCL
->inSection("", "fun", "aaaa"));
232 EXPECT_TRUE(SCL
->inSection("", "fun", "aaaabbbaaa"));
235 TEST_F(SpecialCaseListTest
, EscapedSymbols
) {
236 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("src:*c\\+\\+abi*\n"
237 "src:*hello\\\\world*\n");
238 EXPECT_TRUE(SCL
->inSection("", "src", "dir/c++abi"));
239 EXPECT_FALSE(SCL
->inSection("", "src", "dir/c\\+\\+abi"));
240 EXPECT_FALSE(SCL
->inSection("", "src", "c\\+\\+abi"));
241 EXPECT_TRUE(SCL
->inSection("", "src", "C:\\hello\\world"));
242 EXPECT_TRUE(SCL
->inSection("", "src", "hello\\world"));
243 EXPECT_FALSE(SCL
->inSection("", "src", "hello\\\\world"));