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 "llvm/Support/VirtualFileSystem.h"
13 #include "gtest/gtest.h"
19 class SpecialCaseListTest
: public ::testing::Test
{
21 std::unique_ptr
<SpecialCaseList
> makeSpecialCaseList(StringRef List
,
23 std::unique_ptr
<MemoryBuffer
> MB
= MemoryBuffer::getMemBuffer(List
);
24 return SpecialCaseList::create(MB
.get(), Error
);
27 std::unique_ptr
<SpecialCaseList
> makeSpecialCaseList(StringRef List
) {
29 auto SCL
= makeSpecialCaseList(List
, Error
);
35 std::string
makeSpecialCaseListFile(StringRef Contents
) {
38 sys::fs::createTemporaryFile("SpecialCaseListTest", "temp", FD
, Path
);
39 raw_fd_ostream
OF(FD
, true, true);
42 return std::string(Path
.str());
46 TEST_F(SpecialCaseListTest
, Basic
) {
47 std::unique_ptr
<SpecialCaseList
> SCL
=
48 makeSpecialCaseList("# This is a comment.\n"
54 EXPECT_TRUE(SCL
->inSection("", "src", "hello"));
55 EXPECT_TRUE(SCL
->inSection("", "src", "bye"));
56 EXPECT_TRUE(SCL
->inSection("", "src", "hi", "category"));
57 EXPECT_TRUE(SCL
->inSection("", "src", "zzzz", "category"));
58 EXPECT_FALSE(SCL
->inSection("", "src", "hi"));
59 EXPECT_FALSE(SCL
->inSection("", "fun", "hello"));
60 EXPECT_FALSE(SCL
->inSection("", "src", "hello", "category"));
62 EXPECT_EQ(3u, SCL
->inSectionBlame("", "src", "hello"));
63 EXPECT_EQ(4u, SCL
->inSectionBlame("", "src", "bye"));
64 EXPECT_EQ(5u, SCL
->inSectionBlame("", "src", "hi", "category"));
65 EXPECT_EQ(6u, SCL
->inSectionBlame("", "src", "zzzz", "category"));
66 EXPECT_EQ(0u, SCL
->inSectionBlame("", "src", "hi"));
67 EXPECT_EQ(0u, SCL
->inSectionBlame("", "fun", "hello"));
68 EXPECT_EQ(0u, SCL
->inSectionBlame("", "src", "hello", "category"));
71 TEST_F(SpecialCaseListTest
, CorrectErrorLineNumberWithBlankLine
) {
73 EXPECT_EQ(nullptr, makeSpecialCaseList("# This is a comment.\n"
78 ((StringRef
)Error
).startswith("malformed section header on line 3:"));
80 EXPECT_EQ(nullptr, makeSpecialCaseList("\n\n\n"
84 ((StringRef
)Error
).startswith("malformed section header on line 4:"));
87 TEST_F(SpecialCaseListTest
, SectionRegexErrorHandling
) {
89 EXPECT_EQ(makeSpecialCaseList("[address", Error
), nullptr);
90 EXPECT_TRUE(((StringRef
)Error
).startswith("malformed section header "));
92 EXPECT_EQ(makeSpecialCaseList("[[]", Error
), nullptr);
93 EXPECT_TRUE(((StringRef
)Error
).startswith("malformed regex for section [: "));
95 EXPECT_EQ(makeSpecialCaseList("src:=", Error
), nullptr);
96 EXPECT_TRUE(((StringRef
)Error
).endswith("Supplied regexp was blank"));
99 TEST_F(SpecialCaseListTest
, Section
) {
100 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("src:global\n"
105 EXPECT_TRUE(SCL
->inSection("arbitrary", "src", "global"));
106 EXPECT_TRUE(SCL
->inSection("", "src", "global"));
107 EXPECT_TRUE(SCL
->inSection("sect1", "src", "test1"));
108 EXPECT_FALSE(SCL
->inSection("sect1-arbitrary", "src", "test1"));
109 EXPECT_FALSE(SCL
->inSection("sect", "src", "test1"));
110 EXPECT_FALSE(SCL
->inSection("sect1", "src", "test2"));
111 EXPECT_TRUE(SCL
->inSection("sect2", "src", "test1"));
112 EXPECT_TRUE(SCL
->inSection("sect3", "src", "test2"));
113 EXPECT_TRUE(SCL
->inSection("sect3-arbitrary", "src", "test2"));
114 EXPECT_FALSE(SCL
->inSection("", "src", "test1"));
115 EXPECT_FALSE(SCL
->inSection("", "src", "test2"));
118 TEST_F(SpecialCaseListTest
, GlobalInit
) {
119 std::unique_ptr
<SpecialCaseList
> SCL
=
120 makeSpecialCaseList("global:foo=init\n");
121 EXPECT_FALSE(SCL
->inSection("", "global", "foo"));
122 EXPECT_FALSE(SCL
->inSection("", "global", "bar"));
123 EXPECT_TRUE(SCL
->inSection("", "global", "foo", "init"));
124 EXPECT_FALSE(SCL
->inSection("", "global", "bar", "init"));
126 SCL
= makeSpecialCaseList("type:t2=init\n");
127 EXPECT_FALSE(SCL
->inSection("", "type", "t1"));
128 EXPECT_FALSE(SCL
->inSection("", "type", "t2"));
129 EXPECT_FALSE(SCL
->inSection("", "type", "t1", "init"));
130 EXPECT_TRUE(SCL
->inSection("", "type", "t2", "init"));
132 SCL
= makeSpecialCaseList("src:hello=init\n");
133 EXPECT_FALSE(SCL
->inSection("", "src", "hello"));
134 EXPECT_FALSE(SCL
->inSection("", "src", "bye"));
135 EXPECT_TRUE(SCL
->inSection("", "src", "hello", "init"));
136 EXPECT_FALSE(SCL
->inSection("", "src", "bye", "init"));
139 TEST_F(SpecialCaseListTest
, Substring
) {
140 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("src:hello\n"
143 EXPECT_FALSE(SCL
->inSection("", "src", "othello"));
144 EXPECT_FALSE(SCL
->inSection("", "fun", "tomfoolery"));
145 EXPECT_FALSE(SCL
->inSection("", "global", "bartender"));
147 SCL
= makeSpecialCaseList("fun:*foo*\n");
148 EXPECT_TRUE(SCL
->inSection("", "fun", "tomfoolery"));
149 EXPECT_TRUE(SCL
->inSection("", "fun", "foobar"));
152 TEST_F(SpecialCaseListTest
, InvalidSpecialCaseList
) {
154 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error
));
155 EXPECT_EQ("malformed line 1: 'badline'", Error
);
156 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error
));
157 EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range",
159 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
162 EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced",
164 std::vector
<std::string
> Files(1, "unexisting");
166 SpecialCaseList::create(Files
, *vfs::getRealFileSystem(), Error
));
167 EXPECT_EQ(0U, Error
.find("can't open file 'unexisting':"));
170 TEST_F(SpecialCaseListTest
, EmptySpecialCaseList
) {
171 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("");
172 EXPECT_FALSE(SCL
->inSection("", "foo", "bar"));
175 TEST_F(SpecialCaseListTest
, MultipleExclusions
) {
176 std::vector
<std::string
> Files
;
177 Files
.push_back(makeSpecialCaseListFile("src:bar\n"
180 Files
.push_back(makeSpecialCaseListFile("src:baz\n"
182 auto SCL
= SpecialCaseList::createOrDie(Files
, *vfs::getRealFileSystem());
183 EXPECT_TRUE(SCL
->inSection("", "src", "bar"));
184 EXPECT_TRUE(SCL
->inSection("", "src", "baz"));
185 EXPECT_FALSE(SCL
->inSection("", "src", "ban"));
186 EXPECT_TRUE(SCL
->inSection("", "src", "ban", "init"));
187 EXPECT_TRUE(SCL
->inSection("", "src", "tomfoolery"));
188 EXPECT_TRUE(SCL
->inSection("", "src", "tomfoglery"));
189 for (auto &Path
: Files
)
190 sys::fs::remove(Path
);
193 TEST_F(SpecialCaseListTest
, NoTrigramsInRules
) {
194 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("fun:b.r\n"
196 EXPECT_TRUE(SCL
->inSection("", "fun", "bar"));
197 EXPECT_FALSE(SCL
->inSection("", "fun", "baz"));
198 EXPECT_TRUE(SCL
->inSection("", "fun", "zakaz"));
199 EXPECT_FALSE(SCL
->inSection("", "fun", "zaraza"));
202 TEST_F(SpecialCaseListTest
, NoTrigramsInARule
) {
203 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("fun:*bar*\n"
205 EXPECT_TRUE(SCL
->inSection("", "fun", "abara"));
206 EXPECT_FALSE(SCL
->inSection("", "fun", "bor"));
207 EXPECT_TRUE(SCL
->inSection("", "fun", "zakaz"));
208 EXPECT_FALSE(SCL
->inSection("", "fun", "zaraza"));
211 TEST_F(SpecialCaseListTest
, RepetitiveRule
) {
212 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("fun:*bar*bar*bar*bar*\n"
214 EXPECT_TRUE(SCL
->inSection("", "fun", "bara"));
215 EXPECT_FALSE(SCL
->inSection("", "fun", "abara"));
216 EXPECT_TRUE(SCL
->inSection("", "fun", "barbarbarbar"));
217 EXPECT_TRUE(SCL
->inSection("", "fun", "abarbarbarbar"));
218 EXPECT_FALSE(SCL
->inSection("", "fun", "abarbarbar"));
221 TEST_F(SpecialCaseListTest
, SpecialSymbolRule
) {
222 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("src:*c\\+\\+abi*\n");
223 EXPECT_TRUE(SCL
->inSection("", "src", "c++abi"));
224 EXPECT_FALSE(SCL
->inSection("", "src", "c\\+\\+abi"));
227 TEST_F(SpecialCaseListTest
, PopularTrigram
) {
228 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("fun:*aaaaaa*\n"
232 EXPECT_TRUE(SCL
->inSection("", "fun", "aaa"));
233 EXPECT_TRUE(SCL
->inSection("", "fun", "aaaa"));
234 EXPECT_TRUE(SCL
->inSection("", "fun", "aaaabbbaaa"));
237 TEST_F(SpecialCaseListTest
, EscapedSymbols
) {
238 std::unique_ptr
<SpecialCaseList
> SCL
= makeSpecialCaseList("src:*c\\+\\+abi*\n"
239 "src:*hello\\\\world*\n");
240 EXPECT_TRUE(SCL
->inSection("", "src", "dir/c++abi"));
241 EXPECT_FALSE(SCL
->inSection("", "src", "dir/c\\+\\+abi"));
242 EXPECT_FALSE(SCL
->inSection("", "src", "c\\+\\+abi"));
243 EXPECT_TRUE(SCL
->inSection("", "src", "C:\\hello\\world"));
244 EXPECT_TRUE(SCL
->inSection("", "src", "hello\\world"));
245 EXPECT_FALSE(SCL
->inSection("", "src", "hello\\\\world"));