SpecialCaseListTest.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/MemoryBuffer.h"
  10. #include "llvm/Support/SpecialCaseList.h"
  11. #include "gtest/gtest.h"
  12. using namespace llvm;
  13. namespace {
  14. class SpecialCaseListTest : public ::testing::Test {
  15. protected:
  16. std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List,
  17. std::string &Error) {
  18. std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(List);
  19. return SpecialCaseList::create(MB.get(), Error);
  20. }
  21. std::unique_ptr<SpecialCaseList> makeSpecialCaseList(StringRef List) {
  22. std::string Error;
  23. auto SCL = makeSpecialCaseList(List, Error);
  24. assert(SCL);
  25. assert(Error == "");
  26. return SCL;
  27. }
  28. };
  29. TEST_F(SpecialCaseListTest, Basic) {
  30. std::unique_ptr<SpecialCaseList> SCL =
  31. makeSpecialCaseList("# This is a comment.\n"
  32. "\n"
  33. "src:hello\n"
  34. "src:bye\n"
  35. "src:hi=category\n"
  36. "src:z*=category\n");
  37. EXPECT_TRUE(SCL->inSection("src", "hello"));
  38. EXPECT_TRUE(SCL->inSection("src", "bye"));
  39. EXPECT_TRUE(SCL->inSection("src", "hi", "category"));
  40. EXPECT_TRUE(SCL->inSection("src", "zzzz", "category"));
  41. EXPECT_FALSE(SCL->inSection("src", "hi"));
  42. EXPECT_FALSE(SCL->inSection("fun", "hello"));
  43. EXPECT_FALSE(SCL->inSection("src", "hello", "category"));
  44. }
  45. TEST_F(SpecialCaseListTest, GlobalInit) {
  46. std::unique_ptr<SpecialCaseList> SCL =
  47. makeSpecialCaseList("global:foo=init\n");
  48. EXPECT_FALSE(SCL->inSection("global", "foo"));
  49. EXPECT_FALSE(SCL->inSection("global", "bar"));
  50. EXPECT_TRUE(SCL->inSection("global", "foo", "init"));
  51. EXPECT_FALSE(SCL->inSection("global", "bar", "init"));
  52. SCL = makeSpecialCaseList("type:t2=init\n");
  53. EXPECT_FALSE(SCL->inSection("type", "t1"));
  54. EXPECT_FALSE(SCL->inSection("type", "t2"));
  55. EXPECT_FALSE(SCL->inSection("type", "t1", "init"));
  56. EXPECT_TRUE(SCL->inSection("type", "t2", "init"));
  57. SCL = makeSpecialCaseList("src:hello=init\n");
  58. EXPECT_FALSE(SCL->inSection("src", "hello"));
  59. EXPECT_FALSE(SCL->inSection("src", "bye"));
  60. EXPECT_TRUE(SCL->inSection("src", "hello", "init"));
  61. EXPECT_FALSE(SCL->inSection("src", "bye", "init"));
  62. }
  63. TEST_F(SpecialCaseListTest, Substring) {
  64. std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("src:hello\n"
  65. "fun:foo\n"
  66. "global:bar\n");
  67. EXPECT_FALSE(SCL->inSection("src", "othello"));
  68. EXPECT_FALSE(SCL->inSection("fun", "tomfoolery"));
  69. EXPECT_FALSE(SCL->inSection("global", "bartender"));
  70. SCL = makeSpecialCaseList("fun:*foo*\n");
  71. EXPECT_TRUE(SCL->inSection("fun", "tomfoolery"));
  72. EXPECT_TRUE(SCL->inSection("fun", "foobar"));
  73. }
  74. TEST_F(SpecialCaseListTest, InvalidSpecialCaseList) {
  75. std::string Error;
  76. EXPECT_EQ(nullptr, makeSpecialCaseList("badline", Error));
  77. EXPECT_EQ("Malformed line 1: 'badline'", Error);
  78. EXPECT_EQ(nullptr, makeSpecialCaseList("src:bad[a-", Error));
  79. EXPECT_EQ("Malformed regex in line 1: 'bad[a-': invalid character range",
  80. Error);
  81. EXPECT_EQ(nullptr, makeSpecialCaseList("src:a.c\n"
  82. "fun:fun(a\n",
  83. Error));
  84. EXPECT_EQ("Malformed regex in line 2: 'fun(a': parentheses not balanced",
  85. Error);
  86. EXPECT_EQ(nullptr, SpecialCaseList::create("unexisting", Error));
  87. EXPECT_EQ(0U, Error.find("Can't open file 'unexisting':"));
  88. }
  89. TEST_F(SpecialCaseListTest, EmptySpecialCaseList) {
  90. std::unique_ptr<SpecialCaseList> SCL = makeSpecialCaseList("");
  91. EXPECT_FALSE(SCL->inSection("foo", "bar"));
  92. }
  93. }