Lines Matching full:code

1 //===- unittest/Format/CleanupTest.cpp - Code cleanup unit tests ----------===//
25 std::string cleanup(StringRef Code, const std::vector<tooling::Range> &Ranges, in cleanup() argument
27 tooling::Replacements Replaces = format::cleanup(Style, Code, Ranges); in cleanup()
29 auto Result = applyAllReplacements(Code, Replaces); in cleanup()
34 // Returns code after cleanup around \p Offsets.
35 std::string cleanupAroundOffsets(ArrayRef<unsigned> Offsets, StringRef Code, in cleanupAroundOffsets() argument
40 return cleanup(Code, Ranges, Style); in cleanupAroundOffsets()
45 std::string Code = "namespace A {\n" in TEST_F() local
56 EXPECT_EQ(Expected, cleanupAroundOffsets({28, 91, 132}, Code)); in TEST_F()
60 std::string Code = "namespace A {\n" in TEST_F() local
71 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); in TEST_F()
72 EXPECT_EQ(Expected, cleanup(Code, Ranges)); in TEST_F()
76 std::string Code = "namespace A {\n\n" in TEST_F() local
83 EXPECT_EQ(Expected, cleanupAroundOffsets({14}, Code)); in TEST_F()
87 std::string Code = "namespace A {\n" in TEST_F() local
95 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); in TEST_F()
96 std::string Result = cleanup(Code, Ranges); in TEST_F()
101 std::string Code = "namespace A\n" in TEST_F() local
112 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); in TEST_F()
115 std::string Result = cleanup(Code, Ranges, Style); in TEST_F()
120 std::string Code = "#ifdef A\n" in TEST_F() local
131 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); in TEST_F()
133 std::string Result = cleanup(Code, Ranges, Style); in TEST_F()
138 std::string Code = "class A {\nA() : , {} };"; in TEST_F() local
140 EXPECT_EQ(Expected, cleanupAroundOffsets({17, 19}, Code)); in TEST_F()
142 Code = "class A {\nA() : x(1), {} };"; in TEST_F()
144 EXPECT_EQ(Expected, cleanupAroundOffsets({23}, Code)); in TEST_F()
146 Code = "class A {\nA() :,,,,{} };"; in TEST_F()
148 EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code)); in TEST_F()
153 std::string Code = "class A {\nA() try : , {} };"; in TEST_F() local
155 EXPECT_EQ(Expected, cleanupAroundOffsets({21, 23}, Code)); in TEST_F()
157 Code = "class A {\nA() try : x(1), {} };"; in TEST_F()
159 EXPECT_EQ(Expected, cleanupAroundOffsets({27}, Code)); in TEST_F()
161 Code = "class A {\nA() try :,,,,{} };"; in TEST_F()
163 EXPECT_EQ(Expected, cleanupAroundOffsets({19}, Code)); in TEST_F()
165 Code = "class A {\nA() try : x(1),,, {} };"; in TEST_F()
167 EXPECT_EQ(Expected, cleanupAroundOffsets({27}, Code)); in TEST_F()
171 Code = "try : , {}"; in TEST_F()
173 EXPECT_EQ(Expected, cleanupAroundOffsets({8}, Code)); in TEST_F()
177 std::string Code = "class A {\nA() : =default; };"; in TEST_F() local
179 EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code)); in TEST_F()
181 Code = "class A {\nA() : , =default; };"; in TEST_F()
183 EXPECT_EQ(Expected, cleanupAroundOffsets({15}, Code)); in TEST_F()
187 std::string Code = "void f() { std::vector<int> v = {1,2,,,3,{4,5}}; }"; in TEST_F() local
189 EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code)); in TEST_F()
191 Code = "int main() { f(1,,2,3,,4);}"; in TEST_F()
193 EXPECT_EQ(Expected, cleanupAroundOffsets({17, 22}, Code)); in TEST_F()
197 std::string Code = "function f() { var x = [a, b, , c]; }"; in TEST_F() local
201 EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code, Style)); in TEST_F()
205 std::string Code = "int main() { f(,1,,2,3,f(1,2,),4,,);}"; in TEST_F() local
207 EXPECT_EQ(Expected, cleanupAroundOffsets({15, 18, 29, 33}, Code)); in TEST_F()
210 Code = "int main() { [](){f(,1,,2,3,f(1,2,),4,,);}();}"; in TEST_F()
212 EXPECT_EQ(Expected, cleanupAroundOffsets({20, 23, 34, 38}, Code)); in TEST_F()
217 // If there was trailing comma in the original code, then trailing comma is in TEST_F()
220 std::string Code = "void f() { std::vector<int> v = {1,2,3,,}; }"; in TEST_F() local
222 EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code)); in TEST_F()
224 // If there was no trailing comma in the original code, then trailing comma in TEST_F()
228 Code = "void f() { std::vector<int> v = {1,2,3,}; }"; in TEST_F()
231 EXPECT_EQ(Expected, cleanupAroundOffsets({39}, Code)); in TEST_F()
233 // Still no trailing comma in the original code, but two elements are deleted, in TEST_F()
235 Code = "void f() { std::vector<int> v = {1, 2, 3, , }; }"; in TEST_F()
238 EXPECT_EQ(Expected, cleanupAroundOffsets({42, 44}, Code)); in TEST_F()
242 std::string Code = "class A {\nA() : x({1}),, {} };"; in TEST_F() local
244 EXPECT_EQ(Expected, cleanupAroundOffsets({24, 26}, Code)); in TEST_F()
248 std::string Code = in TEST_F() local
255 std::string Result = cleanup(Code, Ranges); in TEST_F()
258 Code = "class A {\nA() : x(1), {} };"; in TEST_F()
262 Result = cleanup(Code, Ranges); in TEST_F()
267 std::string Code = in TEST_F() local
270 EXPECT_EQ(Expected, cleanupAroundOffsets({25, 40}, Code)); in TEST_F()
272 Code = "class A {\nA() : x({1}), // comment\n {} };"; in TEST_F()
274 EXPECT_EQ(Expected, cleanupAroundOffsets({25}, Code)); in TEST_F()
276 Code = "class A {\nA() : x({1}), // comment\n , y(1),{} };"; in TEST_F()
278 EXPECT_EQ(Expected, cleanupAroundOffsets({38}, Code)); in TEST_F()
280 Code = "class A {\nA() : x({1}), \n/* comment */, y(1),{} };"; in TEST_F()
282 EXPECT_EQ(Expected, cleanupAroundOffsets({40}, Code)); in TEST_F()
284 Code = "class A {\nA() : , // comment\n y(1),{} };"; in TEST_F()
286 EXPECT_EQ(Expected, cleanupAroundOffsets({17}, Code)); in TEST_F()
288 Code = "class A {\nA() // comment\n : ,,{} };"; in TEST_F()
290 EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code)); in TEST_F()
292 Code = "class A {\nA() // comment\n : ,,=default; };"; in TEST_F()
294 EXPECT_EQ(Expected, cleanupAroundOffsets({30}, Code)); in TEST_F()
298 std::string Code = "namespace A {\n" in TEST_F() local
309 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); in TEST_F()
310 std::string Result = cleanup(Code, Ranges); in TEST_F()
329 inline std::string apply(StringRef Code, in apply() argument
331 auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style); in apply()
334 auto Result = applyAllReplacements(Code, *CleanReplaces); in apply()
339 inline std::string formatAndApply(StringRef Code, in formatAndApply() argument
341 auto CleanReplaces = cleanupAroundReplacements(Code, Replaces, Style); in formatAndApply()
344 auto FormattedReplaces = formatReplacements(Code, *CleanReplaces, Style); in formatAndApply()
347 auto Result = applyAllReplacements(Code, *FormattedReplaces); in formatAndApply()
352 int getOffset(StringRef Code, int Line, int Column) { in getOffset() argument
354 FileID ID = Context.createInMemoryFile(FileName, Code); in getOffset()
365 std::string Code = "namespace A {\n" in TEST_F() local
381 toReplacements({createReplacement(getOffset(Code, 3, 3), 6, ""), in TEST_F()
382 createReplacement(getOffset(Code, 9, 34), 6, "")}); in TEST_F()
384 EXPECT_EQ(Expected, formatAndApply(Code, Replaces)); in TEST_F()
388 std::string Code = "#include \"x/fix.h\"\n" in TEST_F() local
405 EXPECT_EQ(Expected, apply(Code, Replaces)); in TEST_F()
409 std::string Code = "#include \"x/fix.h\"\n" in TEST_F() local
427 EXPECT_EQ(Expected, apply(Code, Replaces)); in TEST_F()
431 std::string Code = "\nint x;"; in TEST_F() local
444 EXPECT_EQ(Expected, formatAndApply(Code, Replaces)); in TEST_F()
448 std::string Code = "\nint x;"; in TEST_F() local
464 EXPECT_EQ(Expected, formatAndApply(Code, Replaces)); in TEST_F()
468 std::string Code = "#include <map>"; in TEST_F() local
469 // FIXME: a better behavior is to only append on newline to Code, but this in TEST_F()
476 EXPECT_EQ(Expected, apply(Code, Replaces)); in TEST_F()
480 std::string Code = "\n" in TEST_F() local
496 {createReplacement(getOffset(Code, 4, 8), 1, "b"), in TEST_F()
501 EXPECT_EQ(Expected, formatAndApply(Code, Replaces)); in TEST_F()
505 std::string Code = "#include \"abc.h\"\n" in TEST_F() local
513 EXPECT_EQ(Expected, apply(Code, Replaces)); in TEST_F()
517 std::string Code = "#include \"a.h\"\n" in TEST_F() local
525 EXPECT_EQ(Expected, apply(Code, Replaces)); in TEST_F()