[docs] Fix build-docs.sh
[llvm-project.git] / llvm / unittests / Support / AnnotationsTest.cpp
blobb2970adc41cbeae61e85c02f4f15500eec713a2e
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"
12 using ::testing::ElementsAre;
13 using ::testing::IsEmpty;
15 namespace {
16 llvm::Annotations::Range range(size_t Begin, size_t End) {
17 llvm::Annotations::Range R;
18 R.Begin = Begin;
19 R.End = End;
20 return R;
23 TEST(AnnotationsTest, CleanedCode) {
24 EXPECT_EQ(llvm::Annotations("foo^bar$nnn[[baz$^[[qux]]]]").code(),
25 "foobarbazqux");
28 TEST(AnnotationsTest, Points) {
29 // A single point.
30 EXPECT_EQ(llvm::Annotations("^ab").point(), 0u);
31 EXPECT_EQ(llvm::Annotations("a^b").point(), 1u);
32 EXPECT_EQ(llvm::Annotations("ab^").point(), 2u);
34 // Multiple points.
35 EXPECT_THAT(llvm::Annotations("^a^bc^d^").points(),
36 ElementsAre(0u, 1u, 3u, 4u));
38 // No points.
39 EXPECT_THAT(llvm::Annotations("ab[[cd]]").points(), IsEmpty());
41 // Consecutive points.
42 EXPECT_THAT(llvm::Annotations("ab^^^cd").points(), ElementsAre(2u, 2u, 2u));
45 TEST(AnnotationsTest, Ranges) {
46 // A single range.
47 EXPECT_EQ(llvm::Annotations("[[a]]bc").range(), range(0, 1));
48 EXPECT_EQ(llvm::Annotations("a[[bc]]d").range(), range(1, 3));
49 EXPECT_EQ(llvm::Annotations("ab[[cd]]").range(), range(2, 4));
51 // Empty range.
52 EXPECT_EQ(llvm::Annotations("[[]]ab").range(), range(0, 0));
53 EXPECT_EQ(llvm::Annotations("a[[]]b").range(), range(1, 1));
54 EXPECT_EQ(llvm::Annotations("ab[[]]").range(), range(2, 2));
56 // Multiple ranges.
57 EXPECT_THAT(llvm::Annotations("[[a]][[b]]cd[[ef]]ef").ranges(),
58 ElementsAre(range(0, 1), range(1, 2), range(4, 6)));
60 // No ranges.
61 EXPECT_THAT(llvm::Annotations("ab^c^defef").ranges(), IsEmpty());
64 TEST(AnnotationsTest, Nested) {
65 llvm::Annotations Annotated("a[[f^oo^bar[[b[[a]]z]]]]bcdef");
66 EXPECT_THAT(Annotated.points(), ElementsAre(2u, 4u));
67 EXPECT_THAT(Annotated.ranges(),
68 ElementsAre(range(8, 9), range(7, 10), range(1, 10)));
71 TEST(AnnotationsTest, Named) {
72 // A single named point or range.
73 EXPECT_EQ(llvm::Annotations("a$foo^b").point("foo"), 1u);
74 EXPECT_EQ(llvm::Annotations("a$foo[[b]]cdef").range("foo"), range(1, 2));
76 // Empty names should also work.
77 EXPECT_EQ(llvm::Annotations("a$^b").point(""), 1u);
78 EXPECT_EQ(llvm::Annotations("a$[[b]]cdef").range(""), range(1, 2));
80 // Multiple named points.
81 llvm::Annotations Annotated("a$p1^bcd$p2^123$p1^345");
82 EXPECT_THAT(Annotated.points(), IsEmpty());
83 EXPECT_THAT(Annotated.points("p1"), ElementsAre(1u, 7u));
84 EXPECT_EQ(Annotated.point("p2"), 4u);
87 TEST(AnnotationsTest, Errors) {
88 // Annotations use llvm_unreachable, it will only crash in debug mode.
89 #ifndef NDEBUG
90 // point() and range() crash on zero or multiple ranges.
91 EXPECT_DEATH(llvm::Annotations("ab[[c]]def").point(),
92 "expected exactly one point");
93 EXPECT_DEATH(llvm::Annotations("a^b^cdef").point(),
94 "expected exactly one point");
96 EXPECT_DEATH(llvm::Annotations("a^bcdef").range(),
97 "expected exactly one range");
98 EXPECT_DEATH(llvm::Annotations("a[[b]]c[[d]]ef").range(),
99 "expected exactly one range");
101 EXPECT_DEATH(llvm::Annotations("$foo^a$foo^a").point("foo"),
102 "expected exactly one point");
103 EXPECT_DEATH(llvm::Annotations("$foo[[a]]bc$foo[[a]]").range("foo"),
104 "expected exactly one range");
106 // Parsing failures.
107 EXPECT_DEATH(llvm::Annotations("ff[[fdfd"), "unmatched \\[\\[");
108 EXPECT_DEATH(llvm::Annotations("ff[[fdjsfjd]]xxx]]"), "unmatched \\]\\]");
109 EXPECT_DEATH(llvm::Annotations("ff$fdsfd"), "unterminated \\$name");
110 #endif
112 } // namespace