1 //===- unittest/Format/FormatTestSelective.cpp - Formatting unit tests ----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14
15 #define DEBUG_TYPE "format-test"
16
17 namespace clang {
18 namespace format {
19 namespace {
20
21 class FormatTestSelective : public ::testing::Test {
22 protected:
format(llvm::StringRef Code,unsigned Offset,unsigned Length)23 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length) {
24 DEBUG(llvm::errs() << "---\n");
25 DEBUG(llvm::errs() << Code << "\n\n");
26 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27 bool IncompleteFormat = false;
28 tooling::Replacements Replaces =
29 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
30 EXPECT_FALSE(IncompleteFormat) << Code << "\n\n";
31 std::string Result = applyAllReplacements(Code, Replaces);
32 EXPECT_NE("", Result);
33 DEBUG(llvm::errs() << "\n" << Result << "\n\n");
34 return Result;
35 }
36
37 FormatStyle Style = getLLVMStyle();
38 };
39
TEST_F(FormatTestSelective,RemovesTrailingWhitespaceOfFormattedLine)40 TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) {
41 EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0));
42 EXPECT_EQ("int a;", format("int a; ", 0, 0));
43 EXPECT_EQ("int a;\n", format("int a; \n \n \n ", 0, 0));
44 EXPECT_EQ("int a;\nint b; ", format("int a; \nint b; ", 0, 0));
45 }
46
TEST_F(FormatTestSelective,FormatsCorrectRegionForLeadingWhitespace)47 TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
48 EXPECT_EQ("{int b;\n"
49 " int a;\n"
50 "}",
51 format("{int b;\n int a;}", 8, 0));
52 EXPECT_EQ("{\n"
53 " int b;\n"
54 " int a;}",
55 format("{int b;\n int a;}", 7, 0));
56
57 Style.ColumnLimit = 12;
58 EXPECT_EQ("#define A \\\n"
59 " int a; \\\n"
60 " int b;",
61 format("#define A \\\n"
62 " int a; \\\n"
63 " int b;",
64 26, 0));
65 EXPECT_EQ("#define A \\\n"
66 " int a; \\\n"
67 " int b;",
68 format("#define A \\\n"
69 " int a; \\\n"
70 " int b;",
71 25, 0));
72 }
73
TEST_F(FormatTestSelective,FormatLineWhenInvokedOnTrailingNewline)74 TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) {
75 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0));
76 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0));
77
78 // This might not strictly be correct, but is likely good in all practical
79 // cases.
80 EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0));
81 }
82
TEST_F(FormatTestSelective,RemovesWhitespaceWhenTriggeredOnEmptyLine)83 TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
84 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0));
85 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0));
86 }
87
TEST_F(FormatTestSelective,ReformatsMovedLines)88 TEST_F(FormatTestSelective, ReformatsMovedLines) {
89 EXPECT_EQ(
90 "template <typename T> T *getFETokenInfo() const {\n"
91 " return static_cast<T *>(FETokenInfo);\n"
92 "}\n"
93 "int a; // <- Should not be formatted",
94 format(
95 "template<typename T>\n"
96 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
97 "int a; // <- Should not be formatted",
98 9, 5));
99 }
100
TEST_F(FormatTestSelective,FormatsIfWithoutCompoundStatement)101 TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) {
102 Style.AllowShortIfStatementsOnASingleLine = true;
103 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1));
104 EXPECT_EQ("if (a) return; // comment",
105 format("if(a)\nreturn; // comment", 20, 1));
106 }
107
TEST_F(FormatTestSelective,FormatsCommentsLocally)108 TEST_F(FormatTestSelective, FormatsCommentsLocally) {
109 EXPECT_EQ("int a; // comment\n"
110 "int b; // comment",
111 format("int a; // comment\n"
112 "int b; // comment",
113 0, 0));
114 EXPECT_EQ("int a; // comment\n"
115 " // line 2\n"
116 "int b;",
117 format("int a; // comment\n"
118 " // line 2\n"
119 "int b;",
120 28, 0));
121 EXPECT_EQ("int aaaaaa; // comment\n"
122 "int b;\n"
123 "int c; // unrelated comment",
124 format("int aaaaaa; // comment\n"
125 "int b;\n"
126 "int c; // unrelated comment",
127 31, 0));
128
129 EXPECT_EQ("int a; // This\n"
130 " // is\n"
131 " // a",
132 format("int a; // This\n"
133 " // is\n"
134 " // a",
135 0, 0));
136 EXPECT_EQ("int a; // This\n"
137 " // is\n"
138 " // a\n"
139 "// This is b\n"
140 "int b;",
141 format("int a; // This\n"
142 " // is\n"
143 " // a\n"
144 "// This is b\n"
145 "int b;",
146 0, 0));
147 EXPECT_EQ("int a; // This\n"
148 " // is\n"
149 " // a\n"
150 "\n"
151 "//This is unrelated",
152 format("int a; // This\n"
153 " // is\n"
154 " // a\n"
155 "\n"
156 "//This is unrelated",
157 0, 0));
158 EXPECT_EQ("int a;\n"
159 "// This is\n"
160 "// not formatted. ",
161 format("int a;\n"
162 "// This is\n"
163 "// not formatted. ",
164 0, 0));
165 }
166
TEST_F(FormatTestSelective,IndividualStatementsOfNestedBlocks)167 TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
168 EXPECT_EQ("DEBUG({\n"
169 " int i;\n"
170 " int j;\n"
171 "});",
172 format("DEBUG( {\n"
173 " int i;\n"
174 " int j;\n"
175 "} ) ;",
176 20, 1));
177 EXPECT_EQ("DEBUG( {\n"
178 " int i;\n"
179 " int j;\n"
180 "} ) ;",
181 format("DEBUG( {\n"
182 " int i;\n"
183 " int j;\n"
184 "} ) ;",
185 41, 1));
186 EXPECT_EQ("DEBUG( {\n"
187 " int i;\n"
188 " int j;\n"
189 "} ) ;",
190 format("DEBUG( {\n"
191 " int i;\n"
192 " int j;\n"
193 "} ) ;",
194 41, 1));
195 EXPECT_EQ("DEBUG({\n"
196 " int i;\n"
197 " int j;\n"
198 "});",
199 format("DEBUG( {\n"
200 " int i;\n"
201 " int j;\n"
202 "} ) ;",
203 20, 1));
204
205 EXPECT_EQ("Debug({\n"
206 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
207 " return;\n"
208 " },\n"
209 " a);",
210 format("Debug({\n"
211 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
212 " return;\n"
213 " },\n"
214 " a);",
215 50, 1));
216 EXPECT_EQ("DEBUG({\n"
217 " DEBUG({\n"
218 " int a;\n"
219 " int b;\n"
220 " }) ;\n"
221 "});",
222 format("DEBUG({\n"
223 " DEBUG({\n"
224 " int a;\n"
225 " int b;\n" // Format this line only.
226 " }) ;\n" // Don't touch this line.
227 "});",
228 35, 0));
229 EXPECT_EQ("DEBUG({\n"
230 " int a; //\n"
231 "});",
232 format("DEBUG({\n"
233 " int a; //\n"
234 "});",
235 0, 0));
236 EXPECT_EQ("someFunction(\n"
237 " [] {\n"
238 " // Only with this comment.\n"
239 " int i; // invoke formatting here.\n"
240 " }, // force line break\n"
241 " aaa);",
242 format("someFunction(\n"
243 " [] {\n"
244 " // Only with this comment.\n"
245 " int i; // invoke formatting here.\n"
246 " }, // force line break\n"
247 " aaa);",
248 63, 1));
249
250 EXPECT_EQ("int longlongname; // comment\n"
251 "int x = f({\n"
252 " int x; // comment\n"
253 " int y; // comment\n"
254 "});",
255 format("int longlongname; // comment\n"
256 "int x = f({\n"
257 " int x; // comment\n"
258 " int y; // comment\n"
259 "});",
260 65, 0));
261 EXPECT_EQ("int s = f({\n"
262 " class X {\n"
263 " public:\n"
264 " void f();\n"
265 " };\n"
266 "});",
267 format("int s = f({\n"
268 " class X {\n"
269 " public:\n"
270 " void f();\n"
271 " };\n"
272 "});",
273 0, 0));
274 }
275
TEST_F(FormatTestSelective,WrongIndent)276 TEST_F(FormatTestSelective, WrongIndent) {
277 EXPECT_EQ("namespace {\n"
278 "int i;\n"
279 "int j;\n"
280 "}",
281 format("namespace {\n"
282 " int i;\n" // Format here.
283 " int j;\n"
284 "}",
285 15, 0));
286 EXPECT_EQ("namespace {\n"
287 " int i;\n"
288 " int j;\n"
289 "}",
290 format("namespace {\n"
291 " int i;\n"
292 " int j;\n" // Format here.
293 "}",
294 24, 0));
295 }
296
TEST_F(FormatTestSelective,AlwaysFormatsEntireMacroDefinitions)297 TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
298 Style.AlignEscapedNewlinesLeft = true;
299 EXPECT_EQ("int i;\n"
300 "#define A \\\n"
301 " int i; \\\n"
302 " int j\n"
303 "int k;",
304 format("int i;\n"
305 "#define A \\\n"
306 " int i ; \\\n"
307 " int j\n"
308 "int k;",
309 8, 0)); // 8: position of "#define".
310 EXPECT_EQ("int i;\n"
311 "#define A \\\n"
312 " int i; \\\n"
313 " int j\n"
314 "int k;",
315 format("int i;\n"
316 "#define A \\\n"
317 " int i ; \\\n"
318 " int j\n"
319 "int k;",
320 45, 0)); // 45: position of "j".
321 }
322
TEST_F(FormatTestSelective,ReformatRegionAdjustsIndent)323 TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
324 EXPECT_EQ("{\n"
325 "{\n"
326 "a;\n"
327 "b;\n"
328 "}\n"
329 "}",
330 format("{\n"
331 "{\n"
332 "a;\n"
333 " b;\n"
334 "}\n"
335 "}",
336 13, 2));
337 EXPECT_EQ("{\n"
338 "{\n"
339 " a;\n"
340 " b;\n"
341 " c;\n"
342 " d;\n"
343 "}\n"
344 "}",
345 format("{\n"
346 "{\n"
347 " a;\n"
348 " b;\n"
349 " c;\n"
350 " d;\n"
351 "}\n"
352 "}",
353 9, 2));
354 EXPECT_EQ("{\n"
355 "{\n"
356 "public:\n"
357 " b;\n"
358 "}\n"
359 "}",
360 format("{\n"
361 "{\n"
362 "public:\n"
363 " b;\n"
364 "}\n"
365 "}",
366 17, 2));
367 EXPECT_EQ("{\n"
368 "{\n"
369 "a;\n"
370 "}\n"
371 "{\n"
372 " b; //\n"
373 "}\n"
374 "}",
375 format("{\n"
376 "{\n"
377 "a;\n"
378 "}\n"
379 "{\n"
380 " b; //\n"
381 "}\n"
382 "}",
383 22, 2));
384 EXPECT_EQ(" {\n"
385 " a; //\n"
386 " }",
387 format(" {\n"
388 "a; //\n"
389 " }",
390 4, 2));
391 EXPECT_EQ("void f() {}\n"
392 "void g() {}",
393 format("void f() {}\n"
394 "void g() {}",
395 13, 0));
396 EXPECT_EQ("int a; // comment\n"
397 " // line 2\n"
398 "int b;",
399 format("int a; // comment\n"
400 " // line 2\n"
401 " int b;",
402 35, 0));
403
404 EXPECT_EQ(" void f() {\n"
405 "#define A 1\n"
406 " }",
407 format(" void f() {\n"
408 " #define A 1\n" // Format this line.
409 " }",
410 20, 0));
411 EXPECT_EQ(" void f() {\n"
412 " int i;\n"
413 "#define A \\\n"
414 " int i; \\\n"
415 " int j;\n"
416 " int k;\n"
417 " }",
418 format(" void f() {\n"
419 " int i;\n"
420 "#define A \\\n"
421 " int i; \\\n"
422 " int j;\n"
423 " int k;\n" // Format this line.
424 " }",
425 67, 0));
426
427 Style.ColumnLimit = 11;
428 EXPECT_EQ(" int a;\n"
429 " void\n"
430 " ffffff() {\n"
431 " }",
432 format(" int a;\n"
433 "void ffffff() {}",
434 11, 0));
435 }
436
TEST_F(FormatTestSelective,UnderstandsTabs)437 TEST_F(FormatTestSelective, UnderstandsTabs) {
438 Style.IndentWidth = 8;
439 Style.UseTab = FormatStyle::UT_Always;
440 Style.AlignEscapedNewlinesLeft = true;
441 EXPECT_EQ("void f() {\n"
442 "\tf();\n"
443 "\tg();\n"
444 "}",
445 format("void f() {\n"
446 "\tf();\n"
447 "\tg();\n"
448 "}",
449 0, 0));
450 EXPECT_EQ("void f() {\n"
451 "\tf();\n"
452 "\tg();\n"
453 "}",
454 format("void f() {\n"
455 "\tf();\n"
456 "\tg();\n"
457 "}",
458 16, 0));
459 EXPECT_EQ("void f() {\n"
460 " \tf();\n"
461 "\tg();\n"
462 "}",
463 format("void f() {\n"
464 " \tf();\n"
465 " \tg();\n"
466 "}",
467 21, 0));
468 }
469
TEST_F(FormatTestSelective,StopFormattingWhenLeavingScope)470 TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) {
471 EXPECT_EQ(
472 "void f() {\n"
473 " if (a) {\n"
474 " g();\n"
475 " h();\n"
476 "}\n"
477 "\n"
478 "void g() {\n"
479 "}",
480 format("void f() {\n"
481 " if (a) {\n" // Assume this was added without the closing brace.
482 " g();\n"
483 " h();\n"
484 "}\n"
485 "\n"
486 "void g() {\n" // Make sure not to format this.
487 "}",
488 15, 0));
489 }
490
491 } // end namespace
492 } // end namespace format
493 } // end namespace clang
494