1 //===- SpecialCaseListTest.cpp - Unit tests for SpecialCaseList -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Support/FileSystem.h"
11 #include "llvm/Support/MemoryBuffer.h"
12 #include "llvm/Support/SpecialCaseList.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);
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"));
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"));
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"));
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"));
84 TEST_F(SpecialCaseListTest, Substring) {
85 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
88 EXPECT_FALSE(SCL->inSection("src", "othello"));
89 EXPECT_FALSE(SCL->inSection("fun", "tomfoolery"));
90 EXPECT_FALSE(SCL->inSection("global", "bartender"));
92 SCL = makeSpecialCaseList("fun:*foo*\n");
93 EXPECT_TRUE(SCL->inSection("fun", "tomfoolery"));
94 EXPECT_TRUE(SCL->inSection("fun", "foobar"));
97 TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
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",
104 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
107 EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced",
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':"));
114 TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
115 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
116 EXPECT_FALSE(SCL->inSection("foo", "bar"));
119 TEST_F(SpecialCaseListTest, MultipleBlacklists) {
120 std::vector<std::string> Files;
121 Files.push_back(makeSpecialCaseListFile("src:bar\n"
124 Files.push_back(makeSpecialCaseListFile("src:baz\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"));