aboutsummaryrefslogtreecommitdiffstats
path: root/unittests/Support/SpecialCaseListTest.cpp
blob: 0657f8003e8be26fa0a16229eb7e52ce02062357 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//===- SpecialCaseListTest.cpp - Unit tests for SpecialCaseList -----------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "llvm/Support/FileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/SpecialCaseList.h"
#include "gtest/gtest.h"

using namespace llvm;

namespace {

class SpecialCaseListTest : public ::testing::Test {
protected:
  std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
                                                       std::string &Error) {
    std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List);
    return SpecialCaseList::create(MB.get(), Error);
  }

  std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) {
    std::string Error;
    auto SCL = makeSpecialCaseList(List, Error);
    assert(SCL);
    assert(Error == "");
    return SCL;
  }

  std::string makeSpecialCaseListFile(StringRef Contents) {
    int FD;
    SmallString<64> Path;
    sys::fs::createTemporaryFile("SpecialCaseListTest", "temp", FD, Path);
    raw_fd_ostream OF(FD, true, true);
    OF << Contents;
    OF.close();
    return Path.str();
  }
};

TEST_F(SpecialCaseListTest, Basic) {
  std::unique_ptr<SpecialCaseList> SCL =
      makeSpecialCaseList("# This is a comment.\n"
                          "\n"
                          "src:hello\n"
                          "src:bye\n"
                          "src:hi=category\n"
                          "src:z*=category\n");
  EXPECT_TRUE(SCL->inSection("src", "hello"));
  EXPECT_TRUE(SCL->inSection("src", "bye"));
  EXPECT_TRUE(SCL->inSection("src", "hi", "category"));
  EXPECT_TRUE(SCL->inSection("src", "zzzz", "category"));
  EXPECT_FALSE(SCL->inSection("src", "hi"));
  EXPECT_FALSE(SCL->inSection("fun", "hello"));
  EXPECT_FALSE(SCL->inSection("src", "hello", "category"));
}

TEST_F(SpecialCaseListTest, GlobalInit) {
  std::unique_ptr<SpecialCaseList> SCL =
      makeSpecialCaseList("global:foo=init\n");
  EXPECT_FALSE(SCL->inSection("global", "foo"));
  EXPECT_FALSE(SCL->inSection("global", "bar"));
  EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
  EXPECT_FALSE(SCL->inSection("global", "bar", "init"));

  SCL = makeSpecialCaseList("type:t2=init\n");
  EXPECT_FALSE(SCL->inSection("type", "t1"));
  EXPECT_FALSE(SCL->inSection("type", "t2"));
  EXPECT_FALSE(SCL->inSection("type", "t1", "init"));
  EXPECT_TRUE(SCL->inSection("type", "t2", "init"));

  SCL = makeSpecialCaseList("src:hello=init\n");
  EXPECT_FALSE(SCL->inSection("src", "hello"));
  EXPECT_FALSE(SCL->inSection("src", "bye"));
  EXPECT_TRUE(SCL->inSection("src", "hello", "init"));
  EXPECT_FALSE(SCL->inSection("src", "bye", "init"));
}

TEST_F(SpecialCaseListTest, Substring) {
  std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
                                                             "fun:foo\n"
                                                             "global:bar\n");
  EXPECT_FALSE(SCL->inSection("src", "othello"));
  EXPECT_FALSE(SCL->inSection("fun", "tomfoolery"));
  EXPECT_FALSE(SCL->inSection("global", "bartender"));

  SCL = makeSpecialCaseList("fun:*foo*\n");
  EXPECT_TRUE(SCL->inSection("fun", "tomfoolery"));
  EXPECT_TRUE(SCL->inSection("fun", "foobar"));
}

TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
  std::string Error;
  EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
  EXPECT_EQ("malformed line 1: 'badline'", Error);
  EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
  EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range",
            Error);
  EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
                                   "fun:fun(a\n",
                                   Error));
  EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced",
            Error);
  std::vector<std::string> Files(1, "unexisting");
  EXPECT_EQ(nullptr, SpecialCaseList::create(Files, Error));
  EXPECT_EQ(0U, Error.find("can't open file 'unexisting':"));
}

TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
  std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
  EXPECT_FALSE(SCL->inSection("foo", "bar"));
}

TEST_F(SpecialCaseListTest, MultipleBlacklists) {
  std::vector<std::string> Files;
  Files.push_back(makeSpecialCaseListFile("src:bar\n"
                                          "src:*foo*\n"
                                          "src:ban=init\n"));
  Files.push_back(makeSpecialCaseListFile("src:baz\n"
                                          "src:*fog*\n"));
  auto SCL = SpecialCaseList::createOrDie(Files);
  EXPECT_TRUE(SCL->inSection("src", "bar"));
  EXPECT_TRUE(SCL->inSection("src", "baz"));
  EXPECT_FALSE(SCL->inSection("src", "ban"));
  EXPECT_TRUE(SCL->inSection("src", "ban", "init"));
  EXPECT_TRUE(SCL->inSection("src", "tomfoolery"));
  EXPECT_TRUE(SCL->inSection("src", "tomfoglery"));
}

}