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/MemoryBuffer.h"
11 #include "llvm/Support/SpecialCaseList.h"
12 #include "gtest/gtest.h"
18 class SpecialCaseListTest : public ::testing::Test {
20 SpecialCaseList *makeSpecialCaseList(StringRef List, std::string &Error) {
21 std::unique_ptr<MemoryBuffer> MB(MemoryBuffer::getMemBuffer(List));
22 return SpecialCaseList::create(MB.get(), Error);
25 SpecialCaseList *makeSpecialCaseList(StringRef List) {
27 SpecialCaseList *SCL = makeSpecialCaseList(List, Error);
34 TEST_F(SpecialCaseListTest, Basic) {
35 std::unique_ptr<SpecialCaseList> SCL(
36 makeSpecialCaseList("# This is a comment.\n"
41 "src:z*=category\n"));
42 EXPECT_TRUE(SCL->inSection("src", "hello"));
43 EXPECT_TRUE(SCL->inSection("src", "bye"));
44 EXPECT_TRUE(SCL->inSection("src", "hi", "category"));
45 EXPECT_TRUE(SCL->inSection("src", "zzzz", "category"));
46 EXPECT_FALSE(SCL->inSection("src", "hi"));
47 EXPECT_FALSE(SCL->inSection("fun", "hello"));
48 EXPECT_FALSE(SCL->inSection("src", "hello", "category"));
51 TEST_F(SpecialCaseListTest, GlobalInitCompat) {
52 std::unique_ptr<SpecialCaseList> SCL(
53 makeSpecialCaseList("global:foo=init\n"));
54 EXPECT_FALSE(SCL->inSection("global", "foo"));
55 EXPECT_FALSE(SCL->inSection("global", "bar"));
56 EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
57 EXPECT_FALSE(SCL->inSection("global", "bar", "init"));
59 SCL.reset(makeSpecialCaseList("global-init:foo\n"));
60 EXPECT_FALSE(SCL->inSection("global", "foo"));
61 EXPECT_FALSE(SCL->inSection("global", "bar"));
62 EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
63 EXPECT_FALSE(SCL->inSection("global", "bar", "init"));
65 SCL.reset(makeSpecialCaseList("type:t2=init\n"));
66 EXPECT_FALSE(SCL->inSection("type", "t1"));
67 EXPECT_FALSE(SCL->inSection("type", "t2"));
68 EXPECT_FALSE(SCL->inSection("type", "t1", "init"));
69 EXPECT_TRUE(SCL->inSection("type", "t2", "init"));
71 SCL.reset(makeSpecialCaseList("global-init-type:t2\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.reset(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"));
83 SCL.reset(makeSpecialCaseList("global-init-src:hello\n"));
84 EXPECT_FALSE(SCL->inSection("src", "hello"));
85 EXPECT_FALSE(SCL->inSection("src", "bye"));
86 EXPECT_TRUE(SCL->inSection("src", "hello", "init"));
87 EXPECT_FALSE(SCL->inSection("src", "bye", "init"));
90 TEST_F(SpecialCaseListTest, Substring) {
91 std::unique_ptr<SpecialCaseList> SCL(makeSpecialCaseList("src:hello\n"
94 EXPECT_FALSE(SCL->inSection("src", "othello"));
95 EXPECT_FALSE(SCL->inSection("fun", "tomfoolery"));
96 EXPECT_FALSE(SCL->inSection("global", "bartender"));
98 SCL.reset(makeSpecialCaseList("fun:*foo*\n"));
99 EXPECT_TRUE(SCL->inSection("fun", "tomfoolery"));
100 EXPECT_TRUE(SCL->inSection("fun", "foobar"));
103 TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
105 EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
106 EXPECT_EQ("Malformed line 1: 'badline'", Error);
107 EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
108 EXPECT_EQ("Malformed regex in line 1: 'bad[a-': invalid character range",
110 EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
113 EXPECT_EQ("Malformed regex in line 2: 'fun(a': parentheses not balanced",
115 EXPECT_EQ(nullptr, SpecialCaseList::create("unexisting", Error));
116 EXPECT_EQ(0U, Error.find("Can't open file 'unexisting':"));
119 TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
120 std::unique_ptr<SpecialCaseList> SCL(makeSpecialCaseList(""));
121 EXPECT_FALSE(SCL->inSection("foo", "bar"));