Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 1 | //===- 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 Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 10 | #include "llvm/Support/FileSystem.h" |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 11 | #include "llvm/Support/MemoryBuffer.h" |
| 12 | #include "llvm/Support/SpecialCaseList.h" |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | namespace { |
| 18 | |
| 19 | class SpecialCaseListTest : public ::testing::Test { |
| 20 | protected: |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 21 | std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List, |
| 22 | std::string &Error) { |
David Blaikie | dfbe3d6 | 2014-08-27 20:14:18 +0000 | [diff] [blame] | 23 | std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 24 | return SpecialCaseList::create(MB.get(), Error); |
| 25 | } |
| 26 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 27 | std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) { |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 28 | std::string Error; |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 29 | auto SCL = makeSpecialCaseList(List, Error); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 30 | assert(SCL); |
| 31 | assert(Error == ""); |
| 32 | return SCL; |
| 33 | } |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 34 | |
| 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 Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | TEST_F(SpecialCaseListTest, Basic) { |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 47 | std::unique_ptr<SpecialCaseList> SCL = |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 48 | makeSpecialCaseList("# This is a comment.\n" |
| 49 | "\n" |
| 50 | "src:hello\n" |
| 51 | "src:bye\n" |
| 52 | "src:hi=category\n" |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 53 | "src:z*=category\n"); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 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")); |
| 61 | } |
| 62 | |
Alexey Samsonov | cfb97aa | 2014-11-20 01:27:19 +0000 | [diff] [blame] | 63 | TEST_F(SpecialCaseListTest, GlobalInit) { |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 64 | std::unique_ptr<SpecialCaseList> SCL = |
| 65 | makeSpecialCaseList("global:foo=init\n"); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 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")); |
| 70 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 71 | SCL = makeSpecialCaseList("type:t2=init\n"); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 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")); |
| 76 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 77 | SCL = makeSpecialCaseList("src:hello=init\n"); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 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")); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | TEST_F(SpecialCaseListTest, Substring) { |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 85 | std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n" |
| 86 | "fun:foo\n" |
| 87 | "global:bar\n"); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 88 | EXPECT_FALSE(SCL->inSection("src", "othello")); |
| 89 | EXPECT_FALSE(SCL->inSection("fun", "tomfoolery")); |
| 90 | EXPECT_FALSE(SCL->inSection("global", "bartender")); |
| 91 | |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 92 | SCL = makeSpecialCaseList("fun:*foo*\n"); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 93 | EXPECT_TRUE(SCL->inSection("fun", "tomfoolery")); |
| 94 | EXPECT_TRUE(SCL->inSection("fun", "foobar")); |
| 95 | } |
| 96 | |
| 97 | TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) { |
| 98 | std::string Error; |
| 99 | EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error)); |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 100 | EXPECT_EQ("malformed line 1: 'badline'", Error); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 101 | EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error)); |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 102 | EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range", |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 103 | Error); |
| 104 | EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n" |
| 105 | "fun:fun(a\n", |
| 106 | Error)); |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 107 | EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced", |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 108 | Error); |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 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':")); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | TEST_F(SpecialCaseListTest, EmptySpecialCaseList) { |
David Blaikie | 15913f4 | 2014-09-02 18:13:54 +0000 | [diff] [blame] | 115 | std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList(""); |
Alexey Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 116 | EXPECT_FALSE(SCL->inSection("foo", "bar")); |
| 117 | } |
| 118 | |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 119 | TEST_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 Samsonov | b7dd329 | 2014-07-09 19:40:08 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Alexey Samsonov | b9b8027 | 2015-02-04 17:39:48 +0000 | [diff] [blame] | 135 | } |