blob: 182218fe96dbea731674f60d5fc650abddc51a14 [file] [log] [blame]
Daniel Jasperd246a5a2015-06-15 15:25:11 +00001//===- 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
17namespace clang {
18namespace format {
19namespace {
20
21class FormatTestSelective : public ::testing::Test {
22protected:
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));
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000027 FormattingAttemptStatus Status;
Daniel Jasperd246a5a2015-06-15 15:25:11 +000028 tooling::Replacements Replaces =
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000029 reformat(Style, Code, Ranges, "<stdin>", &Status);
30 EXPECT_TRUE(Status.FormatComplete) << Code << "\n\n";
Eric Liu4f8d9942016-07-11 13:53:12 +000031 auto Result = applyAllReplacements(Code, Replaces);
32 EXPECT_TRUE(static_cast<bool>(Result));
33 DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
34 return *Result;
Daniel Jasperd246a5a2015-06-15 15:25:11 +000035 }
36
37 FormatStyle Style = getLLVMStyle();
38};
39
40TEST_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
47TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
Daniel Jaspera1036e52015-10-28 01:08:22 +000048 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));
Daniel Jasperd246a5a2015-06-15 15:25:11 +000056
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
74TEST_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
83TEST_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
88TEST_F(FormatTestSelective, ReformatsMovedLines) {
89 EXPECT_EQ(
90 "template <typename T> T *getFETokenInfo() const {\n"
91 " return static_cast<T *>(FETokenInfo);\n"
92 "}\n"
Daniel Jaspera1036e52015-10-28 01:08:22 +000093 "int a; // <- Should not be formatted",
Daniel Jasperd246a5a2015-06-15 15:25:11 +000094 format(
95 "template<typename T>\n"
96 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
Daniel Jaspera1036e52015-10-28 01:08:22 +000097 "int a; // <- Should not be formatted",
Daniel Jasperd246a5a2015-06-15 15:25:11 +000098 9, 5));
99}
100
101TEST_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
108TEST_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));
Krasimir Georgiev91834222017-01-25 13:58:58 +0000114 EXPECT_EQ("int a; // comment\n"
115 " // line 2\n"
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000116 "int b;",
117 format("int a; // comment\n"
118 " // line 2\n"
119 "int b;",
120 28, 0));
Krasimir Georgiev91834222017-01-25 13:58:58 +0000121 EXPECT_EQ("int a; // comment\n"
122 "// comment 2\n"
123 "int b;",
124 format("int a; // comment\n"
125 "// comment 2\n"
126 "int b;", 28, 0));
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000127 EXPECT_EQ("int aaaaaa; // comment\n"
128 "int b;\n"
129 "int c; // unrelated comment",
130 format("int aaaaaa; // comment\n"
131 "int b;\n"
132 "int c; // unrelated comment",
133 31, 0));
134
135 EXPECT_EQ("int a; // This\n"
136 " // is\n"
137 " // a",
138 format("int a; // This\n"
139 " // is\n"
140 " // a",
141 0, 0));
142 EXPECT_EQ("int a; // This\n"
143 " // is\n"
144 " // a\n"
145 "// This is b\n"
146 "int b;",
147 format("int a; // This\n"
148 " // is\n"
149 " // a\n"
150 "// This is b\n"
151 "int b;",
152 0, 0));
153 EXPECT_EQ("int a; // This\n"
154 " // is\n"
155 " // a\n"
156 "\n"
Daniel Jaspera1036e52015-10-28 01:08:22 +0000157 "//This is unrelated",
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000158 format("int a; // This\n"
159 " // is\n"
160 " // a\n"
161 "\n"
Daniel Jaspera1036e52015-10-28 01:08:22 +0000162 "//This is unrelated",
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000163 0, 0));
164 EXPECT_EQ("int a;\n"
165 "// This is\n"
166 "// not formatted. ",
167 format("int a;\n"
168 "// This is\n"
169 "// not formatted. ",
170 0, 0));
Daniel Jasper417fc812016-01-09 15:56:53 +0000171 EXPECT_EQ("int x; // Format this line.\n"
172 "int xx; //\n"
173 "int xxxxx; //",
174 format("int x; // Format this line.\n"
175 "int xx; //\n"
176 "int xxxxx; //",
177 0, 0));
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000178}
179
180TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
181 EXPECT_EQ("DEBUG({\n"
182 " int i;\n"
183 " int j;\n"
184 "});",
185 format("DEBUG( {\n"
186 " int i;\n"
187 " int j;\n"
188 "} ) ;",
189 20, 1));
190 EXPECT_EQ("DEBUG( {\n"
191 " int i;\n"
192 " int j;\n"
193 "} ) ;",
194 format("DEBUG( {\n"
195 " int i;\n"
196 " int j;\n"
197 "} ) ;",
198 41, 1));
199 EXPECT_EQ("DEBUG( {\n"
200 " int i;\n"
201 " int j;\n"
202 "} ) ;",
203 format("DEBUG( {\n"
204 " int i;\n"
205 " int j;\n"
206 "} ) ;",
207 41, 1));
208 EXPECT_EQ("DEBUG({\n"
209 " int i;\n"
210 " int j;\n"
211 "});",
212 format("DEBUG( {\n"
213 " int i;\n"
214 " int j;\n"
215 "} ) ;",
216 20, 1));
217
218 EXPECT_EQ("Debug({\n"
219 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
220 " return;\n"
221 " },\n"
222 " a);",
223 format("Debug({\n"
224 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
225 " return;\n"
226 " },\n"
227 " a);",
228 50, 1));
229 EXPECT_EQ("DEBUG({\n"
230 " DEBUG({\n"
231 " int a;\n"
232 " int b;\n"
233 " }) ;\n"
234 "});",
235 format("DEBUG({\n"
236 " DEBUG({\n"
237 " int a;\n"
238 " int b;\n" // Format this line only.
239 " }) ;\n" // Don't touch this line.
240 "});",
241 35, 0));
242 EXPECT_EQ("DEBUG({\n"
243 " int a; //\n"
244 "});",
245 format("DEBUG({\n"
246 " int a; //\n"
247 "});",
248 0, 0));
249 EXPECT_EQ("someFunction(\n"
250 " [] {\n"
251 " // Only with this comment.\n"
252 " int i; // invoke formatting here.\n"
253 " }, // force line break\n"
254 " aaa);",
255 format("someFunction(\n"
256 " [] {\n"
257 " // Only with this comment.\n"
258 " int i; // invoke formatting here.\n"
259 " }, // force line break\n"
260 " aaa);",
261 63, 1));
262
263 EXPECT_EQ("int longlongname; // comment\n"
264 "int x = f({\n"
265 " int x; // comment\n"
266 " int y; // comment\n"
267 "});",
268 format("int longlongname; // comment\n"
269 "int x = f({\n"
270 " int x; // comment\n"
271 " int y; // comment\n"
272 "});",
273 65, 0));
274 EXPECT_EQ("int s = f({\n"
275 " class X {\n"
276 " public:\n"
277 " void f();\n"
278 " };\n"
279 "});",
280 format("int s = f({\n"
281 " class X {\n"
282 " public:\n"
283 " void f();\n"
284 " };\n"
285 "});",
286 0, 0));
Daniel Jasper35ca66d2016-02-29 12:26:20 +0000287 EXPECT_EQ("SomeFunction(\n"
288 " [] {\n"
289 " int i;\n"
290 " return i;\n" // Format this line.
291 " },\n"
292 " [] {\n"
293 " return 2;\n" // Don't fix this.
294 " });",
295 format("SomeFunction(\n"
296 " [] {\n"
297 " int i;\n"
298 " return i;\n" // Format this line.
299 " },\n"
300 " [] {\n"
301 " return 2;\n" // Don't fix this.
302 " });",
303 40, 0));
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000304}
305
Daniel Jasperf83834f2015-11-02 20:02:49 +0000306TEST_F(FormatTestSelective, WrongIndent) {
307 EXPECT_EQ("namespace {\n"
308 "int i;\n"
309 "int j;\n"
310 "}",
311 format("namespace {\n"
312 " int i;\n" // Format here.
313 " int j;\n"
314 "}",
315 15, 0));
316 EXPECT_EQ("namespace {\n"
317 " int i;\n"
318 " int j;\n"
319 "}",
320 format("namespace {\n"
321 " int i;\n"
322 " int j;\n" // Format here.
323 "}",
324 24, 0));
325}
326
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000327TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
Daniel Jasper7fdbb3f2017-05-08 15:08:00 +0000328 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000329 EXPECT_EQ("int i;\n"
330 "#define A \\\n"
331 " int i; \\\n"
332 " int j\n"
333 "int k;",
334 format("int i;\n"
335 "#define A \\\n"
336 " int i ; \\\n"
337 " int j\n"
338 "int k;",
339 8, 0)); // 8: position of "#define".
340 EXPECT_EQ("int i;\n"
341 "#define A \\\n"
342 " int i; \\\n"
343 " int j\n"
344 "int k;",
345 format("int i;\n"
346 "#define A \\\n"
347 " int i ; \\\n"
348 " int j\n"
349 "int k;",
350 45, 0)); // 45: position of "j".
351}
352
353TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
354 EXPECT_EQ("{\n"
355 "{\n"
356 "a;\n"
357 "b;\n"
358 "}\n"
359 "}",
360 format("{\n"
361 "{\n"
362 "a;\n"
363 " b;\n"
364 "}\n"
365 "}",
366 13, 2));
367 EXPECT_EQ("{\n"
368 "{\n"
369 " a;\n"
Daniel Jaspera1036e52015-10-28 01:08:22 +0000370 " b;\n"
371 " c;\n"
372 " d;\n"
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000373 "}\n"
374 "}",
375 format("{\n"
376 "{\n"
377 " a;\n"
Daniel Jaspera1036e52015-10-28 01:08:22 +0000378 " b;\n"
379 " c;\n"
380 " d;\n"
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000381 "}\n"
382 "}",
383 9, 2));
384 EXPECT_EQ("{\n"
385 "{\n"
386 "public:\n"
387 " b;\n"
388 "}\n"
389 "}",
390 format("{\n"
391 "{\n"
392 "public:\n"
393 " b;\n"
394 "}\n"
395 "}",
396 17, 2));
397 EXPECT_EQ("{\n"
398 "{\n"
399 "a;\n"
400 "}\n"
401 "{\n"
402 " b; //\n"
403 "}\n"
404 "}",
405 format("{\n"
406 "{\n"
407 "a;\n"
408 "}\n"
409 "{\n"
410 " b; //\n"
411 "}\n"
412 "}",
413 22, 2));
414 EXPECT_EQ(" {\n"
415 " a; //\n"
416 " }",
417 format(" {\n"
418 "a; //\n"
419 " }",
420 4, 2));
421 EXPECT_EQ("void f() {}\n"
422 "void g() {}",
423 format("void f() {}\n"
424 "void g() {}",
425 13, 0));
426 EXPECT_EQ("int a; // comment\n"
427 " // line 2\n"
428 "int b;",
429 format("int a; // comment\n"
430 " // line 2\n"
431 " int b;",
432 35, 0));
433
434 EXPECT_EQ(" void f() {\n"
435 "#define A 1\n"
436 " }",
437 format(" void f() {\n"
438 " #define A 1\n" // Format this line.
439 " }",
440 20, 0));
441 EXPECT_EQ(" void f() {\n"
442 " int i;\n"
443 "#define A \\\n"
444 " int i; \\\n"
445 " int j;\n"
446 " int k;\n"
447 " }",
448 format(" void f() {\n"
449 " int i;\n"
450 "#define A \\\n"
451 " int i; \\\n"
452 " int j;\n"
453 " int k;\n" // Format this line.
454 " }",
455 67, 0));
456
457 Style.ColumnLimit = 11;
458 EXPECT_EQ(" int a;\n"
459 " void\n"
460 " ffffff() {\n"
461 " }",
462 format(" int a;\n"
463 "void ffffff() {}",
464 11, 0));
465}
466
467TEST_F(FormatTestSelective, UnderstandsTabs) {
468 Style.IndentWidth = 8;
469 Style.UseTab = FormatStyle::UT_Always;
Daniel Jasper7fdbb3f2017-05-08 15:08:00 +0000470 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000471 EXPECT_EQ("void f() {\n"
472 "\tf();\n"
473 "\tg();\n"
474 "}",
475 format("void f() {\n"
476 "\tf();\n"
477 "\tg();\n"
478 "}",
479 0, 0));
480 EXPECT_EQ("void f() {\n"
481 "\tf();\n"
482 "\tg();\n"
483 "}",
484 format("void f() {\n"
485 "\tf();\n"
486 "\tg();\n"
487 "}",
488 16, 0));
489 EXPECT_EQ("void f() {\n"
490 " \tf();\n"
491 "\tg();\n"
492 "}",
493 format("void f() {\n"
494 " \tf();\n"
495 " \tg();\n"
496 "}",
497 21, 0));
498}
499
Daniel Jasperf67c3242015-11-01 00:27:35 +0000500TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) {
501 EXPECT_EQ(
502 "void f() {\n"
503 " if (a) {\n"
504 " g();\n"
505 " h();\n"
506 "}\n"
507 "\n"
508 "void g() {\n"
509 "}",
510 format("void f() {\n"
511 " if (a) {\n" // Assume this was added without the closing brace.
512 " g();\n"
513 " h();\n"
514 "}\n"
515 "\n"
516 "void g() {\n" // Make sure not to format this.
517 "}",
518 15, 0));
519}
520
Daniel Jasper97439922016-03-17 13:03:41 +0000521TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) {
522 Style = getGoogleStyle(FormatStyle::LK_JavaScript);
523 EXPECT_EQ(
524 "var x = \"a\";\n"
525 "var x = 'a';\n"
526 "var x = \"a\";",
527 format("var x = \"a\";\n"
528 "var x = \"a\";\n"
529 "var x = \"a\";",
530 20, 0));
531}
532
Krasimir Georgieva1c30932017-05-19 10:34:57 +0000533TEST_F(FormatTestSelective, KeepsIndentAfterCommentSectionImport) {
534 std::string Code = "#include <a> // line 1\n" // 23 chars long
535 " // line 2\n" // 23 chars long
536 "\n" // this newline is char 47
537 "int i;"; // this line is not indented
538 EXPECT_EQ(Code, format(Code, 47, 1));
539}
540
Daniel Jasperd246a5a2015-06-15 15:25:11 +0000541} // end namespace
542} // end namespace format
543} // end namespace clang