1 //===- unittest/Format/FormatTestSelective.cpp - Formatting unit tests ----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
15 #define DEBUG_TYPE "format-test"
21 class FormatTestSelective : public ::testing::Test {
23 std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length) {
24 LLVM_DEBUG(llvm::errs() << "---\n");
25 LLVM_DEBUG(llvm::errs() << Code << "\n\n");
26 std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
27 FormattingAttemptStatus Status;
28 tooling::Replacements Replaces =
29 reformat(Style, Code, Ranges, "<stdin>", &Status);
30 EXPECT_TRUE(Status.FormatComplete) << Code << "\n\n";
31 auto Result = applyAllReplacements(Code, Replaces);
32 EXPECT_TRUE(static_cast<bool>(Result));
33 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
37 FormatStyle Style = getLLVMStyle();
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));
47 TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
51 format("{int b;\n int a;}", 8, 0));
55 format("{int b;\n int a;}", 7, 0));
57 Style.ColumnLimit = 12;
58 EXPECT_EQ("#define A \\\n"
61 format("#define A \\\n"
65 EXPECT_EQ("#define A \\\n"
68 format("#define A \\\n"
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));
78 // This might not strictly be correct, but is likely good in all practical
80 EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0));
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));
88 TEST_F(FormatTestSelective, ReformatsMovedLines) {
90 "template <typename T> T *getFETokenInfo() const {\n"
91 " return static_cast<T *>(FETokenInfo);\n"
93 "int a; // <- Should not be formatted",
95 "template<typename T>\n"
96 "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
97 "int a; // <- Should not be formatted",
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));
108 TEST_F(FormatTestSelective, FormatsCommentsLocally) {
109 EXPECT_EQ("int a; // comment\n"
111 format("int a; // comment\n"
114 EXPECT_EQ("int a; // comment\n"
117 format("int a; // comment\n"
121 EXPECT_EQ("int a; // comment\n"
124 format("int a; // comment\n"
127 EXPECT_EQ("int aaaaaa; // comment\n"
129 "int c; // unrelated comment",
130 format("int aaaaaa; // comment\n"
132 "int c; // unrelated comment",
135 EXPECT_EQ("int a; // This\n"
138 format("int a; // This\n"
142 EXPECT_EQ("int a; // This\n"
147 format("int a; // This\n"
153 EXPECT_EQ("int a; // This\n"
157 "//This is unrelated",
158 format("int a; // This\n"
162 "//This is unrelated",
166 "// not formatted. ",
169 "// not formatted. ",
171 EXPECT_EQ("int x; // Format this line.\n"
174 format("int x; // Format this line.\n"
180 TEST_F(FormatTestSelective, ContinueReindenting) {
181 // When we change an indent, we continue formatting as long as following
182 // lines are not indented correctly.
198 TEST_F(FormatTestSelective, ReindentClosingBrace) {
212 EXPECT_EQ("void f() {\n"
220 format("void f() {\n"
229 EXPECT_EQ("int i = []() {\n"
236 format("int i = []() {\n"
246 TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
247 EXPECT_EQ("DEBUG({\n"
256 EXPECT_EQ("DEBUG( {\n"
265 EXPECT_EQ("DEBUG( {\n"
274 EXPECT_EQ("DEBUG({\n"
284 EXPECT_EQ("Debug({\n"
285 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
290 " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
295 EXPECT_EQ("DEBUG({\n"
304 " int b;\n" // Format this line only.
305 " }) ;\n" // Don't touch this line.
308 EXPECT_EQ("DEBUG({\n"
315 EXPECT_EQ("someFunction(\n"
317 " // Only with this comment.\n"
318 " int i; // invoke formatting here.\n"
319 " }, // force line break\n"
321 format("someFunction(\n"
323 " // Only with this comment.\n"
324 " int i; // invoke formatting here.\n"
325 " }, // force line break\n"
329 EXPECT_EQ("int longlongname; // comment\n"
331 " int x; // comment\n"
332 " int y; // comment\n"
334 format("int longlongname; // comment\n"
336 " int x; // comment\n"
337 " int y; // comment\n"
340 EXPECT_EQ("int s = f({\n"
346 format("int s = f({\n"
353 EXPECT_EQ("SomeFunction(\n"
356 " return i;\n" // Format this line.
359 " return 2;\n" // Don't fix this.
361 format("SomeFunction(\n"
364 " return i;\n" // Format this line.
367 " return 2;\n" // Don't fix this.
372 TEST_F(FormatTestSelective, WrongIndent) {
373 EXPECT_EQ("namespace {\n"
377 format("namespace {\n"
378 " int i;\n" // Format here.
382 EXPECT_EQ("namespace {\n"
386 format("namespace {\n"
388 " int j;\n" // Format here.
393 TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
394 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
405 8, 0)); // 8: position of "#define".
416 45, 0)); // 45: position of "j".
419 TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
487 EXPECT_EQ("void f() {}\n"
489 format("void f() {}\n"
492 EXPECT_EQ("int a; // comment\n"
495 format("int a; // comment\n"
500 EXPECT_EQ(" void f() {\n"
503 format(" void f() {\n"
504 " #define A 1\n" // Format this line.
507 EXPECT_EQ(" void f() {\n"
514 format(" void f() {\n"
519 " int k;\n" // Format this line.
523 Style.ColumnLimit = 11;
524 EXPECT_EQ(" int a;\n"
533 TEST_F(FormatTestSelective, UnderstandsTabs) {
534 Style.IndentWidth = 8;
535 Style.UseTab = FormatStyle::UT_Always;
536 Style.AlignEscapedNewlines = FormatStyle::ENAS_Left;
537 EXPECT_EQ("void f() {\n"
541 format("void f() {\n"
546 EXPECT_EQ("void f() {\n"
550 format("void f() {\n"
555 EXPECT_EQ("void f() {\n"
559 format("void f() {\n"
566 TEST_F(FormatTestSelective, StopFormattingWhenLeavingScope) {
576 format("void f() {\n"
577 " if (a) {\n" // Assume this was added without the closing brace.
582 "void g() {\n" // Make sure not to format this.
587 TEST_F(FormatTestSelective, SelectivelyRequoteJavaScript) {
588 Style = getGoogleStyle(FormatStyle::LK_JavaScript);
593 format("var x = \"a\";\n"
599 TEST_F(FormatTestSelective, KeepsIndentAfterCommentSectionImport) {
600 std::string Code = "#include <a> // line 1\n" // 23 chars long
601 " // line 2\n" // 23 chars long
602 "\n" // this newline is char 47
603 "int i;"; // this line is not indented
604 EXPECT_EQ(Code, format(Code, 47, 1));
608 } // end namespace format
609 } // end namespace clang