1 //===----- unittests/AnnotationsTest.cpp ----------------------------------===//
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
7 //===----------------------------------------------------------------------===//
8 #include "llvm/Testing/Annotations/Annotations.h"
9 #include "gmock/gmock.h"
10 #include "gtest/gtest.h"
12 using ::testing::AllOf
;
13 using ::testing::ElementsAre
;
14 using ::testing::IsEmpty
;
15 using ::testing::Pair
;
16 using ::testing::ResultOf
;
17 using ::testing::UnorderedElementsAre
;
20 MATCHER_P2(pair
, first_matcher
, second_matcher
, "") {
21 return testing::ExplainMatchResult(
22 AllOf(ResultOf([](const auto &entry
) { return entry
.getKey(); },
24 ResultOf([](const auto &entry
) { return entry
.getValue(); },
26 arg
, result_listener
);
29 llvm::Annotations::Range
range(size_t Begin
, size_t End
) {
30 llvm::Annotations::Range R
;
36 TEST(AnnotationsTest
, CleanedCode
) {
37 EXPECT_EQ(llvm::Annotations("foo^bar$nnn[[baz$^[[qux]]]]").code(),
41 TEST(AnnotationsTest
, Points
) {
43 EXPECT_EQ(llvm::Annotations("^ab").point(), 0u);
44 EXPECT_EQ(llvm::Annotations("a^b").point(), 1u);
45 EXPECT_EQ(llvm::Annotations("ab^").point(), 2u);
48 EXPECT_THAT(llvm::Annotations("^a^bc^d^").points(),
49 ElementsAre(0u, 1u, 3u, 4u));
52 EXPECT_THAT(llvm::Annotations("ab[[cd]]").points(), IsEmpty());
54 // Consecutive points.
55 EXPECT_THAT(llvm::Annotations("ab^^^cd").points(), ElementsAre(2u, 2u, 2u));
58 TEST(AnnotationsTest
, AllPoints
) {
60 EXPECT_THAT(llvm::Annotations("0$p1^123$p2^456$p1^$p1^78^9").all_points(),
61 UnorderedElementsAre(pair("", ElementsAre(9u)),
62 pair("p1", ElementsAre(1u, 7u, 7u)),
63 pair("p2", ElementsAre(4u))));
66 EXPECT_THAT(llvm::Annotations("ab[[cd]]").all_points(), IsEmpty());
69 TEST(AnnotationsTest
, Ranges
) {
71 EXPECT_EQ(llvm::Annotations("[[a]]bc").range(), range(0, 1));
72 EXPECT_EQ(llvm::Annotations("a[[bc]]d").range(), range(1, 3));
73 EXPECT_EQ(llvm::Annotations("ab[[cd]]").range(), range(2, 4));
76 EXPECT_EQ(llvm::Annotations("[[]]ab").range(), range(0, 0));
77 EXPECT_EQ(llvm::Annotations("a[[]]b").range(), range(1, 1));
78 EXPECT_EQ(llvm::Annotations("ab[[]]").range(), range(2, 2));
81 EXPECT_THAT(llvm::Annotations("[[a]][[b]]cd[[ef]]ef").ranges(),
82 ElementsAre(range(0, 1), range(1, 2), range(4, 6)));
85 EXPECT_THAT(llvm::Annotations("ab^c^defef").ranges(), IsEmpty());
88 TEST(AnnotationsTest
, AllRanges
) {
91 llvm::Annotations("[[]]01$outer[[2[[[[$inner[[3]]]]]]456]]7$outer[[89]]")
94 pair("", ElementsAre(range(0, 0), range(3, 4), range(3, 4))),
95 pair("outer", ElementsAre(range(2, 7), range(8, 10))),
96 pair("inner", ElementsAre(range(3, 4)))));
99 EXPECT_THAT(llvm::Annotations("ab^c^defef").all_ranges(), IsEmpty());
102 TEST(AnnotationsTest
, Nested
) {
103 llvm::Annotations
Annotated("a[[f^oo^bar[[b[[a]]z]]]]bcdef");
104 EXPECT_THAT(Annotated
.points(), ElementsAre(2u, 4u));
105 EXPECT_THAT(Annotated
.ranges(),
106 ElementsAre(range(8, 9), range(7, 10), range(1, 10)));
109 TEST(AnnotationsTest
, Payload
) {
110 // // A single unnamed point or range with unspecified payload
111 EXPECT_THAT(llvm::Annotations("a$^b").pointWithPayload(), Pair(1u, ""));
112 EXPECT_THAT(llvm::Annotations("a$[[b]]cdef").rangeWithPayload(),
113 Pair(range(1, 2), ""));
115 // A single unnamed point or range with empty payload
116 EXPECT_THAT(llvm::Annotations("a$()^b").pointWithPayload(), Pair(1u, ""));
117 EXPECT_THAT(llvm::Annotations("a$()[[b]]cdef").rangeWithPayload(),
118 Pair(range(1, 2), ""));
120 // A single unnamed point or range with payload.
121 EXPECT_THAT(llvm::Annotations("a$(foo)^b").pointWithPayload(),
123 EXPECT_THAT(llvm::Annotations("a$(foo)[[b]]cdef").rangeWithPayload(),
124 Pair(range(1, 2), "foo"));
126 // A single named point or range with payload
127 EXPECT_THAT(llvm::Annotations("a$name(foo)^b").pointWithPayload("name"),
130 llvm::Annotations("a$name(foo)[[b]]cdef").rangeWithPayload("name"),
131 Pair(range(1, 2), "foo"));
133 // Multiple named points with payload.
134 llvm::Annotations
Annotated("a$p1(p1)^bcd$p2(p2)^123$p1^345");
135 EXPECT_THAT(Annotated
.points(), IsEmpty());
136 EXPECT_THAT(Annotated
.pointsWithPayload("p1"),
137 ElementsAre(Pair(1u, "p1"), Pair(7u, "")));
138 EXPECT_THAT(Annotated
.pointWithPayload("p2"), Pair(4u, "p2"));
141 TEST(AnnotationsTest
, Named
) {
142 // A single named point or range.
143 EXPECT_EQ(llvm::Annotations("a$foo^b").point("foo"), 1u);
144 EXPECT_EQ(llvm::Annotations("a$foo[[b]]cdef").range("foo"), range(1, 2));
146 // Empty names should also work.
147 EXPECT_EQ(llvm::Annotations("a$^b").point(""), 1u);
148 EXPECT_EQ(llvm::Annotations("a$[[b]]cdef").range(""), range(1, 2));
150 // Multiple named points.
151 llvm::Annotations
Annotated("a$p1^bcd$p2^123$p1^345");
152 EXPECT_THAT(Annotated
.points(), IsEmpty());
153 EXPECT_THAT(Annotated
.points("p1"), ElementsAre(1u, 7u));
154 EXPECT_EQ(Annotated
.point("p2"), 4u);
157 TEST(AnnotationsTest
, Errors
) {
158 // Annotations use llvm_unreachable, it will only crash in debug mode.
160 // point() and range() crash on zero or multiple ranges.
161 EXPECT_DEATH(llvm::Annotations("ab[[c]]def").point(),
162 "expected exactly one point");
163 EXPECT_DEATH(llvm::Annotations("a^b^cdef").point(),
164 "expected exactly one point");
166 EXPECT_DEATH(llvm::Annotations("a^bcdef").range(),
167 "expected exactly one range");
168 EXPECT_DEATH(llvm::Annotations("a[[b]]c[[d]]ef").range(),
169 "expected exactly one range");
171 EXPECT_DEATH(llvm::Annotations("$foo^a$foo^a").point("foo"),
172 "expected exactly one point");
173 EXPECT_DEATH(llvm::Annotations("$foo[[a]]bc$foo[[a]]").range("foo"),
174 "expected exactly one range");
177 EXPECT_DEATH(llvm::Annotations("ff[[fdfd"), "unmatched \\[\\[");
178 EXPECT_DEATH(llvm::Annotations("ff[[fdjsfjd]]xxx]]"), "unmatched \\]\\]");
179 EXPECT_DEATH(llvm::Annotations("ff$fdsfd"), "unterminated \\$name");
180 EXPECT_DEATH(llvm::Annotations("ff$("), "unterminated payload");
181 EXPECT_DEATH(llvm::Annotations("ff$name("), "unterminated payload");