SpecialCaseListTest.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #include "llvm/Support/FileSystem.h"
  10. #include "llvm/Support/MemoryBuffer.h"
  11. #include "llvm/Support/SpecialCaseList.h"
  12. #include "gtest/gtest.h"
  13. using namespace llvm;
  14. namespace {
  15. class SpecialCaseListTest : public ::testing::Test {
  16. protected:
  17. std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
  18. std::string &Error) {
  19. std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List);
  20. return SpecialCaseList::create(MB.get(), Error);
  21. }
  22. std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) {
  23. std::string Error;
  24. auto SCL = makeSpecialCaseList(List, Error);
  25. assert(SCL);
  26. assert(Error == "");
  27. return SCL;
  28. }
  29. std::string makeSpecialCaseListFile(StringRef Contents) {
  30. int FD;
  31. SmallString<64> Path;
  32. sys::fs::createTemporaryFile("SpecialCaseListTest", "temp", FD, Path);
  33. raw_fd_ostream OF(FD, true, true);
  34. OF << Contents;
  35. OF.close();
  36. return Path.str();
  37. }
  38. };
  39. TEST_F(SpecialCaseListTest, Basic) {
  40. std::unique_ptr<SpecialCaseList> SCL =
  41. makeSpecialCaseList("# This is a comment.\n"
  42. "\n"
  43. "src:hello\n"
  44. "src:bye\n"
  45. "src:hi=category\n"
  46. "src:z*=category\n");
  47. EXPECT_TRUE(SCL->inSection("src", "hello"));
  48. EXPECT_TRUE(SCL->inSection("src", "bye"));
  49. EXPECT_TRUE(SCL->inSection("src", "hi", "category"));
  50. EXPECT_TRUE(SCL->inSection("src", "zzzz", "category"));
  51. EXPECT_FALSE(SCL->inSection("src", "hi"));
  52. EXPECT_FALSE(SCL->inSection("fun", "hello"));
  53. EXPECT_FALSE(SCL->inSection("src", "hello", "category"));
  54. }
  55. TEST_F(SpecialCaseListTest, GlobalInit) {
  56. std::unique_ptr<SpecialCaseList> SCL =
  57. makeSpecialCaseList("global:foo=init\n");
  58. EXPECT_FALSE(SCL->inSection("global", "foo"));
  59. EXPECT_FALSE(SCL->inSection("global", "bar"));
  60. EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
  61. EXPECT_FALSE(SCL->inSection("global", "bar", "init"));
  62. SCL = makeSpecialCaseList("type:t2=init\n");
  63. EXPECT_FALSE(SCL->inSection("type", "t1"));
  64. EXPECT_FALSE(SCL->inSection("type", "t2"));
  65. EXPECT_FALSE(SCL->inSection("type", "t1", "init"));
  66. EXPECT_TRUE(SCL->inSection("type", "t2", "init"));
  67. SCL = makeSpecialCaseList("src:hello=init\n");
  68. EXPECT_FALSE(SCL->inSection("src", "hello"));
  69. EXPECT_FALSE(SCL->inSection("src", "bye"));
  70. EXPECT_TRUE(SCL->inSection("src", "hello", "init"));
  71. EXPECT_FALSE(SCL->inSection("src", "bye", "init"));
  72. }
  73. TEST_F(SpecialCaseListTest, Substring) {
  74. std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
  75. "fun:foo\n"
  76. "global:bar\n");
  77. EXPECT_FALSE(SCL->inSection("src", "othello"));
  78. EXPECT_FALSE(SCL->inSection("fun", "tomfoolery"));
  79. EXPECT_FALSE(SCL->inSection("global", "bartender"));
  80. SCL = makeSpecialCaseList("fun:*foo*\n");
  81. EXPECT_TRUE(SCL->inSection("fun", "tomfoolery"));
  82. EXPECT_TRUE(SCL->inSection("fun", "foobar"));
  83. }
  84. TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
  85. std::string Error;
  86. EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
  87. EXPECT_EQ("malformed line 1: 'badline'", Error);
  88. EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
  89. EXPECT_EQ("malformed regex in line 1: 'bad[a-': invalid character range",
  90. Error);
  91. EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
  92. "fun:fun(a\n",
  93. Error));
  94. EXPECT_EQ("malformed regex in line 2: 'fun(a': parentheses not balanced",
  95. Error);
  96. std::vector<std::string> Files(1, "unexisting");
  97. EXPECT_EQ(nullptr, SpecialCaseList::create(Files, Error));
  98. EXPECT_EQ(0U, Error.find("can't open file 'unexisting':"));
  99. }
  100. TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
  101. std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
  102. EXPECT_FALSE(SCL->inSection("foo", "bar"));
  103. }
  104. TEST_F(SpecialCaseListTest, MultipleBlacklists) {
  105. std::vector<std::string> Files;
  106. Files.push_back(makeSpecialCaseListFile("src:bar\n"
  107. "src:*foo*\n"
  108. "src:ban=init\n"));
  109. Files.push_back(makeSpecialCaseListFile("src:baz\n"
  110. "src:*fog*\n"));
  111. auto SCL = SpecialCaseList::createOrDie(Files);
  112. EXPECT_TRUE(SCL->inSection("src", "bar"));
  113. EXPECT_TRUE(SCL->inSection("src", "baz"));
  114. EXPECT_FALSE(SCL->inSection("src", "ban"));
  115. EXPECT_TRUE(SCL->inSection("src", "ban", "init"));
  116. EXPECT_TRUE(SCL->inSection("src", "tomfoolery"));
  117. EXPECT_TRUE(SCL->inSection("src", "tomfoglery"));
  118. }
  119. }