blob: 8d2cb5a2fcbd9c530be338b535d3b1f0e34d56cf [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));
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
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) {
48 EXPECT_EQ("int b;\nint a;", format("int b;\n int a;", 7, 0));
49 EXPECT_EQ("int b;\n int a;", format("int b;\n int a;", 6, 0));
50
51 Style.ColumnLimit = 12;
52 EXPECT_EQ("#define A \\\n"
53 " int a; \\\n"
54 " int b;",
55 format("#define A \\\n"
56 " int a; \\\n"
57 " int b;",
58 26, 0));
59 EXPECT_EQ("#define A \\\n"
60 " int a; \\\n"
61 " int b;",
62 format("#define A \\\n"
63 " int a; \\\n"
64 " int b;",
65 25, 0));
66}
67
68TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) {
69 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0));
70 EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0));
71
72 // This might not strictly be correct, but is likely good in all practical
73 // cases.
74 EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0));
75}
76
77TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
78 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0));
79 EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0));
80}
81
82TEST_F(FormatTestSelective, ReformatsMovedLines) {
83 EXPECT_EQ(
84 "template <typename T> T *getFETokenInfo() const {\n"
85 " return static_cast<T *>(FETokenInfo);\n"
86 "}\n"
87 " int a; // <- Should not be formatted",
88 format(
89 "template<typename T>\n"
90 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
91 " int a; // <- Should not be formatted",
92 9, 5));
93}
94
95TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) {
96 Style.AllowShortIfStatementsOnASingleLine = true;
97 EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1));
98 EXPECT_EQ("if (a) return; // comment",
99 format("if(a)\nreturn; // comment", 20, 1));
100}
101
102TEST_F(FormatTestSelective, FormatsCommentsLocally) {
103 EXPECT_EQ("int a; // comment\n"
104 "int b; // comment",
105 format("int a; // comment\n"
106 "int b; // comment",
107 0, 0));
108 EXPECT_EQ("int a; // comment\n"
109 " // line 2\n"
110 "int b;",
111 format("int a; // comment\n"
112 " // line 2\n"
113 "int b;",
114 28, 0));
115 EXPECT_EQ("int aaaaaa; // comment\n"
116 "int b;\n"
117 "int c; // unrelated comment",
118 format("int aaaaaa; // comment\n"
119 "int b;\n"
120 "int c; // unrelated comment",
121 31, 0));
122
123 EXPECT_EQ("int a; // This\n"
124 " // is\n"
125 " // a",
126 format("int a; // This\n"
127 " // is\n"
128 " // a",
129 0, 0));
130 EXPECT_EQ("int a; // This\n"
131 " // is\n"
132 " // a\n"
133 "// This is b\n"
134 "int b;",
135 format("int a; // This\n"
136 " // is\n"
137 " // a\n"
138 "// This is b\n"
139 "int b;",
140 0, 0));
141 EXPECT_EQ("int a; // This\n"
142 " // is\n"
143 " // a\n"
144 "\n"
145 " // This is unrelated",
146 format("int a; // This\n"
147 " // is\n"
148 " // a\n"
149 "\n"
150 " // This is unrelated",
151 0, 0));
152 EXPECT_EQ("int a;\n"
153 "// This is\n"
154 "// not formatted. ",
155 format("int a;\n"
156 "// This is\n"
157 "// not formatted. ",
158 0, 0));
159}
160
161TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
162 EXPECT_EQ("DEBUG({\n"
163 " int i;\n"
164 " int j;\n"
165 "});",
166 format("DEBUG( {\n"
167 " int i;\n"
168 " int j;\n"
169 "} ) ;",
170 20, 1));
171 EXPECT_EQ("DEBUG( {\n"
172 " int i;\n"
173 " int j;\n"
174 "} ) ;",
175 format("DEBUG( {\n"
176 " int i;\n"
177 " int j;\n"
178 "} ) ;",
179 41, 1));
180 EXPECT_EQ("DEBUG( {\n"
181 " int i;\n"
182 " int j;\n"
183 "} ) ;",
184 format("DEBUG( {\n"
185 " int i;\n"
186 " int j;\n"
187 "} ) ;",
188 41, 1));
189 EXPECT_EQ("DEBUG({\n"
190 " int i;\n"
191 " int j;\n"
192 "});",
193 format("DEBUG( {\n"
194 " int i;\n"
195 " int j;\n"
196 "} ) ;",
197 20, 1));
198
199 EXPECT_EQ("Debug({\n"
200 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
201 " return;\n"
202 " },\n"
203 " a);",
204 format("Debug({\n"
205 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
206 " return;\n"
207 " },\n"
208 " a);",
209 50, 1));
210 EXPECT_EQ("DEBUG({\n"
211 " DEBUG({\n"
212 " int a;\n"
213 " int b;\n"
214 " }) ;\n"
215 "});",
216 format("DEBUG({\n"
217 " DEBUG({\n"
218 " int a;\n"
219 " int b;\n" // Format this line only.
220 " }) ;\n" // Don't touch this line.
221 "});",
222 35, 0));
223 EXPECT_EQ("DEBUG({\n"
224 " int a; //\n"
225 "});",
226 format("DEBUG({\n"
227 " int a; //\n"
228 "});",
229 0, 0));
230 EXPECT_EQ("someFunction(\n"
231 " [] {\n"
232 " // Only with this comment.\n"
233 " int i; // invoke formatting here.\n"
234 " }, // force line break\n"
235 " aaa);",
236 format("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 63, 1));
243
244 EXPECT_EQ("int longlongname; // comment\n"
245 "int x = f({\n"
246 " int x; // comment\n"
247 " int y; // comment\n"
248 "});",
249 format("int longlongname; // comment\n"
250 "int x = f({\n"
251 " int x; // comment\n"
252 " int y; // comment\n"
253 "});",
254 65, 0));
255 EXPECT_EQ("int s = f({\n"
256 " class X {\n"
257 " public:\n"
258 " void f();\n"
259 " };\n"
260 "});",
261 format("int s = f({\n"
262 " class X {\n"
263 " public:\n"
264 " void f();\n"
265 " };\n"
266 "});",
267 0, 0));
268}
269
270TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
271 Style.AlignEscapedNewlinesLeft = true;
272 EXPECT_EQ("int i;\n"
273 "#define A \\\n"
274 " int i; \\\n"
275 " int j\n"
276 "int k;",
277 format("int i;\n"
278 "#define A \\\n"
279 " int i ; \\\n"
280 " int j\n"
281 "int k;",
282 8, 0)); // 8: position of "#define".
283 EXPECT_EQ("int i;\n"
284 "#define A \\\n"
285 " int i; \\\n"
286 " int j\n"
287 "int k;",
288 format("int i;\n"
289 "#define A \\\n"
290 " int i ; \\\n"
291 " int j\n"
292 "int k;",
293 45, 0)); // 45: position of "j".
294}
295
296TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
297 EXPECT_EQ("{\n"
298 "{\n"
299 "a;\n"
300 "b;\n"
301 "}\n"
302 "}",
303 format("{\n"
304 "{\n"
305 "a;\n"
306 " b;\n"
307 "}\n"
308 "}",
309 13, 2));
310 EXPECT_EQ("{\n"
311 "{\n"
312 " a;\n"
313 "b;\n"
314 "}\n"
315 "}",
316 format("{\n"
317 "{\n"
318 " a;\n"
319 "b;\n"
320 "}\n"
321 "}",
322 9, 2));
323 EXPECT_EQ("{\n"
324 "{\n"
325 "public:\n"
326 " b;\n"
327 "}\n"
328 "}",
329 format("{\n"
330 "{\n"
331 "public:\n"
332 " b;\n"
333 "}\n"
334 "}",
335 17, 2));
336 EXPECT_EQ("{\n"
337 "{\n"
338 "a;\n"
339 "}\n"
340 "{\n"
341 " b; //\n"
342 "}\n"
343 "}",
344 format("{\n"
345 "{\n"
346 "a;\n"
347 "}\n"
348 "{\n"
349 " b; //\n"
350 "}\n"
351 "}",
352 22, 2));
353 EXPECT_EQ(" {\n"
354 " a; //\n"
355 " }",
356 format(" {\n"
357 "a; //\n"
358 " }",
359 4, 2));
360 EXPECT_EQ("void f() {}\n"
361 "void g() {}",
362 format("void f() {}\n"
363 "void g() {}",
364 13, 0));
365 EXPECT_EQ("int a; // comment\n"
366 " // line 2\n"
367 "int b;",
368 format("int a; // comment\n"
369 " // line 2\n"
370 " int b;",
371 35, 0));
372
373 EXPECT_EQ(" void f() {\n"
374 "#define A 1\n"
375 " }",
376 format(" void f() {\n"
377 " #define A 1\n" // Format this line.
378 " }",
379 20, 0));
380 EXPECT_EQ(" void f() {\n"
381 " int i;\n"
382 "#define A \\\n"
383 " int i; \\\n"
384 " int j;\n"
385 " int k;\n"
386 " }",
387 format(" void f() {\n"
388 " int i;\n"
389 "#define A \\\n"
390 " int i; \\\n"
391 " int j;\n"
392 " int k;\n" // Format this line.
393 " }",
394 67, 0));
395
396 Style.ColumnLimit = 11;
397 EXPECT_EQ(" int a;\n"
398 " void\n"
399 " ffffff() {\n"
400 " }",
401 format(" int a;\n"
402 "void ffffff() {}",
403 11, 0));
404}
405
406TEST_F(FormatTestSelective, UnderstandsTabs) {
407 Style.IndentWidth = 8;
408 Style.UseTab = FormatStyle::UT_Always;
409 Style.AlignEscapedNewlinesLeft = true;
410 EXPECT_EQ("void f() {\n"
411 "\tf();\n"
412 "\tg();\n"
413 "}",
414 format("void f() {\n"
415 "\tf();\n"
416 "\tg();\n"
417 "}",
418 0, 0));
419 EXPECT_EQ("void f() {\n"
420 "\tf();\n"
421 "\tg();\n"
422 "}",
423 format("void f() {\n"
424 "\tf();\n"
425 "\tg();\n"
426 "}",
427 16, 0));
428 EXPECT_EQ("void f() {\n"
429 " \tf();\n"
430 "\tg();\n"
431 "}",
432 format("void f() {\n"
433 " \tf();\n"
434 " \tg();\n"
435 "}",
436 21, 0));
437}
438
439} // end namespace
440} // end namespace format
441} // end namespace clang