@@ -22,71 +22,62 @@ MATCHER_P2(RenameRange, Code, Range, "") {
22
22
return replacementToEdit (Code, arg).range == Range;
23
23
}
24
24
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
+
25
41
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[] = {
30
45
// 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
+
43
52
// 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
+
60
61
// 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" ,
77
69
};
78
- for (const Test & T : Tests) {
79
- Annotations Code (T. Before );
70
+ for (const auto T : Tests) {
71
+ Annotations Code (T);
80
72
auto TU = TestTU::withCode (Code.code ());
81
73
auto AST = TU.build ();
74
+ llvm::StringRef NewName = " abcde" ;
82
75
auto RenameResult =
83
- renameWithinFile (AST, testPath (TU.Filename ), Code.point (), " abcde " );
76
+ renameWithinFile (AST, testPath (TU.Filename ), Code.point (), NewName );
84
77
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);
90
81
}
91
82
}
92
83
@@ -176,25 +167,24 @@ TEST(RenameTest, Renameable) {
176
167
TU.ExtraArgs .push_back (" -xobjective-c++-header" );
177
168
}
178
169
auto AST = TU.build ();
179
-
170
+ llvm::StringRef NewName = " dummyNewName " ;
180
171
auto Results = renameWithinFile (AST, testPath (TU.Filename ), T.point (),
181
- " dummyNewName " , Case.Index );
172
+ NewName , Case.Index );
182
173
bool WantRename = true ;
183
174
if (T.ranges ().empty ())
184
175
WantRename = false ;
185
176
if (!WantRename) {
186
177
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 ();
189
180
auto ActualMessage = llvm::toString (Results.takeError ());
190
181
EXPECT_THAT (ActualMessage, testing::HasSubstr (Case.ErrorMessage ));
191
182
} else {
192
183
EXPECT_TRUE (bool (Results)) << " renameWithinFile returned an error: "
193
184
<< 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);
198
188
}
199
189
}
200
190
}
0 commit comments