blob: 740dbfef404474060c7f569a747c3aefbfa72a19 [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
10#include "llvm/Support/MemoryBuffer.h"
11#include "llvm/Support/SpecialCaseList.h"
12#include "gtest/gtest.h"
13
14using namespace llvm;
15
16namespace {
17
18class SpecialCaseListTest : public ::testing::Test {
19protected:
David Blaikie15913f42014-09-02 18:13:54 +000020 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
21 std::string &Error) {
David Blaikiedfbe3d62014-08-27 20:14:18 +000022 std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000023 return SpecialCaseList::create(MB.get(), Error);
24 }
25
David Blaikie15913f42014-09-02 18:13:54 +000026 std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) {
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000027 std::string Error;
David Blaikie15913f42014-09-02 18:13:54 +000028 auto SCL = makeSpecialCaseList(List, Error);
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000029 assert(SCL);
30 assert(Error == "");
31 return SCL;
32 }
33};
34
35TEST_F(SpecialCaseListTest, Basic) {
David Blaikie15913f42014-09-02 18:13:54 +000036 std::unique_ptr<SpecialCaseList> SCL =
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000037 makeSpecialCaseList("# This is a comment.\n"
38 "\n"
39 "src:hello\n"
40 "src:bye\n"
41 "src:hi=category\n"
David Blaikie15913f42014-09-02 18:13:54 +000042 "src:z*=category\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000043 EXPECT_TRUE(SCL->inSection("src", "hello"));
44 EXPECT_TRUE(SCL->inSection("src", "bye"));
45 EXPECT_TRUE(SCL->inSection("src", "hi", "category"));
46 EXPECT_TRUE(SCL->inSection("src", "zzzz", "category"));
47 EXPECT_FALSE(SCL->inSection("src", "hi"));
48 EXPECT_FALSE(SCL->inSection("fun", "hello"));
49 EXPECT_FALSE(SCL->inSection("src", "hello", "category"));
50}
51
Alexey Samsonovcfb97aa2014-11-20 01:27:19 +000052TEST_F(SpecialCaseListTest, GlobalInit) {
David Blaikie15913f42014-09-02 18:13:54 +000053 std::unique_ptr<SpecialCaseList> SCL =
54 makeSpecialCaseList("global:foo=init\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000055 EXPECT_FALSE(SCL->inSection("global", "foo"));
56 EXPECT_FALSE(SCL->inSection("global", "bar"));
57 EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
58 EXPECT_FALSE(SCL->inSection("global", "bar", "init"));
59
David Blaikie15913f42014-09-02 18:13:54 +000060 SCL = makeSpecialCaseList("type:t2=init\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000061 EXPECT_FALSE(SCL->inSection("type", "t1"));
62 EXPECT_FALSE(SCL->inSection("type", "t2"));
63 EXPECT_FALSE(SCL->inSection("type", "t1", "init"));
64 EXPECT_TRUE(SCL->inSection("type", "t2", "init"));
65
David Blaikie15913f42014-09-02 18:13:54 +000066 SCL = makeSpecialCaseList("src:hello=init\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000067 EXPECT_FALSE(SCL->inSection("src", "hello"));
68 EXPECT_FALSE(SCL->inSection("src", "bye"));
69 EXPECT_TRUE(SCL->inSection("src", "hello", "init"));
70 EXPECT_FALSE(SCL->inSection("src", "bye", "init"));
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000071}
72
73TEST_F(SpecialCaseListTest, Substring) {
David Blaikie15913f42014-09-02 18:13:54 +000074 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
75 "fun:foo\n"
76 "global:bar\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000077 EXPECT_FALSE(SCL->inSection("src", "othello"));
78 EXPECT_FALSE(SCL->inSection("fun", "tomfoolery"));
79 EXPECT_FALSE(SCL->inSection("global", "bartender"));
80
David Blaikie15913f42014-09-02 18:13:54 +000081 SCL = makeSpecialCaseList("fun:*foo*\n");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +000082 EXPECT_TRUE(SCL->inSection("fun", "tomfoolery"));
83 EXPECT_TRUE(SCL->inSection("fun", "foobar"));
84}
85
86TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
87 std::string Error;
88 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
89 EXPECT_EQ("Malformed line 1: 'badline'", Error);
90 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
91 EXPECT_EQ("Malformed regex in line 1: 'bad[a-': invalid character range",
92 Error);
93 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
94 "fun:fun(a\n",
95 Error));
96 EXPECT_EQ("Malformed regex in line 2: 'fun(a': parentheses not balanced",
97 Error);
98 EXPECT_EQ(nullptr, SpecialCaseList::create("unexisting", Error));
99 EXPECT_EQ(0U, Error.find("Can't open file 'unexisting':"));
100}
101
102TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
David Blaikie15913f42014-09-02 18:13:54 +0000103 std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
Alexey Samsonovb7dd3292014-07-09 19:40:08 +0000104 EXPECT_FALSE(SCL->inSection("foo", "bar"));
105}
106
107}
108
109