diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 1f91157f2cc46..ce22be44c255d 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -41,18 +41,18 @@ TEST_F(FormatTest, LLVMStyleOverride) { TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { verifyFormat(";"); } TEST_F(FormatTest, FormatsGlobalStatementsAt0) { - EXPECT_EQ("int i;", format(" int i;")); - EXPECT_EQ("\nint i;", format(" \n\t \v \f int i;")); - EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); - EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); + verifyFormat("int i;", " int i;"); + verifyFormat("\nint i;", " \n\t \v \f int i;"); + verifyFormat("int i;\nint j;", " int i; int j;"); + verifyFormat("int i;\nint j;", " int i;\n int j;"); } TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { - EXPECT_EQ("int i;", format("int\ni;")); + verifyFormat("int i;", "int\ni;"); } TEST_F(FormatTest, FormatsNestedBlockStatements) { - EXPECT_EQ("{\n {\n {}\n }\n}", format("{{{}}}")); + verifyFormat("{\n {\n {}\n }\n}", "{{{}}}"); } TEST_F(FormatTest, FormatsNestedCall) { @@ -74,332 +74,308 @@ TEST_F(FormatTest, NestedNameSpecifiers) { } TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) { - EXPECT_EQ("if (a) {\n" - " f();\n" - "}", - format("if(a){f();}")); + verifyFormat("if (a) {\n" + " f();\n" + "}", + "if(a){f();}"); EXPECT_EQ(4, ReplacementCount); - EXPECT_EQ("if (a) {\n" - " f();\n" - "}", - format("if (a) {\n" - " f();\n" - "}")); + verifyNoChange("if (a) {\n" + " f();\n" + "}"); EXPECT_EQ(0, ReplacementCount); - EXPECT_EQ("/*\r\n" - "\r\n" - "*/", - format("/*\r\n" - "\r\n" - "*/")); + verifyNoChange("/*\r\n" + "\r\n" + "*/"); EXPECT_EQ(0, ReplacementCount); } TEST_F(FormatTest, RemovesEmptyLines) { - EXPECT_EQ("class C {\n" - " int i;\n" - "};", - format("class C {\n" - " int i;\n" - "\n" - "};")); + verifyFormat("class C {\n" + " int i;\n" + "};", + "class C {\n" + " int i;\n" + "\n" + "};"); // Don't remove empty lines at the start of namespaces or extern "C" blocks. - EXPECT_EQ("namespace N {\n" - "\n" - "int i;\n" - "}", - format("namespace N {\n" - "\n" - "int i;\n" - "}", - getGoogleStyle())); - EXPECT_EQ("/* something */ namespace N {\n" - "\n" - "int i;\n" - "}", - format("/* something */ namespace N {\n" - "\n" - "int i;\n" - "}", - getGoogleStyle())); - EXPECT_EQ("inline namespace N {\n" - "\n" - "int i;\n" - "}", - format("inline namespace N {\n" - "\n" - "int i;\n" - "}", - getGoogleStyle())); - EXPECT_EQ("/* something */ inline namespace N {\n" - "\n" - "int i;\n" - "}", - format("/* something */ inline namespace N {\n" - "\n" - "int i;\n" - "}", - getGoogleStyle())); - EXPECT_EQ("export namespace N {\n" - "\n" - "int i;\n" - "}", - format("export namespace N {\n" - "\n" - "int i;\n" - "}", - getGoogleStyle())); - EXPECT_EQ("extern /**/ \"C\" /**/ {\n" - "\n" - "int i;\n" - "}", - format("extern /**/ \"C\" /**/ {\n" - "\n" - "int i;\n" - "}", - getGoogleStyle())); + verifyFormat("namespace N {\n" + "\n" + "int i;\n" + "}", + "namespace N {\n" + "\n" + "int i;\n" + "}", + getGoogleStyle()); + verifyFormat("/* something */ namespace N {\n" + "\n" + "int i;\n" + "}", + "/* something */ namespace N {\n" + "\n" + "int i;\n" + "}", + getGoogleStyle()); + verifyFormat("inline namespace N {\n" + "\n" + "int i;\n" + "}", + "inline namespace N {\n" + "\n" + "int i;\n" + "}", + getGoogleStyle()); + verifyFormat("/* something */ inline namespace N {\n" + "\n" + "int i;\n" + "}", + "/* something */ inline namespace N {\n" + "\n" + "int i;\n" + "}", + getGoogleStyle()); + verifyFormat("export namespace N {\n" + "\n" + "int i;\n" + "}", + "export namespace N {\n" + "\n" + "int i;\n" + "}", + getGoogleStyle()); + verifyFormat("extern /**/ \"C\" /**/ {\n" + "\n" + "int i;\n" + "}", + "extern /**/ \"C\" /**/ {\n" + "\n" + "int i;\n" + "}", + getGoogleStyle()); auto CustomStyle = getLLVMStyle(); CustomStyle.BreakBeforeBraces = FormatStyle::BS_Custom; CustomStyle.BraceWrapping.AfterNamespace = true; CustomStyle.KeepEmptyLinesAtTheStartOfBlocks = false; - EXPECT_EQ("namespace N\n" - "{\n" - "\n" - "int i;\n" - "}", - format("namespace N\n" - "{\n" - "\n" - "\n" - "int i;\n" - "}", - CustomStyle)); - EXPECT_EQ("/* something */ namespace N\n" - "{\n" - "\n" - "int i;\n" - "}", - format("/* something */ namespace N {\n" - "\n" - "\n" - "int i;\n" - "}", - CustomStyle)); - EXPECT_EQ("inline namespace N\n" - "{\n" - "\n" - "int i;\n" - "}", - format("inline namespace N\n" - "{\n" - "\n" - "\n" - "int i;\n" - "}", - CustomStyle)); - EXPECT_EQ("/* something */ inline namespace N\n" - "{\n" - "\n" - "int i;\n" - "}", - format("/* something */ inline namespace N\n" - "{\n" - "\n" - "int i;\n" - "}", - CustomStyle)); - EXPECT_EQ("export namespace N\n" - "{\n" - "\n" - "int i;\n" - "}", - format("export namespace N\n" - "{\n" - "\n" - "int i;\n" - "}", - CustomStyle)); - EXPECT_EQ("namespace a\n" - "{\n" - "namespace b\n" - "{\n" - "\n" - "class AA {};\n" - "\n" - "} // namespace b\n" - "} // namespace a", - format("namespace a\n" - "{\n" - "namespace b\n" - "{\n" - "\n" - "\n" - "class AA {};\n" - "\n" - "\n" - "}\n" - "}", - CustomStyle)); - EXPECT_EQ("namespace A /* comment */\n" - "{\n" - "class B {}\n" - "} // namespace A", - format("namespace A /* comment */ { class B {} }", CustomStyle)); - EXPECT_EQ("namespace A\n" - "{ /* comment */\n" - "class B {}\n" - "} // namespace A", - format("namespace A {/* comment */ class B {} }", CustomStyle)); - EXPECT_EQ("namespace A\n" - "{ /* comment */\n" - "\n" - "class B {}\n" - "\n" - "" - "} // namespace A", - format("namespace A { /* comment */\n" - "\n" - "\n" - "class B {}\n" - "\n" - "\n" - "}", - CustomStyle)); - EXPECT_EQ("namespace A /* comment */\n" - "{\n" - "\n" - "class B {}\n" - "\n" - "} // namespace A", - format("namespace A/* comment */ {\n" - "\n" - "\n" - "class B {}\n" - "\n" - "\n" - "}", - CustomStyle)); + verifyFormat("namespace N\n" + "{\n" + "\n" + "int i;\n" + "}", + "namespace N\n" + "{\n" + "\n" + "\n" + "int i;\n" + "}", + CustomStyle); + verifyFormat("/* something */ namespace N\n" + "{\n" + "\n" + "int i;\n" + "}", + "/* something */ namespace N {\n" + "\n" + "\n" + "int i;\n" + "}", + CustomStyle); + verifyFormat("inline namespace N\n" + "{\n" + "\n" + "int i;\n" + "}", + "inline namespace N\n" + "{\n" + "\n" + "\n" + "int i;\n" + "}", + CustomStyle); + verifyFormat("/* something */ inline namespace N\n" + "{\n" + "\n" + "int i;\n" + "}", + "/* something */ inline namespace N\n" + "{\n" + "\n" + "int i;\n" + "}", + CustomStyle); + verifyFormat("export namespace N\n" + "{\n" + "\n" + "int i;\n" + "}", + "export namespace N\n" + "{\n" + "\n" + "int i;\n" + "}", + CustomStyle); + verifyFormat("namespace a\n" + "{\n" + "namespace b\n" + "{\n" + "\n" + "class AA {};\n" + "\n" + "} // namespace b\n" + "} // namespace a", + "namespace a\n" + "{\n" + "namespace b\n" + "{\n" + "\n" + "\n" + "class AA {};\n" + "\n" + "\n" + "}\n" + "}", + CustomStyle); + verifyFormat("namespace A /* comment */\n" + "{\n" + "class B {}\n" + "} // namespace A", + "namespace A /* comment */ { class B {} }", CustomStyle); + verifyFormat("namespace A\n" + "{ /* comment */\n" + "class B {}\n" + "} // namespace A", + "namespace A {/* comment */ class B {} }", CustomStyle); + verifyFormat("namespace A\n" + "{ /* comment */\n" + "\n" + "class B {}\n" + "\n" + "" + "} // namespace A", + "namespace A { /* comment */\n" + "\n" + "\n" + "class B {}\n" + "\n" + "\n" + "}", + CustomStyle); + verifyFormat("namespace A /* comment */\n" + "{\n" + "\n" + "class B {}\n" + "\n" + "} // namespace A", + "namespace A/* comment */ {\n" + "\n" + "\n" + "class B {}\n" + "\n" + "\n" + "}", + CustomStyle); // ...but do keep inlining and removing empty lines for non-block extern "C" // functions. verifyGoogleFormat("extern \"C\" int f() { return 42; }"); - EXPECT_EQ("extern \"C\" int f() {\n" - " int i = 42;\n" - " return i;\n" - "}", - format("extern \"C\" int f() {\n" - "\n" - " int i = 42;\n" - " return i;\n" - "}", - getGoogleStyle())); + verifyFormat("extern \"C\" int f() {\n" + " int i = 42;\n" + " return i;\n" + "}", + "extern \"C\" int f() {\n" + "\n" + " int i = 42;\n" + " return i;\n" + "}", + getGoogleStyle()); // Remove empty lines at the beginning and end of blocks. - EXPECT_EQ("void f() {\n" - "\n" - " if (a) {\n" - "\n" - " f();\n" - " }\n" - "}", - format("void f() {\n" - "\n" - " if (a) {\n" - "\n" - " f();\n" - "\n" - " }\n" - "\n" - "}")); - EXPECT_EQ("void f() {\n" - " if (a) {\n" - " f();\n" - " }\n" - "}", - format("void f() {\n" - "\n" - " if (a) {\n" - "\n" - " f();\n" - "\n" - " }\n" - "\n" - "}", - getGoogleStyle())); + verifyFormat("void f() {\n" + "\n" + " if (a) {\n" + "\n" + " f();\n" + " }\n" + "}", + "void f() {\n" + "\n" + " if (a) {\n" + "\n" + " f();\n" + "\n" + " }\n" + "\n" + "}"); + verifyFormat("void f() {\n" + " if (a) {\n" + " f();\n" + " }\n" + "}", + "void f() {\n" + "\n" + " if (a) {\n" + "\n" + " f();\n" + "\n" + " }\n" + "\n" + "}", + getGoogleStyle()); // Don't remove empty lines in more complex control statements. - EXPECT_EQ("void f() {\n" - " if (a) {\n" - " f();\n" - "\n" - " } else if (b) {\n" - " f();\n" - " }\n" - "}", - format("void f() {\n" - " if (a) {\n" - " f();\n" - "\n" - " } else if (b) {\n" - " f();\n" - "\n" - " }\n" - "\n" - "}")); + verifyFormat("void f() {\n" + " if (a) {\n" + " f();\n" + "\n" + " } else if (b) {\n" + " f();\n" + " }\n" + "}", + "void f() {\n" + " if (a) {\n" + " f();\n" + "\n" + " } else if (b) {\n" + " f();\n" + "\n" + " }\n" + "\n" + "}"); // Don't remove empty lines before namespace endings. FormatStyle LLVMWithNoNamespaceFix = getLLVMStyle(); LLVMWithNoNamespaceFix.FixNamespaceComments = false; - EXPECT_EQ("namespace {\n" - "int i;\n" - "\n" - "}", - format("namespace {\n" - "int i;\n" - "\n" - "}", - LLVMWithNoNamespaceFix)); - EXPECT_EQ("namespace {\n" - "int i;\n" - "}", - format("namespace {\n" - "int i;\n" - "}", - LLVMWithNoNamespaceFix)); - EXPECT_EQ("namespace {\n" - "int i;\n" - "\n" - "};", - format("namespace {\n" - "int i;\n" - "\n" - "};", - LLVMWithNoNamespaceFix)); - EXPECT_EQ("namespace {\n" - "int i;\n" - "};", - format("namespace {\n" - "int i;\n" - "};", - LLVMWithNoNamespaceFix)); - EXPECT_EQ("namespace {\n" - "int i;\n" - "\n" - "}", - format("namespace {\n" - "int i;\n" - "\n" - "}")); - EXPECT_EQ("namespace {\n" - "int i;\n" - "\n" - "} // namespace", - format("namespace {\n" - "int i;\n" - "\n" - "} // namespace")); + verifyNoChange("namespace {\n" + "int i;\n" + "\n" + "}", + LLVMWithNoNamespaceFix); + verifyFormat("namespace {\n" + "int i;\n" + "}", + LLVMWithNoNamespaceFix); + verifyNoChange("namespace {\n" + "int i;\n" + "\n" + "};", + LLVMWithNoNamespaceFix); + verifyFormat("namespace {\n" + "int i;\n" + "};", + LLVMWithNoNamespaceFix); + verifyNoChange("namespace {\n" + "int i;\n" + "\n" + "}"); + verifyFormat("namespace {\n" + "int i;\n" + "\n" + "} // namespace", + "namespace {\n" + "int i;\n" + "\n" + "} // namespace"); FormatStyle Style = getLLVMStyle(); Style.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; @@ -409,21 +385,21 @@ TEST_F(FormatTest, RemovesEmptyLines) { Style.BraceWrapping.AfterFunction = true; Style.KeepEmptyLinesAtTheStartOfBlocks = false; - EXPECT_EQ("class Foo\n" - "{\n" - " Foo() {}\n" - "\n" - " void funk() {}\n" - "};", - format("class Foo\n" - "{\n" - " Foo()\n" - " {\n" - " }\n" - "\n" - " void funk() {}\n" - "};", - Style)); + verifyFormat("class Foo\n" + "{\n" + " Foo() {}\n" + "\n" + " void funk() {}\n" + "};", + "class Foo\n" + "{\n" + " Foo()\n" + " {\n" + " }\n" + "\n" + " void funk() {}\n" + "};", + Style); } TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { @@ -1827,10 +1803,8 @@ TEST_F(FormatTest, UnderstandsMacros) { verifyFormat("/* comment */ #define A (parentheses)"); verifyFormat("/* comment */ /* another comment */ #define A (parentheses)"); // Even the partial code should never be merged. - EXPECT_EQ("/* comment */ #define A (parentheses)\n" - "#", - format("/* comment */ #define A (parentheses)\n" - "#")); + verifyNoChange("/* comment */ #define A (parentheses)\n" + "#"); verifyFormat("/* comment */ #define A (parentheses)\n" "#\n"); verifyFormat("/* comment */ #define A (parentheses)\n" @@ -1882,18 +1856,18 @@ TEST_F(FormatTest, ShortBlocksInMacrosDontMergeWithCodeAfterMacro) { Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; Style.BreakBeforeBraces = FormatStyle::BS_Allman; - EXPECT_EQ("#define A \\\n" - " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" - " { \\\n" - " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" - " }\n" - "X;", - format("#define A \\\n" - " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" - " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" - " }\n" - "X;", - Style)); + verifyFormat("#define A \\\n" + " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" + " { \\\n" + " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" + " }\n" + "X;", + "#define A \\\n" + " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" + " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" + " }\n" + "X;", + Style); } TEST_F(FormatTest, ParseIfElse) { @@ -2562,45 +2536,34 @@ TEST_F(FormatTest, FormatsSwitchStatement) { " break;\n" " }\n" "});"); - EXPECT_EQ("DEBUG({\n" - " switch (x) {\n" - " case A:\n" - " f();\n" - " break;\n" - " // On B:\n" - " case B:\n" - " g();\n" - " break;\n" - " }\n" - "});", - format("DEBUG({\n" - " switch (x) {\n" - " case A:\n" - " f();\n" - " break;\n" - " // On B:\n" - " case B:\n" - " g();\n" - " break;\n" - " }\n" - "});")); - EXPECT_EQ("switch (n) {\n" - "case 0: {\n" - " return false;\n" - "}\n" - "default: {\n" - " return true;\n" - "}\n" - "}", - format("switch (n)\n" - "{\n" - "case 0: {\n" - " return false;\n" - "}\n" - "default: {\n" - " return true;\n" - "}\n" - "}")); + verifyNoChange("DEBUG({\n" + " switch (x) {\n" + " case A:\n" + " f();\n" + " break;\n" + " // On B:\n" + " case B:\n" + " g();\n" + " break;\n" + " }\n" + "});"); + verifyFormat("switch (n) {\n" + "case 0: {\n" + " return false;\n" + "}\n" + "default: {\n" + " return true;\n" + "}\n" + "}", + "switch (n)\n" + "{\n" + "case 0: {\n" + " return false;\n" + "}\n" + "default: {\n" + " return true;\n" + "}\n" + "}"); verifyFormat("switch (a) {\n" "case (b):\n" " return;\n" @@ -2665,101 +2628,101 @@ TEST_F(FormatTest, FormatsSwitchStatement) { Style.BreakBeforeBraces = FormatStyle::BS_Custom; Style.BraceWrapping.AfterCaseLabel = true; Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; - EXPECT_EQ("switch (n)\n" - "{\n" - " case 0:\n" - " {\n" - " return false;\n" - " }\n" - " default:\n" - " {\n" - " return true;\n" - " }\n" - "}", - format("switch (n) {\n" - " case 0: {\n" - " return false;\n" - " }\n" - " default: {\n" - " return true;\n" - " }\n" - "}", - Style)); + verifyFormat("switch (n)\n" + "{\n" + " case 0:\n" + " {\n" + " return false;\n" + " }\n" + " default:\n" + " {\n" + " return true;\n" + " }\n" + "}", + "switch (n) {\n" + " case 0: {\n" + " return false;\n" + " }\n" + " default: {\n" + " return true;\n" + " }\n" + "}", + Style); Style.BraceWrapping.AfterCaseLabel = false; - EXPECT_EQ("switch (n)\n" - "{\n" - " case 0: {\n" - " return false;\n" - " }\n" - " default: {\n" - " return true;\n" - " }\n" - "}", - format("switch (n) {\n" - " case 0:\n" - " {\n" - " return false;\n" - " }\n" - " default:\n" - " {\n" - " return true;\n" - " }\n" - "}", - Style)); - Style.IndentCaseLabels = false; - Style.IndentCaseBlocks = true; - EXPECT_EQ("switch (n)\n" - "{\n" - "case 0:\n" - " {\n" - " return false;\n" - " }\n" - "case 1:\n" - " break;\n" - "default:\n" - " {\n" - " return true;\n" - " }\n" - "}", - format("switch (n) {\n" - "case 0: {\n" - " return false;\n" - "}\n" - "case 1:\n" - " break;\n" - "default: {\n" - " return true;\n" - "}\n" - "}", - Style)); - Style.IndentCaseLabels = true; - Style.IndentCaseBlocks = true; - EXPECT_EQ("switch (n)\n" - "{\n" - " case 0:\n" - " {\n" - " return false;\n" - " }\n" - " case 1:\n" - " break;\n" - " default:\n" - " {\n" - " return true;\n" - " }\n" - "}", - format("switch (n) {\n" - "case 0: {\n" - " return false;\n" - "}\n" - "case 1:\n" - " break;\n" - "default: {\n" - " return true;\n" - "}\n" - "}", - Style)); -} - + verifyFormat("switch (n)\n" + "{\n" + " case 0: {\n" + " return false;\n" + " }\n" + " default: {\n" + " return true;\n" + " }\n" + "}", + "switch (n) {\n" + " case 0:\n" + " {\n" + " return false;\n" + " }\n" + " default:\n" + " {\n" + " return true;\n" + " }\n" + "}", + Style); + Style.IndentCaseLabels = false; + Style.IndentCaseBlocks = true; + verifyFormat("switch (n)\n" + "{\n" + "case 0:\n" + " {\n" + " return false;\n" + " }\n" + "case 1:\n" + " break;\n" + "default:\n" + " {\n" + " return true;\n" + " }\n" + "}", + "switch (n) {\n" + "case 0: {\n" + " return false;\n" + "}\n" + "case 1:\n" + " break;\n" + "default: {\n" + " return true;\n" + "}\n" + "}", + Style); + Style.IndentCaseLabels = true; + Style.IndentCaseBlocks = true; + verifyFormat("switch (n)\n" + "{\n" + " case 0:\n" + " {\n" + " return false;\n" + " }\n" + " case 1:\n" + " break;\n" + " default:\n" + " {\n" + " return true;\n" + " }\n" + "}", + "switch (n) {\n" + "case 0: {\n" + " return false;\n" + "}\n" + "case 1:\n" + " break;\n" + "default: {\n" + " return true;\n" + "}\n" + "}", + Style); +} + TEST_F(FormatTest, CaseRanges) { verifyFormat("switch (x) {\n" "case 'A' ... 'Z':\n" @@ -2857,60 +2820,60 @@ TEST_F(FormatTest, ShortCaseLabels) { " // comment line 2\n" "}", Style); - EXPECT_EQ("switch (a) {\n" - "case 1:\n" - " x = 8;\n" - " // fall through\n" - "case 2: x = 8;\n" - "// comment\n" - "case 3:\n" - " return; /* comment line 1\n" - " * comment line 2 */\n" - "case 4: i = 8;\n" - "// something else\n" - "#if FOO\n" - "case 5: break;\n" - "#endif\n" - "}", - format("switch (a) {\n" - "case 1: x = 8;\n" - " // fall through\n" - "case 2:\n" - " x = 8;\n" - "// comment\n" - "case 3:\n" - " return; /* comment line 1\n" - " * comment line 2 */\n" - "case 4:\n" - " i = 8;\n" - "// something else\n" - "#if FOO\n" - "case 5: break;\n" - "#endif\n" - "}", - Style)); - EXPECT_EQ("switch (a) {\n" - "case 0:\n" - " return; // long long long long long long long long long long " - "long long comment\n" - " // line\n" - "}", - format("switch (a) {\n" - "case 0: return; // long long long long long long long long " - "long long long long comment line\n" - "}", - Style)); - EXPECT_EQ("switch (a) {\n" - "case 0:\n" - " return; /* long long long long long long long long long long " - "long long comment\n" - " line */\n" - "}", - format("switch (a) {\n" - "case 0: return; /* long long long long long long long long " - "long long long long comment line */\n" - "}", - Style)); + verifyFormat("switch (a) {\n" + "case 1:\n" + " x = 8;\n" + " // fall through\n" + "case 2: x = 8;\n" + "// comment\n" + "case 3:\n" + " return; /* comment line 1\n" + " * comment line 2 */\n" + "case 4: i = 8;\n" + "// something else\n" + "#if FOO\n" + "case 5: break;\n" + "#endif\n" + "}", + "switch (a) {\n" + "case 1: x = 8;\n" + " // fall through\n" + "case 2:\n" + " x = 8;\n" + "// comment\n" + "case 3:\n" + " return; /* comment line 1\n" + " * comment line 2 */\n" + "case 4:\n" + " i = 8;\n" + "// something else\n" + "#if FOO\n" + "case 5: break;\n" + "#endif\n" + "}", + Style); + verifyFormat("switch (a) {\n" + "case 0:\n" + " return; // long long long long long long long long long long " + "long long comment\n" + " // line\n" + "}", + "switch (a) {\n" + "case 0: return; // long long long long long long long long " + "long long long long comment line\n" + "}", + Style); + verifyFormat("switch (a) {\n" + "case 0:\n" + " return; /* long long long long long long long long long long " + "long long comment\n" + " line */\n" + "}", + "switch (a) {\n" + "case 0: return; /* long long long long long long long long " + "long long long long comment line */\n" + "}", + Style); verifyFormat("switch (a) {\n" "#if FOO\n" "case 0: return 0;\n" @@ -2951,44 +2914,44 @@ TEST_F(FormatTest, ShortCaseLabels) { Style.ColumnLimit = 80; Style.AllowShortCaseLabelsOnASingleLine = false; Style.IndentCaseLabels = true; - EXPECT_EQ("switch (n) {\n" - " default /*comments*/:\n" - " return true;\n" - " case 0:\n" - " return false;\n" - "}", - format("switch (n) {\n" - "default/*comments*/:\n" - " return true;\n" - "case 0:\n" - " return false;\n" - "}", - Style)); + verifyFormat("switch (n) {\n" + " default /*comments*/:\n" + " return true;\n" + " case 0:\n" + " return false;\n" + "}", + "switch (n) {\n" + "default/*comments*/:\n" + " return true;\n" + "case 0:\n" + " return false;\n" + "}", + Style); Style.AllowShortCaseLabelsOnASingleLine = true; Style.BreakBeforeBraces = FormatStyle::BS_Custom; Style.BraceWrapping.AfterCaseLabel = true; Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; - EXPECT_EQ("switch (n)\n" - "{\n" - " case 0:\n" - " {\n" - " return false;\n" - " }\n" - " default:\n" - " {\n" - " return true;\n" - " }\n" - "}", - format("switch (n) {\n" - " case 0: {\n" - " return false;\n" - " }\n" - " default:\n" - " {\n" - " return true;\n" - " }\n" - "}", - Style)); + verifyFormat("switch (n)\n" + "{\n" + " case 0:\n" + " {\n" + " return false;\n" + " }\n" + " default:\n" + " {\n" + " return true;\n" + " }\n" + "}", + "switch (n) {\n" + " case 0: {\n" + " return false;\n" + " }\n" + " default:\n" + " {\n" + " return true;\n" + " }\n" + "}", + Style); } TEST_F(FormatTest, FormatsLabels) { @@ -3109,59 +3072,58 @@ TEST_F(FormatTest, MultiLineControlStatements) { Style.BreakBeforeBraces = FormatStyle::BraceBreakingStyle::BS_Custom; Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; // Short lines should keep opening brace on same line. - EXPECT_EQ("if (foo) {\n" - " bar();\n" - "}", - format("if(foo){bar();}", Style)); - EXPECT_EQ("if (foo) {\n" - " bar();\n" - "} else {\n" - " baz();\n" - "}", - format("if(foo){bar();}else{baz();}", Style)); - EXPECT_EQ("if (foo && bar) {\n" - " baz();\n" - "}", - format("if(foo&&bar){baz();}", Style)); - EXPECT_EQ("if (foo) {\n" - " bar();\n" - "} else if (baz) {\n" - " quux();\n" - "}", - format("if(foo){bar();}else if(baz){quux();}", Style)); - EXPECT_EQ( - "if (foo) {\n" - " bar();\n" - "} else if (baz) {\n" - " quux();\n" - "} else {\n" - " foobar();\n" - "}", - format("if(foo){bar();}else if(baz){quux();}else{foobar();}", Style)); - EXPECT_EQ("for (;;) {\n" - " foo();\n" - "}", - format("for(;;){foo();}")); - EXPECT_EQ("while (1) {\n" - " foo();\n" - "}", - format("while(1){foo();}", Style)); - EXPECT_EQ("switch (foo) {\n" - "case bar:\n" - " return;\n" - "}", - format("switch(foo){case bar:return;}", Style)); - EXPECT_EQ("try {\n" - " foo();\n" - "} catch (...) {\n" - " bar();\n" - "}", - format("try{foo();}catch(...){bar();}", Style)); - EXPECT_EQ("do {\n" - " foo();\n" - "} while (bar &&\n" - " baz);", - format("do{foo();}while(bar&&baz);", Style)); + verifyFormat("if (foo) {\n" + " bar();\n" + "}", + "if(foo){bar();}", Style); + verifyFormat("if (foo) {\n" + " bar();\n" + "} else {\n" + " baz();\n" + "}", + "if(foo){bar();}else{baz();}", Style); + verifyFormat("if (foo && bar) {\n" + " baz();\n" + "}", + "if(foo&&bar){baz();}", Style); + verifyFormat("if (foo) {\n" + " bar();\n" + "} else if (baz) {\n" + " quux();\n" + "}", + "if(foo){bar();}else if(baz){quux();}", Style); + verifyFormat("if (foo) {\n" + " bar();\n" + "} else if (baz) {\n" + " quux();\n" + "} else {\n" + " foobar();\n" + "}", + "if(foo){bar();}else if(baz){quux();}else{foobar();}", Style); + verifyFormat("for (;;) {\n" + " foo();\n" + "}", + "for(;;){foo();}"); + verifyFormat("while (1) {\n" + " foo();\n" + "}", + "while(1){foo();}", Style); + verifyFormat("switch (foo) {\n" + "case bar:\n" + " return;\n" + "}", + "switch(foo){case bar:return;}", Style); + verifyFormat("try {\n" + " foo();\n" + "} catch (...) {\n" + " bar();\n" + "}", + "try{foo();}catch(...){bar();}", Style); + verifyFormat("do {\n" + " foo();\n" + "} while (bar &&\n" + " baz);", + "do{foo();}while(bar&&baz);", Style); // Long lines should put opening brace on new line. verifyFormat("void f() {\n" " if (a1 && a2 &&\n" @@ -3171,118 +3133,116 @@ TEST_F(FormatTest, MultiLineControlStatements) { " }\n" "}", "void f(){if(a1&&a2&&a3){quux();}}", Style); - EXPECT_EQ("if (foo && bar &&\n" - " baz)\n" - "{\n" - " quux();\n" - "}", - format("if(foo&&bar&&baz){quux();}", Style)); - EXPECT_EQ("if (foo && bar &&\n" - " baz)\n" - "{\n" - " quux();\n" - "}", - format("if (foo && bar &&\n" - " baz) {\n" - " quux();\n" - "}", - Style)); - EXPECT_EQ("if (foo) {\n" - " bar();\n" - "} else if (baz ||\n" - " quux)\n" - "{\n" - " foobar();\n" - "}", - format("if(foo){bar();}else if(baz||quux){foobar();}", Style)); - EXPECT_EQ( - "if (foo) {\n" - " bar();\n" - "} else if (baz ||\n" - " quux)\n" - "{\n" - " foobar();\n" - "} else {\n" - " barbaz();\n" - "}", - format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", - Style)); - EXPECT_EQ("for (int i = 0;\n" - " i < 10; ++i)\n" - "{\n" - " foo();\n" - "}", - format("for(int i=0;i<10;++i){foo();}", Style)); - EXPECT_EQ("foreach (int i,\n" - " list)\n" - "{\n" - " foo();\n" - "}", - format("foreach(int i, list){foo();}", Style)); + verifyFormat("if (foo && bar &&\n" + " baz)\n" + "{\n" + " quux();\n" + "}", + "if(foo&&bar&&baz){quux();}", Style); + verifyFormat("if (foo && bar &&\n" + " baz)\n" + "{\n" + " quux();\n" + "}", + "if (foo && bar &&\n" + " baz) {\n" + " quux();\n" + "}", + Style); + verifyFormat("if (foo) {\n" + " bar();\n" + "} else if (baz ||\n" + " quux)\n" + "{\n" + " foobar();\n" + "}", + "if(foo){bar();}else if(baz||quux){foobar();}", Style); + verifyFormat("if (foo) {\n" + " bar();\n" + "} else if (baz ||\n" + " quux)\n" + "{\n" + " foobar();\n" + "} else {\n" + " barbaz();\n" + "}", + "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", + Style); + verifyFormat("for (int i = 0;\n" + " i < 10; ++i)\n" + "{\n" + " foo();\n" + "}", + "for(int i=0;i<10;++i){foo();}", Style); + verifyFormat("foreach (int i,\n" + " list)\n" + "{\n" + " foo();\n" + "}", + "foreach(int i, list){foo();}", Style); Style.ColumnLimit = 40; // to concentrate at brace wrapping, not line wrap due to column limit - EXPECT_EQ("foreach (int i, list) {\n" - " foo();\n" - "}", - format("foreach(int i, list){foo();}", Style)); + verifyFormat("foreach (int i, list) {\n" + " foo();\n" + "}", + "foreach(int i, list){foo();}", Style); Style.ColumnLimit = 20; // to concentrate at brace wrapping, not line wrap due to column limit - EXPECT_EQ("while (foo || bar ||\n" - " baz)\n" - "{\n" - " quux();\n" - "}", - format("while(foo||bar||baz){quux();}", Style)); - EXPECT_EQ("switch (\n" - " foo = barbaz)\n" - "{\n" - "case quux:\n" - " return;\n" - "}", - format("switch(foo=barbaz){case quux:return;}", Style)); - EXPECT_EQ("try {\n" - " foo();\n" - "} catch (\n" - " Exception &bar)\n" - "{\n" - " baz();\n" - "}", - format("try{foo();}catch(Exception&bar){baz();}", Style)); + verifyFormat("while (foo || bar ||\n" + " baz)\n" + "{\n" + " quux();\n" + "}", + "while(foo||bar||baz){quux();}", Style); + verifyFormat("switch (\n" + " foo = barbaz)\n" + "{\n" + "case quux:\n" + " return;\n" + "}", + "switch(foo=barbaz){case quux:return;}", Style); + verifyFormat("try {\n" + " foo();\n" + "} catch (\n" + " Exception &bar)\n" + "{\n" + " baz();\n" + "}", + "try{foo();}catch(Exception&bar){baz();}", Style); Style.ColumnLimit = 40; // to concentrate at brace wrapping, not line wrap due to column limit - EXPECT_EQ("try {\n" - " foo();\n" - "} catch (Exception &bar) {\n" - " baz();\n" - "}", - format("try{foo();}catch(Exception&bar){baz();}", Style)); + verifyFormat("try {\n" + " foo();\n" + "} catch (Exception &bar) {\n" + " baz();\n" + "}", + "try{foo();}catch(Exception&bar){baz();}", Style); Style.ColumnLimit = 20; // to concentrate at brace wrapping, not line wrap due to column limit Style.BraceWrapping.BeforeElse = true; - EXPECT_EQ( - "if (foo) {\n" - " bar();\n" - "}\n" - "else if (baz ||\n" - " quux)\n" - "{\n" - " foobar();\n" - "}\n" - "else {\n" - " barbaz();\n" - "}", - format("if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", - Style)); + verifyFormat("if (foo) {\n" + " bar();\n" + "}\n" + "else if (baz ||\n" + " quux)\n" + "{\n" + " foobar();\n" + "}\n" + "else {\n" + " barbaz();\n" + "}", + "if(foo){bar();}else if(baz||quux){foobar();}else{barbaz();}", + Style); Style.BraceWrapping.BeforeCatch = true; - EXPECT_EQ("try {\n" - " foo();\n" - "}\n" - "catch (...) {\n" - " baz();\n" - "}", - format("try{foo();}catch(...){baz();}", Style)); + verifyFormat("try {\n" + " foo();\n" + "}\n" + "catch (...) {\n" + " baz();\n" + "}", + "try{foo();}catch(...){baz();}", Style); Style.BraceWrapping.AfterFunction = true; Style.BraceWrapping.AfterStruct = false; @@ -3469,61 +3429,61 @@ TEST_F(FormatTest, UnderstandsAccessSpecifiers) { } TEST_F(FormatTest, SeparatesLogicalBlocks) { - EXPECT_EQ("class A {\n" - "public:\n" - " void f();\n" - "\n" - "private:\n" - " void g() {}\n" - " // test\n" - "protected:\n" - " int h;\n" - "};", - format("class A {\n" - "public:\n" - "void f();\n" - "private:\n" - "void g() {}\n" - "// test\n" - "protected:\n" - "int h;\n" - "};")); - EXPECT_EQ("class A {\n" - "protected:\n" - "public:\n" - " void f();\n" - "};", - format("class A {\n" - "protected:\n" - "\n" - "public:\n" - "\n" - " void f();\n" - "};")); + verifyFormat("class A {\n" + "public:\n" + " void f();\n" + "\n" + "private:\n" + " void g() {}\n" + " // test\n" + "protected:\n" + " int h;\n" + "};", + "class A {\n" + "public:\n" + "void f();\n" + "private:\n" + "void g() {}\n" + "// test\n" + "protected:\n" + "int h;\n" + "};"); + verifyFormat("class A {\n" + "protected:\n" + "public:\n" + " void f();\n" + "};", + "class A {\n" + "protected:\n" + "\n" + "public:\n" + "\n" + " void f();\n" + "};"); // Even ensure proper spacing inside macros. - EXPECT_EQ("#define B \\\n" - " class A { \\\n" - " protected: \\\n" - " public: \\\n" - " void f(); \\\n" - " };", - format("#define B \\\n" - " class A { \\\n" - " protected: \\\n" - " \\\n" - " public: \\\n" - " \\\n" - " void f(); \\\n" - " };", - getGoogleStyle())); + verifyFormat("#define B \\\n" + " class A { \\\n" + " protected: \\\n" + " public: \\\n" + " void f(); \\\n" + " };", + "#define B \\\n" + " class A { \\\n" + " protected: \\\n" + " \\\n" + " public: \\\n" + " \\\n" + " void f(); \\\n" + " };", + getGoogleStyle()); // But don't remove empty lines after macros ending in access specifiers. - EXPECT_EQ("#define A private:\n" - "\n" - "int i;", - format("#define A private:\n" - "\n" - "int i;")); + verifyFormat("#define A private:\n" + "\n" + "int i;", + "#define A private:\n" + "\n" + "int i;"); } TEST_F(FormatTest, FormatsClasses) { @@ -3689,21 +3649,21 @@ TEST_F(FormatTest, FormatsEnum) { verifyFormat("enum ShortEnum { A, B, C };"); verifyGoogleFormat("enum ShortEnum { A, B, C };"); - EXPECT_EQ("enum KeepEmptyLines {\n" - " ONE,\n" - "\n" - " TWO,\n" - "\n" - " THREE\n" - "}", - format("enum KeepEmptyLines {\n" - " ONE,\n" - "\n" - " TWO,\n" - "\n" - "\n" - " THREE\n" - "}")); + verifyFormat("enum KeepEmptyLines {\n" + " ONE,\n" + "\n" + " TWO,\n" + "\n" + " THREE\n" + "}", + "enum KeepEmptyLines {\n" + " ONE,\n" + "\n" + " TWO,\n" + "\n" + "\n" + " THREE\n" + "}"); verifyFormat("enum E { // comment\n" " ONE,\n" " TWO\n" @@ -4051,43 +4011,40 @@ TEST_F(FormatTest, FormatsNamespaces) { "int SomeVariable = 0; // comment\n" "} // namespace", LLVMWithNoNamespaceFix); - EXPECT_EQ("#ifndef HEADER_GUARD\n" - "#define HEADER_GUARD\n" - "namespace my_namespace {\n" - "int i;\n" - "} // my_namespace\n" - "#endif // HEADER_GUARD", - format("#ifndef HEADER_GUARD\n" - " #define HEADER_GUARD\n" - " namespace my_namespace {\n" - "int i;\n" - "} // my_namespace\n" - "#endif // HEADER_GUARD", - LLVMWithNoNamespaceFix)); - - EXPECT_EQ("namespace A::B {\n" - "class C {};\n" - "}", - format("namespace A::B {\n" - "class C {};\n" - "}", - LLVMWithNoNamespaceFix)); + verifyFormat("#ifndef HEADER_GUARD\n" + "#define HEADER_GUARD\n" + "namespace my_namespace {\n" + "int i;\n" + "} // my_namespace\n" + "#endif // HEADER_GUARD", + "#ifndef HEADER_GUARD\n" + " #define HEADER_GUARD\n" + " namespace my_namespace {\n" + "int i;\n" + "} // my_namespace\n" + "#endif // HEADER_GUARD", + LLVMWithNoNamespaceFix); + + verifyFormat("namespace A::B {\n" + "class C {};\n" + "}", + LLVMWithNoNamespaceFix); FormatStyle Style = getLLVMStyle(); Style.NamespaceIndentation = FormatStyle::NI_All; - EXPECT_EQ("namespace out {\n" - " int i;\n" - " namespace in {\n" - " int i;\n" - " } // namespace in\n" - "} // namespace out", - format("namespace out {\n" - "int i;\n" - "namespace in {\n" - "int i;\n" - "} // namespace in\n" - "} // namespace out", - Style)); + verifyFormat("namespace out {\n" + " int i;\n" + " namespace in {\n" + " int i;\n" + " } // namespace in\n" + "} // namespace out", + "namespace out {\n" + "int i;\n" + "namespace in {\n" + "int i;\n" + "} // namespace in\n" + "} // namespace out", + Style); FormatStyle ShortInlineFunctions = getLLVMStyle(); ShortInlineFunctions.NamespaceIndentation = FormatStyle::NI_All; @@ -4194,19 +4151,19 @@ TEST_F(FormatTest, FormatsNamespaces) { ShortInlineFunctions); Style.NamespaceIndentation = FormatStyle::NI_Inner; - EXPECT_EQ("namespace out {\n" - "int i;\n" - "namespace in {\n" - " int i;\n" - "} // namespace in\n" - "} // namespace out", - format("namespace out {\n" - "int i;\n" - "namespace in {\n" - "int i;\n" - "} // namespace in\n" - "} // namespace out", - Style)); + verifyFormat("namespace out {\n" + "int i;\n" + "namespace in {\n" + " int i;\n" + "} // namespace in\n" + "} // namespace out", + "namespace out {\n" + "int i;\n" + "namespace in {\n" + "int i;\n" + "} // namespace in\n" + "} // namespace out", + Style); Style.NamespaceIndentation = FormatStyle::NI_None; verifyFormat("template \n" @@ -4285,51 +4242,39 @@ TEST_F(FormatTest, NamespaceMacros) { "}} // TESTSUITE(A::B)", Style); - EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" - "}} // TESTSUITE(out::in)", - format("TESTSUITE(out) {\n" - "TESTSUITE(in) {\n" - "} // TESTSUITE(in)\n" - "} // TESTSUITE(out)", - Style)); + verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n" + "}} // TESTSUITE(out::in)", + "TESTSUITE(out) {\n" + "TESTSUITE(in) {\n" + "} // TESTSUITE(in)\n" + "} // TESTSUITE(out)", + Style); - EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n" - "}} // TESTSUITE(out::in)", - format("TESTSUITE(out) {\n" - "TESTSUITE(in) {\n" - "} // TESTSUITE(in)\n" - "} // TESTSUITE(out)", - Style)); + verifyFormat("TESTSUITE(out) { TESTSUITE(in) {\n" + "}} // TESTSUITE(out::in)", + "TESTSUITE(out) {\n" + "TESTSUITE(in) {\n" + "} // TESTSUITE(in)\n" + "} // TESTSUITE(out)", + Style); // Do not merge different namespaces/macros - EXPECT_EQ("namespace out {\n" - "TESTSUITE(in) {\n" - "} // TESTSUITE(in)\n" - "} // namespace out", - format("namespace out {\n" - "TESTSUITE(in) {\n" - "} // TESTSUITE(in)\n" - "} // namespace out", - Style)); - EXPECT_EQ("TESTSUITE(out) {\n" - "namespace in {\n" - "} // namespace in\n" - "} // TESTSUITE(out)", - format("TESTSUITE(out) {\n" - "namespace in {\n" - "} // namespace in\n" - "} // TESTSUITE(out)", - Style)); + verifyFormat("namespace out {\n" + "TESTSUITE(in) {\n" + "} // TESTSUITE(in)\n" + "} // namespace out", + Style); + verifyFormat("TESTSUITE(out) {\n" + "namespace in {\n" + "} // namespace in\n" + "} // TESTSUITE(out)", + Style); Style.NamespaceMacros.push_back("FOOBAR"); - EXPECT_EQ("TESTSUITE(out) {\n" - "FOOBAR(in) {\n" - "} // FOOBAR(in)\n" - "} // TESTSUITE(out)", - format("TESTSUITE(out) {\n" - "FOOBAR(in) {\n" - "} // FOOBAR(in)\n" - "} // TESTSUITE(out)", - Style)); + verifyFormat("TESTSUITE(out) {\n" + "FOOBAR(in) {\n" + "} // FOOBAR(in)\n" + "} // TESTSUITE(out)", + Style); } TEST_F(FormatTest, FormatsCompactNamespaces) { @@ -4341,109 +4286,103 @@ TEST_F(FormatTest, FormatsCompactNamespaces) { "}} // namespace A::B", Style); - EXPECT_EQ("namespace out { namespace in {\n" - "}} // namespace out::in", - format("namespace out {\n" - "namespace in {\n" - "} // namespace in\n" - "} // namespace out", - Style)); + verifyFormat("namespace out { namespace in {\n" + "}} // namespace out::in", + "namespace out {\n" + "namespace in {\n" + "} // namespace in\n" + "} // namespace out", + Style); // Only namespaces which have both consecutive opening and end get compacted - EXPECT_EQ("namespace out {\n" - "namespace in1 {\n" - "} // namespace in1\n" - "namespace in2 {\n" - "} // namespace in2\n" - "} // namespace out", - format("namespace out {\n" - "namespace in1 {\n" - "} // namespace in1\n" - "namespace in2 {\n" - "} // namespace in2\n" - "} // namespace out", - Style)); + verifyFormat("namespace out {\n" + "namespace in1 {\n" + "} // namespace in1\n" + "namespace in2 {\n" + "} // namespace in2\n" + "} // namespace out", + Style); - EXPECT_EQ("namespace out {\n" - "int i;\n" - "namespace in {\n" - "int j;\n" - "} // namespace in\n" - "int k;\n" - "} // namespace out", - format("namespace out { int i;\n" - "namespace in { int j; } // namespace in\n" - "int k; } // namespace out", - Style)); + verifyFormat("namespace out {\n" + "int i;\n" + "namespace in {\n" + "int j;\n" + "} // namespace in\n" + "int k;\n" + "} // namespace out", + "namespace out { int i;\n" + "namespace in { int j; } // namespace in\n" + "int k; } // namespace out", + Style); - EXPECT_EQ("namespace A { namespace B { namespace C {\n" - "}}} // namespace A::B::C", - format("namespace A { namespace B {\n" - "namespace C {\n" - "}} // namespace B::C\n" - "} // namespace A", - Style)); + verifyFormat("namespace A { namespace B { namespace C {\n" + "}}} // namespace A::B::C", + "namespace A { namespace B {\n" + "namespace C {\n" + "}} // namespace B::C\n" + "} // namespace A", + Style); Style.ColumnLimit = 40; - EXPECT_EQ("namespace aaaaaaaaaa {\n" - "namespace bbbbbbbbbb {\n" - "}} // namespace aaaaaaaaaa::bbbbbbbbbb", - format("namespace aaaaaaaaaa {\n" - "namespace bbbbbbbbbb {\n" - "} // namespace bbbbbbbbbb\n" - "} // namespace aaaaaaaaaa", - Style)); - - EXPECT_EQ("namespace aaaaaa { namespace bbbbbb {\n" - "namespace cccccc {\n" - "}}} // namespace aaaaaa::bbbbbb::cccccc", - format("namespace aaaaaa {\n" - "namespace bbbbbb {\n" - "namespace cccccc {\n" - "} // namespace cccccc\n" - "} // namespace bbbbbb\n" - "} // namespace aaaaaa", - Style)); + verifyFormat("namespace aaaaaaaaaa {\n" + "namespace bbbbbbbbbb {\n" + "}} // namespace aaaaaaaaaa::bbbbbbbbbb", + "namespace aaaaaaaaaa {\n" + "namespace bbbbbbbbbb {\n" + "} // namespace bbbbbbbbbb\n" + "} // namespace aaaaaaaaaa", + Style); + + verifyFormat("namespace aaaaaa { namespace bbbbbb {\n" + "namespace cccccc {\n" + "}}} // namespace aaaaaa::bbbbbb::cccccc", + "namespace aaaaaa {\n" + "namespace bbbbbb {\n" + "namespace cccccc {\n" + "} // namespace cccccc\n" + "} // namespace bbbbbb\n" + "} // namespace aaaaaa", + Style); Style.ColumnLimit = 80; // Extra semicolon after 'inner' closing brace prevents merging - EXPECT_EQ("namespace out { namespace in {\n" - "}; } // namespace out::in", - format("namespace out {\n" - "namespace in {\n" - "}; // namespace in\n" - "} // namespace out", - Style)); + verifyFormat("namespace out { namespace in {\n" + "}; } // namespace out::in", + "namespace out {\n" + "namespace in {\n" + "}; // namespace in\n" + "} // namespace out", + Style); // Extra semicolon after 'outer' closing brace is conserved - EXPECT_EQ("namespace out { namespace in {\n" - "}}; // namespace out::in", - format("namespace out {\n" - "namespace in {\n" - "} // namespace in\n" - "}; // namespace out", - Style)); + verifyFormat("namespace out { namespace in {\n" + "}}; // namespace out::in", + "namespace out {\n" + "namespace in {\n" + "} // namespace in\n" + "}; // namespace out", + Style); Style.NamespaceIndentation = FormatStyle::NI_All; - EXPECT_EQ("namespace out { namespace in {\n" - " int i;\n" - "}} // namespace out::in", - format("namespace out {\n" - "namespace in {\n" - "int i;\n" - "} // namespace in\n" - "} // namespace out", - Style)); - EXPECT_EQ("namespace out { namespace mid {\n" - " namespace in {\n" - " int j;\n" - " } // namespace in\n" - " int k;\n" - "}} // namespace out::mid", - format("namespace out { namespace mid {\n" - "namespace in { int j; } // namespace in\n" - "int k; }} // namespace out::mid", - Style)); + verifyFormat("namespace out { namespace in {\n" + " int i;\n" + "}} // namespace out::in", + "namespace out {\n" + "namespace in {\n" + "int i;\n" + "} // namespace in\n" + "} // namespace out", + Style); + verifyFormat("namespace out { namespace mid {\n" + " namespace in {\n" + " int j;\n" + " } // namespace in\n" + " int k;\n" + "}} // namespace out::mid", + "namespace out { namespace mid {\n" + "namespace in { int j; } // namespace in\n" + "int k; }} // namespace out::mid", + Style); verifyFormat("namespace A { namespace B { namespace C {\n" " int i;\n" @@ -4486,26 +4425,26 @@ TEST_F(FormatTest, FormatsCompactNamespaces) { Style); Style.NamespaceIndentation = FormatStyle::NI_Inner; - EXPECT_EQ("namespace out { namespace in {\n" - " int i;\n" - "}} // namespace out::in", - format("namespace out {\n" - "namespace in {\n" - "int i;\n" - "} // namespace in\n" - "} // namespace out", - Style)); - EXPECT_EQ("namespace out { namespace mid { namespace in {\n" - " int i;\n" - "}}} // namespace out::mid::in", - format("namespace out {\n" - "namespace mid {\n" - "namespace in {\n" - "int i;\n" - "} // namespace in\n" - "} // namespace mid\n" - "} // namespace out", - Style)); + verifyFormat("namespace out { namespace in {\n" + " int i;\n" + "}} // namespace out::in", + "namespace out {\n" + "namespace in {\n" + "int i;\n" + "} // namespace in\n" + "} // namespace out", + Style); + verifyFormat("namespace out { namespace mid { namespace in {\n" + " int i;\n" + "}}} // namespace out::mid::in", + "namespace out {\n" + "namespace mid {\n" + "namespace in {\n" + "int i;\n" + "} // namespace in\n" + "} // namespace mid\n" + "} // namespace out", + Style); Style.CompactNamespaces = true; Style.AllowShortLambdasOnASingleLine = FormatStyle::SLS_None; @@ -4514,13 +4453,13 @@ TEST_F(FormatTest, FormatsCompactNamespaces) { verifyFormat("namespace out { namespace in {\n" "}} // namespace out::in", Style); - EXPECT_EQ("namespace out { namespace in {\n" - "}} // namespace out::in", - format("namespace out {\n" - "namespace in {\n" - "} // namespace in\n" - "} // namespace out", - Style)); + verifyFormat("namespace out { namespace in {\n" + "}} // namespace out::in", + "namespace out {\n" + "namespace in {\n" + "} // namespace in\n" + "} // namespace out", + Style); } TEST_F(FormatTest, FormatsExternC) { @@ -4636,7 +4575,7 @@ TEST_F(FormatTest, FormatsInlineASM) { " \"xchgq\\t%%rbx, %%rsi\\n\\t\"\n" " : \"=a\"(*rEAX), \"=S\"(*rEBX), \"=c\"(*rECX), \"=d\"(*rEDX)\n" " : \"a\"(value));"); - EXPECT_EQ( + verifyFormat( "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" " __asm {\n" " mov edx,[that] // vtable in edx\n" @@ -4644,31 +4583,27 @@ TEST_F(FormatTest, FormatsInlineASM) { " call [edx][eax*4] // stdcall\n" " }\n" "}", - format("void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" - " __asm {\n" - " mov edx,[that] // vtable in edx\n" - " mov eax,methodIndex\n" - " call [edx][eax*4] // stdcall\n" - " }\n" - "}")); - EXPECT_EQ("_asm {\n" - " xor eax, eax;\n" - " cpuid;\n" - "}", - format("_asm {\n" - " xor eax, eax;\n" - " cpuid;\n" - "}")); + "void NS_InvokeByIndex(void *that, unsigned int methodIndex) {\n" + " __asm {\n" + " mov edx,[that] // vtable in edx\n" + " mov eax,methodIndex\n" + " call [edx][eax*4] // stdcall\n" + " }\n" + "}"); + verifyNoChange("_asm {\n" + " xor eax, eax;\n" + " cpuid;\n" + "}"); verifyFormat("void function() {\n" " // comment\n" " asm(\"\");\n" "}"); - EXPECT_EQ("__asm {\n" - "}\n" - "int i;", - format("__asm {\n" - "}\n" - "int i;")); + verifyFormat("__asm {\n" + "}\n" + "int i;", + "__asm {\n" + "}\n" + "int i;"); auto Style = getLLVMStyleWithColumns(0); const StringRef Code1{"asm(\"xyz\" : \"=a\"(a), \"=d\"(b) : \"a\"(data));"}; @@ -4906,14 +4841,14 @@ TEST_F(FormatTest, StaticInitializers) { // Here, everything other than the "}" would fit on a line. verifyFormat("static int LooooooooooooooooooooooooongVariable[1] = {\n" " 10000000000000000000000000};"); - EXPECT_EQ("S s = {a,\n" - "\n" - " b};", - format("S s = {\n" - " a,\n" - "\n" - " b\n" - "};")); + verifyFormat("S s = {a,\n" + "\n" + " b};", + "S s = {\n" + " a,\n" + "\n" + " b\n" + "};"); // FIXME: This would fit into the column limit if we'd fit "{ {" on the first // line. However, the formatting looks a bit off and this probably doesn't @@ -5216,16 +5151,16 @@ TEST_F(FormatTest, BreaksStringLiteralsOnlyInDefine) { getLLVMStyleWithColumns(40)); verifyFormat("#line 11111 \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", getLLVMStyleWithColumns(40)); - EXPECT_EQ("#define Q \\\n" - " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" - " \"aaaaaaaa.cpp\"", - format("#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", - getLLVMStyleWithColumns(40))); + verifyFormat("#define Q \\\n" + " \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/\" \\\n" + " \"aaaaaaaa.cpp\"", + "#define Q \"/aaaaaaaaa/aaaaaaaaaaaaaaaaaaa/aaaaaaaa.cpp\"", + getLLVMStyleWithColumns(40)); } TEST_F(FormatTest, UnderstandsLinePPDirective) { - EXPECT_EQ("# 123 \"A string literal\"", - format(" # 123 \"A string literal\"")); + verifyFormat("# 123 \"A string literal\"", + " # 123 \"A string literal\""); } TEST_F(FormatTest, LayoutUnknownPPDirective) { @@ -5234,30 +5169,28 @@ TEST_F(FormatTest, LayoutUnknownPPDirective) { } TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { - EXPECT_EQ("#line 42 \"test\"", - format("# \\\n line \\\n 42 \\\n \"test\"")); - EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B", - getLLVMStyleWithColumns(12))); + verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\""); + verifyFormat("#define A B", "# \\\n define \\\n A \\\n B", + getLLVMStyleWithColumns(12)); } TEST_F(FormatTest, EndOfFileEndsPPDirective) { - EXPECT_EQ("#line 42 \"test\"", - format("# \\\n line \\\n 42 \\\n \"test\"")); - EXPECT_EQ("#define A B", format("# \\\n define \\\n A \\\n B")); + verifyFormat("#line 42 \"test\"", "# \\\n line \\\n 42 \\\n \"test\""); + verifyFormat("#define A B", "# \\\n define \\\n A \\\n B"); } TEST_F(FormatTest, DoesntRemoveUnknownTokens) { verifyFormat("#define A \\x20"); verifyFormat("#define A \\ x20"); - EXPECT_EQ("#define A \\ x20", format("#define A \\ x20")); + verifyFormat("#define A \\ x20", "#define A \\ x20"); verifyFormat("#define A ''"); verifyFormat("#define A ''qqq"); verifyFormat("#define A `qqq"); verifyFormat("f(\"aaaa, bbbb, \"\\\"ccccc\\\"\");"); - EXPECT_EQ("const char *c = STRINGIFY(\n" - "\\na : b);", - format("const char * c = STRINGIFY(\n" - "\\na : b);")); + verifyFormat("const char *c = STRINGIFY(\n" + "\\na : b);", + "const char * c = STRINGIFY(\n" + "\\na : b);"); verifyFormat("a\r\\"); verifyFormat("a\v\\"); @@ -5547,40 +5480,40 @@ TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { } TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { - EXPECT_EQ("// somecomment\n" - "#include \"a.h\"\n" - "#define A( \\\n" - " A, B)\n" - "#include \"b.h\"\n" - "// somecomment", - format(" // somecomment\n" - " #include \"a.h\"\n" - "#define A(A,\\\n" - " B)\n" - " #include \"b.h\"\n" - " // somecomment", - getLLVMStyleWithColumns(13))); + verifyFormat("// somecomment\n" + "#include \"a.h\"\n" + "#define A( \\\n" + " A, B)\n" + "#include \"b.h\"\n" + "// somecomment", + " // somecomment\n" + " #include \"a.h\"\n" + "#define A(A,\\\n" + " B)\n" + " #include \"b.h\"\n" + " // somecomment", + getLLVMStyleWithColumns(13)); } TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); } TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { - EXPECT_EQ("#define A \\\n" - " c; \\\n" - " e;\n" - "f;", - format("#define A c; e;\n" - "f;", - getLLVMStyleWithColumns(14))); + verifyFormat("#define A \\\n" + " c; \\\n" + " e;\n" + "f;", + "#define A c; e;\n" + "f;", + getLLVMStyleWithColumns(14)); } TEST_F(FormatTest, LayoutRemainingTokens) { verifyFormat("{}"); } TEST_F(FormatTest, MacroDefinitionInsideStatement) { - EXPECT_EQ("int x,\n" - "#define A\n" - " y;", - format("int x,\n#define A\ny;")); + verifyFormat("int x,\n" + "#define A\n" + " y;", + "int x,\n#define A\ny;"); } TEST_F(FormatTest, HashInMacroDefinition) { @@ -5622,28 +5555,21 @@ TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { } TEST_F(FormatTest, EmptyLinesInMacroDefinitions) { - EXPECT_EQ("#define A b;", format("#define A \\\n" - " \\\n" - " b;", - getLLVMStyleWithColumns(25))); - EXPECT_EQ("#define A \\\n" - " \\\n" - " a; \\\n" - " b;", - format("#define A \\\n" - " \\\n" - " a; \\\n" - " b;", - getLLVMStyleWithColumns(11))); - EXPECT_EQ("#define A \\\n" - " a; \\\n" - " \\\n" - " b;", - format("#define A \\\n" - " a; \\\n" - " \\\n" - " b;", - getLLVMStyleWithColumns(11))); + verifyFormat("#define A b;", + "#define A \\\n" + " \\\n" + " b;", + getLLVMStyleWithColumns(25)); + verifyNoChange("#define A \\\n" + " \\\n" + " a; \\\n" + " b;", + getLLVMStyleWithColumns(11)); + verifyNoChange("#define A \\\n" + " a; \\\n" + " \\\n" + " b;", + getLLVMStyleWithColumns(11)); } TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { @@ -5715,161 +5641,158 @@ TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) { verifyFormat("SOME_TYPE_NAME abc;"); // Gated on the newline. - EXPECT_EQ("class A : public QObject {\n" - " Q_OBJECT\n" - "\n" - " A() {}\n" - "};", - format("class A : public QObject {\n" - " Q_OBJECT\n" - "\n" - " A() {\n}\n" - "} ;")); - EXPECT_EQ("MACRO\n" - "/*static*/ int i;", - format("MACRO\n" - " /*static*/ int i;")); - EXPECT_EQ("SOME_MACRO\n" - "namespace {\n" - "void f();\n" - "} // namespace", - format("SOME_MACRO\n" - " namespace {\n" - "void f( );\n" - "} // namespace")); + verifyFormat("class A : public QObject {\n" + " Q_OBJECT\n" + "\n" + " A() {}\n" + "};", + "class A : public QObject {\n" + " Q_OBJECT\n" + "\n" + " A() {\n}\n" + "} ;"); + verifyFormat("MACRO\n" + "/*static*/ int i;", + "MACRO\n" + " /*static*/ int i;"); + verifyFormat("SOME_MACRO\n" + "namespace {\n" + "void f();\n" + "} // namespace", + "SOME_MACRO\n" + " namespace {\n" + "void f( );\n" + "} // namespace"); // Only if the identifier contains at least 5 characters. - EXPECT_EQ("HTTP f();", format("HTTP\nf();")); + verifyFormat("HTTP f();", "HTTP\nf();"); verifyNoChange("MACRO\nf();"); // Only if everything is upper case. - EXPECT_EQ("class A : public QObject {\n" - " Q_Object A() {}\n" - "};", - format("class A : public QObject {\n" - " Q_Object\n" - " A() {\n}\n" - "} ;")); + verifyFormat("class A : public QObject {\n" + " Q_Object A() {}\n" + "};", + "class A : public QObject {\n" + " Q_Object\n" + " A() {\n}\n" + "} ;"); // Only if the next line can actually start an unwrapped line. - EXPECT_EQ("SOME_WEIRD_LOG_MACRO << SomeThing;", - format("SOME_WEIRD_LOG_MACRO\n" - "<< SomeThing;")); + verifyFormat("SOME_WEIRD_LOG_MACRO << SomeThing;", "SOME_WEIRD_LOG_MACRO\n" + "<< SomeThing;"); verifyFormat("VISIT_GL_CALL(GenBuffers, void, (GLsizei n, GLuint* buffers), " "(n, buffers))", getChromiumStyle(FormatStyle::LK_Cpp)); // See PR41483 - EXPECT_EQ("/**/ FOO(a)\n" - "FOO(b)", - format("/**/ FOO(a)\n" - "FOO(b)")); + verifyNoChange("/**/ FOO(a)\n" + "FOO(b)"); } TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { - EXPECT_EQ("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" - "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" - "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" - "class X {};\n" - "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" - "int *createScopDetectionPass() { return 0; }", - format(" INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" - " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" - " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" - " class X {};\n" - " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" - " int *createScopDetectionPass() { return 0; }")); + verifyFormat("INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" + "INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" + "INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" + "class X {};\n" + "INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" + "int *createScopDetectionPass() { return 0; }", + " INITIALIZE_PASS_BEGIN(ScopDetection, \"polly-detect\")\n" + " INITIALIZE_AG_DEPENDENCY(AliasAnalysis)\n" + " INITIALIZE_PASS_DEPENDENCY(DominatorTree)\n" + " class X {};\n" + " INITIALIZE_PASS_END(ScopDetection, \"polly-detect\")\n" + " int *createScopDetectionPass() { return 0; }"); // FIXME: We could probably treat IPC_BEGIN_MESSAGE_MAP/IPC_END_MESSAGE_MAP as // braces, so that inner block is indented one level more. - EXPECT_EQ("int q() {\n" - " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" - " IPC_MESSAGE_HANDLER(xxx, qqq)\n" - " IPC_END_MESSAGE_MAP()\n" - "}", - format("int q() {\n" - " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" - " IPC_MESSAGE_HANDLER(xxx, qqq)\n" - " IPC_END_MESSAGE_MAP()\n" - "}")); + verifyFormat("int q() {\n" + " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" + " IPC_MESSAGE_HANDLER(xxx, qqq)\n" + " IPC_END_MESSAGE_MAP()\n" + "}", + "int q() {\n" + " IPC_BEGIN_MESSAGE_MAP(WebKitTestController, message)\n" + " IPC_MESSAGE_HANDLER(xxx, qqq)\n" + " IPC_END_MESSAGE_MAP()\n" + "}"); // Same inside macros. - EXPECT_EQ("#define LIST(L) \\\n" - " L(A) \\\n" - " L(B) \\\n" - " L(C)", - format("#define LIST(L) \\\n" - " L(A) \\\n" - " L(B) \\\n" - " L(C)", - getGoogleStyle())); + verifyFormat("#define LIST(L) \\\n" + " L(A) \\\n" + " L(B) \\\n" + " L(C)", + "#define LIST(L) \\\n" + " L(A) \\\n" + " L(B) \\\n" + " L(C)", + getGoogleStyle()); // These must not be recognized as macros. - EXPECT_EQ("int q() {\n" - " f(x);\n" - " f(x) {}\n" - " f(x)->g();\n" - " f(x)->*g();\n" - " f(x).g();\n" - " f(x) = x;\n" - " f(x) += x;\n" - " f(x) -= x;\n" - " f(x) *= x;\n" - " f(x) /= x;\n" - " f(x) %= x;\n" - " f(x) &= x;\n" - " f(x) |= x;\n" - " f(x) ^= x;\n" - " f(x) >>= x;\n" - " f(x) <<= x;\n" - " f(x)[y].z();\n" - " LOG(INFO) << x;\n" - " ifstream(x) >> x;\n" - "}", - format("int q() {\n" - " f(x)\n;\n" - " f(x)\n {}\n" - " f(x)\n->g();\n" - " f(x)\n->*g();\n" - " f(x)\n.g();\n" - " f(x)\n = x;\n" - " f(x)\n += x;\n" - " f(x)\n -= x;\n" - " f(x)\n *= x;\n" - " f(x)\n /= x;\n" - " f(x)\n %= x;\n" - " f(x)\n &= x;\n" - " f(x)\n |= x;\n" - " f(x)\n ^= x;\n" - " f(x)\n >>= x;\n" - " f(x)\n <<= x;\n" - " f(x)\n[y].z();\n" - " LOG(INFO)\n << x;\n" - " ifstream(x)\n >> x;\n" - "}")); - EXPECT_EQ("int q() {\n" - " F(x)\n" - " if (1) {\n" - " }\n" - " F(x)\n" - " while (1) {\n" - " }\n" - " F(x)\n" - " G(x);\n" - " F(x)\n" - " try {\n" - " Q();\n" - " } catch (...) {\n" - " }\n" - "}", - format("int q() {\n" - "F(x)\n" - "if (1) {}\n" - "F(x)\n" - "while (1) {}\n" - "F(x)\n" - "G(x);\n" - "F(x)\n" - "try { Q(); } catch (...) {}\n" - "}")); + verifyFormat("int q() {\n" + " f(x);\n" + " f(x) {}\n" + " f(x)->g();\n" + " f(x)->*g();\n" + " f(x).g();\n" + " f(x) = x;\n" + " f(x) += x;\n" + " f(x) -= x;\n" + " f(x) *= x;\n" + " f(x) /= x;\n" + " f(x) %= x;\n" + " f(x) &= x;\n" + " f(x) |= x;\n" + " f(x) ^= x;\n" + " f(x) >>= x;\n" + " f(x) <<= x;\n" + " f(x)[y].z();\n" + " LOG(INFO) << x;\n" + " ifstream(x) >> x;\n" + "}", + "int q() {\n" + " f(x)\n;\n" + " f(x)\n {}\n" + " f(x)\n->g();\n" + " f(x)\n->*g();\n" + " f(x)\n.g();\n" + " f(x)\n = x;\n" + " f(x)\n += x;\n" + " f(x)\n -= x;\n" + " f(x)\n *= x;\n" + " f(x)\n /= x;\n" + " f(x)\n %= x;\n" + " f(x)\n &= x;\n" + " f(x)\n |= x;\n" + " f(x)\n ^= x;\n" + " f(x)\n >>= x;\n" + " f(x)\n <<= x;\n" + " f(x)\n[y].z();\n" + " LOG(INFO)\n << x;\n" + " ifstream(x)\n >> x;\n" + "}"); + verifyFormat("int q() {\n" + " F(x)\n" + " if (1) {\n" + " }\n" + " F(x)\n" + " while (1) {\n" + " }\n" + " F(x)\n" + " G(x);\n" + " F(x)\n" + " try {\n" + " Q();\n" + " } catch (...) {\n" + " }\n" + "}", + "int q() {\n" + "F(x)\n" + "if (1) {}\n" + "F(x)\n" + "while (1) {}\n" + "F(x)\n" + "G(x);\n" + "F(x)\n" + "try { Q(); } catch (...) {}\n" + "}"); EXPECT_EQ("class A {\n" " A() : t(0) {}\n" " A(int i) noexcept() : {}\n" @@ -5888,33 +5811,33 @@ TEST_F(FormatTest, MacroCallsWithoutTrailingSemicolon) { Style.BreakBeforeBraces = FormatStyle::BS_Custom; Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; Style.BraceWrapping.AfterFunction = true; - EXPECT_EQ("void f()\n" - "try\n" - "{\n" - "}", - format("void f() try {\n" - "}", - Style)); - EXPECT_EQ("class SomeClass {\n" - "public:\n" - " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" - "};", - format("class SomeClass {\n" - "public:\n" - " SomeClass()\n" - " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" - "};")); - EXPECT_EQ("class SomeClass {\n" - "public:\n" - " SomeClass()\n" - " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" - "};", - format("class SomeClass {\n" - "public:\n" - " SomeClass()\n" - " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" - "};", - getLLVMStyleWithColumns(40))); + verifyFormat("void f()\n" + "try\n" + "{\n" + "}", + "void f() try {\n" + "}", + Style); + verifyFormat("class SomeClass {\n" + "public:\n" + " SomeClass() EXCLUSIVE_LOCK_FUNCTION(mu_);\n" + "};", + "class SomeClass {\n" + "public:\n" + " SomeClass()\n" + " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" + "};"); + verifyFormat("class SomeClass {\n" + "public:\n" + " SomeClass()\n" + " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" + "};", + "class SomeClass {\n" + "public:\n" + " SomeClass()\n" + " EXCLUSIVE_LOCK_FUNCTION(mu_);\n" + "};", + getLLVMStyleWithColumns(40)); verifyFormat("MACRO(>)"); @@ -6098,50 +6021,50 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) { // #ifndef and #define but all other conditions hold. This is because when // the #define line is parsed, UnwrappedLineParser::Lines doesn't hold the // previous code line yet, so we can't detect it. - EXPECT_EQ("#ifndef NOT_GUARD\n" - "code();\n" - "#define NOT_GUARD\n" - "code();\n" - "#endif", - format("#ifndef NOT_GUARD\n" - "code();\n" - "# define NOT_GUARD\n" - "code();\n" - "#endif", - Style)); + verifyFormat("#ifndef NOT_GUARD\n" + "code();\n" + "#define NOT_GUARD\n" + "code();\n" + "#endif", + "#ifndef NOT_GUARD\n" + "code();\n" + "# define NOT_GUARD\n" + "code();\n" + "#endif", + Style); // FIXME: This doesn't handle cases where legitimate preprocessor lines may // be outside an include guard. Examples are #pragma once and // #pragma GCC diagnostic, or anything else that does not change the meaning // of the file if it's included multiple times. - EXPECT_EQ("#ifdef WIN32\n" - "# pragma once\n" - "#endif\n" - "#ifndef HEADER_H\n" - "# define HEADER_H\n" - "code();\n" - "#endif", - format("#ifdef WIN32\n" - "# pragma once\n" - "#endif\n" - "#ifndef HEADER_H\n" - "#define HEADER_H\n" - "code();\n" - "#endif", - Style)); + verifyFormat("#ifdef WIN32\n" + "# pragma once\n" + "#endif\n" + "#ifndef HEADER_H\n" + "# define HEADER_H\n" + "code();\n" + "#endif", + "#ifdef WIN32\n" + "# pragma once\n" + "#endif\n" + "#ifndef HEADER_H\n" + "#define HEADER_H\n" + "code();\n" + "#endif", + Style); // FIXME: This does not detect when there is a single non-preprocessor line // in front of an include-guard-like structure where other conditions hold // because ScopedLineState hides the line. - EXPECT_EQ("code();\n" - "#ifndef HEADER_H\n" - "#define HEADER_H\n" - "code();\n" - "#endif", - format("code();\n" - "#ifndef HEADER_H\n" - "# define HEADER_H\n" - "code();\n" - "#endif", - Style)); + verifyFormat("code();\n" + "#ifndef HEADER_H\n" + "#define HEADER_H\n" + "code();\n" + "#endif", + "code();\n" + "#ifndef HEADER_H\n" + "# define HEADER_H\n" + "code();\n" + "#endif", + Style); // Keep comments aligned with #, otherwise indent comments normally. These // tests cannot use verifyFormat because messUp manipulates leading // whitespace. @@ -6168,7 +6091,7 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) { " // Code. Not aligned with #\n" "# define C 0\n" "#endif"; - EXPECT_EQ(Expected, format(ToFormat, Style)); + verifyFormat(Expected, ToFormat, Style); verifyNoChange(Expected, Style); } // Keep block quotes aligned. @@ -6195,7 +6118,7 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) { " /* Code. Not aligned with # */\n" "# define C 0\n" "#endif"; - EXPECT_EQ(Expected, format(ToFormat, Style)); + verifyFormat(Expected, ToFormat, Style); verifyNoChange(Expected, Style); } // Keep comments aligned with un-indented directives. @@ -6218,7 +6141,7 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) { "#define B 0\n" " // Code. Not aligned with #\n" "#define C 0\n"; - EXPECT_EQ(Expected, format(ToFormat, Style)); + verifyFormat(Expected, ToFormat, Style); verifyNoChange(Expected, Style); } // Test AfterHash with tabs. @@ -6305,7 +6228,7 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) { "#endif\n" "#endif\n" "}"; - EXPECT_EQ(Expected, format(ToFormat, Style)); + verifyFormat(Expected, ToFormat, Style); verifyNoChange(Expected, Style); } { @@ -6335,7 +6258,7 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) { "#endif\n" "#endif\n" "}"; - EXPECT_EQ(Expected, format(ToFormat, Style)); + verifyFormat(Expected, ToFormat, Style); verifyNoChange(Expected, Style); } @@ -6489,70 +6412,55 @@ TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { } TEST_F(FormatTest, FormatUnbalancedStructuralElements) { - EXPECT_EQ("#define A \\\n { \\\n {\nint i;", - format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); - EXPECT_EQ("#define A \\\n } \\\n }\nint i;", - format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); + verifyFormat("#define A \\\n { \\\n {\nint i;", + "#define A { {\nint i;", getLLVMStyleWithColumns(11)); + verifyFormat("#define A \\\n } \\\n }\nint i;", + "#define A } }\nint i;", getLLVMStyleWithColumns(11)); } TEST_F(FormatTest, EscapedNewlines) { FormatStyle Narrow = getLLVMStyleWithColumns(11); - EXPECT_EQ("#define A \\\n int i; \\\n int j;", - format("#define A \\\nint i;\\\n int j;", Narrow)); - EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;")); - EXPECT_EQ("template f();", format("\\\ntemplate f();")); - EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */")); + verifyFormat("#define A \\\n int i; \\\n int j;", + "#define A \\\nint i;\\\n int j;", Narrow); + verifyFormat("#define A\n\nint i;", "#define A \\\n\n int i;"); + verifyFormat("template f();", "\\\ntemplate f();"); + verifyFormat("/* \\ \\ \\\n */", "\\\n/* \\ \\ \\\n */"); verifyNoChange(""); FormatStyle AlignLeft = getLLVMStyle(); AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; - EXPECT_EQ("#define MACRO(x) \\\n" - "private: \\\n" - " int x(int a);", - format("#define MACRO(x) \\\n" - "private: \\\n" - " int x(int a);", - AlignLeft)); + verifyFormat("#define MACRO(x) \\\n" + "private: \\\n" + " int x(int a);", + AlignLeft); // CRLF line endings - EXPECT_EQ("#define A \\\r\n int i; \\\r\n int j;", - format("#define A \\\r\nint i;\\\r\n int j;", Narrow)); - EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;")); - EXPECT_EQ("template f();", format("\\\ntemplate f();")); - EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */")); + verifyFormat("#define A \\\r\n int i; \\\r\n int j;", + "#define A \\\r\nint i;\\\r\n int j;", Narrow); + verifyFormat("#define A\r\n\r\nint i;", "#define A \\\r\n\r\n int i;"); + verifyFormat("template f();", "\\\ntemplate f();"); + verifyFormat("/* \\ \\ \\\r\n */", "\\\r\n/* \\ \\ \\\r\n */"); verifyNoChange(""); - EXPECT_EQ("#define MACRO(x) \\\r\n" - "private: \\\r\n" - " int x(int a);", - format("#define MACRO(x) \\\r\n" - "private: \\\r\n" - " int x(int a);", - AlignLeft)); + verifyFormat("#define MACRO(x) \\\r\n" + "private: \\\r\n" + " int x(int a);", + AlignLeft); FormatStyle DontAlign = getLLVMStyle(); DontAlign.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; DontAlign.MaxEmptyLinesToKeep = 3; // FIXME: can't use verifyFormat here because the newline before // "public:" is not inserted the first time it's reformatted - EXPECT_EQ("#define A \\\n" - " class Foo { \\\n" - " void bar(); \\\n" - "\\\n" - "\\\n" - "\\\n" - " public: \\\n" - " void baz(); \\\n" - " };", - format("#define A \\\n" - " class Foo { \\\n" - " void bar(); \\\n" - "\\\n" - "\\\n" - "\\\n" - " public: \\\n" - " void baz(); \\\n" - " };", - DontAlign)); + verifyNoChange("#define A \\\n" + " class Foo { \\\n" + " void bar(); \\\n" + "\\\n" + "\\\n" + "\\\n" + " public: \\\n" + " void baz(); \\\n" + " };", + DontAlign); } TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { @@ -6564,26 +6472,25 @@ TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { } TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { - EXPECT_EQ( - "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" - " \\\n" - " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" - "\n" - "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" - " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);", - format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" - "\\\n" - "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" - " \n" - " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" - " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);")); + verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" + " \\\n" + " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" + "\n" + "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" + " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);", + " #define ALooooooooooooooooooooooooooooooooooooooongMacro(" + "\\\n" + "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" + " \n" + " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" + " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);"); } TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { - EXPECT_EQ("int\n" - "#define A\n" - " a;", - format("int\n#define A\na;")); + verifyFormat("int\n" + "#define A\n" + " a;", + "int\n#define A\na;"); verifyFormat("functionCallTo(\n" " someOtherFunction(\n" " withSomeParameters, whichInSequence,\n" @@ -6757,9 +6664,9 @@ TEST_F(FormatTest, LayoutBlockInsideParens) { } TEST_F(FormatTest, LayoutBlockInsideStatement) { - EXPECT_EQ("SOME_MACRO { int i; }\n" - "int i;", - format(" SOME_MACRO {int i;} int i;")); + verifyFormat("SOME_MACRO { int i; }\n" + "int i;", + " SOME_MACRO {int i;} int i;"); } TEST_F(FormatTest, LayoutNestedBlocks) { @@ -6785,37 +6692,37 @@ TEST_F(FormatTest, LayoutNestedBlocks) { " },\n" " a);"); - EXPECT_EQ("call(parameter, {\n" - " something();\n" - " // Comment too\n" - " // looooooooooong.\n" - " somethingElse();\n" - "});", - format("call(parameter, {\n" - " something();\n" - " // Comment too looooooooooong.\n" - " somethingElse();\n" - "});", - getLLVMStyleWithColumns(29))); - EXPECT_EQ("DEBUG({ int i; });", format("DEBUG({ int i; });")); - EXPECT_EQ("DEBUG({ // comment\n" - " int i;\n" - "});", - format("DEBUG({ // comment\n" - "int i;\n" - "});")); - EXPECT_EQ("DEBUG({\n" - " int i;\n" - "\n" - " // comment\n" - " int j;\n" - "});", - format("DEBUG({\n" - " int i;\n" - "\n" - " // comment\n" - " int j;\n" - "});")); + verifyFormat("call(parameter, {\n" + " something();\n" + " // Comment too\n" + " // looooooooooong.\n" + " somethingElse();\n" + "});", + "call(parameter, {\n" + " something();\n" + " // Comment too looooooooooong.\n" + " somethingElse();\n" + "});", + getLLVMStyleWithColumns(29)); + verifyFormat("DEBUG({ int i; });", "DEBUG({ int i; });"); + verifyFormat("DEBUG({ // comment\n" + " int i;\n" + "});", + "DEBUG({ // comment\n" + "int i;\n" + "});"); + verifyFormat("DEBUG({\n" + " int i;\n" + "\n" + " // comment\n" + " int j;\n" + "});", + "DEBUG({\n" + " int i;\n" + "\n" + " // comment\n" + " int j;\n" + "});"); verifyFormat("DEBUG({\n" " if (a)\n" @@ -6840,24 +6747,24 @@ TEST_F(FormatTest, LayoutNestedBlocks) { } TEST_F(FormatTest, FormatNestedBlocksInMacros) { - EXPECT_EQ("#define MACRO() \\\n" - " Debug(aaa, /* force line break */ \\\n" - " { \\\n" - " int i; \\\n" - " int j; \\\n" - " })", - format("#define MACRO() Debug(aaa, /* force line break */ \\\n" - " { int i; int j; })", - getGoogleStyle())); - - EXPECT_EQ("#define A \\\n" - " [] { \\\n" - " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" - " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" - " }", - format("#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" - "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", - getGoogleStyle())); + verifyFormat("#define MACRO() \\\n" + " Debug(aaa, /* force line break */ \\\n" + " { \\\n" + " int i; \\\n" + " int j; \\\n" + " })", + "#define MACRO() Debug(aaa, /* force line break */ \\\n" + " { int i; int j; })", + getGoogleStyle()); + + verifyFormat("#define A \\\n" + " [] { \\\n" + " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" + " xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n" + " }", + "#define A [] { xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx( \\\n" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); }", + getGoogleStyle()); } TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { @@ -6866,9 +6773,9 @@ TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) { verifyFormat("enum E {}"); FormatStyle Style = getLLVMStyle(); Style.SpaceInEmptyBlock = true; - EXPECT_EQ("void f() { }", format("void f() {}", Style)); + verifyFormat("void f() { }", "void f() {}", Style); Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Empty; - EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); + verifyFormat("while (true) { }", "while (true) {}", Style); Style.BreakBeforeBraces = FormatStyle::BS_Custom; Style.BraceWrapping.BeforeElse = false; Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_Always; @@ -7184,13 +7091,13 @@ TEST_F(FormatTest, ExpressionIndentationBreakingBeforeOperators) { Style.UseTab = FormatStyle::UT_Always; Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; Style.AlignOperands = FormatStyle::OAS_DontAlign; - EXPECT_EQ("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" - "\t&& (someOtherLongishConditionPart1\n" - "\t\t|| someOtherEvenLongerNestedConditionPart2);", - format("return someVeryVeryLongConditionThatBarelyFitsOnALine && " - "(someOtherLongishConditionPart1 || " - "someOtherEvenLongerNestedConditionPart2);", - Style)); + verifyFormat("return someVeryVeryLongConditionThatBarelyFitsOnALine\n" + "\t&& (someOtherLongishConditionPart1\n" + "\t\t|| someOtherEvenLongerNestedConditionPart2);", + "return someVeryVeryLongConditionThatBarelyFitsOnALine && " + "(someOtherLongishConditionPart1 || " + "someOtherEvenLongerNestedConditionPart2);", + Style); } TEST_F(FormatTest, ExpressionIndentationStrictAlign) { @@ -7559,12 +7466,12 @@ TEST_F(FormatTest, ConstructorInitializers) { " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", OnePerLine); - EXPECT_EQ("Constructor()\n" - " : // Comment forcing unwanted break.\n" - " aaaa(aaaa) {}", - format("Constructor() :\n" - " // Comment forcing unwanted break.\n" - " aaaa(aaaa) {}")); + verifyFormat("Constructor()\n" + " : // Comment forcing unwanted break.\n" + " aaaa(aaaa) {}", + "Constructor() :\n" + " // Comment forcing unwanted break.\n" + " aaaa(aaaa) {}"); } TEST_F(FormatTest, AllowAllConstructorInitializersOnNextLine) { @@ -8351,13 +8258,13 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) { TEST_F(FormatTest, DontBreakBeforeQualifiedOperator) { // Regression test for https://bugs.llvm.org/show_bug.cgi?id=40516: // Prefer keeping `::` followed by `operator` together. - EXPECT_EQ("const aaaa::bbbbbbb &\n" - "ccccccccc::operator++() {\n" - " stuff();\n" - "}", - format("const aaaa::bbbbbbb\n" - "&ccccccccc::operator++() { stuff(); }", - getLLVMStyleWithColumns(40))); + verifyFormat("const aaaa::bbbbbbb &\n" + "ccccccccc::operator++() {\n" + " stuff();\n" + "}", + "const aaaa::bbbbbbb\n" + "&ccccccccc::operator++() { stuff(); }", + getLLVMStyleWithColumns(40)); } TEST_F(FormatTest, TrailingReturnType) { @@ -8606,12 +8513,12 @@ TEST_F(FormatTest, BreaksDesireably) { " }\n }\n}"); // Break on an outer level if there was a break on an inner level. - EXPECT_EQ("f(g(h(a, // comment\n" - " b, c),\n" - " d, e),\n" - " x, y);", - format("f(g(h(a, // comment\n" - " b, c), d, e), x, y);")); + verifyFormat("f(g(h(a, // comment\n" + " b, c),\n" + " d, e),\n" + " x, y);", + "f(g(h(a, // comment\n" + " b, c), d, e), x, y);"); // Prefer breaking similar line breaks. verifyFormat( @@ -9411,7 +9318,7 @@ TEST_F(FormatTest, BreaksConditionalExpressions) { " ddddd;", Style); - EXPECT_EQ( + verifyFormat( "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" " /*\n" " */\n" @@ -9422,18 +9329,17 @@ TEST_F(FormatTest, BreaksConditionalExpressions) { " }\n" " } :\n" " function() {};", - format( - "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" - " /*\n" - " */\n" - " function() {\n" - " try {\n" - " return JJJJJJJJJJJJJJ(\n" - " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" - " }\n" - " } :\n" - " function() {};", - getGoogleStyle(FormatStyle::LK_JavaScript))); + "MMMMMMMMMMMMMMMMMMMMMMMMMMM = A ?\n" + " /*\n" + " */\n" + " function() {\n" + " try {\n" + " return JJJJJJJJJJJJJJ(\n" + " pppppppppppppppppppppppppppppppppppppppppppppppppp);\n" + " }\n" + " } :\n" + " function() {};", + getGoogleStyle(FormatStyle::LK_JavaScript)); } TEST_F(FormatTest, BreaksConditionalExpressionsAfterOperator) { @@ -9722,19 +9628,19 @@ TEST_F(FormatTest, AlignsStringLiterals) { verifyFormat("someFunction(\"Always break between multi-line\"\n" " \" string literals\",\n" " also, other, parameters);"); - EXPECT_EQ("fun + \"1243\" /* comment */\n" - " \"5678\";", - format("fun + \"1243\" /* comment */\n" - " \"5678\";", - getLLVMStyleWithColumns(28))); - EXPECT_EQ( + verifyFormat("fun + \"1243\" /* comment */\n" + " \"5678\";", + "fun + \"1243\" /* comment */\n" + " \"5678\";", + getLLVMStyleWithColumns(28)); + verifyFormat( "aaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" " \"aaaaaaaaaaaaaaaaaaaaa\"\n" " \"aaaaaaaaaaaaaaaa\";", - format("aaaaaa =" - "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " - "aaaaaaaaaaaaaaaaaaaaa\" " - "\"aaaaaaaaaaaaaaaa\";")); + "aaaaaa =" + "\"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaa\" " + "\"aaaaaaaaaaaaaaaa\";"); verifyFormat("a = a + \"a\"\n" " \"a\"\n" " \"a\";"); @@ -10152,28 +10058,25 @@ TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) { Break); // Treat literals with escaped newlines like multi-line string literals. - EXPECT_EQ("x = \"a\\\n" - "b\\\n" - "c\";", - format("x = \"a\\\n" - "b\\\n" - "c\";", - NoBreak)); - EXPECT_EQ("xxxx =\n" - " \"a\\\n" - "b\\\n" - "c\";", - format("xxxx = \"a\\\n" - "b\\\n" - "c\";", - Break)); - - EXPECT_EQ("NSString *const kString =\n" - " @\"aaaa\"\n" - " @\"bbbb\";", - format("NSString *const kString = @\"aaaa\"\n" - "@\"bbbb\";", - Break)); + verifyNoChange("x = \"a\\\n" + "b\\\n" + "c\";", + NoBreak); + verifyFormat("xxxx =\n" + " \"a\\\n" + "b\\\n" + "c\";", + "xxxx = \"a\\\n" + "b\\\n" + "c\";", + Break); + + verifyFormat("NSString *const kString =\n" + " @\"aaaa\"\n" + " @\"bbbb\";", + "NSString *const kString = @\"aaaa\"\n" + "@\"bbbb\";", + Break); Break.ColumnLimit = 0; verifyFormat("const char *hello = \"hello llvm\";", Break); @@ -10245,9 +10148,9 @@ TEST_F(FormatTest, AlignsPipes) { " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); // Incomplete string literal. - EXPECT_EQ("llvm::errs() << \"\n" - " << a;", - format("llvm::errs() << \"\n<cccccc)\n" @@ -10495,14 +10398,14 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>(\n" " bbbbbbbbbbbbbbbbbbbbbbbb);", getLLVMStyleWithColumns(72)); - EXPECT_EQ("static_cast *>(\n" - "\n" - ");", - format("static_cast*>(\n" - "\n" - " );")); + verifyFormat("static_cast *>(\n" + "\n" + ");", + "static_cast*>(\n" + "\n" + " );"); verifyFormat("int aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " const typename aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaa);"); @@ -10571,52 +10474,46 @@ TEST_F(FormatTest, WrapsTemplateDeclarations) { TEST_F(FormatTest, WrapsTemplateDeclarationsWithComments) { FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp); Style.ColumnLimit = 60; - EXPECT_EQ("// Baseline - no comments.\n" - "template <\n" - " typename aaaaaaaaaaaaaaaaaaaaaa::value>\n" - "void f() {}", - format("// Baseline - no comments.\n" - "template <\n" - " typename aaaaaaaaaaaaaaaaaaaaaa::value>\n" - "void f() {}", - Style)); + verifyFormat("// Baseline - no comments.\n" + "template <\n" + " typename aaaaaaaaaaaaaaaaaaaaaa::value>\n" + "void f() {}", + Style); - EXPECT_EQ("template <\n" - " typename aaaaaaaaaa::value> // trailing\n" - "void f() {}", - format("template <\n" - " typename aaaaaaaaaa::value> // trailing\n" - "void f() {}", - Style)); + verifyFormat("template <\n" + " typename aaaaaaaaaa::value> // trailing\n" + "void f() {}", + "template <\n" + " typename aaaaaaaaaa::value> // trailing\n" + "void f() {}", + Style); - EXPECT_EQ( + verifyFormat( "template <\n" " typename aaaaaaaaaa::value> /* line */\n" "void f() {}", - format("template ::value> /* line */\n" - "void f() {}", - Style)); - - EXPECT_EQ( - "template <\n" - " typename aaaaaaaaaa::value> // trailing\n" - " // multiline\n" + "template ::value> /* line */\n" "void f() {}", - format("template <\n" - " typename aaaaaaaaaa::value> // trailing\n" - " // multiline\n" - "void f() {}", - Style)); + Style); - EXPECT_EQ( + verifyFormat("template <\n" + " typename aaaaaaaaaa::value> // trailing\n" + " // multiline\n" + "void f() {}", + "template <\n" + " typename aaaaaaaaaa::value> // trailing\n" + " // multiline\n" + "void f() {}", + Style); + + verifyFormat( "template ::value> // trailing loooong\n" "void f() {}", - format( - "template <\n" - " typename aaaaaaaaaa::value> // trailing loooong\n" - "void f() {}", - Style)); + "template <\n" + " typename aaaaaaaaaa::value> // trailing loooong\n" + "void f() {}", + Style); } TEST_F(FormatTest, WrapsTemplateParameters) { @@ -10715,12 +10612,12 @@ TEST_F(FormatTest, UnderstandsTemplateParameters) { verifyGoogleFormat("A<::A> a;"); verifyGoogleFormat("A< ::A> a;"); verifyGoogleFormat("A< ::A > a;"); - EXPECT_EQ("A>> a;", format("A >> a;", getGoogleStyle())); - EXPECT_EQ("A>> a;", format("A> > a;", getGoogleStyle())); - EXPECT_EQ("A<::A> a;", format("A< ::A> a;", getGoogleStyle())); - EXPECT_EQ("A<::A> a;", format("A<::A > a;", getGoogleStyle())); - EXPECT_EQ("auto x = [] { A>> a; };", - format("auto x=[]{A >> a;};", getGoogleStyle())); + verifyFormat("A>> a;", "A >> a;", getGoogleStyle()); + verifyFormat("A>> a;", "A> > a;", getGoogleStyle()); + verifyFormat("A<::A> a;", "A< ::A> a;", getGoogleStyle()); + verifyFormat("A<::A> a;", "A<::A > a;", getGoogleStyle()); + verifyFormat("auto x = [] { A>> a; };", "auto x=[]{A >> a;};", + getGoogleStyle()); verifyFormat("A> a;", getChromiumStyle(FormatStyle::LK_Cpp)); @@ -10793,15 +10690,15 @@ TEST_F(FormatTest, UnderstandsShiftOperators) { } TEST_F(FormatTest, BitshiftOperatorWidth) { - EXPECT_EQ("int a = 1 << 2; /* foo\n" - " bar */", - format("int a=1<<2; /* foo\n" - " bar */")); + verifyFormat("int a = 1 << 2; /* foo\n" + " bar */", + "int a=1<<2; /* foo\n" + " bar */"); - EXPECT_EQ("int b = 256 >> 1; /* foo\n" - " bar */", - format("int b =256>>1 ; /* foo\n" - " bar */")); + verifyFormat("int b = 256 >> 1; /* foo\n" + " bar */", + "int b =256>>1 ; /* foo\n" + " bar */"); } TEST_F(FormatTest, UnderstandsBinaryOperators) { @@ -11250,10 +11147,10 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) { verifyFormat("void operator delete[](void *foo) ATTRIB;"); verifyFormat("void operator delete(void *ptr) noexcept;"); - EXPECT_EQ("void new(link p);\n" - "void delete(link p);", - format("void new (link p);\n" - "void delete (link p);")); + verifyFormat("void new(link p);\n" + "void delete(link p);", + "void new (link p);\n" + "void delete (link p);"); } TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { @@ -11641,15 +11538,15 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { verifyFormat("DatumHandle const *operator->() const { return input_; }"); verifyFormat("return options != nullptr && operator==(*options);"); - EXPECT_EQ("#define OP(x) \\\n" - " ostream &operator<<(ostream &s, const A &a) { \\\n" - " return s << a.DebugString(); \\\n" - " }", - format("#define OP(x) \\\n" - " ostream &operator<<(ostream &s, const A &a) { \\\n" - " return s << a.DebugString(); \\\n" - " }", - getLLVMStyleWithColumns(50))); + verifyFormat("#define OP(x) \\\n" + " ostream &operator<<(ostream &s, const A &a) { \\\n" + " return s << a.DebugString(); \\\n" + " }", + "#define OP(x) \\\n" + " ostream &operator<<(ostream &s, const A &a) { \\\n" + " return s << a.DebugString(); \\\n" + " }", + getLLVMStyleWithColumns(50)); verifyFormat("#define FOO \\\n" " void foo() { \\\n" @@ -11876,23 +11773,15 @@ TEST_F(FormatTest, AttributesAfterMacro) { " //...\n" "}"); - EXPECT_EQ("MACRO\n\n" - "__attribute__((maybe_unused)) int foo() {\n" - " //...\n" - "}", - format("MACRO\n\n" - "__attribute__((maybe_unused)) int foo() {\n" - " //...\n" - "}")); - - EXPECT_EQ("MACRO\n\n" - "[[nodiscard]] int foo() {\n" - " //...\n" - "}", - format("MACRO\n\n" - "[[nodiscard]] int foo() {\n" - " //...\n" - "}")); + verifyNoChange("MACRO\n\n" + "__attribute__((maybe_unused)) int foo() {\n" + " //...\n" + "}"); + + verifyNoChange("MACRO\n\n" + "[[nodiscard]] int foo() {\n" + " //...\n" + "}"); } TEST_F(FormatTest, AttributePenaltyBreaking) { @@ -11926,36 +11815,36 @@ TEST_F(FormatTest, UnderstandsEllipsis) { } TEST_F(FormatTest, AdaptivelyFormatsPointersAndReferences) { - EXPECT_EQ("int *a;\n" - "int *a;\n" - "int *a;", - format("int *a;\n" - "int* a;\n" - "int *a;", - getGoogleStyle())); - EXPECT_EQ("int* a;\n" - "int* a;\n" - "int* a;", - format("int* a;\n" - "int* a;\n" - "int *a;", - getGoogleStyle())); - EXPECT_EQ("int *a;\n" - "int *a;\n" - "int *a;", - format("int *a;\n" - "int * a;\n" - "int * a;", - getGoogleStyle())); - EXPECT_EQ("auto x = [] {\n" - " int *a;\n" - " int *a;\n" - " int *a;\n" - "};", - format("auto x=[]{int *a;\n" - "int * a;\n" - "int * a;};", - getGoogleStyle())); + verifyFormat("int *a;\n" + "int *a;\n" + "int *a;", + "int *a;\n" + "int* a;\n" + "int *a;", + getGoogleStyle()); + verifyFormat("int* a;\n" + "int* a;\n" + "int* a;", + "int* a;\n" + "int* a;\n" + "int *a;", + getGoogleStyle()); + verifyFormat("int *a;\n" + "int *a;\n" + "int *a;", + "int *a;\n" + "int * a;\n" + "int * a;", + getGoogleStyle()); + verifyFormat("auto x = [] {\n" + " int *a;\n" + " int *a;\n" + " int *a;\n" + "};", + "auto x=[]{int *a;\n" + "int * a;\n" + "int * a;};", + getGoogleStyle()); } TEST_F(FormatTest, UnderstandsRvalueReferences) { @@ -12474,29 +12363,18 @@ TEST_F(FormatTest, FormatsAccessModifiers) { "};", Style); Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; - EXPECT_EQ("struct foo {\n" - "\n" - "private:\n" - " void f() {}\n" - "\n" - "private:\n" - " int i;\n" - "\n" - "protected:\n" - " int j;\n" - "};", - format("struct foo {\n" - "\n" - "private:\n" - " void f() {}\n" - "\n" - "private:\n" - " int i;\n" - "\n" - "protected:\n" - " int j;\n" - "};", - Style)); + verifyNoChange("struct foo {\n" + "\n" + "private:\n" + " void f() {}\n" + "\n" + "private:\n" + " int i;\n" + "\n" + "protected:\n" + " int j;\n" + "};", + Style); verifyFormat("struct foo {\n" "private:\n" " void f() {}\n" @@ -12506,25 +12384,16 @@ TEST_F(FormatTest, FormatsAccessModifiers) { " int j;\n" "};", Style); - EXPECT_EQ("struct foo { /* comment */\n" - "\n" - "private:\n" - " int i;\n" - " // comment\n" - "\n" - "private:\n" - " int j;\n" - "};", - format("struct foo { /* comment */\n" - "\n" - "private:\n" - " int i;\n" - " // comment\n" - "\n" - "private:\n" - " int j;\n" - "};", - Style)); + verifyNoChange("struct foo { /* comment */\n" + "\n" + "private:\n" + " int i;\n" + " // comment\n" + "\n" + "private:\n" + " int j;\n" + "};", + Style); verifyFormat("struct foo { /* comment */\n" "private:\n" " int i;\n" @@ -12533,31 +12402,19 @@ TEST_F(FormatTest, FormatsAccessModifiers) { " int j;\n" "};", Style); - EXPECT_EQ("struct foo {\n" - "#ifdef FOO\n" - "#endif\n" - "\n" - "private:\n" - " int i;\n" - "#ifdef FOO\n" - "\n" - "private:\n" - "#endif\n" - " int j;\n" - "};", - format("struct foo {\n" - "#ifdef FOO\n" - "#endif\n" - "\n" - "private:\n" - " int i;\n" - "#ifdef FOO\n" - "\n" - "private:\n" - "#endif\n" - " int j;\n" - "};", - Style)); + verifyNoChange("struct foo {\n" + "#ifdef FOO\n" + "#endif\n" + "\n" + "private:\n" + " int i;\n" + "#ifdef FOO\n" + "\n" + "private:\n" + "#endif\n" + " int j;\n" + "};", + Style); verifyFormat("struct foo {\n" "#ifdef FOO\n" "#endif\n" @@ -12713,138 +12570,102 @@ TEST_F(FormatTest, FormatsAfterAccessModifiers) { Style); // Check if MaxEmptyLinesToKeep is respected. - EXPECT_EQ("struct foo {\n" - "private:\n" - " void f() {}\n" - "\n" - "private:\n" - " int i;\n" - "\n" - "protected:\n" - " int j;\n" - "};", - format("struct foo {\n" - "private:\n" - "\n\n\n" - " void f() {}\n" - "\n" - "private:\n" - "\n\n\n" - " int i;\n" - "\n" - "protected:\n" - "\n\n\n" - " int j;\n" - "};", - Style)); + verifyFormat("struct foo {\n" + "private:\n" + " void f() {}\n" + "\n" + "private:\n" + " int i;\n" + "\n" + "protected:\n" + " int j;\n" + "};", + "struct foo {\n" + "private:\n" + "\n\n\n" + " void f() {}\n" + "\n" + "private:\n" + "\n\n\n" + " int i;\n" + "\n" + "protected:\n" + "\n\n\n" + " int j;\n" + "};", + Style); Style.MaxEmptyLinesToKeep = 1u; - EXPECT_EQ("struct foo {\n" - "private:\n" - "\n" - " void f() {}\n" - "\n" - "private:\n" - "\n" - " int i;\n" - "\n" - "protected:\n" - "\n" - " int j;\n" - "};", - format("struct foo {\n" - "private:\n" - "\n" - " void f() {}\n" - "\n" - "private:\n" - "\n" - " int i;\n" - "\n" - "protected:\n" - "\n" - " int j;\n" - "};", - Style)); + verifyNoChange("struct foo {\n" + "private:\n" + "\n" + " void f() {}\n" + "\n" + "private:\n" + "\n" + " int i;\n" + "\n" + "protected:\n" + "\n" + " int j;\n" + "};", + Style); // Check if no lines are kept. - EXPECT_EQ("struct foo {\n" - "private:\n" - " void f() {}\n" - "\n" - "private:\n" - " int i;\n" - "\n" - "protected:\n" - " int j;\n" - "};", - format("struct foo {\n" - "private:\n" - " void f() {}\n" - "\n" - "private:\n" - " int i;\n" - "\n" - "protected:\n" - " int j;\n" - "};", - Style)); + verifyFormat("struct foo {\n" + "private:\n" + " void f() {}\n" + "\n" + "private:\n" + " int i;\n" + "\n" + "protected:\n" + " int j;\n" + "};", + Style); // Check if MaxEmptyLinesToKeep is respected. - EXPECT_EQ("struct foo {\n" - "private:\n" - "\n" - " void f() {}\n" - "\n" - "private:\n" - "\n" - " int i;\n" - "\n" - "protected:\n" - "\n" - " int j;\n" - "};", - format("struct foo {\n" - "private:\n" - "\n\n\n" - " void f() {}\n" - "\n" - "private:\n" - "\n\n\n" - " int i;\n" - "\n" - "protected:\n" - "\n\n\n" - " int j;\n" - "};", - Style)); + verifyFormat("struct foo {\n" + "private:\n" + "\n" + " void f() {}\n" + "\n" + "private:\n" + "\n" + " int i;\n" + "\n" + "protected:\n" + "\n" + " int j;\n" + "};", + "struct foo {\n" + "private:\n" + "\n\n\n" + " void f() {}\n" + "\n" + "private:\n" + "\n\n\n" + " int i;\n" + "\n" + "protected:\n" + "\n\n\n" + " int j;\n" + "};", + Style); Style.MaxEmptyLinesToKeep = 10u; - EXPECT_EQ("struct foo {\n" - "private:\n" - "\n\n\n" - " void f() {}\n" - "\n" - "private:\n" - "\n\n\n" - " int i;\n" - "\n" - "protected:\n" - "\n\n\n" - " int j;\n" - "};", - format("struct foo {\n" - "private:\n" - "\n\n\n" - " void f() {}\n" - "\n" - "private:\n" - "\n\n\n" - " int i;\n" - "\n" - "protected:\n" - "\n\n\n" - " int j;\n" - "};", - Style)); + verifyNoChange("struct foo {\n" + "private:\n" + "\n\n\n" + " void f() {}\n" + "\n" + "private:\n" + "\n\n\n" + " int i;\n" + "\n" + "protected:\n" + "\n\n\n" + " int j;\n" + "};", + Style); // Test with comments. Style = getLLVMStyle(); @@ -13048,89 +12869,59 @@ TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; Style.MaxEmptyLinesToKeep = 10u; - EXPECT_EQ("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - format("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - Style)); + verifyNoChange("struct foo {\n" + "private:\n" + "\n\n\n" + "protected:\n" + "};", + Style); Style.MaxEmptyLinesToKeep = 3u; - EXPECT_EQ("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - format("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - Style)); + verifyNoChange("struct foo {\n" + "private:\n" + "\n\n\n" + "protected:\n" + "};", + Style); Style.MaxEmptyLinesToKeep = 1u; - EXPECT_EQ("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - format("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - Style)); // Based on new lines in original document and not - // on the setting. + verifyNoChange("struct foo {\n" + "private:\n" + "\n\n\n" + "protected:\n" + "};", + Style); // Based on new lines in original document and not + // on the setting. Style.MaxEmptyLinesToKeep = 10u; Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; // Newlines are kept if they are greater than zero, // test::messUp removes all new lines which changes the logic - EXPECT_EQ("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - format("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - Style)); + verifyNoChange("struct foo {\n" + "private:\n" + "\n\n\n" + "protected:\n" + "};", + Style); Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; // test::messUp removes all new lines which changes the logic - EXPECT_EQ("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - format("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - Style)); + verifyNoChange("struct foo {\n" + "private:\n" + "\n\n\n" + "protected:\n" + "};", + Style); Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Leave; Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; - EXPECT_EQ("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - format("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - Style)); // test::messUp removes all new lines which changes - // the logic. + verifyNoChange("struct foo {\n" + "private:\n" + "\n\n\n" + "protected:\n" + "};", + Style); // test::messUp removes all new lines which changes + // the logic. Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Leave; @@ -13147,18 +12938,13 @@ TEST_F(FormatTest, FormatsAfterAndBeforeAccessModifiersInteraction) { Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Always; Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Never; - EXPECT_EQ("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - format("struct foo {\n" - "private:\n" - "\n\n\n" - "protected:\n" - "};", - Style)); // test::messUp removes all new lines which changes - // the logic. + verifyNoChange("struct foo {\n" + "private:\n" + "\n\n\n" + "protected:\n" + "};", + Style); // test::messUp removes all new lines which changes + // the logic. Style.EmptyLineBeforeAccessModifier = FormatStyle::ELBAMS_Never; Style.EmptyLineAfterAccessModifier = FormatStyle::ELAAMS_Always; @@ -13267,8 +13053,8 @@ TEST_F(FormatTest, HandlesIncludeDirectives) { "#include \"some very long include path\"\n" "#include ", getLLVMStyleWithColumns(35)); - EXPECT_EQ("#include \"a.h\"", format("#include \"a.h\"")); - EXPECT_EQ("#include ", format("#include")); + verifyFormat("#include \"a.h\"", "#include \"a.h\""); + verifyFormat("#include ", "#include"); verifyFormat("#import "); verifyFormat("#import "); @@ -13334,21 +13120,21 @@ TEST_F(FormatTest, IncorrectCodeTrailingStuff) { } TEST_F(FormatTest, IncorrectCodeMissingSemicolon) { - EXPECT_EQ("void f() { return }", format("void f ( ) { return }")); - EXPECT_EQ("void f() {\n" - " if (a)\n" - " return\n" - "}", - format("void f ( ) { if ( a ) return }")); - EXPECT_EQ("namespace N {\n" - "void f()\n" - "}", - format("namespace N { void f() }")); - EXPECT_EQ("namespace N {\n" - "void f() {}\n" - "void g()\n" - "} // namespace N", - format("namespace N { void f( ) { } void g( ) }")); + verifyFormat("void f() { return }", "void f ( ) { return }"); + verifyFormat("void f() {\n" + " if (a)\n" + " return\n" + "}", + "void f ( ) { if ( a ) return }"); + verifyFormat("namespace N {\n" + "void f()\n" + "}", + "namespace N { void f() }"); + verifyFormat("namespace N {\n" + "void f() {}\n" + "void g()\n" + "} // namespace N", + "namespace N { void f( ) { } void g( ) }"); } TEST_F(FormatTest, IndentationWithinColumnLimitNotPossible) { @@ -13424,21 +13210,21 @@ TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { } TEST_F(FormatTest, IncorrectCodeErrorDetection) { - EXPECT_EQ("{\n {}", format("{\n{\n}")); - EXPECT_EQ("{\n {}", format("{\n {\n}")); - EXPECT_EQ("{\n {}", format("{\n {\n }")); - EXPECT_EQ("{\n {}\n}\n}", format("{\n {\n }\n }\n}")); - - EXPECT_EQ("{\n" - " {\n" - " breakme(\n" - " qwe);\n" - " }", - format("{\n" - " {\n" - " breakme(qwe);\n" - "}", - getLLVMStyleWithColumns(10))); + verifyFormat("{\n {}", "{\n{\n}"); + verifyFormat("{\n {}", "{\n {\n}"); + verifyFormat("{\n {}", "{\n {\n }"); + verifyFormat("{\n {}\n}\n}", "{\n {\n }\n }\n}"); + + verifyFormat("{\n" + " {\n" + " breakme(\n" + " qwe);\n" + " }", + "{\n" + " {\n" + " breakme(qwe);\n" + "}", + getLLVMStyleWithColumns(10)); } TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { @@ -13590,14 +13376,14 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { NoBinPacking); NoBinPacking.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; - EXPECT_EQ("static uint8 CddDp83848Reg[] = {\n" - " CDDDP83848_BMCR_REGISTER,\n" - " CDDDP83848_BMSR_REGISTER,\n" - " CDDDP83848_RBR_REGISTER};", - format("static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" - " CDDDP83848_BMSR_REGISTER,\n" - " CDDDP83848_RBR_REGISTER};", - NoBinPacking)); + verifyFormat("static uint8 CddDp83848Reg[] = {\n" + " CDDDP83848_BMCR_REGISTER,\n" + " CDDDP83848_BMSR_REGISTER,\n" + " CDDDP83848_RBR_REGISTER};", + "static uint8 CddDp83848Reg[] = {CDDDP83848_BMCR_REGISTER,\n" + " CDDDP83848_BMSR_REGISTER,\n" + " CDDDP83848_RBR_REGISTER};", + NoBinPacking); // FIXME: The alignment of these trailing comments might be bad. Then again, // this might be utterly useless in real code. @@ -13609,34 +13395,34 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { // In braced lists, the first comment is always assumed to belong to the // first element. Thus, it can be moved to the next or previous line as // appropriate. - EXPECT_EQ("function({// First element:\n" - " 1,\n" - " // Second element:\n" - " 2});", - format("function({\n" - " // First element:\n" - " 1,\n" - " // Second element:\n" - " 2});")); - EXPECT_EQ("std::vector MyNumbers{\n" - " // First element:\n" - " 1,\n" - " // Second element:\n" - " 2};", - format("std::vector MyNumbers{// First element:\n" - " 1,\n" - " // Second element:\n" - " 2};", - getLLVMStyleWithColumns(30))); + verifyFormat("function({// First element:\n" + " 1,\n" + " // Second element:\n" + " 2});", + "function({\n" + " // First element:\n" + " 1,\n" + " // Second element:\n" + " 2});"); + verifyFormat("std::vector MyNumbers{\n" + " // First element:\n" + " 1,\n" + " // Second element:\n" + " 2};", + "std::vector MyNumbers{// First element:\n" + " 1,\n" + " // Second element:\n" + " 2};", + getLLVMStyleWithColumns(30)); // A trailing comma should still lead to an enforced line break and no // binpacking. - EXPECT_EQ("vector SomeVector = {\n" - " // aaa\n" - " 1,\n" - " 2,\n" - "};", - format("vector SomeVector = { // aaa\n" - " 1, 2, };")); + verifyFormat("vector SomeVector = {\n" + " // aaa\n" + " 1,\n" + " 2,\n" + "};", + "vector SomeVector = { // aaa\n" + " 1, 2, };"); // C++11 brace initializer list l-braces should not be treated any differently // when breaking before lambda bodies is enabled @@ -13737,20 +13523,20 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) { " 1, 2, 3, 4 };", SpaceBetweenBraces); SpaceBetweenBraces.ColumnLimit = 20; - EXPECT_EQ("vector< int > x{\n" - " 1, 2, 3, 4 };", - format("vectorx{1,2,3,4};", SpaceBetweenBraces)); + verifyFormat("vector< int > x{\n" + " 1, 2, 3, 4 };", + "vectorx{1,2,3,4};", SpaceBetweenBraces); SpaceBetweenBraces.ColumnLimit = 24; - EXPECT_EQ("vector< int > x{ 1, 2,\n" - " 3, 4 };", - format("vectorx{1,2,3,4};", SpaceBetweenBraces)); - EXPECT_EQ("vector< int > x{\n" - " 1,\n" - " 2,\n" - " 3,\n" - " 4,\n" - "};", - format("vectorx{1,2,3,4,};", SpaceBetweenBraces)); + verifyFormat("vector< int > x{ 1, 2,\n" + " 3, 4 };", + "vectorx{1,2,3,4};", SpaceBetweenBraces); + verifyFormat("vector< int > x{\n" + " 1,\n" + " 2,\n" + " 3,\n" + " 4,\n" + "};", + "vectorx{1,2,3,4,};", SpaceBetweenBraces); verifyFormat("vector< int > x{};", SpaceBetweenBraces); SpaceBetweenBraces.SpacesInParens = FormatStyle::SIPO_Custom; SpaceBetweenBraces.SpacesInParensOptions.InEmptyParentheses = true; @@ -13979,15 +13765,15 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { "};"); FormatStyle NoColumnLimit = getLLVMStyleWithColumns(0); - EXPECT_EQ("A() : b(0) {}", format("A():b(0){}", NoColumnLimit)); - EXPECT_EQ("class C {\n" - " A() : b(0) {}\n" - "};", - format("class C{A():b(0){}};", NoColumnLimit)); - EXPECT_EQ("A()\n" - " : b(0) {\n" - "}", - format("A()\n:b(0)\n{\n}", NoColumnLimit)); + verifyFormat("A() : b(0) {}", "A():b(0){}", NoColumnLimit); + verifyFormat("class C {\n" + " A() : b(0) {}\n" + "};", + "class C{A():b(0){}};", NoColumnLimit); + verifyFormat("A()\n" + " : b(0) {\n" + "}", + "A()\n:b(0)\n{\n}", NoColumnLimit); FormatStyle NoColumnLimitWrapAfterFunction = NoColumnLimit; NoColumnLimitWrapAfterFunction.BreakBeforeBraces = FormatStyle::BS_Custom; @@ -14006,14 +13792,14 @@ TEST_F(FormatTest, PullTrivialFunctionDefinitionsIntoSingleLine) { FormatStyle DoNotMergeNoColumnLimit = NoColumnLimit; DoNotMergeNoColumnLimit.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_None; - EXPECT_EQ("A()\n" - " : b(0) {\n" - "}", - format("A():b(0){}", DoNotMergeNoColumnLimit)); - EXPECT_EQ("A()\n" - " : b(0) {\n" - "}", - format("A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit)); + verifyFormat("A()\n" + " : b(0) {\n" + "}", + "A():b(0){}", DoNotMergeNoColumnLimit); + verifyFormat("A()\n" + " : b(0) {\n" + "}", + "A()\n:b(0)\n{\n}", DoNotMergeNoColumnLimit); verifyFormat("#define A \\\n" " void f() { \\\n" @@ -14642,10 +14428,9 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { verifyNoChange("#error Leave all white!!!!! space* alone!"); - EXPECT_EQ("#warning Leave all white!!!!! space* alone!", - format("#warning Leave all white!!!!! space* alone!")); - EXPECT_EQ("#error 1", format(" # error 1")); - EXPECT_EQ("#warning 1", format(" # warning 1")); + verifyNoChange("#warning Leave all white!!!!! space* alone!"); + verifyFormat("#error 1", " # error 1"); + verifyFormat("#warning 1", " # warning 1"); } TEST_F(FormatTest, FormatHashIfExpressions) { @@ -14669,8 +14454,7 @@ TEST_F(FormatTest, MergeHandlingInTheFaceOfPreprocessorDirectives) { verifyFormat("void f() { f(); }\n#error E", AllowsMergedIf); verifyFormat("if (true) return 42;\n#error E", AllowsMergedIf); verifyFormat("if (true)\n#error E\n return 42;", AllowsMergedIf); - EXPECT_EQ("if (true) return 42;", - format("if (true)\nreturn 42;", AllowsMergedIf)); + verifyFormat("if (true) return 42;", "if (true)\nreturn 42;", AllowsMergedIf); FormatStyle ShortMergedIf = AllowsMergedIf; ShortMergedIf.ColumnLimit = 25; verifyFormat("#define A \\\n" @@ -14781,21 +14565,19 @@ TEST_F(FormatTest, SkipsDeeplyNestedLines) { TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); - EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", - format("-(NSUInteger)indexOfObject:(id)anObject;")); - EXPECT_EQ("- (NSInteger)Mthod1;", format("-(NSInteger)Mthod1;")); - EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); - EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", - format("-(NSInteger)Method3:(id)anObject;")); - EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", - format("-(NSInteger)Method4:(id)anObject;")); - EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", - format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); + verifyFormat("- (NSUInteger)indexOfObject:(id)anObject;", + "-(NSUInteger)indexOfObject:(id)anObject;"); + verifyFormat("- (NSInteger)Mthod1;", "-(NSInteger)Mthod1;"); + verifyFormat("+ (id)Mthod2;", "+(id)Mthod2;"); + verifyFormat("- (NSInteger)Method3:(id)anObject;", + "-(NSInteger)Method3:(id)anObject;"); + verifyFormat("- (NSInteger)Method4:(id)anObject;", + "-(NSInteger)Method4:(id)anObject;"); + verifyFormat("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", + "-(NSInteger)Method5:(id)anObject:(id)AnotherObject;"); verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;"); - EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject " - "forAllCells:(BOOL)flag;", - format("- (void)sendAction:(SEL)aSelector to:(id)anObject " - "forAllCells:(BOOL)flag;")); + verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject " + "forAllCells:(BOOL)flag;"); // Very long objectiveC method declaration. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" @@ -14847,87 +14629,91 @@ TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { } TEST_F(FormatTest, BreaksStringLiterals) { + // FIXME: unstable test case EXPECT_EQ("\"some text \"\n" "\"other\";", format("\"some text other\";", getLLVMStyleWithColumns(12))); + // FIXME: unstable test case EXPECT_EQ("\"some text \"\n" "\"other\";", format("\\\n\"some text other\";", getLLVMStyleWithColumns(12))); - EXPECT_EQ( - "#define A \\\n" - " \"some \" \\\n" - " \"text \" \\\n" - " \"other\";", - format("#define A \"some text other\";", getLLVMStyleWithColumns(12))); - EXPECT_EQ( - "#define A \\\n" - " \"so \" \\\n" - " \"text \" \\\n" - " \"other\";", - format("#define A \"so text other\";", getLLVMStyleWithColumns(12))); + verifyFormat("#define A \\\n" + " \"some \" \\\n" + " \"text \" \\\n" + " \"other\";", + "#define A \"some text other\";", getLLVMStyleWithColumns(12)); + verifyFormat("#define A \\\n" + " \"so \" \\\n" + " \"text \" \\\n" + " \"other\";", + "#define A \"so text other\";", getLLVMStyleWithColumns(12)); verifyFormat("\"some text\"", getLLVMStyleWithColumns(1)); verifyFormat("\"some text\"", getLLVMStyleWithColumns(11)); + // FIXME: unstable test case EXPECT_EQ("\"some \"\n" "\"text\"", format("\"some text\"", getLLVMStyleWithColumns(10))); + // FIXME: unstable test case EXPECT_EQ("\"some \"\n" "\"text\"", format("\"some text\"", getLLVMStyleWithColumns(7))); + // FIXME: unstable test case EXPECT_EQ("\"some\"\n" "\" tex\"\n" "\"t\"", format("\"some text\"", getLLVMStyleWithColumns(6))); + // FIXME: unstable test case EXPECT_EQ("\"some\"\n" "\" tex\"\n" "\" and\"", format("\"some tex and\"", getLLVMStyleWithColumns(6))); + // FIXME: unstable test case EXPECT_EQ("\"some\"\n" "\"/tex\"\n" "\"/and\"", format("\"some/tex/and\"", getLLVMStyleWithColumns(6))); - EXPECT_EQ("variable =\n" - " \"long string \"\n" - " \"literal\";", - format("variable = \"long string literal\";", - getLLVMStyleWithColumns(20))); + verifyFormat("variable =\n" + " \"long string \"\n" + " \"literal\";", + "variable = \"long string literal\";", + getLLVMStyleWithColumns(20)); - EXPECT_EQ("variable = f(\n" - " \"long string \"\n" - " \"literal\",\n" - " short,\n" - " loooooooooooooooooooong);", - format("variable = f(\"long string literal\", short, " - "loooooooooooooooooooong);", - getLLVMStyleWithColumns(20))); + verifyFormat("variable = f(\n" + " \"long string \"\n" + " \"literal\",\n" + " short,\n" + " loooooooooooooooooooong);", + "variable = f(\"long string literal\", short, " + "loooooooooooooooooooong);", + getLLVMStyleWithColumns(20)); - EXPECT_EQ( - "f(g(\"long string \"\n" - " \"literal\"),\n" - " b);", - format("f(g(\"long string literal\"), b);", getLLVMStyleWithColumns(20))); - EXPECT_EQ("f(g(\"long string \"\n" - " \"literal\",\n" - " a),\n" - " b);", - format("f(g(\"long string literal\", a), b);", - getLLVMStyleWithColumns(20))); - EXPECT_EQ( - "f(\"one two\".split(\n" - " variable));", - format("f(\"one two\".split(variable));", getLLVMStyleWithColumns(20))); - EXPECT_EQ("f(\"one two three four five six \"\n" - " \"seven\".split(\n" - " really_looooong_variable));", - format("f(\"one two three four five six seven\"." - "split(really_looooong_variable));", - getLLVMStyleWithColumns(33))); - - EXPECT_EQ("f(\"some \"\n" - " \"text\",\n" - " other);", - format("f(\"some text\", other);", getLLVMStyleWithColumns(10))); + verifyFormat("f(g(\"long string \"\n" + " \"literal\"),\n" + " b);", + "f(g(\"long string literal\"), b);", + getLLVMStyleWithColumns(20)); + verifyFormat("f(g(\"long string \"\n" + " \"literal\",\n" + " a),\n" + " b);", + "f(g(\"long string literal\", a), b);", + getLLVMStyleWithColumns(20)); + verifyFormat("f(\"one two\".split(\n" + " variable));", + "f(\"one two\".split(variable));", getLLVMStyleWithColumns(20)); + verifyFormat("f(\"one two three four five six \"\n" + " \"seven\".split(\n" + " really_looooong_variable));", + "f(\"one two three four five six seven\"." + "split(really_looooong_variable));", + getLLVMStyleWithColumns(33)); + + verifyFormat("f(\"some \"\n" + " \"text\",\n" + " other);", + "f(\"some text\", other);", getLLVMStyleWithColumns(10)); // Only break as a last resort. verifyFormat( @@ -14935,20 +14721,24 @@ TEST_F(FormatTest, BreaksStringLiterals) { " aaaaaaaaaaaaaaaaaaaa,\n" " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); + // FIXME: unstable test case EXPECT_EQ("\"splitmea\"\n" "\"trandomp\"\n" "\"oint\"", format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); + // FIXME: unstable test case EXPECT_EQ("\"split/\"\n" "\"pathat/\"\n" "\"slashes\"", format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); + // FIXME: unstable test case EXPECT_EQ("\"split/\"\n" "\"pathat/\"\n" "\"slashes\"", format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); + // FIXME: unstable test case EXPECT_EQ("\"split at \"\n" "\"spaces/at/\"\n" "\"slashes.at.any$\"\n" @@ -14966,30 +14756,30 @@ TEST_F(FormatTest, BreaksStringLiterals) { // Verify that splitting the strings understands // Style::AlwaysBreakBeforeMultilineStrings. - EXPECT_EQ("aaaaaaaaaaaa(\n" - " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" - " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", - format("aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " - "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " - "aaaaaaaaaaaaaaaaaaaaaa\");", - getGoogleStyle())); - EXPECT_EQ("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" - " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", - format("return \"aaaaaaaaaaaaaaaaaaaaaa " - "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " - "aaaaaaaaaaaaaaaaaaaaaa\";", - getGoogleStyle())); - EXPECT_EQ("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" - " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", - format("llvm::outs() << " - "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" - "aaaaaaaaaaaaaaaaaaa\";")); - EXPECT_EQ("ffff(\n" - " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" - " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", - format("ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", - getGoogleStyle())); + verifyFormat("aaaaaaaaaaaa(\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");", + "aaaaaaaaaaaa(\"aaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaa\");", + getGoogleStyle()); + verifyFormat("return \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\";", + "return \"aaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaa\";", + getGoogleStyle()); + verifyFormat("llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", + "llvm::outs() << " + "\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaa\";"); + verifyFormat("ffff(\n" + " {\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \"\n" + " \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", + "ffff({\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"});", + getGoogleStyle()); FormatStyle Style = getLLVMStyleWithColumns(12); Style.BreakStringLiterals = false; @@ -14997,27 +14787,28 @@ TEST_F(FormatTest, BreaksStringLiterals) { FormatStyle AlignLeft = getLLVMStyleWithColumns(12); AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; - EXPECT_EQ("#define A \\\n" - " \"some \" \\\n" - " \"text \" \\\n" - " \"other\";", - format("#define A \"some text other\";", AlignLeft)); + verifyFormat("#define A \\\n" + " \"some \" \\\n" + " \"text \" \\\n" + " \"other\";", + "#define A \"some text other\";", AlignLeft); } TEST_F(FormatTest, BreaksStringLiteralsAtColumnLimit) { - EXPECT_EQ("C a = \"some more \"\n" - " \"text\";", - format("C a = \"some more text\";", getLLVMStyleWithColumns(18))); + verifyFormat("C a = \"some more \"\n" + " \"text\";", + "C a = \"some more text\";", getLLVMStyleWithColumns(18)); } TEST_F(FormatTest, FullyRemoveEmptyLines) { FormatStyle NoEmptyLines = getLLVMStyleWithColumns(80); NoEmptyLines.MaxEmptyLinesToKeep = 0; - EXPECT_EQ("int i = a(b());", - format("int i=a(\n\n b(\n\n\n )\n\n);", NoEmptyLines)); + verifyFormat("int i = a(b());", "int i=a(\n\n b(\n\n\n )\n\n);", + NoEmptyLines); } TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { + // FIXME: unstable test case EXPECT_EQ( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "(\n" @@ -15028,24 +14819,28 @@ TEST_F(FormatTest, BreaksStringLiteralsWithTabs) { } TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { + // FIXME: unstable test case EXPECT_EQ( "u8\"utf8 string \"\n" "u8\"literal\";", format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); + // FIXME: unstable test case EXPECT_EQ( "u\"utf16 string \"\n" "u\"literal\";", format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); + // FIXME: unstable test case EXPECT_EQ( "U\"utf32 string \"\n" "U\"literal\";", format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); + // FIXME: unstable test case EXPECT_EQ("L\"wide string \"\n" "L\"literal\";", format("L\"wide string literal\";", getGoogleStyleWithColumns(16))); - EXPECT_EQ("@\"NSString \"\n" - "@\"literal\";", - format("@\"NSString literal\";", getGoogleStyleWithColumns(19))); + verifyFormat("@\"NSString \"\n" + "@\"literal\";", + "@\"NSString literal\";", getGoogleStyleWithColumns(19)); verifyFormat(R"(NSString *s = @"那那那那";)", getLLVMStyleWithColumns(26)); // This input makes clang-format try to split the incomplete unicode escape @@ -15066,16 +14861,17 @@ TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { FormatStyle Style = getLLVMStyleWithColumns(20); + // FIXME: unstable test case EXPECT_EQ( "_T(\"aaaaaaaaaaaaaa\")\n" "_T(\"aaaaaaaaaaaaaa\")\n" "_T(\"aaaaaaaaaaaa\")", format(" _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", Style)); - EXPECT_EQ("f(x,\n" - " _T(\"aaaaaaaaaaaa\")\n" - " _T(\"aaa\"),\n" - " z);", - format("f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style)); + verifyFormat("f(x,\n" + " _T(\"aaaaaaaaaaaa\")\n" + " _T(\"aaa\"),\n" + " z);", + "f(x, _T(\"aaaaaaaaaaaaaaa\"), z);", Style); // FIXME: Handle embedded spaces in one iteration. // EXPECT_EQ("_T(\"aaaaaaaaaaaaa\")\n" @@ -15084,25 +14880,24 @@ TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { // "_T(\"a\")", // format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", // getLLVMStyleWithColumns(20))); - EXPECT_EQ( - "_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", - format(" _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style)); - EXPECT_EQ("f(\n" - "#if !TEST\n" - " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" - "#endif\n" - ");", - format("f(\n" - "#if !TEST\n" - "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" - "#endif\n" - ");")); - EXPECT_EQ("f(\n" - "\n" - " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", - format("f(\n" - "\n" - "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));")); + verifyFormat("_T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", + " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", Style); + verifyFormat("f(\n" + "#if !TEST\n" + " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" + "#endif\n" + ");", + "f(\n" + "#if !TEST\n" + "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\")\n" + "#endif\n" + ");"); + verifyFormat("f(\n" + "\n" + " _T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));", + "f(\n" + "\n" + "_T(\"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXn\"));"); // Regression test for accessing tokens past the end of a vector in the // TokenLexer. verifyNoCrash(R"(_T( @@ -15114,84 +14909,82 @@ TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { TEST_F(FormatTest, BreaksStringLiteralOperands) { // In a function call with two operands, the second can be broken with no line // break before it. - EXPECT_EQ( - "func(a, \"long long \"\n" - " \"long long\");", - format("func(a, \"long long long long\");", getLLVMStyleWithColumns(24))); + verifyFormat("func(a, \"long long \"\n" + " \"long long\");", + "func(a, \"long long long long\");", + getLLVMStyleWithColumns(24)); // In a function call with three operands, the second must be broken with a // line break before it. - EXPECT_EQ("func(a,\n" - " \"long long long \"\n" - " \"long\",\n" - " c);", - format("func(a, \"long long long long\", c);", - getLLVMStyleWithColumns(24))); + verifyFormat("func(a,\n" + " \"long long long \"\n" + " \"long\",\n" + " c);", + "func(a, \"long long long long\", c);", + getLLVMStyleWithColumns(24)); // In a function call with three operands, the third must be broken with a // line break before it. - EXPECT_EQ("func(a, b,\n" - " \"long long long \"\n" - " \"long\");", - format("func(a, b, \"long long long long\");", - getLLVMStyleWithColumns(24))); + verifyFormat("func(a, b,\n" + " \"long long long \"\n" + " \"long\");", + "func(a, b, \"long long long long\");", + getLLVMStyleWithColumns(24)); // In a function call with three operands, both the second and the third must // be broken with a line break before them. - EXPECT_EQ("func(a,\n" - " \"long long long \"\n" - " \"long\",\n" - " \"long long long \"\n" - " \"long\");", - format("func(a, \"long long long long\", \"long long long long\");", - getLLVMStyleWithColumns(24))); + verifyFormat("func(a,\n" + " \"long long long \"\n" + " \"long\",\n" + " \"long long long \"\n" + " \"long\");", + "func(a, \"long long long long\", \"long long long long\");", + getLLVMStyleWithColumns(24)); // In a chain of << with two operands, the second can be broken with no line // break before it. - EXPECT_EQ("a << \"line line \"\n" - " \"line\";", - format("a << \"line line line\";", getLLVMStyleWithColumns(20))); + verifyFormat("a << \"line line \"\n" + " \"line\";", + "a << \"line line line\";", getLLVMStyleWithColumns(20)); // In a chain of << with three operands, the second can be broken with no line // break before it. - EXPECT_EQ( - "abcde << \"line \"\n" - " \"line line\"\n" - " << c;", - format("abcde << \"line line line\" << c;", getLLVMStyleWithColumns(20))); + verifyFormat("abcde << \"line \"\n" + " \"line line\"\n" + " << c;", + "abcde << \"line line line\" << c;", + getLLVMStyleWithColumns(20)); // In a chain of << with three operands, the third must be broken with a line // break before it. - EXPECT_EQ( - "a << b\n" - " << \"line line \"\n" - " \"line\";", - format("a << b << \"line line line\";", getLLVMStyleWithColumns(20))); + verifyFormat("a << b\n" + " << \"line line \"\n" + " \"line\";", + "a << b << \"line line line\";", getLLVMStyleWithColumns(20)); // In a chain of << with three operands, the second can be broken with no line // break before it and the third must be broken with a line break before it. - EXPECT_EQ("abcd << \"line line \"\n" - " \"line\"\n" - " << \"line line \"\n" - " \"line\";", - format("abcd << \"line line line\" << \"line line line\";", - getLLVMStyleWithColumns(20))); + verifyFormat("abcd << \"line line \"\n" + " \"line\"\n" + " << \"line line \"\n" + " \"line\";", + "abcd << \"line line line\" << \"line line line\";", + getLLVMStyleWithColumns(20)); // In a chain of binary operators with two operands, the second can be broken // with no line break before it. - EXPECT_EQ( - "abcd + \"line line \"\n" - " \"line line\";", - format("abcd + \"line line line line\";", getLLVMStyleWithColumns(20))); + verifyFormat("abcd + \"line line \"\n" + " \"line line\";", + "abcd + \"line line line line\";", getLLVMStyleWithColumns(20)); // In a chain of binary operators with three operands, the second must be // broken with a line break before it. - EXPECT_EQ("abcd +\n" - " \"line line \"\n" - " \"line line\" +\n" - " e;", - format("abcd + \"line line line line\" + e;", - getLLVMStyleWithColumns(20))); + verifyFormat("abcd +\n" + " \"line line \"\n" + " \"line line\" +\n" + " e;", + "abcd + \"line line line line\" + e;", + getLLVMStyleWithColumns(20)); // In a function call with two operands, with AlignAfterOpenBracket enabled, // the first must be broken with a line break before it. FormatStyle Style = getLLVMStyleWithColumns(25); Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; - EXPECT_EQ("someFunction(\n" - " \"long long long \"\n" - " \"long\",\n" - " a);", - format("someFunction(\"long long long long\", a);", Style)); + verifyFormat("someFunction(\n" + " \"long long long \"\n" + " \"long\",\n" + " a);", + "someFunction(\"long long long long\", a);", Style); Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; verifyFormat("someFunction(\n" " \"long long long \"\n" @@ -15202,80 +14995,76 @@ TEST_F(FormatTest, BreaksStringLiteralOperands) { } TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { - EXPECT_EQ( - "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", - format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";")); + verifyFormat("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", + "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";"); } TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { - EXPECT_EQ("f(g(R\"x(raw literal)x\", a), b);", - format("f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle())); - EXPECT_EQ("fffffffffff(g(R\"x(\n" - "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\",\n" - " a),\n" - " b);", - format("fffffffffff(g(R\"x(\n" - "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\", a), b);", - getGoogleStyleWithColumns(20))); - EXPECT_EQ("fffffffffff(\n" - " g(R\"x(qqq\n" - "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\",\n" - " a),\n" - " b);", - format("fffffffffff(g(R\"x(qqq\n" - "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\", a), b);", - getGoogleStyleWithColumns(20))); - - EXPECT_EQ("fffffffffff(R\"x(\n" - "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\");", - format("fffffffffff(R\"x(\n" - "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\");", - getGoogleStyleWithColumns(20))); - EXPECT_EQ("fffffffffff(R\"x(\n" - "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\" + bbbbbb);", - format("fffffffffff(R\"x(\n" - "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\" + bbbbbb);", - getGoogleStyleWithColumns(20))); - EXPECT_EQ("fffffffffff(\n" - " R\"x(\n" - "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\" +\n" - " bbbbbb);", - format("fffffffffff(\n" - " R\"x(\n" - "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\" + bbbbbb);", - getGoogleStyleWithColumns(20))); - EXPECT_EQ("fffffffffff(R\"(single line raw string)\" + bbbbbb);", - format("fffffffffff(\n" - " R\"(single line raw string)\" + bbbbbb);")); + verifyFormat("f(g(R\"x(raw literal)x\", a), b);", + "f(g(R\"x(raw literal)x\", a), b);", getGoogleStyle()); + verifyFormat("fffffffffff(g(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\",\n" + " a),\n" + " b);", + "fffffffffff(g(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\", a), b);", + getGoogleStyleWithColumns(20)); + verifyFormat("fffffffffff(\n" + " g(R\"x(qqq\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\",\n" + " a),\n" + " b);", + "fffffffffff(g(R\"x(qqq\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\", a), b);", + getGoogleStyleWithColumns(20)); + + verifyNoChange("fffffffffff(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\");", + getGoogleStyleWithColumns(20)); + verifyFormat("fffffffffff(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\" + bbbbbb);", + "fffffffffff(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\" + bbbbbb);", + getGoogleStyleWithColumns(20)); + verifyFormat("fffffffffff(\n" + " R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\" +\n" + " bbbbbb);", + "fffffffffff(\n" + " R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\" + bbbbbb);", + getGoogleStyleWithColumns(20)); + verifyFormat("fffffffffff(R\"(single line raw string)\" + bbbbbb);", + "fffffffffff(\n" + " R\"(single line raw string)\" + bbbbbb);"); } TEST_F(FormatTest, SkipsUnknownStringLiterals) { verifyFormat("string a = \"unterminated;"); - EXPECT_EQ("function(\"unterminated,\n" - " OtherParameter);", - format("function( \"unterminated,\n" - " OtherParameter);")); + verifyFormat("function(\"unterminated,\n" + " OtherParameter);", + "function( \"unterminated,\n" + " OtherParameter);"); } TEST_F(FormatTest, DoesNotTryToParseUDLiteralsInPreCpp11Code) { FormatStyle Style = getLLVMStyle(); Style.Standard = FormatStyle::LS_Cpp03; - EXPECT_EQ("#define x(_a) printf(\"foo\" _a);", - format("#define x(_a) printf(\"foo\"_a);", Style)); + verifyFormat("#define x(_a) printf(\"foo\" _a);", + "#define x(_a) printf(\"foo\"_a);", Style); } TEST_F(FormatTest, CppLexVersion) { @@ -15294,78 +15083,85 @@ TEST_F(FormatTest, CppLexVersion) { TEST_F(FormatTest, UnderstandsCpp1y) { verifyFormat("int bi{1'000'000};"); } TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) { - EXPECT_EQ("someFunction(\"aaabbbcccd\"\n" - " \"ddeeefff\");", - format("someFunction(\"aaabbbcccdddeeefff\");", - getLLVMStyleWithColumns(25))); - EXPECT_EQ("someFunction1234567890(\n" - " \"aaabbbcccdddeeefff\");", - format("someFunction1234567890(\"aaabbbcccdddeeefff\");", - getLLVMStyleWithColumns(26))); - EXPECT_EQ("someFunction1234567890(\n" - " \"aaabbbcccdddeeeff\"\n" - " \"f\");", - format("someFunction1234567890(\"aaabbbcccdddeeefff\");", - getLLVMStyleWithColumns(25))); - EXPECT_EQ("someFunction1234567890(\n" - " \"aaabbbcccdddeeeff\"\n" - " \"f\");", - format("someFunction1234567890(\"aaabbbcccdddeeefff\");", - getLLVMStyleWithColumns(24))); - EXPECT_EQ("someFunction(\n" - " \"aaabbbcc ddde \"\n" - " \"efff\");", - format("someFunction(\"aaabbbcc ddde efff\");", - getLLVMStyleWithColumns(25))); - EXPECT_EQ("someFunction(\"aaabbbccc \"\n" - " \"ddeeefff\");", - format("someFunction(\"aaabbbccc ddeeefff\");", - getLLVMStyleWithColumns(25))); - EXPECT_EQ("someFunction1234567890(\n" - " \"aaabb \"\n" - " \"cccdddeeefff\");", - format("someFunction1234567890(\"aaabb cccdddeeefff\");", - getLLVMStyleWithColumns(25))); - EXPECT_EQ("#define A \\\n" - " string s = \\\n" - " \"123456789\" \\\n" - " \"0\"; \\\n" - " int i;", - format("#define A string s = \"1234567890\"; int i;", - getLLVMStyleWithColumns(20))); - EXPECT_EQ("someFunction(\n" - " \"aaabbbcc \"\n" - " \"dddeeefff\");", - format("someFunction(\"aaabbbcc dddeeefff\");", - getLLVMStyleWithColumns(25))); + verifyFormat("someFunction(\"aaabbbcccd\"\n" + " \"ddeeefff\");", + "someFunction(\"aaabbbcccdddeeefff\");", + getLLVMStyleWithColumns(25)); + verifyFormat("someFunction1234567890(\n" + " \"aaabbbcccdddeeefff\");", + "someFunction1234567890(\"aaabbbcccdddeeefff\");", + getLLVMStyleWithColumns(26)); + verifyFormat("someFunction1234567890(\n" + " \"aaabbbcccdddeeeff\"\n" + " \"f\");", + "someFunction1234567890(\"aaabbbcccdddeeefff\");", + getLLVMStyleWithColumns(25)); + verifyFormat("someFunction1234567890(\n" + " \"aaabbbcccdddeeeff\"\n" + " \"f\");", + "someFunction1234567890(\"aaabbbcccdddeeefff\");", + getLLVMStyleWithColumns(24)); + verifyFormat("someFunction(\n" + " \"aaabbbcc ddde \"\n" + " \"efff\");", + "someFunction(\"aaabbbcc ddde efff\");", + getLLVMStyleWithColumns(25)); + verifyFormat("someFunction(\"aaabbbccc \"\n" + " \"ddeeefff\");", + "someFunction(\"aaabbbccc ddeeefff\");", + getLLVMStyleWithColumns(25)); + verifyFormat("someFunction1234567890(\n" + " \"aaabb \"\n" + " \"cccdddeeefff\");", + "someFunction1234567890(\"aaabb cccdddeeefff\");", + getLLVMStyleWithColumns(25)); + verifyFormat("#define A \\\n" + " string s = \\\n" + " \"123456789\" \\\n" + " \"0\"; \\\n" + " int i;", + "#define A string s = \"1234567890\"; int i;", + getLLVMStyleWithColumns(20)); + verifyFormat("someFunction(\n" + " \"aaabbbcc \"\n" + " \"dddeeefff\");", + "someFunction(\"aaabbbcc dddeeefff\");", + getLLVMStyleWithColumns(25)); } TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) { verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3)); verifyFormat("\"\\\"", getLLVMStyleWithColumns(2)); + // FIXME: unstable test case EXPECT_EQ("\"test\"\n" "\"\\n\"", format("\"test\\n\"", getLLVMStyleWithColumns(7))); + // FIXME: unstable test case EXPECT_EQ("\"tes\\\\\"\n" "\"n\"", format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); + // FIXME: unstable test case EXPECT_EQ("\"\\\\\\\\\"\n" "\"\\n\"", format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7)); + // FIXME: unstable test case EXPECT_EQ("\"\\uff01\"\n" "\"test\"", format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); verifyFormat("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)); + // FIXME: unstable test case EXPECT_EQ("\"\\x000000000001\"\n" "\"next\"", format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15)); verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7)); + // FIXME: unstable test case EXPECT_EQ("\"test\"\n" "\"\\000000\"\n" "\"000001\"", format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); + // FIXME: unstable test case EXPECT_EQ("\"test\\000\"\n" "\"00000000\"\n" "\"1\"", @@ -15428,18 +15224,18 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { Tab.UseTab = FormatStyle::UT_Always; Tab.AlignEscapedNewlines = FormatStyle::ENAS_Left; - EXPECT_EQ("if (aaaaaaaa && // q\n" - " bb)\t\t// w\n" - "\t;", - format("if (aaaaaaaa &&// q\n" - "bb)// w\n" - ";", - Tab)); - EXPECT_EQ("if (aaa && bbb) // w\n" - "\t;", - format("if(aaa&&bbb)// w\n" - ";", - Tab)); + verifyFormat("if (aaaaaaaa && // q\n" + " bb)\t\t// w\n" + "\t;", + "if (aaaaaaaa &&// q\n" + "bb)// w\n" + ";", + Tab); + verifyFormat("if (aaa && bbb) // w\n" + "\t;", + "if(aaa&&bbb)// w\n" + ";", + Tab); verifyFormat("class X {\n" "\tvoid f() {\n" @@ -15506,15 +15302,15 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { Tab.TabWidth = 8; Tab.IndentWidth = 8; - EXPECT_EQ("/*\n" - "\t a\t\tcomment\n" - "\t in multiple lines\n" - " */", - format(" /*\t \t \n" - " \t \t a\t\tcomment\t \t\n" - " \t \t in multiple lines\t\n" - " \t */", - Tab)); + verifyFormat("/*\n" + "\t a\t\tcomment\n" + "\t in multiple lines\n" + " */", + " /*\t \t \n" + " \t \t a\t\tcomment\t \t\n" + " \t \t in multiple lines\t\n" + " \t */", + Tab); TabAlignment.UseTab = FormatStyle::UT_ForIndentation; TabAlignment.PointerAlignment = FormatStyle::PAS_Left; @@ -15552,13 +15348,13 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { "\ta3\n" "};", Tab); - EXPECT_EQ("if (aaaaaaaa && // q\n" - " bb) // w\n" - "\t;", - format("if (aaaaaaaa &&// q\n" - "bb)// w\n" - ";", - Tab)); + verifyFormat("if (aaaaaaaa && // q\n" + " bb) // w\n" + "\t;", + "if (aaaaaaaa &&// q\n" + "bb)// w\n" + ";", + Tab); verifyFormat("class X {\n" "\tvoid f() {\n" "\t\tsomeFunction(parameter1,\n" @@ -15576,69 +15372,59 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { "\t p);\n" "}", Tab); - EXPECT_EQ("{\n" - "\t/* aaaa\n" - "\t bbbb */\n" - "}", - format("{\n" - "/* aaaa\n" - " bbbb */\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "\t bbbbbbbbbbbbb\n" - "\t*/\n" - "}", - format("{\n" - "/*\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" - "*/\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "\t// bbbbbbbbbbbbb\n" - "}", - format("{\n" - "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "\t bbbbbbbbbbbbb\n" - "\t*/\n" - "}", - format("{\n" - "\t/*\n" - "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" - "\t*/\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\n" - "\t*/\n" - "}", - format("{\n" - "\t/*\n" - "\n" - "\t*/\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - " asdf\n" - "\t*/\n" - "}", - format("{\n" - "\t/*\n" - " asdf\n" - "\t*/\n" - "}", - Tab)); + verifyFormat("{\n" + "\t/* aaaa\n" + "\t bbbb */\n" + "}", + "{\n" + "/* aaaa\n" + " bbbb */\n" + "}", + Tab); + verifyFormat("{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + "{\n" + "/*\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "*/\n" + "}", + Tab); + verifyFormat("{\n" + "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t// bbbbbbbbbbbbb\n" + "}", + "{\n" + "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "}", + Tab); + verifyFormat("{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + "{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + Tab); + verifyNoChange("{\n" + "\t/*\n" + "\n" + "\t*/\n" + "}", + Tab); + verifyNoChange("{\n" + "\t/*\n" + " asdf\n" + "\t*/\n" + "}", + Tab); verifyFormat("void f() {\n" "\treturn true ? aaaaaaaaaaaaaaaaaa\n" @@ -15660,66 +15446,66 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { TabNoBreak); Tab.UseTab = FormatStyle::UT_Never; - EXPECT_EQ("/*\n" - " a\t\tcomment\n" - " in multiple lines\n" - " */", - format(" /*\t \t \n" - " \t \t a\t\tcomment\t \t\n" - " \t \t in multiple lines\t\n" - " \t */", - Tab)); - EXPECT_EQ("/* some\n" - " comment */", - format(" \t \t /* some\n" - " \t \t comment */", - Tab)); - EXPECT_EQ("int a; /* some\n" - " comment */", - format(" \t \t int a; /* some\n" - " \t \t comment */", - Tab)); - - EXPECT_EQ("int a; /* some\n" - "comment */", - format(" \t \t int\ta; /* some\n" - " \t \t comment */", - Tab)); - EXPECT_EQ("f(\"\t\t\"); /* some\n" - " comment */", - format(" \t \t f(\"\t\t\"); /* some\n" - " \t \t comment */", - Tab)); - EXPECT_EQ("{\n" - " /*\n" - " * Comment\n" - " */\n" - " int i;\n" - "}", - format("{\n" - "\t/*\n" - "\t * Comment\n" - "\t */\n" - "\t int i;\n" - "}", - Tab)); + verifyFormat("/*\n" + " a\t\tcomment\n" + " in multiple lines\n" + " */", + " /*\t \t \n" + " \t \t a\t\tcomment\t \t\n" + " \t \t in multiple lines\t\n" + " \t */", + Tab); + verifyFormat("/* some\n" + " comment */", + " \t \t /* some\n" + " \t \t comment */", + Tab); + verifyFormat("int a; /* some\n" + " comment */", + " \t \t int a; /* some\n" + " \t \t comment */", + Tab); - Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; - Tab.TabWidth = 8; - Tab.IndentWidth = 8; - EXPECT_EQ("if (aaaaaaaa && // q\n" - " bb) // w\n" - "\t;", - format("if (aaaaaaaa &&// q\n" - "bb)// w\n" - ";", - Tab)); - EXPECT_EQ("if (aaa && bbb) // w\n" - "\t;", - format("if(aaa&&bbb)// w\n" - ";", - Tab)); - verifyFormat("class X {\n" + verifyFormat("int a; /* some\n" + "comment */", + " \t \t int\ta; /* some\n" + " \t \t comment */", + Tab); + verifyFormat("f(\"\t\t\"); /* some\n" + " comment */", + " \t \t f(\"\t\t\"); /* some\n" + " \t \t comment */", + Tab); + verifyFormat("{\n" + " /*\n" + " * Comment\n" + " */\n" + " int i;\n" + "}", + "{\n" + "\t/*\n" + "\t * Comment\n" + "\t */\n" + "\t int i;\n" + "}", + Tab); + + Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; + Tab.TabWidth = 8; + Tab.IndentWidth = 8; + verifyFormat("if (aaaaaaaa && // q\n" + " bb) // w\n" + "\t;", + "if (aaaaaaaa &&// q\n" + "bb)// w\n" + ";", + Tab); + verifyFormat("if (aaa && bbb) // w\n" + "\t;", + "if(aaa&&bbb)// w\n" + ";", + Tab); + verifyFormat("class X {\n" "\tvoid f() {\n" "\t\tsomeFunction(parameter1,\n" "\t\t\t parameter2);\n" @@ -15762,15 +15548,15 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { Tab); Tab.TabWidth = 8; Tab.IndentWidth = 8; - EXPECT_EQ("/*\n" - "\t a\t\tcomment\n" - "\t in multiple lines\n" - " */", - format(" /*\t \t \n" - " \t \t a\t\tcomment\t \t\n" - " \t \t in multiple lines\t\n" - " \t */", - Tab)); + verifyFormat("/*\n" + "\t a\t\tcomment\n" + "\t in multiple lines\n" + " */", + " /*\t \t \n" + " \t \t a\t\tcomment\t \t\n" + " \t \t in multiple lines\t\n" + " \t */", + Tab); verifyFormat("{\n" "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" @@ -15786,13 +15572,13 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { "\ta3\n" "};", Tab); - EXPECT_EQ("if (aaaaaaaa && // q\n" - " bb) // w\n" - "\t;", - format("if (aaaaaaaa &&// q\n" - "bb)// w\n" - ";", - Tab)); + verifyFormat("if (aaaaaaaa && // q\n" + " bb) // w\n" + "\t;", + "if (aaaaaaaa &&// q\n" + "bb)// w\n" + ";", + Tab); verifyFormat("class X {\n" "\tvoid f() {\n" "\t\tsomeFunction(parameter1,\n" @@ -15810,125 +15596,115 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { "\t p);\n" "}", Tab); - EXPECT_EQ("{\n" - "\t/* aaaa\n" - "\t bbbb */\n" - "}", - format("{\n" - "/* aaaa\n" - " bbbb */\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "\t bbbbbbbbbbbbb\n" - "\t*/\n" - "}", - format("{\n" - "/*\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" - "*/\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "\t// bbbbbbbbbbbbb\n" - "}", - format("{\n" - "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "\t bbbbbbbbbbbbb\n" - "\t*/\n" - "}", - format("{\n" - "\t/*\n" - "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" - "\t*/\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\n" - "\t*/\n" - "}", - format("{\n" - "\t/*\n" - "\n" - "\t*/\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - " asdf\n" - "\t*/\n" - "}", - format("{\n" - "\t/*\n" - " asdf\n" - "\t*/\n" - "}", - Tab)); - EXPECT_EQ("/* some\n" - " comment */", - format(" \t \t /* some\n" - " \t \t comment */", - Tab)); - EXPECT_EQ("int a; /* some\n" - " comment */", - format(" \t \t int a; /* some\n" - " \t \t comment */", - Tab)); - EXPECT_EQ("int a; /* some\n" - "comment */", - format(" \t \t int\ta; /* some\n" - " \t \t comment */", - Tab)); - EXPECT_EQ("f(\"\t\t\"); /* some\n" - " comment */", - format(" \t \t f(\"\t\t\"); /* some\n" - " \t \t comment */", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\t * Comment\n" - "\t */\n" - "\tint i;\n" - "}", - format("{\n" - "\t/*\n" - "\t * Comment\n" - "\t */\n" - "\t int i;\n" - "}", - Tab)); + verifyFormat("{\n" + "\t/* aaaa\n" + "\t bbbb */\n" + "}", + "{\n" + "/* aaaa\n" + " bbbb */\n" + "}", + Tab); + verifyFormat("{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + "{\n" + "/*\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "*/\n" + "}", + Tab); + verifyFormat("{\n" + "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t// bbbbbbbbbbbbb\n" + "}", + "{\n" + "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "}", + Tab); + verifyFormat("{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + "{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + Tab); + verifyNoChange("{\n" + "\t/*\n" + "\n" + "\t*/\n" + "}", + Tab); + verifyNoChange("{\n" + "\t/*\n" + " asdf\n" + "\t*/\n" + "}", + Tab); + verifyFormat("/* some\n" + " comment */", + " \t \t /* some\n" + " \t \t comment */", + Tab); + verifyFormat("int a; /* some\n" + " comment */", + " \t \t int a; /* some\n" + " \t \t comment */", + Tab); + verifyFormat("int a; /* some\n" + "comment */", + " \t \t int\ta; /* some\n" + " \t \t comment */", + Tab); + verifyFormat("f(\"\t\t\"); /* some\n" + " comment */", + " \t \t f(\"\t\t\"); /* some\n" + " \t \t comment */", + Tab); + verifyFormat("{\n" + "\t/*\n" + "\t * Comment\n" + "\t */\n" + "\tint i;\n" + "}", + "{\n" + "\t/*\n" + "\t * Comment\n" + "\t */\n" + "\t int i;\n" + "}", + Tab); Tab.TabWidth = 2; Tab.IndentWidth = 2; - EXPECT_EQ("{\n" - "\t/* aaaa\n" - "\t\t bbbb */\n" - "}", - format("{\n" - "/* aaaa\n" - "\t bbbb */\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "\t\tbbbbbbbbbbbbb\n" - "\t*/\n" - "}", - format("{\n" - "/*\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" - "*/\n" - "}", - Tab)); + verifyFormat("{\n" + "\t/* aaaa\n" + "\t\t bbbb */\n" + "}", + "{\n" + "/* aaaa\n" + "\t bbbb */\n" + "}", + Tab); + verifyFormat("{\n" + "\t/*\n" + "\t\taaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t\tbbbbbbbbbbbbb\n" + "\t*/\n" + "}", + "{\n" + "/*\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "*/\n" + "}", + Tab); Tab.AlignConsecutiveAssignments.Enabled = true; Tab.AlignConsecutiveDeclarations.Enabled = true; Tab.TabWidth = 4; @@ -15946,18 +15722,18 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { Tab.UseTab = FormatStyle::UT_AlignWithSpaces; Tab.TabWidth = 8; Tab.IndentWidth = 8; - EXPECT_EQ("if (aaaaaaaa && // q\n" - " bb) // w\n" - "\t;", - format("if (aaaaaaaa &&// q\n" - "bb)// w\n" - ";", - Tab)); - EXPECT_EQ("if (aaa && bbb) // w\n" - "\t;", - format("if(aaa&&bbb)// w\n" - ";", - Tab)); + verifyFormat("if (aaaaaaaa && // q\n" + " bb) // w\n" + "\t;", + "if (aaaaaaaa &&// q\n" + "bb)// w\n" + ";", + Tab); + verifyFormat("if (aaa && bbb) // w\n" + "\t;", + "if(aaa&&bbb)// w\n" + ";", + Tab); verifyFormat("class X {\n" "\tvoid f() {\n" "\t\tsomeFunction(parameter1,\n" @@ -16001,15 +15777,15 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { Tab); Tab.TabWidth = 8; Tab.IndentWidth = 8; - EXPECT_EQ("/*\n" - " a\t\tcomment\n" - " in multiple lines\n" - " */", - format(" /*\t \t \n" - " \t \t a\t\tcomment\t \t\n" - " \t \t in multiple lines\t\n" - " \t */", - Tab)); + verifyFormat("/*\n" + " a\t\tcomment\n" + " in multiple lines\n" + " */", + " /*\t \t \n" + " \t \t a\t\tcomment\t \t\n" + " \t \t in multiple lines\t\n" + " \t */", + Tab); verifyFormat("{\n" "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" @@ -16025,13 +15801,13 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { "\ta3\n" "};", Tab); - EXPECT_EQ("if (aaaaaaaa && // q\n" - " bb) // w\n" - "\t;", - format("if (aaaaaaaa &&// q\n" - "bb)// w\n" - ";", - Tab)); + verifyFormat("if (aaaaaaaa && // q\n" + " bb) // w\n" + "\t;", + "if (aaaaaaaa &&// q\n" + "bb)// w\n" + ";", + Tab); verifyFormat("class X {\n" "\tvoid f() {\n" "\t\tsomeFunction(parameter1,\n" @@ -16049,125 +15825,115 @@ TEST_F(FormatTest, ConfigurableUseOfTab) { "\t p);\n" "}", Tab); - EXPECT_EQ("{\n" - "\t/* aaaa\n" - "\t bbbb */\n" - "}", - format("{\n" - "/* aaaa\n" - " bbbb */\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "\t bbbbbbbbbbbbb\n" - "\t*/\n" - "}", - format("{\n" - "/*\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" - "*/\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "\t// bbbbbbbbbbbbb\n" - "}", - format("{\n" - "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "\t bbbbbbbbbbbbb\n" - "\t*/\n" - "}", - format("{\n" - "\t/*\n" - "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" - "\t*/\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\n" - "\t*/\n" - "}", - format("{\n" - "\t/*\n" - "\n" - "\t*/\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - " asdf\n" - "\t*/\n" - "}", - format("{\n" - "\t/*\n" - " asdf\n" - "\t*/\n" - "}", - Tab)); - EXPECT_EQ("/* some\n" - " comment */", - format(" \t \t /* some\n" - " \t \t comment */", - Tab)); - EXPECT_EQ("int a; /* some\n" - " comment */", - format(" \t \t int a; /* some\n" - " \t \t comment */", - Tab)); - EXPECT_EQ("int a; /* some\n" - "comment */", - format(" \t \t int\ta; /* some\n" - " \t \t comment */", - Tab)); - EXPECT_EQ("f(\"\t\t\"); /* some\n" - " comment */", - format(" \t \t f(\"\t\t\"); /* some\n" - " \t \t comment */", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\t * Comment\n" - "\t */\n" - "\tint i;\n" - "}", - format("{\n" - "\t/*\n" - "\t * Comment\n" - "\t */\n" - "\t int i;\n" - "}", - Tab)); + verifyFormat("{\n" + "\t/* aaaa\n" + "\t bbbb */\n" + "}", + "{\n" + "/* aaaa\n" + " bbbb */\n" + "}", + Tab); + verifyFormat("{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + "{\n" + "/*\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "*/\n" + "}", + Tab); + verifyFormat("{\n" + "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t// bbbbbbbbbbbbb\n" + "}", + "{\n" + "\t// aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "}", + Tab); + verifyFormat("{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + "{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + Tab); + verifyNoChange("{\n" + "\t/*\n" + "\n" + "\t*/\n" + "}", + Tab); + verifyNoChange("{\n" + "\t/*\n" + " asdf\n" + "\t*/\n" + "}", + Tab); + verifyFormat("/* some\n" + " comment */", + " \t \t /* some\n" + " \t \t comment */", + Tab); + verifyFormat("int a; /* some\n" + " comment */", + " \t \t int a; /* some\n" + " \t \t comment */", + Tab); + verifyFormat("int a; /* some\n" + "comment */", + " \t \t int\ta; /* some\n" + " \t \t comment */", + Tab); + verifyFormat("f(\"\t\t\"); /* some\n" + " comment */", + " \t \t f(\"\t\t\"); /* some\n" + " \t \t comment */", + Tab); + verifyFormat("{\n" + "\t/*\n" + "\t * Comment\n" + "\t */\n" + "\tint i;\n" + "}", + "{\n" + "\t/*\n" + "\t * Comment\n" + "\t */\n" + "\t int i;\n" + "}", + Tab); Tab.TabWidth = 2; Tab.IndentWidth = 2; - EXPECT_EQ("{\n" - "\t/* aaaa\n" - "\t bbbb */\n" - "}", - format("{\n" - "/* aaaa\n" - " bbbb */\n" - "}", - Tab)); - EXPECT_EQ("{\n" - "\t/*\n" - "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" - "\t bbbbbbbbbbbbb\n" - "\t*/\n" - "}", - format("{\n" - "/*\n" - " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" - "*/\n" - "}", - Tab)); + verifyFormat("{\n" + "\t/* aaaa\n" + "\t bbbb */\n" + "}", + "{\n" + "/* aaaa\n" + " bbbb */\n" + "}", + Tab); + verifyFormat("{\n" + "\t/*\n" + "\t aaaaaaaaaaaaaaaaaaaaaaaaaa\n" + "\t bbbbbbbbbbbbb\n" + "\t*/\n" + "}", + "{\n" + "/*\n" + " aaaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbb\n" + "*/\n" + "}", + Tab); Tab.AlignConsecutiveAssignments.Enabled = true; Tab.AlignConsecutiveDeclarations.Enabled = true; Tab.TabWidth = 4; @@ -16205,118 +15971,118 @@ TEST_F(FormatTest, ZeroTabWidth) { Tab.IndentWidth = 8; Tab.UseTab = FormatStyle::UT_Never; Tab.TabWidth = 0; - EXPECT_EQ("void a(){\n" - " // line starts with '\t'\n" - "};", - format("void a(){\n" - "\t// line starts with '\t'\n" - "};", - Tab)); + verifyFormat("void a(){\n" + " // line starts with '\t'\n" + "};", + "void a(){\n" + "\t// line starts with '\t'\n" + "};", + Tab); - EXPECT_EQ("void a(){\n" - " // line starts with '\t'\n" - "};", - format("void a(){\n" - "\t\t// line starts with '\t'\n" - "};", - Tab)); + verifyFormat("void a(){\n" + " // line starts with '\t'\n" + "};", + "void a(){\n" + "\t\t// line starts with '\t'\n" + "};", + Tab); Tab.UseTab = FormatStyle::UT_ForIndentation; - EXPECT_EQ("void a(){\n" - " // line starts with '\t'\n" - "};", - format("void a(){\n" - "\t// line starts with '\t'\n" - "};", - Tab)); + verifyFormat("void a(){\n" + " // line starts with '\t'\n" + "};", + "void a(){\n" + "\t// line starts with '\t'\n" + "};", + Tab); - EXPECT_EQ("void a(){\n" - " // line starts with '\t'\n" - "};", - format("void a(){\n" - "\t\t// line starts with '\t'\n" - "};", - Tab)); + verifyFormat("void a(){\n" + " // line starts with '\t'\n" + "};", + "void a(){\n" + "\t\t// line starts with '\t'\n" + "};", + Tab); Tab.UseTab = FormatStyle::UT_ForContinuationAndIndentation; - EXPECT_EQ("void a(){\n" - " // line starts with '\t'\n" - "};", - format("void a(){\n" - "\t// line starts with '\t'\n" - "};", - Tab)); + verifyFormat("void a(){\n" + " // line starts with '\t'\n" + "};", + "void a(){\n" + "\t// line starts with '\t'\n" + "};", + Tab); - EXPECT_EQ("void a(){\n" - " // line starts with '\t'\n" - "};", - format("void a(){\n" - "\t\t// line starts with '\t'\n" - "};", - Tab)); + verifyFormat("void a(){\n" + " // line starts with '\t'\n" + "};", + "void a(){\n" + "\t\t// line starts with '\t'\n" + "};", + Tab); Tab.UseTab = FormatStyle::UT_AlignWithSpaces; - EXPECT_EQ("void a(){\n" - " // line starts with '\t'\n" - "};", - format("void a(){\n" - "\t// line starts with '\t'\n" - "};", - Tab)); + verifyFormat("void a(){\n" + " // line starts with '\t'\n" + "};", + "void a(){\n" + "\t// line starts with '\t'\n" + "};", + Tab); - EXPECT_EQ("void a(){\n" - " // line starts with '\t'\n" - "};", - format("void a(){\n" - "\t\t// line starts with '\t'\n" - "};", - Tab)); + verifyFormat("void a(){\n" + " // line starts with '\t'\n" + "};", + "void a(){\n" + "\t\t// line starts with '\t'\n" + "};", + Tab); Tab.UseTab = FormatStyle::UT_Always; - EXPECT_EQ("void a(){\n" - "// line starts with '\t'\n" - "};", - format("void a(){\n" - "\t// line starts with '\t'\n" - "};", - Tab)); + verifyFormat("void a(){\n" + "// line starts with '\t'\n" + "};", + "void a(){\n" + "\t// line starts with '\t'\n" + "};", + Tab); - EXPECT_EQ("void a(){\n" - "// line starts with '\t'\n" - "};", - format("void a(){\n" - "\t\t// line starts with '\t'\n" - "};", - Tab)); + verifyFormat("void a(){\n" + "// line starts with '\t'\n" + "};", + "void a(){\n" + "\t\t// line starts with '\t'\n" + "};", + Tab); } TEST_F(FormatTest, CalculatesOriginalColumn) { - EXPECT_EQ("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" - "q\"; /* some\n" - " comment */", - format(" \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" - "q\"; /* some\n" - " comment */")); - EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" - "/* some\n" - " comment */", - format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" - " /* some\n" - " comment */")); - EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" - "qqq\n" - "/* some\n" - " comment */", - format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" - "qqq\n" - " /* some\n" - " comment */")); - EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" - "wwww; /* some\n" - " comment */", - format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" - "wwww; /* some\n" - " comment */")); + verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "q\"; /* some\n" + " comment */", + " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "q\"; /* some\n" + " comment */"); + verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" + "/* some\n" + " comment */", + "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" + " /* some\n" + " comment */"); + verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "qqq\n" + "/* some\n" + " comment */", + "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "qqq\n" + " /* some\n" + " comment */"); + verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "wwww; /* some\n" + " comment */", + " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "wwww; /* some\n" + " comment */"); } TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { @@ -17520,100 +17286,100 @@ TEST_F(FormatTest, AlignConsecutiveMacros) { Style.MaxEmptyLinesToKeep = 10; Style.ReflowComments = false; Style.AlignConsecutiveMacros.AcrossComments = true; - EXPECT_EQ("#define a 3\n" - "// line comment\n" - "#define bbbb 4\n" - "#define ccc (5)", - format("#define a 3\n" - "// line comment\n" - "#define bbbb 4\n" - "#define ccc (5)", - Style)); + verifyFormat("#define a 3\n" + "// line comment\n" + "#define bbbb 4\n" + "#define ccc (5)", + "#define a 3\n" + "// line comment\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); - EXPECT_EQ("#define a 3\n" - "/* block comment */\n" - "#define bbbb 4\n" - "#define ccc (5)", - format("#define a 3\n" - "/* block comment */\n" - "#define bbbb 4\n" - "#define ccc (5)", - Style)); + verifyFormat("#define a 3\n" + "/* block comment */\n" + "#define bbbb 4\n" + "#define ccc (5)", + "#define a 3\n" + "/* block comment */\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); - EXPECT_EQ("#define a 3\n" - "/* multi-line *\n" - " * block comment */\n" - "#define bbbb 4\n" - "#define ccc (5)", - format("#define a 3\n" - "/* multi-line *\n" - " * block comment */\n" - "#define bbbb 4\n" - "#define ccc (5)", - Style)); + verifyFormat("#define a 3\n" + "/* multi-line *\n" + " * block comment */\n" + "#define bbbb 4\n" + "#define ccc (5)", + "#define a 3\n" + "/* multi-line *\n" + " * block comment */\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); - EXPECT_EQ("#define a 3\n" - "// multi-line line comment\n" - "//\n" - "#define bbbb 4\n" - "#define ccc (5)", - format("#define a 3\n" - "// multi-line line comment\n" - "//\n" - "#define bbbb 4\n" - "#define ccc (5)", - Style)); + verifyFormat("#define a 3\n" + "// multi-line line comment\n" + "//\n" + "#define bbbb 4\n" + "#define ccc (5)", + "#define a 3\n" + "// multi-line line comment\n" + "//\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); - EXPECT_EQ("#define a 3\n" - "// empty lines still break.\n" - "\n" - "#define bbbb 4\n" - "#define ccc (5)", - format("#define a 3\n" - "// empty lines still break.\n" - "\n" - "#define bbbb 4\n" - "#define ccc (5)", - Style)); + verifyFormat("#define a 3\n" + "// empty lines still break.\n" + "\n" + "#define bbbb 4\n" + "#define ccc (5)", + "#define a 3\n" + "// empty lines still break.\n" + "\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); // Test across empty lines Style.AlignConsecutiveMacros.AcrossComments = false; Style.AlignConsecutiveMacros.AcrossEmptyLines = true; - EXPECT_EQ("#define a 3\n" - "\n" - "#define bbbb 4\n" - "#define ccc (5)", - format("#define a 3\n" - "\n" - "#define bbbb 4\n" - "#define ccc (5)", - Style)); + verifyFormat("#define a 3\n" + "\n" + "#define bbbb 4\n" + "#define ccc (5)", + "#define a 3\n" + "\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); - EXPECT_EQ("#define a 3\n" - "\n" - "\n" - "\n" - "#define bbbb 4\n" - "#define ccc (5)", - format("#define a 3\n" - "\n" - "\n" - "\n" - "#define bbbb 4\n" - "#define ccc (5)", - Style)); + verifyFormat("#define a 3\n" + "\n" + "\n" + "\n" + "#define bbbb 4\n" + "#define ccc (5)", + "#define a 3\n" + "\n" + "\n" + "\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); - EXPECT_EQ("#define a 3\n" - "// comments should break alignment\n" - "//\n" - "#define bbbb 4\n" - "#define ccc (5)", - format("#define a 3\n" - "// comments should break alignment\n" - "//\n" - "#define bbbb 4\n" - "#define ccc (5)", - Style)); + verifyFormat("#define a 3\n" + "// comments should break alignment\n" + "//\n" + "#define bbbb 4\n" + "#define ccc (5)", + "#define a 3\n" + "// comments should break alignment\n" + "//\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); // Test across empty lines and comments Style.AlignConsecutiveMacros.AcrossComments = true; @@ -17624,45 +17390,45 @@ TEST_F(FormatTest, AlignConsecutiveMacros) { "#define ccc (5)", Style); - EXPECT_EQ("#define a 3\n" - "\n" - "\n" - "/* multi-line *\n" - " * block comment */\n" - "\n" - "\n" - "#define bbbb 4\n" - "#define ccc (5)", - format("#define a 3\n" - "\n" - "\n" - "/* multi-line *\n" - " * block comment */\n" - "\n" - "\n" - "#define bbbb 4\n" - "#define ccc (5)", - Style)); + verifyFormat("#define a 3\n" + "\n" + "\n" + "/* multi-line *\n" + " * block comment */\n" + "\n" + "\n" + "#define bbbb 4\n" + "#define ccc (5)", + "#define a 3\n" + "\n" + "\n" + "/* multi-line *\n" + " * block comment */\n" + "\n" + "\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); - EXPECT_EQ("#define a 3\n" - "\n" - "\n" - "/* multi-line *\n" - " * block comment */\n" - "\n" - "\n" - "#define bbbb 4\n" - "#define ccc (5)", - format("#define a 3\n" - "\n" - "\n" - "/* multi-line *\n" - " * block comment */\n" - "\n" - "\n" - "#define bbbb 4\n" - "#define ccc (5)", - Style)); + verifyFormat("#define a 3\n" + "\n" + "\n" + "/* multi-line *\n" + " * block comment */\n" + "\n" + "\n" + "#define bbbb 4\n" + "#define ccc (5)", + "#define a 3\n" + "\n" + "\n" + "/* multi-line *\n" + " * block comment */\n" + "\n" + "\n" + "#define bbbb 4\n" + "#define ccc (5)", + Style); } TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { @@ -17673,135 +17439,135 @@ TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLines) { Alignment.MaxEmptyLinesToKeep = 10; /* Test alignment across empty lines */ - EXPECT_EQ("int a = 5;\n" - "\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "int oneTwoThree= 123;", - Alignment)); - EXPECT_EQ("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;", - Alignment)); - EXPECT_EQ("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;\n" - "int oneTwo = 12;", - format("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;\n" - "int oneTwo = 12;", - Alignment)); - - /* Test across comments */ - EXPECT_EQ("int a = 5;\n" - "/* block comment */\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "/* block comment */\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "// line comment\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "// line comment\n" - "int oneTwoThree=123;", - Alignment)); - - /* Test across comments and newlines */ - EXPECT_EQ("int a = 5;\n" - "\n" - "/* block comment */\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "/* block comment */\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "\n" - "// line comment\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "// line comment\n" - "int oneTwoThree=123;", - Alignment)); -} - -TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { - FormatStyle Alignment = getLLVMStyle(); - Alignment.AlignConsecutiveDeclarations.Enabled = true; - Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true; - Alignment.AlignConsecutiveDeclarations.AcrossComments = true; + verifyFormat("int a = 5;\n" + "\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "int oneTwoThree= 123;", + Alignment); + verifyFormat("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + Alignment); + verifyFormat("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + "int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + Alignment); + + /* Test across comments */ + verifyFormat("int a = 5;\n" + "/* block comment */\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "/* block comment */\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "// line comment\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "// line comment\n" + "int oneTwoThree=123;", + Alignment); + + /* Test across comments and newlines */ + verifyFormat("int a = 5;\n" + "\n" + "/* block comment */\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "/* block comment */\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "\n" + "// line comment\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "// line comment\n" + "int oneTwoThree=123;", + Alignment); +} + +TEST_F(FormatTest, AlignConsecutiveDeclarationsAcrossEmptyLinesAndComments) { + FormatStyle Alignment = getLLVMStyle(); + Alignment.AlignConsecutiveDeclarations.Enabled = true; + Alignment.AlignConsecutiveDeclarations.AcrossEmptyLines = true; + Alignment.AlignConsecutiveDeclarations.AcrossComments = true; Alignment.MaxEmptyLinesToKeep = 10; /* Test alignment across empty lines */ - EXPECT_EQ("int a = 5;\n" - "\n" - "float const oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "float const oneTwoThree = 123;", - Alignment)); - EXPECT_EQ("int a = 5;\n" - "float const one = 1;\n" - "\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "float const one = 1;\n" - "\n" - "int oneTwoThree = 123;", - Alignment)); + verifyFormat("int a = 5;\n" + "\n" + "float const oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "float const oneTwoThree = 123;", + Alignment); + verifyFormat("int a = 5;\n" + "float const one = 1;\n" + "\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "float const one = 1;\n" + "\n" + "int oneTwoThree = 123;", + Alignment); /* Test across comments */ - EXPECT_EQ("float const a = 5;\n" - "/* block comment */\n" - "int oneTwoThree = 123;", - format("float const a = 5;\n" - "/* block comment */\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("float const a = 5;\n" - "// line comment\n" - "int oneTwoThree = 123;", - format("float const a = 5;\n" - "// line comment\n" - "int oneTwoThree=123;", - Alignment)); + verifyFormat("float const a = 5;\n" + "/* block comment */\n" + "int oneTwoThree = 123;", + "float const a = 5;\n" + "/* block comment */\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("float const a = 5;\n" + "// line comment\n" + "int oneTwoThree = 123;", + "float const a = 5;\n" + "// line comment\n" + "int oneTwoThree=123;", + Alignment); /* Test across comments and newlines */ - EXPECT_EQ("float const a = 5;\n" - "\n" - "/* block comment */\n" - "int oneTwoThree = 123;", - format("float const a = 5;\n" - "\n" - "/* block comment */\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("float const a = 5;\n" - "\n" - "// line comment\n" - "int oneTwoThree = 123;", - format("float const a = 5;\n" - "\n" - "// line comment\n" - "int oneTwoThree=123;", - Alignment)); + verifyFormat("float const a = 5;\n" + "\n" + "/* block comment */\n" + "int oneTwoThree = 123;", + "float const a = 5;\n" + "\n" + "/* block comment */\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("float const a = 5;\n" + "\n" + "// line comment\n" + "int oneTwoThree = 123;", + "float const a = 5;\n" + "\n" + "// line comment\n" + "int oneTwoThree=123;", + Alignment); } TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { @@ -17812,64 +17578,64 @@ TEST_F(FormatTest, AlignConsecutiveBitFieldsAcrossEmptyLinesAndComments) { Alignment.MaxEmptyLinesToKeep = 10; /* Test alignment across empty lines */ - EXPECT_EQ("int a : 5;\n" - "\n" - "int longbitfield : 6;", - format("int a : 5;\n" - "\n" - "int longbitfield : 6;", - Alignment)); - EXPECT_EQ("int a : 5;\n" - "int one : 1;\n" - "\n" - "int longbitfield : 6;", - format("int a : 5;\n" - "int one : 1;\n" - "\n" - "int longbitfield : 6;", - Alignment)); + verifyFormat("int a : 5;\n" + "\n" + "int longbitfield : 6;", + "int a : 5;\n" + "\n" + "int longbitfield : 6;", + Alignment); + verifyFormat("int a : 5;\n" + "int one : 1;\n" + "\n" + "int longbitfield : 6;", + "int a : 5;\n" + "int one : 1;\n" + "\n" + "int longbitfield : 6;", + Alignment); /* Test across comments */ - EXPECT_EQ("int a : 5;\n" - "/* block comment */\n" - "int longbitfield : 6;", - format("int a : 5;\n" - "/* block comment */\n" - "int longbitfield : 6;", - Alignment)); - EXPECT_EQ("int a : 5;\n" - "int one : 1;\n" - "// line comment\n" - "int longbitfield : 6;", - format("int a : 5;\n" - "int one : 1;\n" - "// line comment\n" - "int longbitfield : 6;", - Alignment)); + verifyFormat("int a : 5;\n" + "/* block comment */\n" + "int longbitfield : 6;", + "int a : 5;\n" + "/* block comment */\n" + "int longbitfield : 6;", + Alignment); + verifyFormat("int a : 5;\n" + "int one : 1;\n" + "// line comment\n" + "int longbitfield : 6;", + "int a : 5;\n" + "int one : 1;\n" + "// line comment\n" + "int longbitfield : 6;", + Alignment); /* Test across comments and newlines */ - EXPECT_EQ("int a : 5;\n" - "/* block comment */\n" - "\n" - "int longbitfield : 6;", - format("int a : 5;\n" - "/* block comment */\n" - "\n" - "int longbitfield : 6;", - Alignment)); - EXPECT_EQ("int a : 5;\n" - "int one : 1;\n" - "\n" - "// line comment\n" - "\n" - "int longbitfield : 6;", - format("int a : 5;\n" - "int one : 1;\n" - "\n" - "// line comment \n" - "\n" - "int longbitfield : 6;", - Alignment)); + verifyFormat("int a : 5;\n" + "/* block comment */\n" + "\n" + "int longbitfield : 6;", + "int a : 5;\n" + "/* block comment */\n" + "\n" + "int longbitfield : 6;", + Alignment); + verifyFormat("int a : 5;\n" + "int one : 1;\n" + "\n" + "// line comment\n" + "\n" + "int longbitfield : 6;", + "int a : 5;\n" + "int one : 1;\n" + "\n" + "// line comment \n" + "\n" + "int longbitfield : 6;", + Alignment); } TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { @@ -17880,84 +17646,84 @@ TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossComments) { Alignment.MaxEmptyLinesToKeep = 10; /* Test alignment across empty lines */ - EXPECT_EQ("int a = 5;\n" - "\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "int oneTwoThree= 123;", - Alignment)); - EXPECT_EQ("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;", - Alignment)); + verifyFormat("int a = 5;\n" + "\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "int oneTwoThree= 123;", + Alignment); + verifyFormat("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + Alignment); /* Test across comments */ - EXPECT_EQ("int a = 5;\n" - "/* block comment */\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "/* block comment */\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "// line comment\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "// line comment\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "/*\n" - " * multi-line block comment\n" - " */\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "/*\n" - " * multi-line block comment\n" - " */\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "//\n" - "// multi-line line comment\n" - "//\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "//\n" - "// multi-line line comment\n" - "//\n" - "int oneTwoThree=123;", - Alignment)); + verifyFormat("int a = 5;\n" + "/* block comment */\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "/* block comment */\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "// line comment\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "// line comment\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "/*\n" + " * multi-line block comment\n" + " */\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "/*\n" + " * multi-line block comment\n" + " */\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "//\n" + "// multi-line line comment\n" + "//\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "//\n" + "// multi-line line comment\n" + "//\n" + "int oneTwoThree=123;", + Alignment); /* Test across comments and newlines */ - EXPECT_EQ("int a = 5;\n" - "\n" - "/* block comment */\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "/* block comment */\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "\n" - "// line comment\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "// line comment\n" - "int oneTwoThree=123;", - Alignment)); + verifyFormat("int a = 5;\n" + "\n" + "/* block comment */\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "/* block comment */\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "\n" + "// line comment\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "// line comment\n" + "int oneTwoThree=123;", + Alignment); } TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { @@ -18034,127 +17800,127 @@ TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { Alignment.MaxEmptyLinesToKeep = 10; /* Test alignment across empty lines */ - EXPECT_EQ("int a = 5;\n" - "\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "int oneTwoThree= 123;", - Alignment)); - EXPECT_EQ("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;", - Alignment)); - EXPECT_EQ("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;\n" - "int oneTwo = 12;", - format("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;\n" - "int oneTwo = 12;", - Alignment)); + verifyFormat("int a = 5;\n" + "\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "int oneTwoThree= 123;", + Alignment); + verifyFormat("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + Alignment); + verifyFormat("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + "int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + Alignment); /* Test across comments */ - EXPECT_EQ("int a = 5;\n" - "/* block comment */\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "/* block comment */\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "// line comment\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "// line comment\n" - "int oneTwoThree=123;", - Alignment)); + verifyFormat("int a = 5;\n" + "/* block comment */\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "/* block comment */\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "// line comment\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "// line comment\n" + "int oneTwoThree=123;", + Alignment); /* Test across comments and newlines */ - EXPECT_EQ("int a = 5;\n" - "\n" - "/* block comment */\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "/* block comment */\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "\n" - "// line comment\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "// line comment\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "//\n" - "// multi-line line comment\n" - "//\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "//\n" - "// multi-line line comment\n" - "//\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "/*\n" - " * multi-line block comment\n" - " */\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "/*\n" - " * multi-line block comment\n" - " */\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "\n" - "/* block comment */\n" - "\n" - "\n" - "\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "/* block comment */\n" - "\n" - "\n" - "\n" - "int oneTwoThree=123;", - Alignment)); - - EXPECT_EQ("int a = 5;\n" - "\n" - "// line comment\n" - "\n" - "\n" - "\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "// line comment\n" - "\n" - "\n" - "\n" - "int oneTwoThree=123;", - Alignment)); + verifyFormat("int a = 5;\n" + "\n" + "/* block comment */\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "/* block comment */\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "\n" + "// line comment\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "// line comment\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "//\n" + "// multi-line line comment\n" + "//\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "//\n" + "// multi-line line comment\n" + "//\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "/*\n" + " * multi-line block comment\n" + " */\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "/*\n" + " * multi-line block comment\n" + " */\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "\n" + "/* block comment */\n" + "\n" + "\n" + "\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "/* block comment */\n" + "\n" + "\n" + "\n" + "int oneTwoThree=123;", + Alignment); + + verifyFormat("int a = 5;\n" + "\n" + "// line comment\n" + "\n" + "\n" + "\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "// line comment\n" + "\n" + "\n" + "\n" + "int oneTwoThree=123;", + Alignment); Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; verifyFormat("#define A \\\n" @@ -18267,13 +18033,13 @@ TEST_F(FormatTest, AlignConsecutiveAssignmentsAcrossEmptyLinesAndComments) { Alignment.ReflowComments = true; Alignment.ColumnLimit = 50; - EXPECT_EQ("int x = 0;\n" - "int yy = 1; /// specificlennospace\n" - "int zzz = 2;", - format("int x = 0;\n" - "int yy = 1; ///specificlennospace\n" - "int zzz = 2;", - Alignment)); + verifyFormat("int x = 0;\n" + "int yy = 1; /// specificlennospace\n" + "int zzz = 2;", + "int x = 0;\n" + "int yy = 1; ///specificlennospace\n" + "int zzz = 2;", + Alignment); } TEST_F(FormatTest, AlignCompoundAssignments) { @@ -18325,82 +18091,82 @@ TEST_F(FormatTest, AlignCompoundAssignments) { "dvsdsv <<= 5;\n" "int dsvvdvsdvvv = 123;", Alignment); - EXPECT_EQ("a += 5;\n" - "one = 1;\n" - "\n" - "oneTwoThree = 123;", - format("a += 5;\n" - "one = 1;\n" - "\n" - "oneTwoThree = 123;", - Alignment)); - EXPECT_EQ("a += 5;\n" - "one = 1;\n" - "//\n" - "oneTwoThree = 123;", - format("a += 5;\n" - "one = 1;\n" - "//\n" - "oneTwoThree = 123;", - Alignment)); + verifyFormat("a += 5;\n" + "one = 1;\n" + "\n" + "oneTwoThree = 123;", + "a += 5;\n" + "one = 1;\n" + "\n" + "oneTwoThree = 123;", + Alignment); + verifyFormat("a += 5;\n" + "one = 1;\n" + "//\n" + "oneTwoThree = 123;", + "a += 5;\n" + "one = 1;\n" + "//\n" + "oneTwoThree = 123;", + Alignment); Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; - EXPECT_EQ("a += 5;\n" - "one = 1;\n" - "\n" - "oneTwoThree = 123;", - format("a += 5;\n" - "one = 1;\n" - "\n" - "oneTwoThree = 123;", - Alignment)); - EXPECT_EQ("a += 5;\n" - "one = 1;\n" - "//\n" - "oneTwoThree = 123;", - format("a += 5;\n" - "one = 1;\n" - "//\n" - "oneTwoThree = 123;", - Alignment)); + verifyFormat("a += 5;\n" + "one = 1;\n" + "\n" + "oneTwoThree = 123;", + "a += 5;\n" + "one = 1;\n" + "\n" + "oneTwoThree = 123;", + Alignment); + verifyFormat("a += 5;\n" + "one = 1;\n" + "//\n" + "oneTwoThree = 123;", + "a += 5;\n" + "one = 1;\n" + "//\n" + "oneTwoThree = 123;", + Alignment); Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = false; Alignment.AlignConsecutiveAssignments.AcrossComments = true; - EXPECT_EQ("a += 5;\n" - "one = 1;\n" - "\n" - "oneTwoThree = 123;", - format("a += 5;\n" - "one = 1;\n" - "\n" - "oneTwoThree = 123;", - Alignment)); - EXPECT_EQ("a += 5;\n" - "one = 1;\n" - "//\n" - "oneTwoThree = 123;", - format("a += 5;\n" - "one = 1;\n" - "//\n" - "oneTwoThree = 123;", - Alignment)); + verifyFormat("a += 5;\n" + "one = 1;\n" + "\n" + "oneTwoThree = 123;", + "a += 5;\n" + "one = 1;\n" + "\n" + "oneTwoThree = 123;", + Alignment); + verifyFormat("a += 5;\n" + "one = 1;\n" + "//\n" + "oneTwoThree = 123;", + "a += 5;\n" + "one = 1;\n" + "//\n" + "oneTwoThree = 123;", + Alignment); Alignment.AlignConsecutiveAssignments.AcrossEmptyLines = true; - EXPECT_EQ("a += 5;\n" - "one >>= 1;\n" - "\n" - "oneTwoThree = 123;", - format("a += 5;\n" - "one >>= 1;\n" - "\n" - "oneTwoThree = 123;", - Alignment)); - EXPECT_EQ("a += 5;\n" - "one = 1;\n" - "//\n" - "oneTwoThree <<= 123;", - format("a += 5;\n" - "one = 1;\n" - "//\n" - "oneTwoThree <<= 123;", - Alignment)); + verifyFormat("a += 5;\n" + "one >>= 1;\n" + "\n" + "oneTwoThree = 123;", + "a += 5;\n" + "one >>= 1;\n" + "\n" + "oneTwoThree = 123;", + Alignment); + verifyFormat("a += 5;\n" + "one = 1;\n" + "//\n" + "oneTwoThree <<= 123;", + "a += 5;\n" + "one = 1;\n" + "//\n" + "oneTwoThree <<= 123;", + Alignment); } TEST_F(FormatTest, AlignConsecutiveAssignments) { @@ -18527,33 +18293,33 @@ TEST_F(FormatTest, AlignConsecutiveAssignments) { Alignment); */ - EXPECT_EQ("int a = 5;\n" - "\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "\n" - "int oneTwoThree= 123;", - Alignment)); - EXPECT_EQ("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;", - format("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;", - Alignment)); - EXPECT_EQ("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;\n" - "int oneTwo = 12;", - format("int a = 5;\n" - "int one = 1;\n" - "\n" - "int oneTwoThree = 123;\n" - "int oneTwo = 12;", - Alignment)); + verifyFormat("int a = 5;\n" + "\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "\n" + "int oneTwoThree= 123;", + Alignment); + verifyFormat("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + "int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + Alignment); + verifyFormat("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + "int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + Alignment); Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; verifyFormat("#define A \\\n" " int aaaa = 12; \\\n" @@ -18665,13 +18431,13 @@ TEST_F(FormatTest, AlignConsecutiveAssignments) { EXPECT_EQ(Alignment.ReflowComments, true); Alignment.ColumnLimit = 50; - EXPECT_EQ("int x = 0;\n" - "int yy = 1; /// specificlennospace\n" - "int zzz = 2;", - format("int x = 0;\n" - "int yy = 1; ///specificlennospace\n" - "int zzz = 2;", - Alignment)); + verifyFormat("int x = 0;\n" + "int yy = 1; /// specificlennospace\n" + "int zzz = 2;", + "int x = 0;\n" + "int yy = 1; ///specificlennospace\n" + "int zzz = 2;", + Alignment); verifyFormat("auto aaaaaaaaaaaaaaaaaaaaa = {};\n" "auto b = [] {\n" @@ -18845,33 +18611,33 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { "const unsigned g;\n" "Const unsigned h;", Alignment); - EXPECT_EQ("float const a = 5;\n" - "\n" - "int oneTwoThree = 123;", - format("float const a = 5;\n" - "\n" - "int oneTwoThree= 123;", - Alignment)); - EXPECT_EQ("float a = 5;\n" - "int one = 1;\n" - "\n" - "unsigned oneTwoThree = 123;", - format("float a = 5;\n" - "int one = 1;\n" - "\n" - "unsigned oneTwoThree = 123;", - Alignment)); - EXPECT_EQ("float a = 5;\n" - "int one = 1;\n" - "\n" - "unsigned oneTwoThree = 123;\n" - "int oneTwo = 12;", - format("float a = 5;\n" - "int one = 1;\n" - "\n" - "unsigned oneTwoThree = 123;\n" - "int oneTwo = 12;", - Alignment)); + verifyFormat("float const a = 5;\n" + "\n" + "int oneTwoThree = 123;", + "float const a = 5;\n" + "\n" + "int oneTwoThree= 123;", + Alignment); + verifyFormat("float a = 5;\n" + "int one = 1;\n" + "\n" + "unsigned oneTwoThree = 123;", + "float a = 5;\n" + "int one = 1;\n" + "\n" + "unsigned oneTwoThree = 123;", + Alignment); + verifyFormat("float a = 5;\n" + "int one = 1;\n" + "\n" + "unsigned oneTwoThree = 123;\n" + "int oneTwo = 12;", + "float a = 5;\n" + "int one = 1;\n" + "\n" + "unsigned oneTwoThree = 123;\n" + "int oneTwo = 12;", + Alignment); // Function prototype alignment verifyFormat("int a();\n" "double b();", @@ -18883,25 +18649,25 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { // We need to set ColumnLimit to zero, in order to stress nested alignments, // otherwise the function parameters will be re-flowed onto a single line. Alignment.ColumnLimit = 0; - EXPECT_EQ("int a(int x,\n" - " float y);\n" - "double b(int x,\n" - " double y);", - format("int a(int x,\n" - " float y);\n" - "double b(int x,\n" - " double y);", - Alignment)); + verifyFormat("int a(int x,\n" + " float y);\n" + "double b(int x,\n" + " double y);", + "int a(int x,\n" + " float y);\n" + "double b(int x,\n" + " double y);", + Alignment); // This ensures that function parameters of function declarations are // correctly indented when their owning functions are indented. // The failure case here is for 'double y' to not be indented enough. - EXPECT_EQ("double a(int x);\n" - "int b(int y,\n" - " double z);", - format("double a(int x);\n" - "int b(int y,\n" - " double z);", - Alignment)); + verifyFormat("double a(int x);\n" + "int b(int y,\n" + " double z);", + "double a(int x);\n" + "int b(int y,\n" + " double z);", + Alignment); // Set ColumnLimit low so that we induce wrapping immediately after // the function name and opening paren. Alignment.ColumnLimit = 13; @@ -18961,56 +18727,56 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { Alignment); // PAS_Right - EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" - " int const i = 1;\n" - " int *j = 2;\n" - " int big = 10000;\n" - "\n" - " unsigned oneTwoThree = 123;\n" - " int oneTwo = 12;\n" - " method();\n" - " float k = 2;\n" - " int ll = 10000;\n" - "}", - format("void SomeFunction(int parameter= 0) {\n" - " int const i= 1;\n" - " int *j=2;\n" - " int big = 10000;\n" - "\n" - "unsigned oneTwoThree =123;\n" - "int oneTwo = 12;\n" - " method();\n" - "float k= 2;\n" - "int ll=10000;\n" - "}", - Alignment)); - EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" - " int const i = 1;\n" - " int **j = 2, ***k;\n" - " int &k = i;\n" - " int &&l = i + j;\n" - " int big = 10000;\n" - "\n" - " unsigned oneTwoThree = 123;\n" - " int oneTwo = 12;\n" - " method();\n" - " float k = 2;\n" - " int ll = 10000;\n" - "}", - format("void SomeFunction(int parameter= 0) {\n" - " int const i= 1;\n" - " int **j=2,***k;\n" - "int &k=i;\n" - "int &&l=i+j;\n" - " int big = 10000;\n" - "\n" - "unsigned oneTwoThree =123;\n" - "int oneTwo = 12;\n" - " method();\n" - "float k= 2;\n" - "int ll=10000;\n" - "}", - Alignment)); + verifyFormat("void SomeFunction(int parameter = 0) {\n" + " int const i = 1;\n" + " int *j = 2;\n" + " int big = 10000;\n" + "\n" + " unsigned oneTwoThree = 123;\n" + " int oneTwo = 12;\n" + " method();\n" + " float k = 2;\n" + " int ll = 10000;\n" + "}", + "void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 10000;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=10000;\n" + "}", + Alignment); + verifyFormat("void SomeFunction(int parameter = 0) {\n" + " int const i = 1;\n" + " int **j = 2, ***k;\n" + " int &k = i;\n" + " int &&l = i + j;\n" + " int big = 10000;\n" + "\n" + " unsigned oneTwoThree = 123;\n" + " int oneTwo = 12;\n" + " method();\n" + " float k = 2;\n" + " int ll = 10000;\n" + "}", + "void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2,***k;\n" + "int &k=i;\n" + "int &&l=i+j;\n" + " int big = 10000;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=10000;\n" + "}", + Alignment); // variables are aligned at their name, pointers are at the right most // position verifyFormat("int *a;\n" @@ -19022,56 +18788,56 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { // PAS_Left FormatStyle AlignmentLeft = Alignment; AlignmentLeft.PointerAlignment = FormatStyle::PAS_Left; - EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" - " int const i = 1;\n" - " int* j = 2;\n" - " int big = 10000;\n" - "\n" - " unsigned oneTwoThree = 123;\n" - " int oneTwo = 12;\n" - " method();\n" - " float k = 2;\n" - " int ll = 10000;\n" - "}", - format("void SomeFunction(int parameter= 0) {\n" - " int const i= 1;\n" - " int *j=2;\n" - " int big = 10000;\n" - "\n" - "unsigned oneTwoThree =123;\n" - "int oneTwo = 12;\n" - " method();\n" - "float k= 2;\n" - "int ll=10000;\n" - "}", - AlignmentLeft)); - EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" - " int const i = 1;\n" - " int** j = 2;\n" - " int& k = i;\n" - " int&& l = i + j;\n" - " int big = 10000;\n" - "\n" - " unsigned oneTwoThree = 123;\n" - " int oneTwo = 12;\n" - " method();\n" - " float k = 2;\n" - " int ll = 10000;\n" - "}", - format("void SomeFunction(int parameter= 0) {\n" - " int const i= 1;\n" - " int **j=2;\n" - "int &k=i;\n" - "int &&l=i+j;\n" - " int big = 10000;\n" - "\n" - "unsigned oneTwoThree =123;\n" - "int oneTwo = 12;\n" - " method();\n" - "float k= 2;\n" - "int ll=10000;\n" - "}", - AlignmentLeft)); + verifyFormat("void SomeFunction(int parameter = 0) {\n" + " int const i = 1;\n" + " int* j = 2;\n" + " int big = 10000;\n" + "\n" + " unsigned oneTwoThree = 123;\n" + " int oneTwo = 12;\n" + " method();\n" + " float k = 2;\n" + " int ll = 10000;\n" + "}", + "void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 10000;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=10000;\n" + "}", + AlignmentLeft); + verifyFormat("void SomeFunction(int parameter = 0) {\n" + " int const i = 1;\n" + " int** j = 2;\n" + " int& k = i;\n" + " int&& l = i + j;\n" + " int big = 10000;\n" + "\n" + " unsigned oneTwoThree = 123;\n" + " int oneTwo = 12;\n" + " method();\n" + " float k = 2;\n" + " int ll = 10000;\n" + "}", + "void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2;\n" + "int &k=i;\n" + "int &&l=i+j;\n" + " int big = 10000;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=10000;\n" + "}", + AlignmentLeft); // variables are aligned at their name, pointers are at the left most position verifyFormat("int* a;\n" "int** b;\n" @@ -19082,56 +18848,56 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { // PAS_Middle FormatStyle AlignmentMiddle = Alignment; AlignmentMiddle.PointerAlignment = FormatStyle::PAS_Middle; - EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" - " int const i = 1;\n" - " int * j = 2;\n" - " int big = 10000;\n" - "\n" - " unsigned oneTwoThree = 123;\n" - " int oneTwo = 12;\n" - " method();\n" - " float k = 2;\n" - " int ll = 10000;\n" - "}", - format("void SomeFunction(int parameter= 0) {\n" - " int const i= 1;\n" - " int *j=2;\n" - " int big = 10000;\n" - "\n" - "unsigned oneTwoThree =123;\n" - "int oneTwo = 12;\n" - " method();\n" - "float k= 2;\n" - "int ll=10000;\n" - "}", - AlignmentMiddle)); - EXPECT_EQ("void SomeFunction(int parameter = 0) {\n" - " int const i = 1;\n" - " int ** j = 2, ***k;\n" - " int & k = i;\n" - " int && l = i + j;\n" - " int big = 10000;\n" - "\n" - " unsigned oneTwoThree = 123;\n" - " int oneTwo = 12;\n" - " method();\n" - " float k = 2;\n" - " int ll = 10000;\n" - "}", - format("void SomeFunction(int parameter= 0) {\n" - " int const i= 1;\n" - " int **j=2,***k;\n" - "int &k=i;\n" - "int &&l=i+j;\n" - " int big = 10000;\n" - "\n" - "unsigned oneTwoThree =123;\n" - "int oneTwo = 12;\n" - " method();\n" - "float k= 2;\n" - "int ll=10000;\n" - "}", - AlignmentMiddle)); + verifyFormat("void SomeFunction(int parameter = 0) {\n" + " int const i = 1;\n" + " int * j = 2;\n" + " int big = 10000;\n" + "\n" + " unsigned oneTwoThree = 123;\n" + " int oneTwo = 12;\n" + " method();\n" + " float k = 2;\n" + " int ll = 10000;\n" + "}", + "void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int *j=2;\n" + " int big = 10000;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=10000;\n" + "}", + AlignmentMiddle); + verifyFormat("void SomeFunction(int parameter = 0) {\n" + " int const i = 1;\n" + " int ** j = 2, ***k;\n" + " int & k = i;\n" + " int && l = i + j;\n" + " int big = 10000;\n" + "\n" + " unsigned oneTwoThree = 123;\n" + " int oneTwo = 12;\n" + " method();\n" + " float k = 2;\n" + " int ll = 10000;\n" + "}", + "void SomeFunction(int parameter= 0) {\n" + " int const i= 1;\n" + " int **j=2,***k;\n" + "int &k=i;\n" + "int &&l=i+j;\n" + " int big = 10000;\n" + "\n" + "unsigned oneTwoThree =123;\n" + "int oneTwo = 12;\n" + " method();\n" + "float k= 2;\n" + "int ll=10000;\n" + "}", + AlignmentMiddle); // variables are aligned at their name, pointers are in the middle verifyFormat("int * a;\n" "int * b;\n" @@ -19308,9 +19074,9 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) { // See PR37175 FormatStyle Style = getMozillaStyle(); Style.AlignConsecutiveDeclarations.Enabled = true; - EXPECT_EQ("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" - "foo(int a);", - format("DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style)); + verifyFormat("DECOR1 /**/ int8_t /**/ DECOR2 /**/\n" + "foo(int a);", + "DECOR1 /**/ int8_t /**/ DECOR2 /**/ foo (int a);", Style); Alignment.PointerAlignment = FormatStyle::PAS_Left; verifyFormat("unsigned int* a;\n" @@ -19950,17 +19716,17 @@ TEST_F(FormatTest, AllmanBraceBreaking) { FormatStyle AllmanBraceStyle = getLLVMStyle(); AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; - EXPECT_EQ("namespace a\n" - "{\n" - "void f();\n" - "void g();\n" - "} // namespace a", - format("namespace a\n" - "{\n" - "void f();\n" - "void g();\n" - "}", - AllmanBraceStyle)); + verifyFormat("namespace a\n" + "{\n" + "void f();\n" + "void g();\n" + "} // namespace a", + "namespace a\n" + "{\n" + "void f();\n" + "void g();\n" + "}", + AllmanBraceStyle); verifyFormat("namespace a\n" "{\n" @@ -20887,7 +20653,7 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { Style); Style.ColumnLimit = 0; - EXPECT_EQ( + verifyFormat( "test demo[] = {\n" " {56, 23, \"hello world i am a very long line that really, " "in any just world, ought to be split over multiple lines\"},\n" @@ -20896,10 +20662,10 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { " { 7, 5, " " \"!!\"},\n" "};", - format("test demo[] = {{56, 23, \"hello world i am a very long line " - "that really, in any just world, ought to be split over multiple " - "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", - Style)); + "test demo[] = {{56, 23, \"hello world i am a very long line " + "that really, in any just world, ought to be split over multiple " + "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", + Style); Style.ColumnLimit = 80; verifyFormat("test demo[] = {\n" @@ -20924,35 +20690,34 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { Style); Style.ColumnLimit = 20; - EXPECT_EQ( - "demo = std::array<\n" - " struct test, 3>{\n" - " test{\n" - " 56, 23,\n" - " \"hello \"\n" - " \"world i \"\n" - " \"am a very \"\n" - " \"long line \"\n" - " \"that \"\n" - " \"really, \"\n" - " \"in any \"\n" - " \"just \"\n" - " \"world, \"\n" - " \"ought to \"\n" - " \"be split \"\n" - " \"over \"\n" - " \"multiple \"\n" - " \"lines\"},\n" - " test{-1, 93463,\n" - " \"world\"},\n" - " test{ 7, 5,\n" - " \"!!\" },\n" - "};", - format("demo = std::array{test{56, 23, \"hello world " - "i am a very long line that really, in any just world, ought " - "to be split over multiple lines\"},test{-1, 93463, \"world\"}," - "test{7, 5, \"!!\"},};", - Style)); + verifyFormat("demo = std::array<\n" + " struct test, 3>{\n" + " test{\n" + " 56, 23,\n" + " \"hello \"\n" + " \"world i \"\n" + " \"am a very \"\n" + " \"long line \"\n" + " \"that \"\n" + " \"really, \"\n" + " \"in any \"\n" + " \"just \"\n" + " \"world, \"\n" + " \"ought to \"\n" + " \"be split \"\n" + " \"over \"\n" + " \"multiple \"\n" + " \"lines\"},\n" + " test{-1, 93463,\n" + " \"world\"},\n" + " test{ 7, 5,\n" + " \"!!\" },\n" + "};", + "demo = std::array{test{56, 23, \"hello world " + "i am a very long line that really, in any just world, ought " + "to be split over multiple lines\"},test{-1, 93463, \"world\"}," + "test{7, 5, \"!!\"},};", + Style); // This caused a core dump by enabling Alignment in the LLVMStyle globally Style = getLLVMStyleWithColumns(50); Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; @@ -20970,7 +20735,7 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { "};", Style); Style.ColumnLimit = 100; - EXPECT_EQ( + verifyFormat( "test demo[] = {\n" " {56, 23,\n" " \"hello world i am a very long line that really, in any just world" @@ -20979,10 +20744,10 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { " {-1, 93463, \"world\"},\n" " { 7, 5, \"!!\"},\n" "};", - format("test demo[] = {{56, 23, \"hello world i am a very long line " - "that really, in any just world, ought to be split over multiple " - "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", - Style)); + "test demo[] = {{56, 23, \"hello world i am a very long line " + "that really, in any just world, ought to be split over multiple " + "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", + Style); Style = getLLVMStyleWithColumns(50); Style.AlignArrayOfStructures = FormatStyle::AIAS_Right; @@ -21011,7 +20776,7 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { " {234, 5, 1, \"gracious\"} // fourth line\n" "};", Style); - EXPECT_EQ( + verifyFormat( "test demo[] = {\n" " {56,\n" " \"hello world i am a very long line that really, in any just world" @@ -21020,10 +20785,10 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresRightAlignment) { " {-1, \"world\", 93463},\n" " { 7, \"!!\", 5},\n" "};", - format("test demo[] = {{56, \"hello world i am a very long line " - "that really, in any just world, ought to be split over multiple " - "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", - Style)); + "test demo[] = {{56, \"hello world i am a very long line " + "that really, in any just world, ought to be split over multiple " + "lines\", 23},{-1, \"world\", 93463},{7, \"!!\", 5},};", + Style); } TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { @@ -21119,7 +20884,7 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { Style); Style.ColumnLimit = 0; - EXPECT_EQ( + verifyFormat( "test demo[] = {\n" " {56, 23, \"hello world i am a very long line that really, in any " "just world, ought to be split over multiple lines\"},\n" @@ -21128,10 +20893,10 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { " {7, 5, \"!!\" " " },\n" "};", - format("test demo[] = {{56, 23, \"hello world i am a very long line " - "that really, in any just world, ought to be split over multiple " - "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", - Style)); + "test demo[] = {{56, 23, \"hello world i am a very long line " + "that really, in any just world, ought to be split over multiple " + "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", + Style); Style.ColumnLimit = 80; verifyFormat("test demo[] = {\n" @@ -21156,6 +20921,7 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { Style); Style.ColumnLimit = 20; + // FIXME: unstable test case EXPECT_EQ( "demo = std::array<\n" " struct test, 3>{\n" @@ -21195,7 +20961,7 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { "};", Style); Style.ColumnLimit = 100; - EXPECT_EQ( + verifyFormat( "test demo[] = {\n" " {56, 23,\n" " \"hello world i am a very long line that really, in any just world" @@ -21204,25 +20970,25 @@ TEST_F(FormatTest, CatchAlignArrayOfStructuresLeftAlignment) { " {-1, 93463, \"world\"},\n" " {7, 5, \"!!\" },\n" "};", - format("test demo[] = {{56, 23, \"hello world i am a very long line " - "that really, in any just world, ought to be split over multiple " - "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", - Style)); + "test demo[] = {{56, 23, \"hello world i am a very long line " + "that really, in any just world, ought to be split over multiple " + "lines\"},{-1, 93463, \"world\"},{7, 5, \"!!\"},};", + Style); } TEST_F(FormatTest, UnderstandsPragmas) { verifyFormat("#pragma omp reduction(| : var)"); verifyFormat("#pragma omp reduction(+ : var)"); - EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " - "(including parentheses).", - format("#pragma mark Any non-hyphenated or hyphenated string " - "(including parentheses).")); + verifyFormat("#pragma mark Any non-hyphenated or hyphenated string " + "(including parentheses).", + "#pragma mark Any non-hyphenated or hyphenated string " + "(including parentheses)."); - EXPECT_EQ("#pragma mark Any non-hyphenated or hyphenated string " - "(including parentheses).", - format("#pragma mark Any non-hyphenated or hyphenated string " - "(including parentheses).")); + verifyFormat("#pragma mark Any non-hyphenated or hyphenated string " + "(including parentheses).", + "#pragma mark Any non-hyphenated or hyphenated string " + "(including parentheses)."); EXPECT_EQ( "#pragma comment(linker, \\\n" @@ -21241,7 +21007,7 @@ TEST_F(FormatTest, UnderstandsPragmaOmpTarget) { verifyFormat("#pragma omp target map(to : var[0 : N])"); verifyFormat("#pragma omp target map(always, to : var[0 : N])"); - EXPECT_EQ( + verifyFormat( "#pragma omp target \\\n" " reduction(+ : var) \\\n" " map(to : A[0 : N]) \\\n" @@ -21250,16 +21016,15 @@ TEST_F(FormatTest, UnderstandsPragmaOmpTarget) { " firstprivate(i) \\\n" " firstprivate(j) \\\n" " firstprivate(k)", - format( - "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) " - "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)", - getLLVMStyleWithColumns(26))); + "#pragma omp target reduction(+:var) map(to:A[0:N]) map(to:B[0:N]) " + "map(from:C[0:N]) firstprivate(i) firstprivate(j) firstprivate(k)", + getLLVMStyleWithColumns(26)); } TEST_F(FormatTest, UnderstandPragmaOption) { verifyFormat("#pragma option -C -A"); - EXPECT_EQ("#pragma option -C -A", format("#pragma option -C -A")); + verifyFormat("#pragma option -C -A", "#pragma option -C -A"); } TEST_F(FormatTest, UnderstandPragmaRegion) { @@ -21272,96 +21037,85 @@ TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { FormatStyle Style = getLLVMStyleWithColumns(20); // See PR41213 - EXPECT_EQ("/*\n" - " *\t9012345\n" - " * /8901\n" - " */", - format("/*\n" - " *\t9012345 /8901\n" - " */", - Style)); - EXPECT_EQ("/*\n" - " *345678\n" - " *\t/8901\n" - " */", - format("/*\n" - " *345678\t/8901\n" - " */", - Style)); + verifyFormat("/*\n" + " *\t9012345\n" + " * /8901\n" + " */", + "/*\n" + " *\t9012345 /8901\n" + " */", + Style); + verifyFormat("/*\n" + " *345678\n" + " *\t/8901\n" + " */", + "/*\n" + " *345678\t/8901\n" + " */", + Style); verifyFormat("int a; // the\n" " // comment", Style); - EXPECT_EQ("int a; /* first line\n" - " * second\n" - " * line third\n" - " * line\n" - " */", - format("int a; /* first line\n" - " * second\n" - " * line third\n" - " * line\n" - " */", - Style)); - EXPECT_EQ("int a; // first line\n" - " // second\n" - " // line third\n" - " // line", - format("int a; // first line\n" - " // second line\n" - " // third line", - Style)); + verifyNoChange("int a; /* first line\n" + " * second\n" + " * line third\n" + " * line\n" + " */", + Style); + verifyFormat("int a; // first line\n" + " // second\n" + " // line third\n" + " // line", + "int a; // first line\n" + " // second line\n" + " // third line", + Style); Style.PenaltyExcessCharacter = 90; verifyFormat("int a; // the comment", Style); - EXPECT_EQ("int a; // the comment\n" - " // aaa", - format("int a; // the comment aaa", Style)); - EXPECT_EQ("int a; /* first line\n" - " * second line\n" - " * third line\n" - " */", - format("int a; /* first line\n" - " * second line\n" - " * third line\n" - " */", - Style)); - EXPECT_EQ("int a; // first line\n" - " // second line\n" - " // third line", - format("int a; // first line\n" - " // second line\n" - " // third line", - Style)); + verifyFormat("int a; // the comment\n" + " // aaa", + "int a; // the comment aaa", Style); + verifyNoChange("int a; /* first line\n" + " * second line\n" + " * third line\n" + " */", + Style); + verifyFormat("int a; // first line\n" + " // second line\n" + " // third line", + Style); // FIXME: Investigate why this is not getting the same layout as the test // above. - EXPECT_EQ("int a; /* first line\n" - " * second line\n" - " * third line\n" - " */", - format("int a; /* first line second line third line" - "\n*/", - Style)); + verifyFormat("int a; /* first line\n" + " * second line\n" + " * third line\n" + " */", + "int a; /* first line second line third line" + "\n*/", + Style); - EXPECT_EQ("// foo bar baz bazfoo\n" - "// foo bar foo bar", - format("// foo bar baz bazfoo\n" - "// foo bar foo bar", - Style)); - EXPECT_EQ("// foo bar baz bazfoo\n" - "// foo bar foo bar", - format("// foo bar baz bazfoo\n" - "// foo bar foo bar", - Style)); + verifyFormat("// foo bar baz bazfoo\n" + "// foo bar foo bar", + "// foo bar baz bazfoo\n" + "// foo bar foo bar", + Style); + verifyFormat("// foo bar baz bazfoo\n" + "// foo bar foo bar", + "// foo bar baz bazfoo\n" + "// foo bar foo bar", + Style); // FIXME: Optimally, we'd keep bazfoo on the first line and reflow bar to the // next one. - EXPECT_EQ("// foo bar baz bazfoo\n" - "// bar foo bar", - format("// foo bar baz bazfoo bar\n" - "// foo bar", - Style)); + verifyFormat("// foo bar baz bazfoo\n" + "// bar foo bar", + "// foo bar baz bazfoo bar\n" + "// foo bar", + Style); + // FIXME: unstable test case EXPECT_EQ("// foo bar baz bazfoo\n" "// foo bar baz bazfoo\n" "// bar foo bar", @@ -21370,6 +21124,7 @@ TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { "// foo bar", Style)); + // FIXME: unstable test case EXPECT_EQ("// foo bar baz bazfoo\n" "// foo bar baz bazfoo\n" "// bar foo bar", @@ -21381,16 +21136,15 @@ TEST_F(FormatTest, OptimizeBreakPenaltyVsExcess) { // Make sure we do not keep protruding characters if strict mode reflow is // cheaper than keeping protruding characters. Style.ColumnLimit = 21; - EXPECT_EQ( - "// foo foo foo foo\n" - "// foo foo foo foo\n" - "// foo foo foo foo", - format("// foo foo foo foo foo foo foo foo foo foo foo foo", Style)); + verifyFormat("// foo foo foo foo\n" + "// foo foo foo foo\n" + "// foo foo foo foo", + "// foo foo foo foo foo foo foo foo foo foo foo foo", Style); - EXPECT_EQ("int a = /* long block\n" - " comment */\n" - " 42;", - format("int a = /* long block comment */ 42;", Style)); + verifyFormat("int a = /* long block\n" + " comment */\n" + " 42;", + "int a = /* long block comment */ 42;", Style); } TEST_F(FormatTest, BreakPenaltyAfterLParen) { @@ -21401,10 +21155,10 @@ TEST_F(FormatTest, BreakPenaltyAfterLParen) { " int aaaaaaaaaaaaaaaaaaaaaaaa);", Style); Style.PenaltyBreakOpenParenthesis = 200; - EXPECT_EQ("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", - format("int foo(\n" - " int aaaaaaaaaaaaaaaaaaaaaaaa);", - Style)); + verifyFormat("int foo(int aaaaaaaaaaaaaaaaaaaaaaaa);", + "int foo(\n" + " int aaaaaaaaaaaaaaaaaaaaaaaa);", + Style); } TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { @@ -21416,11 +21170,11 @@ TEST_F(FormatTest, BreakPenaltyAfterCastLParen) { Style); Style.PenaltyBreakOpenParenthesis = 100000; - EXPECT_EQ("foo((int)\n" - " aaaaaaaaaaaaaaaaaaaaaaaa);", - format("foo((\n" - "int)aaaaaaaaaaaaaaaaaaaaaaaa);", - Style)); + verifyFormat("foo((int)\n" + " aaaaaaaaaaaaaaaaaaaaaaaa);", + "foo((\n" + "int)aaaaaaaaaaaaaaaaaaaaaaaa);", + Style); } TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { @@ -21437,23 +21191,24 @@ TEST_F(FormatTest, BreakPenaltyAfterForLoopLParen) { Style); Style.PenaltyBreakOpenParenthesis = 1250; - EXPECT_EQ("for (int iiiiiiiiiiiiiiiii =\n" - " 0;\n" - " iiiiiiiiiiiiiiiii <\n" - " 2;\n" - " iiiiiiiiiiiiiiiii++) {\n" - "}", - format("for (\n" - " int iiiiiiiiiiiiiiiii =\n" - " 0;\n" - " iiiiiiiiiiiiiiiii <\n" - " 2;\n" - " iiiiiiiiiiiiiiiii++) {\n" - "}", - Style)); + verifyFormat("for (int iiiiiiiiiiiiiiiii =\n" + " 0;\n" + " iiiiiiiiiiiiiiiii <\n" + " 2;\n" + " iiiiiiiiiiiiiiiii++) {\n" + "}", + "for (\n" + " int iiiiiiiiiiiiiiiii =\n" + " 0;\n" + " iiiiiiiiiiiiiiiii <\n" + " 2;\n" + " iiiiiiiiiiiiiiiii++) {\n" + "}", + Style); } TEST_F(FormatTest, WorksFor8bitEncodings) { + // FIXME: unstable test case EXPECT_EQ("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 \"\n" "\"\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \"\n" "\"\xe7\xe8\xec\xed\xfe\xfe \"\n" @@ -21492,24 +21247,29 @@ TEST_F(FormatTest, SplitsUTF8Strings) { // bytes in UTF8. The characters can be displayed in very different manner // (zero-width, single width with a substitution glyph, expanded to their code // (e.g. "<8d>"), so there's no single correct way to handle them. + // FIXME: unstable test case EXPECT_EQ("\"aaaaÄ\"\n" "\"\xc2\x8d\";", format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); + // FIXME: unstable test case EXPECT_EQ("\"aaaaaaaÄ\"\n" "\"\xc2\x8d\";", format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); + // FIXME: unstable test case EXPECT_EQ("\"Однажды, в \"\n" "\"студёную \"\n" "\"зимнюю \"\n" "\"пору,\"", format("\"Однажды, в студёную зимнюю пору,\"", getLLVMStyleWithColumns(13))); + // FIXME: unstable test case EXPECT_EQ( "\"一 二 三 \"\n" "\"四 五六 \"\n" "\"七 八 九 \"\n" "\"十\"", format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); + // FIXME: unstable test case EXPECT_EQ("\"一\t\"\n" "\"二 \t\"\n" "\"三 四 \"\n" @@ -21521,57 +21281,57 @@ TEST_F(FormatTest, SplitsUTF8Strings) { getLLVMStyleWithColumns(11))); // UTF8 character in an escape sequence. + // FIXME: unstable test case EXPECT_EQ("\"aaaaaa\"\n" "\"\\\xC2\x8D\"", format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); } TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) { - EXPECT_EQ("const char *sssss =\n" - " \"一二三四五六七八\\\n" - " 九 十\";", - format("const char *sssss = \"一二三四五六七八\\\n" - " 九 十\";", - getLLVMStyleWithColumns(30))); + verifyFormat("const char *sssss =\n" + " \"一二三四五六七八\\\n" + " 九 十\";", + "const char *sssss = \"一二三四五六七八\\\n" + " 九 十\";", + getLLVMStyleWithColumns(30)); } TEST_F(FormatTest, SplitsUTF8LineComments) { verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)); - EXPECT_EQ("// Я из лесу\n" - "// вышел; был\n" - "// сильный\n" - "// мороз.", - format("// Я из лесу вышел; был сильный мороз.", - getLLVMStyleWithColumns(13))); - EXPECT_EQ("// 一二三\n" - "// 四五六七\n" - "// 八 九\n" - "// 十", - format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); + verifyFormat("// Я из лесу\n" + "// вышел; был\n" + "// сильный\n" + "// мороз.", + "// Я из лесу вышел; был сильный мороз.", + getLLVMStyleWithColumns(13)); + verifyFormat("// 一二三\n" + "// 四五六七\n" + "// 八 九\n" + "// 十", + "// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9)); } TEST_F(FormatTest, SplitsUTF8BlockComments) { - EXPECT_EQ("/* Гляжу,\n" - " * поднимается\n" - " * медленно в\n" - " * гору\n" - " * Лошадка,\n" - " * везущая\n" - " * хворосту\n" - " * воз. */", - format("/* Гляжу, поднимается медленно в гору\n" - " * Лошадка, везущая хворосту воз. */", - getLLVMStyleWithColumns(13))); - EXPECT_EQ( - "/* 一二三\n" - " * 四五六七\n" - " * 八 九\n" - " * 十 */", - format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); - EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n" - " * 𝕓𝕪𝕥𝕖\n" - " * 𝖀𝕿𝕱-𝟠 */", - format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12))); + verifyFormat("/* Гляжу,\n" + " * поднимается\n" + " * медленно в\n" + " * гору\n" + " * Лошадка,\n" + " * везущая\n" + " * хворосту\n" + " * воз. */", + "/* Гляжу, поднимается медленно в гору\n" + " * Лошадка, везущая хворосту воз. */", + getLLVMStyleWithColumns(13)); + verifyFormat("/* 一二三\n" + " * 四五六七\n" + " * 八 九\n" + " * 十 */", + "/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9)); + verifyFormat("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n" + " * 𝕓𝕪𝕥𝕖\n" + " * 𝖀𝕿𝕱-𝟠 */", + "/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)); } #endif // _MSC_VER @@ -21817,32 +21577,33 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) { Style); // Wrap before binary operators. - EXPECT_EQ("void f()\n" - "{\n" - " if (aaaaaaaaaaaaaaaa\n" - " && bbbbbbbbbbbbbbbbbbbbbbbb\n" - " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" - " return;\n" - "}", - format("void f() {\n" - "if (aaaaaaaaaaaaaaaa\n" - "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" - "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" - "return;\n" - "}", - Style)); + verifyFormat( + "void f()\n" + "{\n" + " if (aaaaaaaaaaaaaaaa\n" + " && bbbbbbbbbbbbbbbbbbbbbbbb\n" + " && (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" + " return;\n" + "}", + "void f() {\n" + "if (aaaaaaaaaaaaaaaa\n" + "&& bbbbbbbbbbbbbbbbbbbbbbbb\n" + "&& (cccccccccccccccccccccccccc || dddddddddddddddddddd))\n" + "return;\n" + "}", + Style); // Allow functions on a single line. verifyFormat("void f() { return; }", Style); // Allow empty blocks on a single line and insert a space in empty blocks. - EXPECT_EQ("void f() { }", format("void f() {}", Style)); - EXPECT_EQ("while (true) { }", format("while (true) {}", Style)); + verifyFormat("void f() { }", "void f() {}", Style); + verifyFormat("while (true) { }", "while (true) {}", Style); // However, don't merge non-empty short loops. - EXPECT_EQ("while (true) {\n" - " continue;\n" - "}", - format("while (true) { continue; }", Style)); + verifyFormat("while (true) {\n" + " continue;\n" + "}", + "while (true) { continue; }", Style); // Constructor initializers are formatted one per line with the "," on the // new line. @@ -21859,11 +21620,11 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) { "{\n" "}", Style); - EXPECT_EQ("SomeClass::Constructor()\n" - " : a(a)\n" - "{\n" - "}", - format("SomeClass::Constructor():a(a){}", Style)); + verifyFormat("SomeClass::Constructor()\n" + " : a(a)\n" + "{\n" + "}", + "SomeClass::Constructor():a(a){}", Style); verifyFormat("SomeClass::Constructor()\n" " : a(a)\n" " , b(b)\n" @@ -21892,69 +21653,69 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) { Style); // Do not align operands. - EXPECT_EQ("ASSERT(aaaa\n" - " || bbbb);", - format("ASSERT ( aaaa\n||bbbb);", Style)); + verifyFormat("ASSERT(aaaa\n" + " || bbbb);", + "ASSERT ( aaaa\n||bbbb);", Style); // Accept input's line breaks. - EXPECT_EQ("if (aaaaaaaaaaaaaaa\n" - " || bbbbbbbbbbbbbbb) {\n" - " i++;\n" - "}", - format("if (aaaaaaaaaaaaaaa\n" - "|| bbbbbbbbbbbbbbb) { i++; }", - Style)); - EXPECT_EQ("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" - " i++;\n" - "}", - format("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style)); + verifyFormat("if (aaaaaaaaaaaaaaa\n" + " || bbbbbbbbbbbbbbb) {\n" + " i++;\n" + "}", + "if (aaaaaaaaaaaaaaa\n" + "|| bbbbbbbbbbbbbbb) { i++; }", + Style); + verifyFormat("if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) {\n" + " i++;\n" + "}", + "if (aaaaaaaaaaaaaaa || bbbbbbbbbbbbbbb) { i++; }", Style); // Don't automatically break all macro definitions (llvm.org/PR17842). verifyFormat("#define aNumber 10", Style); // However, generally keep the line breaks that the user authored. - EXPECT_EQ("#define aNumber \\\n" - " 10", - format("#define aNumber \\\n" - " 10", - Style)); + verifyFormat("#define aNumber \\\n" + " 10", + "#define aNumber \\\n" + " 10", + Style); // Keep empty and one-element array literals on a single line. - EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" - " copyItems:YES];", - format("NSArray*a=[[NSArray alloc] initWithArray:@[]\n" - "copyItems:YES];", - Style)); - EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" - " copyItems:YES];", - format("NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" - " copyItems:YES];", - Style)); + verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" + " copyItems:YES];", + "NSArray*a=[[NSArray alloc] initWithArray:@[]\n" + "copyItems:YES];", + Style); + verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" + " copyItems:YES];", + "NSArray*a=[[NSArray alloc]initWithArray:@[ @\"a\" ]\n" + " copyItems:YES];", + Style); // FIXME: This does not seem right, there should be more indentation before // the array literal's entries. Nested blocks have the same problem. - EXPECT_EQ("NSArray* a = [[NSArray alloc] initWithArray:@[\n" - " @\"a\",\n" - " @\"a\"\n" - "]\n" - " copyItems:YES];", - format("NSArray* a = [[NSArray alloc] initWithArray:@[\n" - " @\"a\",\n" - " @\"a\"\n" - " ]\n" - " copyItems:YES];", - Style)); - EXPECT_EQ( + verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n" + " @\"a\",\n" + " @\"a\"\n" + "]\n" + " copyItems:YES];", + "NSArray* a = [[NSArray alloc] initWithArray:@[\n" + " @\"a\",\n" + " @\"a\"\n" + " ]\n" + " copyItems:YES];", + Style); + verifyFormat( "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" " copyItems:YES];", - format("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" - " copyItems:YES];", - Style)); + "NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\", @\"a\" ]\n" + " copyItems:YES];", + Style); verifyFormat("[self.a b:c c:d];", Style); - EXPECT_EQ("[self.a b:c\n" - " c:d];", - format("[self.a b:c\n" - "c:d];", - Style)); + verifyFormat("[self.a b:c\n" + " c:d];", + "[self.a b:c\n" + "c:d];", + Style); } TEST_F(FormatTest, FormatsLambdas) { @@ -21994,14 +21755,14 @@ TEST_F(FormatTest, FormatsLambdas) { verifyFormat("SomeFunction([]() { // A cool function...\n" " return 43;\n" "});"); - EXPECT_EQ("SomeFunction([]() {\n" - "#define A a\n" - " return 43;\n" - "});", - format("SomeFunction([](){\n" - "#define A a\n" - "return 43;\n" - "});")); + verifyFormat("SomeFunction([]() {\n" + "#define A a\n" + " return 43;\n" + "});", + "SomeFunction([](){\n" + "#define A a\n" + "return 43;\n" + "});"); verifyFormat("void f() {\n" " SomeFunction([](decltype(x), A *a) {});\n" " SomeFunction([](typeof(x), A *a) {});\n" @@ -23004,23 +22765,23 @@ TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { " }\n" "}];", ZeroColumn); - EXPECT_EQ("[[SessionService sharedService]\n" - " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" - " if (window) {\n" - " [self windowDidLoad:window];\n" - " } else {\n" - " [self errorLoadingWindow];\n" - " }\n" - " }];", - format("[[SessionService sharedService]\n" - "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" - " if (window) {\n" - " [self windowDidLoad:window];\n" - " } else {\n" - " [self errorLoadingWindow];\n" - " }\n" - "}];", - ZeroColumn)); + verifyFormat("[[SessionService sharedService]\n" + " loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" + " if (window) {\n" + " [self windowDidLoad:window];\n" + " } else {\n" + " [self errorLoadingWindow];\n" + " }\n" + " }];", + "[[SessionService sharedService]\n" + "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n" + " if (window) {\n" + " [self windowDidLoad:window];\n" + " } else {\n" + " [self errorLoadingWindow];\n" + " }\n" + "}];", + ZeroColumn); verifyFormat("[myObject doSomethingWith:arg1\n" " firstBlock:^(Foo *a) {\n" " // ...\n" @@ -23049,147 +22810,143 @@ TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) { ZeroColumn); ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; - EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };", - format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); + verifyFormat("void (^largeBlock)(void) = ^{ int i; };", + "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn); ZeroColumn.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; - EXPECT_EQ("void (^largeBlock)(void) = ^{\n" - " int i;\n" - "};", - format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn)); + verifyFormat("void (^largeBlock)(void) = ^{\n" + " int i;\n" + "};", + "void (^largeBlock)(void) = ^{ int i; };", ZeroColumn); } TEST_F(FormatTest, SupportsCRLF) { - EXPECT_EQ("int a;\r\n" - "int b;\r\n" - "int c;", - format("int a;\r\n" - " int b;\r\n" - " int c;")); - EXPECT_EQ("int a;\r\n" - "int b;\r\n" - "int c;\r\n", - format("int a;\r\n" - " int b;\n" - " int c;\r\n")); - EXPECT_EQ("int a;\n" - "int b;\n" - "int c;", - format("int a;\r\n" - " int b;\n" - " int c;")); + verifyFormat("int a;\r\n" + "int b;\r\n" + "int c;", + "int a;\r\n" + " int b;\r\n" + " int c;"); + verifyFormat("int a;\r\n" + "int b;\r\n" + "int c;\r\n", + "int a;\r\n" + " int b;\n" + " int c;\r\n"); + verifyFormat("int a;\n" + "int b;\n" + "int c;", + "int a;\r\n" + " int b;\n" + " int c;"); + // FIXME: unstable test case EXPECT_EQ("\"aaaaaaa \"\r\n" "\"bbbbbbb\";\r\n", format("\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10))); - EXPECT_EQ("#define A \\\r\n" - " b; \\\r\n" - " c; \\\r\n" - " d;", - format("#define A \\\r\n" - " b; \\\r\n" - " c; d; ", - getGoogleStyle())); - - EXPECT_EQ("/*\r\n" - "multi line block comments\r\n" - "should not introduce\r\n" - "an extra carriage return\r\n" - "*/", - format("/*\r\n" - "multi line block comments\r\n" - "should not introduce\r\n" - "an extra carriage return\r\n" - "*/")); - EXPECT_EQ("/*\r\n" - "\r\n" - "*/", - format("/*\r\n" - " \r\r\r\n" - "*/")); + verifyFormat("#define A \\\r\n" + " b; \\\r\n" + " c; \\\r\n" + " d;", + "#define A \\\r\n" + " b; \\\r\n" + " c; d; ", + getGoogleStyle()); + + verifyNoChange("/*\r\n" + "multi line block comments\r\n" + "should not introduce\r\n" + "an extra carriage return\r\n" + "*/"); + verifyFormat("/*\r\n" + "\r\n" + "*/", + "/*\r\n" + " \r\r\r\n" + "*/"); FormatStyle style = getLLVMStyle(); EXPECT_EQ(style.LineEnding, FormatStyle::LE_DeriveLF); - EXPECT_EQ("union FooBarBazQux {\n" - " int foo;\n" - " int bar;\n" - " int baz;\n" - "};", - format("union FooBarBazQux {\r\n" - " int foo;\n" - " int bar;\r\n" - " int baz;\n" - "};", - style)); + verifyFormat("union FooBarBazQux {\n" + " int foo;\n" + " int bar;\n" + " int baz;\n" + "};", + "union FooBarBazQux {\r\n" + " int foo;\n" + " int bar;\r\n" + " int baz;\n" + "};", + style); style.LineEnding = FormatStyle::LE_DeriveCRLF; - EXPECT_EQ("union FooBarBazQux {\r\n" - " int foo;\r\n" - " int bar;\r\n" - " int baz;\r\n" - "};", - format("union FooBarBazQux {\r\n" - " int foo;\n" - " int bar;\r\n" - " int baz;\n" - "};", - style)); + verifyFormat("union FooBarBazQux {\r\n" + " int foo;\r\n" + " int bar;\r\n" + " int baz;\r\n" + "};", + "union FooBarBazQux {\r\n" + " int foo;\n" + " int bar;\r\n" + " int baz;\n" + "};", + style); style.LineEnding = FormatStyle::LE_LF; - EXPECT_EQ("union FooBarBazQux {\n" - " int foo;\n" - " int bar;\n" - " int baz;\n" - " int qux;\n" - "};", - format("union FooBarBazQux {\r\n" - " int foo;\n" - " int bar;\r\n" - " int baz;\n" - " int qux;\r\n" - "};", - style)); + verifyFormat("union FooBarBazQux {\n" + " int foo;\n" + " int bar;\n" + " int baz;\n" + " int qux;\n" + "};", + "union FooBarBazQux {\r\n" + " int foo;\n" + " int bar;\r\n" + " int baz;\n" + " int qux;\r\n" + "};", + style); style.LineEnding = FormatStyle::LE_CRLF; - EXPECT_EQ("union FooBarBazQux {\r\n" - " int foo;\r\n" - " int bar;\r\n" - " int baz;\r\n" - " int qux;\r\n" - "};", - format("union FooBarBazQux {\r\n" - " int foo;\n" - " int bar;\r\n" - " int baz;\n" - " int qux;\n" - "};", - style)); + verifyFormat("union FooBarBazQux {\r\n" + " int foo;\r\n" + " int bar;\r\n" + " int baz;\r\n" + " int qux;\r\n" + "};", + "union FooBarBazQux {\r\n" + " int foo;\n" + " int bar;\r\n" + " int baz;\n" + " int qux;\n" + "};", + style); style.LineEnding = FormatStyle::LE_DeriveLF; - EXPECT_EQ("union FooBarBazQux {\r\n" - " int foo;\r\n" - " int bar;\r\n" - " int baz;\r\n" - " int qux;\r\n" - "};", - format("union FooBarBazQux {\r\n" - " int foo;\n" - " int bar;\r\n" - " int baz;\n" - " int qux;\r\n" - "};", - style)); + verifyFormat("union FooBarBazQux {\r\n" + " int foo;\r\n" + " int bar;\r\n" + " int baz;\r\n" + " int qux;\r\n" + "};", + "union FooBarBazQux {\r\n" + " int foo;\n" + " int bar;\r\n" + " int baz;\n" + " int qux;\r\n" + "};", + style); style.LineEnding = FormatStyle::LE_DeriveCRLF; - EXPECT_EQ("union FooBarBazQux {\n" - " int foo;\n" - " int bar;\n" - " int baz;\n" - " int qux;\n" - "};", - format("union FooBarBazQux {\r\n" - " int foo;\n" - " int bar;\r\n" - " int baz;\n" - " int qux;\n" - "};", - style)); + verifyFormat("union FooBarBazQux {\n" + " int foo;\n" + " int bar;\n" + " int baz;\n" + " int qux;\n" + "};", + "union FooBarBazQux {\r\n" + " int foo;\n" + " int bar;\r\n" + " int baz;\n" + " int qux;\n" + "};", + style); } TEST_F(FormatTest, MunchSemicolonAfterBlocks) { @@ -23203,18 +22960,18 @@ TEST_F(FormatTest, ConfigurableContinuationIndentWidth) { FormatStyle TwoIndent = getLLVMStyleWithColumns(15); TwoIndent.ContinuationIndentWidth = 2; - EXPECT_EQ("int i =\n" - " longFunction(\n" - " arg);", - format("int i = longFunction(arg);", TwoIndent)); + verifyFormat("int i =\n" + " longFunction(\n" + " arg);", + "int i = longFunction(arg);", TwoIndent); FormatStyle SixIndent = getLLVMStyleWithColumns(20); SixIndent.ContinuationIndentWidth = 6; - EXPECT_EQ("int i =\n" - " longFunction(\n" - " arg);", - format("int i = longFunction(arg);", SixIndent)); + verifyFormat("int i =\n" + " longFunction(\n" + " arg);", + "int i = longFunction(arg);", SixIndent); } TEST_F(FormatTest, WrappedClosingParenthesisIndent) { @@ -23315,10 +23072,10 @@ TEST_F(FormatTest, TripleAngleBrackets) { verifyFormat("f<<<1, 1>>>();"); verifyFormat("f<<<1, 1, 1, s>>>();"); verifyFormat("f<<>>();"); - EXPECT_EQ("f<<<1, 1>>>();", format("f <<< 1, 1 >>> ();")); + verifyFormat("f<<<1, 1>>>();", "f <<< 1, 1 >>> ();"); verifyFormat("f<<<1, 1>>>();"); verifyFormat("f<1><<<1, 1>>>();"); - EXPECT_EQ("f<<<1, 1>>>();", format("f< param > <<< 1, 1 >>> ();")); + verifyFormat("f<<<1, 1>>>();", "f< param > <<< 1, 1 >>> ();"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaaaaaaaaaa<<<\n 1, 1>>>();"); verifyFormat("aaaaaaaaaaaaaaa\n" @@ -23327,7 +23084,7 @@ TEST_F(FormatTest, TripleAngleBrackets) { TEST_F(FormatTest, MergeLessLessAtEnd) { verifyFormat("<<"); - EXPECT_EQ("< < <", format("\\\n<<<")); + verifyFormat("< < <", "\\\n<<<"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaallvm::outs() <<"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" @@ -23350,94 +23107,85 @@ TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) { TEST_F(FormatTest, HandleConflictMarkers) { // Git/SVN conflict markers. - EXPECT_EQ("int a;\n" - "void f() {\n" - " callme(some(parameter1,\n" - "<<<<<<< text by the vcs\n" - " parameter2),\n" - "||||||| text by the vcs\n" - " parameter2),\n" - " parameter3,\n" - "======= text by the vcs\n" - " parameter2, parameter3),\n" - ">>>>>>> text by the vcs\n" - " otherparameter);", - format("int a;\n" - "void f() {\n" - " callme(some(parameter1,\n" - "<<<<<<< text by the vcs\n" - " parameter2),\n" - "||||||| text by the vcs\n" - " parameter2),\n" - " parameter3,\n" - "======= text by the vcs\n" - " parameter2,\n" - " parameter3),\n" - ">>>>>>> text by the vcs\n" - " otherparameter);")); + verifyFormat("int a;\n" + "void f() {\n" + " callme(some(parameter1,\n" + "<<<<<<< text by the vcs\n" + " parameter2),\n" + "||||||| text by the vcs\n" + " parameter2),\n" + " parameter3,\n" + "======= text by the vcs\n" + " parameter2, parameter3),\n" + ">>>>>>> text by the vcs\n" + " otherparameter);", + "int a;\n" + "void f() {\n" + " callme(some(parameter1,\n" + "<<<<<<< text by the vcs\n" + " parameter2),\n" + "||||||| text by the vcs\n" + " parameter2),\n" + " parameter3,\n" + "======= text by the vcs\n" + " parameter2,\n" + " parameter3),\n" + ">>>>>>> text by the vcs\n" + " otherparameter);"); // Perforce markers. - EXPECT_EQ("void f() {\n" - " function(\n" - ">>>> text by the vcs\n" - " parameter,\n" - "==== text by the vcs\n" - " parameter,\n" - "==== text by the vcs\n" - " parameter,\n" - "<<<< text by the vcs\n" - " parameter);", - format("void f() {\n" - " function(\n" - ">>>> text by the vcs\n" - " parameter,\n" - "==== text by the vcs\n" - " parameter,\n" - "==== text by the vcs\n" - " parameter,\n" - "<<<< text by the vcs\n" - " parameter);")); - - EXPECT_EQ("<<<<<<<\n" - "|||||||\n" - "=======\n" - ">>>>>>>", - format("<<<<<<<\n" - "|||||||\n" - "=======\n" - ">>>>>>>")); - - EXPECT_EQ("<<<<<<<\n" - "|||||||\n" - "int i;\n" - "=======\n" - ">>>>>>>", - format("<<<<<<<\n" - "|||||||\n" - "int i;\n" - "=======\n" - ">>>>>>>")); + verifyFormat("void f() {\n" + " function(\n" + ">>>> text by the vcs\n" + " parameter,\n" + "==== text by the vcs\n" + " parameter,\n" + "==== text by the vcs\n" + " parameter,\n" + "<<<< text by the vcs\n" + " parameter);", + "void f() {\n" + " function(\n" + ">>>> text by the vcs\n" + " parameter,\n" + "==== text by the vcs\n" + " parameter,\n" + "==== text by the vcs\n" + " parameter,\n" + "<<<< text by the vcs\n" + " parameter);"); + + verifyNoChange("<<<<<<<\n" + "|||||||\n" + "=======\n" + ">>>>>>>"); + + verifyNoChange("<<<<<<<\n" + "|||||||\n" + "int i;\n" + "=======\n" + ">>>>>>>"); // FIXME: Handle parsing of macros around conflict markers correctly: - EXPECT_EQ("#define Macro \\\n" - "<<<<<<<\n" - "Something \\\n" - "|||||||\n" - "Else \\\n" - "=======\n" - "Other \\\n" - ">>>>>>>\n" - " End int i;", - format("#define Macro \\\n" - "<<<<<<<\n" - " Something \\\n" - "|||||||\n" - " Else \\\n" - "=======\n" - " Other \\\n" - ">>>>>>>\n" - " End\n" - "int i;")); + verifyFormat("#define Macro \\\n" + "<<<<<<<\n" + "Something \\\n" + "|||||||\n" + "Else \\\n" + "=======\n" + "Other \\\n" + ">>>>>>>\n" + " End int i;", + "#define Macro \\\n" + "<<<<<<<\n" + " Something \\\n" + "|||||||\n" + " Else \\\n" + "=======\n" + " Other \\\n" + ">>>>>>>\n" + " End\n" + "int i;"); verifyFormat(R"(==== #ifdef A @@ -23449,45 +23197,45 @@ b } TEST_F(FormatTest, DisableRegions) { - EXPECT_EQ("int i;\n" - "// clang-format off\n" - " int j;\n" - "// clang-format on\n" - "int k;", - format(" int i;\n" - " // clang-format off\n" - " int j;\n" - " // clang-format on\n" - " int k;")); - EXPECT_EQ("int i;\n" - "/* clang-format off */\n" - " int j;\n" - "/* clang-format on */\n" - "int k;", - format(" int i;\n" - " /* clang-format off */\n" - " int j;\n" - " /* clang-format on */\n" - " int k;")); + verifyFormat("int i;\n" + "// clang-format off\n" + " int j;\n" + "// clang-format on\n" + "int k;", + " int i;\n" + " // clang-format off\n" + " int j;\n" + " // clang-format on\n" + " int k;"); + verifyFormat("int i;\n" + "/* clang-format off */\n" + " int j;\n" + "/* clang-format on */\n" + "int k;", + " int i;\n" + " /* clang-format off */\n" + " int j;\n" + " /* clang-format on */\n" + " int k;"); // Don't reflow comments within disabled regions. - EXPECT_EQ("// clang-format off\n" - "// long long long long long long line\n" - "/* clang-format on */\n" - "/* long long long\n" - " * long long long\n" - " * line */\n" - "int i;\n" - "/* clang-format off */\n" - "/* long long long long long long line */", - format("// clang-format off\n" - "// long long long long long long line\n" - "/* clang-format on */\n" - "/* long long long long long long line */\n" - "int i;\n" - "/* clang-format off */\n" - "/* long long long long long long line */", - getLLVMStyleWithColumns(20))); + verifyFormat("// clang-format off\n" + "// long long long long long long line\n" + "/* clang-format on */\n" + "/* long long long\n" + " * long long long\n" + " * line */\n" + "int i;\n" + "/* clang-format off */\n" + "/* long long long long long long line */", + "// clang-format off\n" + "// long long long long long long line\n" + "/* clang-format on */\n" + "/* long long long long long long line */\n" + "int i;\n" + "/* clang-format off */\n" + "/* long long long long long long line */", + getLLVMStyleWithColumns(20)); verifyFormat("int *i;\n" "// clang-format off:\n" @@ -23531,40 +23279,40 @@ TEST_F(FormatTest, FormatsTableGenCode) { } TEST_F(FormatTest, ArrayOfTemplates) { - EXPECT_EQ("auto a = new unique_ptr[10];", - format("auto a = new unique_ptr [ 10];")); + verifyFormat("auto a = new unique_ptr[10];", + "auto a = new unique_ptr [ 10];"); FormatStyle Spaces = getLLVMStyle(); Spaces.SpacesInSquareBrackets = true; - EXPECT_EQ("auto a = new unique_ptr[ 10 ];", - format("auto a = new unique_ptr [10];", Spaces)); + verifyFormat("auto a = new unique_ptr[ 10 ];", + "auto a = new unique_ptr [10];", Spaces); } TEST_F(FormatTest, ArrayAsTemplateType) { - EXPECT_EQ("auto a = unique_ptr[10]>;", - format("auto a = unique_ptr < Foo < Bar>[ 10]> ;")); + verifyFormat("auto a = unique_ptr[10]>;", + "auto a = unique_ptr < Foo < Bar>[ 10]> ;"); FormatStyle Spaces = getLLVMStyle(); Spaces.SpacesInSquareBrackets = true; - EXPECT_EQ("auto a = unique_ptr[ 10 ]>;", - format("auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces)); + verifyFormat("auto a = unique_ptr[ 10 ]>;", + "auto a = unique_ptr < Foo < Bar>[10]> ;", Spaces); } TEST_F(FormatTest, NoSpaceAfterSuper) { verifyFormat("__super::FooBar();"); } TEST_F(FormatTest, FormatSortsUsingDeclarations) { - EXPECT_EQ("using std::cin;\n" - "using std::cout;", - format("using std::cout;\n" - "using std::cin;", - getGoogleStyle())); + verifyFormat("using std::cin;\n" + "using std::cout;", + "using std::cout;\n" + "using std::cin;", + getGoogleStyle()); } TEST_F(FormatTest, UTF8CharacterLiteralCpp03) { FormatStyle Style = getLLVMStyle(); Style.Standard = FormatStyle::LS_Cpp03; // cpp03 recognize this string as identifier u8 and literal character 'a' - EXPECT_EQ("auto c = u8 'a';", format("auto c = u8'a';", Style)); + verifyFormat("auto c = u8 'a';", "auto c = u8'a';", Style); } TEST_F(FormatTest, UTF8CharacterLiteralCpp11) { @@ -23582,29 +23330,24 @@ TEST_F(FormatTest, StructuredBindings) { // Structured bindings is a C++17 feature. // all modes, including C++11, C++14 and C++17 verifyFormat("auto [a, b] = f();"); - EXPECT_EQ("auto [a, b] = f();", format("auto[a, b] = f();")); - EXPECT_EQ("const auto [a, b] = f();", format("const auto[a, b] = f();")); - EXPECT_EQ("auto const [a, b] = f();", format("auto const[a, b] = f();")); - EXPECT_EQ("auto const volatile [a, b] = f();", - format("auto const volatile[a, b] = f();")); - EXPECT_EQ("auto [a, b, c] = f();", format("auto [ a , b,c ] = f();")); - EXPECT_EQ("auto &[a, b, c] = f();", - format("auto &[ a , b,c ] = f();")); - EXPECT_EQ("auto &&[a, b, c] = f();", - format("auto &&[ a , b,c ] = f();")); - EXPECT_EQ("auto const &[a, b] = f();", format("auto const&[a, b] = f();")); - EXPECT_EQ("auto const volatile &&[a, b] = f();", - format("auto const volatile &&[a, b] = f();")); - EXPECT_EQ("auto const &&[a, b] = f();", - format("auto const && [a, b] = f();")); - EXPECT_EQ("const auto &[a, b] = f();", - format("const auto & [a, b] = f();")); - EXPECT_EQ("const auto volatile &&[a, b] = f();", - format("const auto volatile &&[a, b] = f();")); - EXPECT_EQ("volatile const auto &&[a, b] = f();", - format("volatile const auto &&[a, b] = f();")); - EXPECT_EQ("const auto &&[a, b] = f();", - format("const auto && [a, b] = f();")); + verifyFormat("auto [a, b] = f();", "auto[a, b] = f();"); + verifyFormat("const auto [a, b] = f();", "const auto[a, b] = f();"); + verifyFormat("auto const [a, b] = f();", "auto const[a, b] = f();"); + verifyFormat("auto const volatile [a, b] = f();", + "auto const volatile[a, b] = f();"); + verifyFormat("auto [a, b, c] = f();", "auto [ a , b,c ] = f();"); + verifyFormat("auto &[a, b, c] = f();", "auto &[ a , b,c ] = f();"); + verifyFormat("auto &&[a, b, c] = f();", "auto &&[ a , b,c ] = f();"); + verifyFormat("auto const &[a, b] = f();", "auto const&[a, b] = f();"); + verifyFormat("auto const volatile &&[a, b] = f();", + "auto const volatile &&[a, b] = f();"); + verifyFormat("auto const &&[a, b] = f();", "auto const && [a, b] = f();"); + verifyFormat("const auto &[a, b] = f();", "const auto & [a, b] = f();"); + verifyFormat("const auto volatile &&[a, b] = f();", + "const auto volatile &&[a, b] = f();"); + verifyFormat("volatile const auto &&[a, b] = f();", + "volatile const auto &&[a, b] = f();"); + verifyFormat("const auto &&[a, b] = f();", "const auto && [a, b] = f();"); // Make sure we don't mistake structured bindings for lambdas. FormatStyle PointerMiddle = getLLVMStyle(); @@ -23622,24 +23365,20 @@ TEST_F(FormatTest, StructuredBindings) { verifyFormat("auto const &&[a2, b]{A * i};"); verifyFormat("auto const && [a3, b]{A * i};", PointerMiddle); - EXPECT_EQ("for (const auto &&[a, b] : some_range) {\n}", - format("for (const auto && [a, b] : some_range) {\n}")); - EXPECT_EQ("for (const auto &[a, b] : some_range) {\n}", - format("for (const auto & [a, b] : some_range) {\n}")); - EXPECT_EQ("for (const auto [a, b] : some_range) {\n}", - format("for (const auto[a, b] : some_range) {\n}")); - EXPECT_EQ("auto [x, y](expr);", format("auto[x,y] (expr);")); - EXPECT_EQ("auto &[x, y](expr);", format("auto & [x,y] (expr);")); - EXPECT_EQ("auto &&[x, y](expr);", format("auto && [x,y] (expr);")); - EXPECT_EQ("auto const &[x, y](expr);", - format("auto const & [x,y] (expr);")); - EXPECT_EQ("auto const &&[x, y](expr);", - format("auto const && [x,y] (expr);")); - EXPECT_EQ("auto [x, y]{expr};", format("auto[x,y] {expr};")); - EXPECT_EQ("auto const &[x, y]{expr};", - format("auto const & [x,y] {expr};")); - EXPECT_EQ("auto const &&[x, y]{expr};", - format("auto const && [x,y] {expr};")); + verifyFormat("for (const auto &&[a, b] : some_range) {\n}", + "for (const auto && [a, b] : some_range) {\n}"); + verifyFormat("for (const auto &[a, b] : some_range) {\n}", + "for (const auto & [a, b] : some_range) {\n}"); + verifyFormat("for (const auto [a, b] : some_range) {\n}", + "for (const auto[a, b] : some_range) {\n}"); + verifyFormat("auto [x, y](expr);", "auto[x,y] (expr);"); + verifyFormat("auto &[x, y](expr);", "auto & [x,y] (expr);"); + verifyFormat("auto &&[x, y](expr);", "auto && [x,y] (expr);"); + verifyFormat("auto const &[x, y](expr);", "auto const & [x,y] (expr);"); + verifyFormat("auto const &&[x, y](expr);", "auto const && [x,y] (expr);"); + verifyFormat("auto [x, y]{expr};", "auto[x,y] {expr};"); + verifyFormat("auto const &[x, y]{expr};", "auto const & [x,y] {expr};"); + verifyFormat("auto const &&[x, y]{expr};", "auto const && [x,y] {expr};"); FormatStyle Spaces = getLLVMStyle(); Spaces.SpacesInSquareBrackets = true; @@ -24198,21 +23937,15 @@ TEST_F(FormatTest, WhitespaceSensitiveMacros) { // Don't use the helpers here, since 'mess up' will change the whitespace // and these are all whitespace sensitive by definition verifyNoChange("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style); - EXPECT_EQ( - "FOO(String-ized&Messy+But\\(: :Still)=Intentional);", - format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style)); + verifyNoChange("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style); verifyNoChange("FOO(String-ized&Messy+But,: :Still=Intentional);", Style); - EXPECT_EQ("FOO(String-ized&Messy+But,: :\n" - " Still=Intentional);", - format("FOO(String-ized&Messy+But,: :\n" - " Still=Intentional);", - Style)); + verifyNoChange("FOO(String-ized&Messy+But,: :\n" + " Still=Intentional);", + Style); Style.AlignConsecutiveAssignments.Enabled = true; - EXPECT_EQ("FOO(String-ized=&Messy+But,: :\n" - " Still=Intentional);", - format("FOO(String-ized=&Messy+But,: :\n" - " Still=Intentional);", - Style)); + verifyNoChange("FOO(String-ized=&Messy+But,: :\n" + " Still=Intentional);", + Style); Style.ColumnLimit = 21; verifyNoChange("FOO(String-ized&Messy+But: :Still=Intentional);", Style); @@ -24234,17 +23967,17 @@ TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { "} // namespace AAA", Style); - EXPECT_EQ("namespace Averyveryveryverylongnamespace {\n" - "int i;\n" - "int j;\n" - "} // namespace Averyveryveryverylongnamespace", - format("namespace Averyveryveryverylongnamespace {\n" - "int i;\n" - "int j;\n" - "}", - Style)); + verifyFormat("namespace Averyveryveryverylongnamespace {\n" + "int i;\n" + "int j;\n" + "} // namespace Averyveryveryverylongnamespace", + "namespace Averyveryveryverylongnamespace {\n" + "int i;\n" + "int j;\n" + "}", + Style); - EXPECT_EQ( + verifyFormat( "namespace " "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" " went::mad::now {\n" @@ -24254,16 +23987,16 @@ TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { " // " "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" "went::mad::now", - format("namespace " - "would::it::save::you::a::lot::of::time::if_::i::" - "just::gave::up::and_::went::mad::now {\n" - "int i;\n" - "int j;\n" - "}", - Style)); + "namespace " + "would::it::save::you::a::lot::of::time::if_::i::" + "just::gave::up::and_::went::mad::now {\n" + "int i;\n" + "int j;\n" + "}", + Style); // This used to duplicate the comment again and again on subsequent runs - EXPECT_EQ( + verifyFormat( "namespace " "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::\n" " went::mad::now {\n" @@ -24273,16 +24006,16 @@ TEST_F(FormatTest, VeryLongNamespaceCommentSplit) { " // " "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::and_::" "went::mad::now", - format("namespace " - "would::it::save::you::a::lot::of::time::if_::i::" - "just::gave::up::and_::went::mad::now {\n" - "int i;\n" - "int j;\n" - "} // namespace\n" - " // " - "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" - "and_::went::mad::now", - Style)); + "namespace " + "would::it::save::you::a::lot::of::time::if_::i::" + "just::gave::up::and_::went::mad::now {\n" + "int i;\n" + "int j;\n" + "} // namespace\n" + " // " + "would::it::save::you::a::lot::of::time::if_::i::just::gave::up::" + "and_::went::mad::now", + Style); } TEST_F(FormatTest, LikelyUnlikely) { @@ -25340,23 +25073,23 @@ TEST_F(FormatTest, StatementAttributeLikeMacros) { verifyFormat(Source, Style); Style.AlignConsecutiveDeclarations.Enabled = true; - EXPECT_EQ("void Foo::slot() {\n" - " unsigned char MyChar = 'x';\n" - " emit signal(MyChar);\n" - " Q_EMIT signal(MyChar);\n" - "}", - format(Source, Style)); + verifyFormat("void Foo::slot() {\n" + " unsigned char MyChar = 'x';\n" + " emit signal(MyChar);\n" + " Q_EMIT signal(MyChar);\n" + "}", + Source, Style); Style.StatementAttributeLikeMacros.push_back("emit"); verifyFormat(Source, Style); Style.StatementAttributeLikeMacros = {}; - EXPECT_EQ("void Foo::slot() {\n" - " unsigned char MyChar = 'x';\n" - " emit signal(MyChar);\n" - " Q_EMIT signal(MyChar);\n" - "}", - format(Source, Style)); + verifyFormat("void Foo::slot() {\n" + " unsigned char MyChar = 'x';\n" + " emit signal(MyChar);\n" + " Q_EMIT signal(MyChar);\n" + "}", + Source, Style); } TEST_F(FormatTest, IndentAccessModifiers) {