From: Daniel Jasper Date: Mon, 15 Jun 2015 15:25:11 +0000 (+0000) Subject: clang-format: NFC. Move testing of selective formatting to a separate file. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5349c83a3b8eb63ba3cf9295b1c39d4af90cec48;p=clang clang-format: NFC. Move testing of selective formatting to a separate file. This is a first step for splitting the huge FormatTest.cpp into separate files to make it easier to find specific tests. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@239730 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/unittests/Format/CMakeLists.txt b/unittests/Format/CMakeLists.txt index 4a7ab79418..6d48cf8713 100644 --- a/unittests/Format/CMakeLists.txt +++ b/unittests/Format/CMakeLists.txt @@ -7,6 +7,7 @@ add_clang_unittest(FormatTests FormatTestJava.cpp FormatTestJS.cpp FormatTestProto.cpp + FormatTestSelective.cpp ) target_link_libraries(FormatTests diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index f561475014..acb120962e 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -16,6 +16,7 @@ namespace clang { namespace format { +namespace { FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } @@ -27,12 +28,12 @@ protected: IC_DoNotCheck }; - std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, - const FormatStyle &Style, + std::string format(llvm::StringRef Code, + const FormatStyle &Style = getLLVMStyle(), IncompleteCheck CheckIncomplete = IC_ExpectComplete) { DEBUG(llvm::errs() << "---\n"); DEBUG(llvm::errs() << Code << "\n\n"); - std::vector Ranges(1, tooling::Range(Offset, Length)); + std::vector Ranges(1, tooling::Range(0, Code.size())); bool IncompleteFormat = false; tooling::Replacements Replaces = reformat(Style, Code, Ranges, "", &IncompleteFormat); @@ -47,12 +48,6 @@ protected: return Result; } - std::string format(llvm::StringRef Code, - const FormatStyle &Style = getLLVMStyle(), - IncompleteCheck CheckIncomplete = IC_ExpectComplete) { - return format(Code, 0, Code.size(), Style, CheckIncomplete); - } - FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { FormatStyle Style = getLLVMStyle(); Style.ColumnLimit = ColumnLimit; @@ -153,54 +148,6 @@ TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { EXPECT_EQ(0, ReplacementCount); } -TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) { - EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle())); - EXPECT_EQ("int a;", format("int a; ")); - EXPECT_EQ("int a;\n", format("int a; \n \n \n ")); - EXPECT_EQ("int a;\nint b; ", - format("int a; \nint b; ", 0, 0, getLLVMStyle())); -} - -TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) { - EXPECT_EQ("int b;\nint a;", - format("int b;\n int a;", 7, 0, getLLVMStyle())); - EXPECT_EQ("int b;\n int a;", - format("int b;\n int a;", 6, 0, getLLVMStyle())); - - EXPECT_EQ("#define A \\\n" - " int a; \\\n" - " int b;", - format("#define A \\\n" - " int a; \\\n" - " int b;", - 26, 0, getLLVMStyleWithColumns(12))); - EXPECT_EQ("#define A \\\n" - " int a; \\\n" - " int b;", - format("#define A \\\n" - " int a; \\\n" - " int b;", - 25, 0, getLLVMStyleWithColumns(12))); -} - -TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) { - EXPECT_EQ("int b;\n\nint a;", - format("int b;\n\nint a;", 8, 0, getLLVMStyle())); - EXPECT_EQ("int b;\n\nint a;", - format("int b;\n\nint a;", 7, 0, getLLVMStyle())); - - // This might not strictly be correct, but is likely good in all practical - // cases. - EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0, getLLVMStyle())); -} - -TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) { - EXPECT_EQ("int a;\n\n int b;", - format("int a;\n \n\n int b;", 8, 0, getLLVMStyle())); - EXPECT_EQ("int a;\n\n int b;", - format("int a;\n \n\n int b;", 9, 0, getLLVMStyle())); -} - TEST_F(FormatTest, RemovesEmptyLines) { EXPECT_EQ("class C {\n" " int i;\n" @@ -316,19 +263,6 @@ TEST_F(FormatTest, RemovesEmptyLines) { "} // namespace")); } -TEST_F(FormatTest, ReformatsMovedLines) { - EXPECT_EQ( - "template T *getFETokenInfo() const {\n" - " return static_cast(FETokenInfo);\n" - "}\n" - " int a; // <- Should not be formatted", - format( - "template\n" - "T *getFETokenInfo() const { return static_cast(FETokenInfo); }\n" - " int a; // <- Should not be formatted", - 9, 5, getLLVMStyle())); -} - TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { verifyFormat("x = (a) and (b);"); verifyFormat("x = (a) or (b);"); @@ -377,10 +311,6 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatement) { "}", AllowsMergedIf); - EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf)); - EXPECT_EQ("if (a) return; // comment", - format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf)); - AllowsMergedIf.ColumnLimit = 14; verifyFormat("if (a) return;", AllowsMergedIf); verifyFormat("if (aaaaaaaaa)\n" @@ -1133,65 +1063,6 @@ TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) { " c); // comment")); } -TEST_F(FormatTest, CanFormatCommentsLocally) { - EXPECT_EQ("int a; // comment\n" - "int b; // comment", - format("int a; // comment\n" - "int b; // comment", - 0, 0, getLLVMStyle())); - EXPECT_EQ("int a; // comment\n" - " // line 2\n" - "int b;", - format("int a; // comment\n" - " // line 2\n" - "int b;", - 28, 0, getLLVMStyle())); - EXPECT_EQ("int aaaaaa; // comment\n" - "int b;\n" - "int c; // unrelated comment", - format("int aaaaaa; // comment\n" - "int b;\n" - "int c; // unrelated comment", - 31, 0, getLLVMStyle())); - - EXPECT_EQ("int a; // This\n" - " // is\n" - " // a", - format("int a; // This\n" - " // is\n" - " // a", - 0, 0, getLLVMStyle())); - EXPECT_EQ("int a; // This\n" - " // is\n" - " // a\n" - "// This is b\n" - "int b;", - format("int a; // This\n" - " // is\n" - " // a\n" - "// This is b\n" - "int b;", - 0, 0, getLLVMStyle())); - EXPECT_EQ("int a; // This\n" - " // is\n" - " // a\n" - "\n" - " // This is unrelated", - format("int a; // This\n" - " // is\n" - " // a\n" - "\n" - " // This is unrelated", - 0, 0, getLLVMStyle())); - EXPECT_EQ("int a;\n" - "// This is\n" - "// not formatted. ", - format("int a;\n" - "// This is\n" - "// not formatted. ", - 0, 0, getLLVMStyle())); -} - TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) { EXPECT_EQ("// comment", format("// comment ")); EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment", @@ -2711,31 +2582,6 @@ TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); } -TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) { - EXPECT_EQ("int i;\n" - "#define A \\\n" - " int i; \\\n" - " int j\n" - "int k;", - format("int i;\n" - "#define A \\\n" - " int i ; \\\n" - " int j\n" - "int k;", - 8, 0, getGoogleStyle())); // 8: position of "#define". - EXPECT_EQ("int i;\n" - "#define A \\\n" - " int i; \\\n" - " int j\n" - "int k;", - format("int i;\n" - "#define A \\\n" - " int i ; \\\n" - " int j\n" - "int k;", - 45, 0, getGoogleStyle())); // 45: position of "j". -} - TEST_F(FormatTest, MacroDefinitionInsideStatement) { EXPECT_EQ("int x,\n" "#define A\n" @@ -3217,30 +3063,6 @@ TEST_F(FormatTest, LayoutBlockInsideParens) { " if (a)\n" " f();\n" "});"); - EXPECT_EQ("int longlongname; // comment\n" - "int x = f({\n" - " int x; // comment\n" - " int y; // comment\n" - "});", - format("int longlongname; // comment\n" - "int x = f({\n" - " int x; // comment\n" - " int y; // comment\n" - "});", - 65, 0, getLLVMStyle())); - EXPECT_EQ("int s = f({\n" - " class X {\n" - " public:\n" - " void f();\n" - " };\n" - "});", - format("int s = f({\n" - " class X {\n" - " public:\n" - " void f();\n" - " };\n" - "});", - 0, 0, getLLVMStyle())); } TEST_F(FormatTest, LayoutBlockInsideStatement) { @@ -3342,90 +3164,6 @@ TEST_F(FormatTest, FormatNestedBlocksInMacros) { getGoogleStyle())); } -TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) { - EXPECT_EQ("DEBUG({\n" - " int i;\n" - " int j;\n" - "});", - format("DEBUG( {\n" - " int i;\n" - " int j;\n" - "} ) ;", - 20, 1, getLLVMStyle())); - EXPECT_EQ("DEBUG( {\n" - " int i;\n" - " int j;\n" - "} ) ;", - format("DEBUG( {\n" - " int i;\n" - " int j;\n" - "} ) ;", - 41, 1, getLLVMStyle())); - EXPECT_EQ("DEBUG( {\n" - " int i;\n" - " int j;\n" - "} ) ;", - format("DEBUG( {\n" - " int i;\n" - " int j;\n" - "} ) ;", - 41, 1, getLLVMStyle())); - EXPECT_EQ("DEBUG({\n" - " int i;\n" - " int j;\n" - "});", - format("DEBUG( {\n" - " int i;\n" - " int j;\n" - "} ) ;", - 20, 1, getLLVMStyle())); - - EXPECT_EQ("Debug({\n" - " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" - " return;\n" - " },\n" - " a);", - format("Debug({\n" - " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" - " return;\n" - " },\n" - " a);", - 50, 1, getLLVMStyle())); - EXPECT_EQ("DEBUG({\n" - " DEBUG({\n" - " int a;\n" - " int b;\n" - " }) ;\n" - "});", - format("DEBUG({\n" - " DEBUG({\n" - " int a;\n" - " int b;\n" // Format this line only. - " }) ;\n" // Don't touch this line. - "});", - 35, 0, getLLVMStyle())); - EXPECT_EQ("DEBUG({\n" - " int a; //\n" - "});", - format("DEBUG({\n" - " int a; //\n" - "});", - 0, 0, getLLVMStyle())); - EXPECT_EQ("someFunction(\n" - " [] {\n" - " // Only with this comment.\n" - " int i; // invoke formatting here.\n" - " }, // force line break\n" - " aaa);", - format("someFunction(\n" - " [] {\n" - " // Only with this comment.\n" - " int i; // invoke formatting here.\n" - " }, // force line break\n" - " aaa);", - 63, 1, getLLVMStyle())); -} - TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { EXPECT_EQ("{}", format("{}")); verifyFormat("enum E {};"); @@ -7626,114 +7364,6 @@ TEST_F(FormatTest, ObjCArrayLiterals) { "]];"); } -TEST_F(FormatTest, ReformatRegionAdjustsIndent) { - EXPECT_EQ("{\n" - "{\n" - "a;\n" - "b;\n" - "}\n" - "}", - format("{\n" - "{\n" - "a;\n" - " b;\n" - "}\n" - "}", - 13, 2, getLLVMStyle())); - EXPECT_EQ("{\n" - "{\n" - " a;\n" - "b;\n" - "}\n" - "}", - format("{\n" - "{\n" - " a;\n" - "b;\n" - "}\n" - "}", - 9, 2, getLLVMStyle())); - EXPECT_EQ("{\n" - "{\n" - "public:\n" - " b;\n" - "}\n" - "}", - format("{\n" - "{\n" - "public:\n" - " b;\n" - "}\n" - "}", - 17, 2, getLLVMStyle())); - EXPECT_EQ("{\n" - "{\n" - "a;\n" - "}\n" - "{\n" - " b; //\n" - "}\n" - "}", - format("{\n" - "{\n" - "a;\n" - "}\n" - "{\n" - " b; //\n" - "}\n" - "}", - 22, 2, getLLVMStyle())); - EXPECT_EQ(" {\n" - " a; //\n" - " }", - format(" {\n" - "a; //\n" - " }", - 4, 2, getLLVMStyle())); - EXPECT_EQ("void f() {}\n" - "void g() {}", - format("void f() {}\n" - "void g() {}", - 13, 0, getLLVMStyle())); - EXPECT_EQ("int a; // comment\n" - " // line 2\n" - "int b;", - format("int a; // comment\n" - " // line 2\n" - " int b;", - 35, 0, getLLVMStyle())); - EXPECT_EQ(" int a;\n" - " void\n" - " ffffff() {\n" - " }", - format(" int a;\n" - "void ffffff() {}", - 11, 0, getLLVMStyleWithColumns(11))); - - EXPECT_EQ(" void f() {\n" - "#define A 1\n" - " }", - format(" void f() {\n" - " #define A 1\n" // Format this line. - " }", - 20, 0, getLLVMStyle())); - EXPECT_EQ(" void f() {\n" - " int i;\n" - "#define A \\\n" - " int i; \\\n" - " int j;\n" - " int k;\n" - " }", - format(" void f() {\n" - " int i;\n" - "#define A \\\n" - " int i; \\\n" - " int j;\n" - " int k;\n" // Format this line. - " }", - 67, 0, getLLVMStyle())); -} - TEST_F(FormatTest, BreaksStringLiterals) { EXPECT_EQ("\"some text \"\n" "\"other\";", @@ -8219,33 +7849,6 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { "\t\t parameter2); \\\n" "\t}", Tab); - EXPECT_EQ("void f() {\n" - "\tf();\n" - "\tg();\n" - "}", - format("void f() {\n" - "\tf();\n" - "\tg();\n" - "}", - 0, 0, Tab)); - EXPECT_EQ("void f() {\n" - "\tf();\n" - "\tg();\n" - "}", - format("void f() {\n" - "\tf();\n" - "\tg();\n" - "}", - 16, 0, Tab)); - EXPECT_EQ("void f() {\n" - " \tf();\n" - "\tg();\n" - "}", - format("void f() {\n" - " \tf();\n" - " \tg();\n" - "}", - 21, 0, Tab)); Tab.TabWidth = 4; Tab.IndentWidth = 8; @@ -10583,5 +10186,6 @@ TEST_F(FormatTest, DoNotCrashOnInvalidInput) { verifyNoCrash("#define a\\\n /**/}"); } -} // end namespace tooling +} // end namespace +} // end namespace format } // end namespace clang diff --git a/unittests/Format/FormatTestSelective.cpp b/unittests/Format/FormatTestSelective.cpp new file mode 100644 index 0000000000..8d2cb5a2fc --- /dev/null +++ b/unittests/Format/FormatTestSelective.cpp @@ -0,0 +1,441 @@ +//===- unittest/Format/FormatTestSelective.cpp - Formatting unit tests ----===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "FormatTestUtils.h" +#include "clang/Format/Format.h" +#include "llvm/Support/Debug.h" +#include "gtest/gtest.h" + +#define DEBUG_TYPE "format-test" + +namespace clang { +namespace format { +namespace { + +class FormatTestSelective : public ::testing::Test { +protected: + std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length) { + DEBUG(llvm::errs() << "---\n"); + DEBUG(llvm::errs() << Code << "\n\n"); + std::vector Ranges(1, tooling::Range(Offset, Length)); + bool IncompleteFormat = false; + tooling::Replacements Replaces = + reformat(Style, Code, Ranges, "", &IncompleteFormat); + EXPECT_FALSE(IncompleteFormat) << Code << "\n\n"; + std::string Result = applyAllReplacements(Code, Replaces); + EXPECT_NE("", Result); + DEBUG(llvm::errs() << "\n" << Result << "\n\n"); + return Result; + } + + FormatStyle Style = getLLVMStyle(); +}; + +TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) { + EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0)); + EXPECT_EQ("int a;", format("int a; ", 0, 0)); + EXPECT_EQ("int a;\n", format("int a; \n \n \n ", 0, 0)); + EXPECT_EQ("int a;\nint b; ", format("int a; \nint b; ", 0, 0)); +} + +TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) { + EXPECT_EQ("int b;\nint a;", format("int b;\n int a;", 7, 0)); + EXPECT_EQ("int b;\n int a;", format("int b;\n int a;", 6, 0)); + + Style.ColumnLimit = 12; + EXPECT_EQ("#define A \\\n" + " int a; \\\n" + " int b;", + format("#define A \\\n" + " int a; \\\n" + " int b;", + 26, 0)); + EXPECT_EQ("#define A \\\n" + " int a; \\\n" + " int b;", + format("#define A \\\n" + " int a; \\\n" + " int b;", + 25, 0)); +} + +TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) { + EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0)); + EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0)); + + // This might not strictly be correct, but is likely good in all practical + // cases. + EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0)); +} + +TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) { + EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0)); + EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0)); +} + +TEST_F(FormatTestSelective, ReformatsMovedLines) { + EXPECT_EQ( + "template T *getFETokenInfo() const {\n" + " return static_cast(FETokenInfo);\n" + "}\n" + " int a; // <- Should not be formatted", + format( + "template\n" + "T *getFETokenInfo() const { return static_cast(FETokenInfo); }\n" + " int a; // <- Should not be formatted", + 9, 5)); +} + +TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) { + Style.AllowShortIfStatementsOnASingleLine = true; + EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1)); + EXPECT_EQ("if (a) return; // comment", + format("if(a)\nreturn; // comment", 20, 1)); +} + +TEST_F(FormatTestSelective, FormatsCommentsLocally) { + EXPECT_EQ("int a; // comment\n" + "int b; // comment", + format("int a; // comment\n" + "int b; // comment", + 0, 0)); + EXPECT_EQ("int a; // comment\n" + " // line 2\n" + "int b;", + format("int a; // comment\n" + " // line 2\n" + "int b;", + 28, 0)); + EXPECT_EQ("int aaaaaa; // comment\n" + "int b;\n" + "int c; // unrelated comment", + format("int aaaaaa; // comment\n" + "int b;\n" + "int c; // unrelated comment", + 31, 0)); + + EXPECT_EQ("int a; // This\n" + " // is\n" + " // a", + format("int a; // This\n" + " // is\n" + " // a", + 0, 0)); + EXPECT_EQ("int a; // This\n" + " // is\n" + " // a\n" + "// This is b\n" + "int b;", + format("int a; // This\n" + " // is\n" + " // a\n" + "// This is b\n" + "int b;", + 0, 0)); + EXPECT_EQ("int a; // This\n" + " // is\n" + " // a\n" + "\n" + " // This is unrelated", + format("int a; // This\n" + " // is\n" + " // a\n" + "\n" + " // This is unrelated", + 0, 0)); + EXPECT_EQ("int a;\n" + "// This is\n" + "// not formatted. ", + format("int a;\n" + "// This is\n" + "// not formatted. ", + 0, 0)); +} + +TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) { + EXPECT_EQ("DEBUG({\n" + " int i;\n" + " int j;\n" + "});", + format("DEBUG( {\n" + " int i;\n" + " int j;\n" + "} ) ;", + 20, 1)); + EXPECT_EQ("DEBUG( {\n" + " int i;\n" + " int j;\n" + "} ) ;", + format("DEBUG( {\n" + " int i;\n" + " int j;\n" + "} ) ;", + 41, 1)); + EXPECT_EQ("DEBUG( {\n" + " int i;\n" + " int j;\n" + "} ) ;", + format("DEBUG( {\n" + " int i;\n" + " int j;\n" + "} ) ;", + 41, 1)); + EXPECT_EQ("DEBUG({\n" + " int i;\n" + " int j;\n" + "});", + format("DEBUG( {\n" + " int i;\n" + " int j;\n" + "} ) ;", + 20, 1)); + + EXPECT_EQ("Debug({\n" + " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" + " return;\n" + " },\n" + " a);", + format("Debug({\n" + " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n" + " return;\n" + " },\n" + " a);", + 50, 1)); + EXPECT_EQ("DEBUG({\n" + " DEBUG({\n" + " int a;\n" + " int b;\n" + " }) ;\n" + "});", + format("DEBUG({\n" + " DEBUG({\n" + " int a;\n" + " int b;\n" // Format this line only. + " }) ;\n" // Don't touch this line. + "});", + 35, 0)); + EXPECT_EQ("DEBUG({\n" + " int a; //\n" + "});", + format("DEBUG({\n" + " int a; //\n" + "});", + 0, 0)); + EXPECT_EQ("someFunction(\n" + " [] {\n" + " // Only with this comment.\n" + " int i; // invoke formatting here.\n" + " }, // force line break\n" + " aaa);", + format("someFunction(\n" + " [] {\n" + " // Only with this comment.\n" + " int i; // invoke formatting here.\n" + " }, // force line break\n" + " aaa);", + 63, 1)); + + EXPECT_EQ("int longlongname; // comment\n" + "int x = f({\n" + " int x; // comment\n" + " int y; // comment\n" + "});", + format("int longlongname; // comment\n" + "int x = f({\n" + " int x; // comment\n" + " int y; // comment\n" + "});", + 65, 0)); + EXPECT_EQ("int s = f({\n" + " class X {\n" + " public:\n" + " void f();\n" + " };\n" + "});", + format("int s = f({\n" + " class X {\n" + " public:\n" + " void f();\n" + " };\n" + "});", + 0, 0)); +} + +TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) { + Style.AlignEscapedNewlinesLeft = true; + EXPECT_EQ("int i;\n" + "#define A \\\n" + " int i; \\\n" + " int j\n" + "int k;", + format("int i;\n" + "#define A \\\n" + " int i ; \\\n" + " int j\n" + "int k;", + 8, 0)); // 8: position of "#define". + EXPECT_EQ("int i;\n" + "#define A \\\n" + " int i; \\\n" + " int j\n" + "int k;", + format("int i;\n" + "#define A \\\n" + " int i ; \\\n" + " int j\n" + "int k;", + 45, 0)); // 45: position of "j". +} + +TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) { + EXPECT_EQ("{\n" + "{\n" + "a;\n" + "b;\n" + "}\n" + "}", + format("{\n" + "{\n" + "a;\n" + " b;\n" + "}\n" + "}", + 13, 2)); + EXPECT_EQ("{\n" + "{\n" + " a;\n" + "b;\n" + "}\n" + "}", + format("{\n" + "{\n" + " a;\n" + "b;\n" + "}\n" + "}", + 9, 2)); + EXPECT_EQ("{\n" + "{\n" + "public:\n" + " b;\n" + "}\n" + "}", + format("{\n" + "{\n" + "public:\n" + " b;\n" + "}\n" + "}", + 17, 2)); + EXPECT_EQ("{\n" + "{\n" + "a;\n" + "}\n" + "{\n" + " b; //\n" + "}\n" + "}", + format("{\n" + "{\n" + "a;\n" + "}\n" + "{\n" + " b; //\n" + "}\n" + "}", + 22, 2)); + EXPECT_EQ(" {\n" + " a; //\n" + " }", + format(" {\n" + "a; //\n" + " }", + 4, 2)); + EXPECT_EQ("void f() {}\n" + "void g() {}", + format("void f() {}\n" + "void g() {}", + 13, 0)); + EXPECT_EQ("int a; // comment\n" + " // line 2\n" + "int b;", + format("int a; // comment\n" + " // line 2\n" + " int b;", + 35, 0)); + + EXPECT_EQ(" void f() {\n" + "#define A 1\n" + " }", + format(" void f() {\n" + " #define A 1\n" // Format this line. + " }", + 20, 0)); + EXPECT_EQ(" void f() {\n" + " int i;\n" + "#define A \\\n" + " int i; \\\n" + " int j;\n" + " int k;\n" + " }", + format(" void f() {\n" + " int i;\n" + "#define A \\\n" + " int i; \\\n" + " int j;\n" + " int k;\n" // Format this line. + " }", + 67, 0)); + + Style.ColumnLimit = 11; + EXPECT_EQ(" int a;\n" + " void\n" + " ffffff() {\n" + " }", + format(" int a;\n" + "void ffffff() {}", + 11, 0)); +} + +TEST_F(FormatTestSelective, UnderstandsTabs) { + Style.IndentWidth = 8; + Style.UseTab = FormatStyle::UT_Always; + Style.AlignEscapedNewlinesLeft = true; + EXPECT_EQ("void f() {\n" + "\tf();\n" + "\tg();\n" + "}", + format("void f() {\n" + "\tf();\n" + "\tg();\n" + "}", + 0, 0)); + EXPECT_EQ("void f() {\n" + "\tf();\n" + "\tg();\n" + "}", + format("void f() {\n" + "\tf();\n" + "\tg();\n" + "}", + 16, 0)); + EXPECT_EQ("void f() {\n" + " \tf();\n" + "\tg();\n" + "}", + format("void f() {\n" + " \tf();\n" + " \tg();\n" + "}", + 21, 0)); +} + +} // end namespace +} // end namespace format +} // end namespace clang