Skip to content

Commit b0eed2a

Browse files
committed
[clangd] Improve the output of rename tests where there are failures.
Summary: Previously, we match ranges, which is hard to spot the difference. Now, we diff the code after rename against the expected result, it produces much nicer output. Reviewers: ilya-biryukov Subscribers: MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D69890
1 parent 8e34dd9 commit b0eed2a

File tree

1 file changed

+54
-64
lines changed

1 file changed

+54
-64
lines changed

clang-tools-extra/clangd/unittests/RenameTests.cpp

Lines changed: 54 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -22,71 +22,62 @@ MATCHER_P2(RenameRange, Code, Range, "") {
2222
return replacementToEdit(Code, arg).range == Range;
2323
}
2424

25+
// Generates an expected rename result by replacing all ranges in the given
26+
// annotation with the NewName.
27+
std::string expectedResult(Annotations Test, llvm::StringRef NewName) {
28+
std::string Result;
29+
unsigned NextChar = 0;
30+
llvm::StringRef Code = Test.code();
31+
for (const auto &R : Test.llvm::Annotations::ranges()) {
32+
assert(R.Begin <= R.End && NextChar <= R.Begin);
33+
Result += Code.substr(NextChar, R.Begin - NextChar);
34+
Result += NewName;
35+
NextChar = R.End;
36+
}
37+
Result += Code.substr(NextChar);
38+
return Result;
39+
}
40+
2541
TEST(RenameTest, SingleFile) {
26-
struct Test {
27-
const char* Before;
28-
const char* After;
29-
} Tests[] = {
42+
// "^" points to the position of the rename, and "[[]]" ranges point to the
43+
// identifier that is being renamed.
44+
llvm::StringRef Tests[] = {
3045
// Rename function.
31-
{
32-
R"cpp(
33-
void foo() {
34-
fo^o();
35-
}
36-
)cpp",
37-
R"cpp(
38-
void abcde() {
39-
abcde();
40-
}
41-
)cpp",
42-
},
46+
R"cpp(
47+
void [[foo]]() {
48+
[[fo^o]]();
49+
}
50+
)cpp",
51+
4352
// Rename type.
44-
{
45-
R"cpp(
46-
struct foo{};
47-
foo test() {
48-
f^oo x;
49-
return x;
50-
}
51-
)cpp",
52-
R"cpp(
53-
struct abcde{};
54-
abcde test() {
55-
abcde x;
56-
return x;
57-
}
58-
)cpp",
59-
},
53+
R"cpp(
54+
struct [[foo]]{};
55+
[[foo]] test() {
56+
[[f^oo]] x;
57+
return x;
58+
}
59+
)cpp",
60+
6061
// Rename variable.
61-
{
62-
R"cpp(
63-
void bar() {
64-
if (auto ^foo = 5) {
65-
foo = 3;
66-
}
67-
}
68-
)cpp",
69-
R"cpp(
70-
void bar() {
71-
if (auto abcde = 5) {
72-
abcde = 3;
73-
}
74-
}
75-
)cpp",
76-
},
62+
R"cpp(
63+
void bar() {
64+
if (auto [[^foo]] = 5) {
65+
[[foo]] = 3;
66+
}
67+
}
68+
)cpp",
7769
};
78-
for (const Test &T : Tests) {
79-
Annotations Code(T.Before);
70+
for (const auto T : Tests) {
71+
Annotations Code(T);
8072
auto TU = TestTU::withCode(Code.code());
8173
auto AST = TU.build();
74+
llvm::StringRef NewName = "abcde";
8275
auto RenameResult =
83-
renameWithinFile(AST, testPath(TU.Filename), Code.point(), "abcde");
76+
renameWithinFile(AST, testPath(TU.Filename), Code.point(), NewName);
8477
ASSERT_TRUE(bool(RenameResult)) << RenameResult.takeError();
85-
auto ApplyResult =
86-
tooling::applyAllReplacements(Code.code(), *RenameResult);
87-
ASSERT_TRUE(bool(ApplyResult)) << ApplyResult.takeError();
88-
89-
EXPECT_EQ(T.After, *ApplyResult) << T.Before;
78+
auto ApplyResult = llvm::cantFail(
79+
tooling::applyAllReplacements(Code.code(), *RenameResult));
80+
EXPECT_EQ(expectedResult(Code, NewName), ApplyResult);
9081
}
9182
}
9283

@@ -176,25 +167,24 @@ TEST(RenameTest, Renameable) {
176167
TU.ExtraArgs.push_back("-xobjective-c++-header");
177168
}
178169
auto AST = TU.build();
179-
170+
llvm::StringRef NewName = "dummyNewName";
180171
auto Results = renameWithinFile(AST, testPath(TU.Filename), T.point(),
181-
"dummyNewName", Case.Index);
172+
NewName, Case.Index);
182173
bool WantRename = true;
183174
if (T.ranges().empty())
184175
WantRename = false;
185176
if (!WantRename) {
186177
assert(Case.ErrorMessage && "Error message must be set!");
187-
EXPECT_FALSE(Results) << "expected renameWithinFile returned an error: "
188-
<< T.code();
178+
EXPECT_FALSE(Results)
179+
<< "expected renameWithinFile returned an error: " << T.code();
189180
auto ActualMessage = llvm::toString(Results.takeError());
190181
EXPECT_THAT(ActualMessage, testing::HasSubstr(Case.ErrorMessage));
191182
} else {
192183
EXPECT_TRUE(bool(Results)) << "renameWithinFile returned an error: "
193184
<< llvm::toString(Results.takeError());
194-
std::vector<testing::Matcher<tooling::Replacement>> Expected;
195-
for (const auto &R : T.ranges())
196-
Expected.push_back(RenameRange(TU.Code, R));
197-
EXPECT_THAT(*Results, UnorderedElementsAreArray(Expected));
185+
auto ApplyResult =
186+
llvm::cantFail(tooling::applyAllReplacements(T.code(), *Results));
187+
EXPECT_EQ(expectedResult(T, NewName), ApplyResult);
198188
}
199189
}
200190
}

0 commit comments

Comments
 (0)