18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Basic/SourceManager.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/Support/Error.h"
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
30 using ::testing::ElementsAre;
31 using ::testing::ElementsAreArray;
32 using ::testing::UnorderedElementsAreArray;
35 std::vector<Range> gatherRanges(
const SelectionRange &SR) {
36 std::vector<Range> Ranges;
37 for (
const SelectionRange *S = &SR; S; S = S->parent.get())
38 Ranges.push_back(S->range);
43 gatherFoldingRanges(llvm::ArrayRef<FoldingRange> FoldingRanges) {
44 std::vector<Range> Ranges;
46 for (
const auto &R : FoldingRanges) {
47 NextRange.start.line = R.startLine;
48 NextRange.start.character = R.startCharacter;
49 NextRange.end.line = R.endLine;
50 NextRange.end.character = R.endCharacter;
51 Ranges.push_back(NextRange);
56 TEST(SemanticSelection, All) {
57 const char *Tests[] = {
58 R
"cpp( // Single statement in a function body.
60 [[[[int v = [[1^00]]]];]]
66 // int v = (10 + 2) * (a + a);
67 [[[[int v = [[[[([[[[10^]] + 2]])]] * (a + a)]]]];]]
70 R"cpp( // Function call.
71 int add(int x, int y) { return x + y; }
73 // int res = add(11, 22);
74 [[[[int res = [[add([[1^1]], 22)]]]];]]
77 R"cpp( // Tricky macros.
80 // int var = (4 + 15 MUL 6 + 10);
81 [[[[int var = [[[[([[4 + [[1^5]]]] MUL]] 6 + 10)]]]];]]
84 R"cpp( // Cursor inside a macro.
85 #define HASH(x) ((x) % 10)
87 [[[[int a = [[HASH([[[[2^3]] + 34]])]]]];]]
90 R"cpp( // Cursor on a macro.
91 #define HASH(x) ((x) % 10)
93 [[[[int a = [[HA^SH(23)]]]];]]
96 R"cpp( // Multiple declaration.
98 [[[[int var1, var^2]], var3;]]
101 R"cpp( // Before comment.
104 [[[[int var2 = [[[[var1]]^ /*some comment*/ + 41]]]];]]
110 R
"cpp( // Single statement in TU.
111 [[int v = [[1^00]]]];
113 R"cpp( // Cursor at end of VarDecl.
114 [[int v = [[100]]^]];
117 R
"cpp( // Cursor in between spaces.
119 int v = 100 + [[^]] 100;
124 struct AAA { struct BBB { static int ccc(); };};
126 // int x = AAA::BBB::ccc();
127 [[[[int x = [[[[AAA::BBB::c^cc]]()]]]];]]
131 struct AAA { struct BBB { static int ccc(); };};
133 // int x = AAA::BBB::ccc();
134 [[[[int x = [[[[[[[[[[AA^A]]::]]BBB::]]ccc]]()]]]];]]
137 R"cpp( // Inside struct.
138 struct A { static int a(); };
141 [[return [[[[1^1]] + 2]]]];
151 // int x = nsa::nsb::ccc();
152 [[[[int x = [[[[nsa::nsb::cc^c]]()]]]];]]
160 for (
const char *Test : Tests) {
161 auto T = Annotations(Test);
164 ElementsAreArray(T.ranges()))
169 TEST(SemanticSelection, RunViaClangdServer) {
171 MockCompilationDatabase CDB;
177 #define HASH(x) ((x) % 10)
181 const char *SourceContents = R
"cpp(
183 [[void bar(int& inp) [[{
184 // inp = HASH(foo(inp));
185 [[inp = [[HASH([[foo([[in^p]])]])]]]];
189 Annotations SourceAnnotations(SourceContents);
190 FS.Files[FooCpp] = std::string(SourceAnnotations.code());
191 Server.addDocument(FooCpp, SourceAnnotations.code());
194 ASSERT_TRUE(
bool(Ranges))
195 <<
"getSemanticRange returned an error: " << Ranges.takeError();
196 ASSERT_EQ(Ranges->size(), SourceAnnotations.points().size());
197 EXPECT_THAT(gatherRanges(Ranges->front()),
198 ElementsAreArray(SourceAnnotations.ranges()));
199 EXPECT_THAT(gatherRanges(Ranges->back()),
200 ElementsAre(SourceAnnotations.range(
"empty")));
203 TEST(FoldingRanges, All) {
204 const char *Tests[] = {
206 [[int global_variable]];
224 [[void getFooBar() { }]]
228 for (
const char *Test : Tests) {
229 auto T = Annotations(Test);
232 UnorderedElementsAreArray(T.ranges()))