AnnotationsTest.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //===----- unittests/AnnotationsTest.cpp ----------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Testing/Support/Annotations.h"
  9. #include "gmock/gmock.h"
  10. #include "gtest/gtest.h"
  11. using ::testing::ElementsAre;
  12. using ::testing::IsEmpty;
  13. namespace {
  14. llvm::Annotations::Range range(size_t Begin, size_t End) {
  15. llvm::Annotations::Range R;
  16. R.Begin = Begin;
  17. R.End = End;
  18. return R;
  19. }
  20. TEST(AnnotationsTest, CleanedCode) {
  21. EXPECT_EQ(llvm::Annotations("foo^bar$nnn[[baz$^[[qux]]]]").code(),
  22. "foobarbazqux");
  23. }
  24. TEST(AnnotationsTest, Points) {
  25. // A single point.
  26. EXPECT_EQ(llvm::Annotations("^ab").point(), 0u);
  27. EXPECT_EQ(llvm::Annotations("a^b").point(), 1u);
  28. EXPECT_EQ(llvm::Annotations("ab^").point(), 2u);
  29. // Multiple points.
  30. EXPECT_THAT(llvm::Annotations("^a^bc^d^").points(),
  31. ElementsAre(0u, 1u, 3u, 4u));
  32. // No points.
  33. EXPECT_THAT(llvm::Annotations("ab[[cd]]").points(), IsEmpty());
  34. // Consecutive points.
  35. EXPECT_THAT(llvm::Annotations("ab^^^cd").points(), ElementsAre(2u, 2u, 2u));
  36. }
  37. TEST(AnnotationsTest, Ranges) {
  38. // A single range.
  39. EXPECT_EQ(llvm::Annotations("[[a]]bc").range(), range(0, 1));
  40. EXPECT_EQ(llvm::Annotations("a[[bc]]d").range(), range(1, 3));
  41. EXPECT_EQ(llvm::Annotations("ab[[cd]]").range(), range(2, 4));
  42. // Empty range.
  43. EXPECT_EQ(llvm::Annotations("[[]]ab").range(), range(0, 0));
  44. EXPECT_EQ(llvm::Annotations("a[[]]b").range(), range(1, 1));
  45. EXPECT_EQ(llvm::Annotations("ab[[]]").range(), range(2, 2));
  46. // Multiple ranges.
  47. EXPECT_THAT(llvm::Annotations("[[a]][[b]]cd[[ef]]ef").ranges(),
  48. ElementsAre(range(0, 1), range(1, 2), range(4, 6)));
  49. // No ranges.
  50. EXPECT_THAT(llvm::Annotations("ab^c^defef").ranges(), IsEmpty());
  51. }
  52. TEST(AnnotationsTest, Nested) {
  53. llvm::Annotations Annotated("a[[f^oo^bar[[b[[a]]z]]]]bcdef");
  54. EXPECT_THAT(Annotated.points(), ElementsAre(2u, 4u));
  55. EXPECT_THAT(Annotated.ranges(),
  56. ElementsAre(range(8, 9), range(7, 10), range(1, 10)));
  57. }
  58. TEST(AnnotationsTest, Named) {
  59. // A single named point or range.
  60. EXPECT_EQ(llvm::Annotations("a$foo^b").point("foo"), 1u);
  61. EXPECT_EQ(llvm::Annotations("a$foo[[b]]cdef").range("foo"), range(1, 2));
  62. // Empty names should also work.
  63. EXPECT_EQ(llvm::Annotations("a$^b").point(""), 1u);
  64. EXPECT_EQ(llvm::Annotations("a$[[b]]cdef").range(""), range(1, 2));
  65. // Multiple named points.
  66. llvm::Annotations Annotated("a$p1^bcd$p2^123$p1^345");
  67. EXPECT_THAT(Annotated.points(), IsEmpty());
  68. EXPECT_THAT(Annotated.points("p1"), ElementsAre(1u, 7u));
  69. EXPECT_EQ(Annotated.point("p2"), 4u);
  70. }
  71. TEST(AnnotationsTest, Errors) {
  72. // Annotations use llvm_unreachable, it will only crash in debug mode.
  73. #ifndef NDEBUG
  74. // point() and range() crash on zero or multiple ranges.
  75. EXPECT_DEATH(llvm::Annotations("ab[[c]]def").point(),
  76. "expected exactly one point");
  77. EXPECT_DEATH(llvm::Annotations("a^b^cdef").point(),
  78. "expected exactly one point");
  79. EXPECT_DEATH(llvm::Annotations("a^bcdef").range(),
  80. "expected exactly one range");
  81. EXPECT_DEATH(llvm::Annotations("a[[b]]c[[d]]ef").range(),
  82. "expected exactly one range");
  83. EXPECT_DEATH(llvm::Annotations("$foo^a$foo^a").point("foo"),
  84. "expected exactly one point");
  85. EXPECT_DEATH(llvm::Annotations("$foo[[a]]bc$foo[[a]]").range("foo"),
  86. "expected exactly one range");
  87. // Parsing failures.
  88. EXPECT_DEATH(llvm::Annotations("ff[[fdfd"), "unmatched \\[\\[");
  89. EXPECT_DEATH(llvm::Annotations("ff[[fdjsfjd]]xxx]]"), "unmatched \\]\\]");
  90. EXPECT_DEATH(llvm::Annotations("ff$fdsfd"), "unterminated \\$name");
  91. #endif
  92. }
  93. } // namespace