blob: 0657f8003e8be26fa0a16229eb7e52ce02062357 [file] [log] [blame]
Alexey Samsonovb7dd3292014-07-09 19:40:08 +00001//===- 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
Alexey Samsonovb9b80272015-02-04 17:39:48 +000010#include "llvm/Support/FileSystem.h"
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000011#include "llvm/Support/MemoryBuffer.h"
12#include "llvm/Support/SpecialCaseList.h"
13#include "gtest/gtest.h"
14
15using namespace llvm;
16
17namespace {
18
19class SpecialCaseListTest : public ::testing::Test {
20protected:
David Blaikie15913f42014-09-02 18:13:54 +000021 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
22 std::string &Error) {
David Blaikiedfbe3d62014-08-27 20:14:18 +000023 std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000024 return SpecialCaseList::create(MB.get(), Error);
25 }
26
David Blaikie15913f42014-09-02 18:13:54 +000027 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) {
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000028 std::string Error;
David Blaikie15913f42014-09-02 18:13:54 +000029 auto SCL = makeSpecialCaseList(List, Error);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000030 assert(SCL);
31 assert(Error == "");
32 return SCL;
33 }
Alexey Samsonovb9b80272015-02-04 17:39:48 +000034
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 }
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000044};
45
46TEST_F(SpecialCaseListTest, Basic) {
David Blaikie15913f42014-09-02 18:13:54 +000047 std::unique_ptr<SpecialCaseList> SCL =
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000048 makeSpecialCaseList("# This is a comment.\n"
49 "\n"
50 "src:hello\n"
51 "src:bye\n"
52 "src:hi=category\n"
David Blaikie15913f42014-09-02 18:13:54 +000053 "src:z*=category\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000054 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
Alexey Samsonovcfb97aa2014-11-20 01:27:19 +000063TEST_F(SpecialCaseListTest, GlobalInit) {
David Blaikie15913f42014-09-02 18:13:54 +000064 std::unique_ptr<SpecialCaseList> SCL =
65 makeSpecialCaseList("global:foo=init\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000066 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
David Blaikie15913f42014-09-02 18:13:54 +000071 SCL = makeSpecialCaseList("type:t2=init\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000072 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
David Blaikie15913f42014-09-02 18:13:54 +000077 SCL = makeSpecialCaseList("src:hello=init\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000078 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"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000082}
83
84TEST_F(SpecialCaseListTest, Substring) {
David Blaikie15913f42014-09-02 18:13:54 +000085 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
86 "fun:foo\n"
87 "global:bar\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000088 EXPECT_FALSE(SCL->inSection("src", "othello"));
89 EXPECT_FALSE(SCL->inSection("fun", "tomfoolery"));
90 EXPECT_FALSE(SCL->inSection("global", "bartender"));
91
David Blaikie15913f42014-09-02 18:13:54 +000092 SCL = makeSpecialCaseList("fun:*foo*\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000093 EXPECT_TRUE(SCL->inSection("fun", "tomfoolery"));
94 EXPECT_TRUE(SCL->inSection("fun", "foobar"));
95}
96
97TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
98 std::string Error;
99 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000100 EXPECT_EQ("malformed line 1: 'badline'", Error);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000101 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000102 EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range",
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000103 Error);
104 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
105 "fun:fun(a\n",
106 Error));
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000107 EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced",
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000108 Error);
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000109 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':"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000112}
113
114TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
David Blaikie15913f42014-09-02 18:13:54 +0000115 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000116 EXPECT_FALSE(SCL->inSection("foo", "bar"));
117}
118
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000119TEST_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"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000133}
134
Alexey Samsonovb9b80272015-02-04 17:39:48 +0000135}