1 //===- SpecialCaseListTest.cpp - Unit tests for SpecialCaseList -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/Support/FileSystem.h"
11 #include "llvm/Support/MemoryBuffer.h"
12 #include "llvm/Support/SpecialCaseList.h"
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16
17 namespace {
18
19 class SpecialCaseListTest : public ::testing::Test {
20 protected:
makeSpecialCaseList(StringRef List,std::string & Error)21 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
22 std::string &Error) {
23 std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List);
24 return SpecialCaseList::create(MB.get(), Error);
25 }
26
makeSpecialCaseList(StringRef List)27 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) {
28 std::string Error;
29 auto SCL = makeSpecialCaseList(List, Error);
30 assert(SCL);
31 assert(Error == "");
32 return SCL;
33 }
34
makeSpecialCaseListFile(StringRef Contents)35 std::string makeSpecialCaseListFile(StringRef Contents) {
36 int FD;
37 SmallString<64> Path;
38 sys::fs::createTemporaryFile("SpecialCaseListTest", "temp", FD, Path);
39 raw_fd_ostream OF(FD, true, true);
40 OF << Contents;
41 OF.close();
42 return Path.str();
43 }
44 };
45
TEST_F(SpecialCaseListTest,Basic)46 TEST_F(SpecialCaseListTest, Basic) {
47 std::unique_ptr<SpecialCaseList> SCL =
48 makeSpecialCaseList("# This is a comment.\n"
49 "\n"
50 "src:hello\n"
51 "src:bye\n"
52 "src:hi=category\n"
53 "src:z*=category\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"));
61 }
62
TEST_F(SpecialCaseListTest,GlobalInit)63 TEST_F(SpecialCaseListTest, GlobalInit) {
64 std::unique_ptr<SpecialCaseList> SCL =
65 makeSpecialCaseList("global:foo=init\n");
66 EXPECT_FALSE(SCL->inSection("global", "foo"));
67 EXPECT_FALSE(SCL->inSection("global", "bar"));
68 EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
69 EXPECT_FALSE(SCL->inSection("global", "bar", "init"));
70
71 SCL = makeSpecialCaseList("type:t2=init\n");
72 EXPECT_FALSE(SCL->inSection("type", "t1"));
73 EXPECT_FALSE(SCL->inSection("type", "t2"));
74 EXPECT_FALSE(SCL->inSection("type", "t1", "init"));
75 EXPECT_TRUE(SCL->inSection("type", "t2", "init"));
76
77 SCL = makeSpecialCaseList("src:hello=init\n");
78 EXPECT_FALSE(SCL->inSection("src", "hello"));
79 EXPECT_FALSE(SCL->inSection("src", "bye"));
80 EXPECT_TRUE(SCL->inSection("src", "hello", "init"));
81 EXPECT_FALSE(SCL->inSection("src", "bye", "init"));
82 }
83
TEST_F(SpecialCaseListTest,Substring)84 TEST_F(SpecialCaseListTest, Substring) {
85 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
86 "fun:foo\n"
87 "global:bar\n");
88 EXPECT_FALSE(SCL->inSection("src", "othello"));
89 EXPECT_FALSE(SCL->inSection("fun", "tomfoolery"));
90 EXPECT_FALSE(SCL->inSection("global", "bartender"));
91
92 SCL = makeSpecialCaseList("fun:*foo*\n");
93 EXPECT_TRUE(SCL->inSection("fun", "tomfoolery"));
94 EXPECT_TRUE(SCL->inSection("fun", "foobar"));
95 }
96
TEST_F(SpecialCaseListTest,InvalidSpecialCaseList)97 TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
98 std::string Error;
99 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
100 EXPECT_EQ("malformed line 1: 'badline'", Error);
101 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
102 EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range",
103 Error);
104 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
105 "fun:fun(a\n",
106 Error));
107 EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced",
108 Error);
109 std::vector<std::string> Files(1, "unexisting");
110 EXPECT_EQ(nullptr, SpecialCaseList::create(Files, Error));
111 EXPECT_EQ(0U, Error.find("can't open file 'unexisting':"));
112 }
113
TEST_F(SpecialCaseListTest,EmptySpecialCaseList)114 TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
115 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
116 EXPECT_FALSE(SCL->inSection("foo", "bar"));
117 }
118
TEST_F(SpecialCaseListTest,MultipleBlacklists)119 TEST_F(SpecialCaseListTest, MultipleBlacklists) {
120 std::vector<std::string> Files;
121 Files.push_back(makeSpecialCaseListFile("src:bar\n"
122 "src:*foo*\n"
123 "src:ban=init\n"));
124 Files.push_back(makeSpecialCaseListFile("src:baz\n"
125 "src:*fog*\n"));
126 auto SCL = SpecialCaseList::createOrDie(Files);
127 EXPECT_TRUE(SCL->inSection("src", "bar"));
128 EXPECT_TRUE(SCL->inSection("src", "baz"));
129 EXPECT_FALSE(SCL->inSection("src", "ban"));
130 EXPECT_TRUE(SCL->inSection("src", "ban", "init"));
131 EXPECT_TRUE(SCL->inSection("src", "tomfoolery"));
132 EXPECT_TRUE(SCL->inSection("src", "tomfoglery"));
133 }
134
135 }
136