diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -27,6 +27,26 @@ FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); } +bool canRunCppTestAsObjCTest(llvm::StringRef Expected, + const FormatStyle &Style) { + bool foundEdgeCase = false; + + // ObjC doesn't have a concept of namespaces + if (Style.FixNamespaceComments) { + foundEdgeCase |= Expected.contains("// namespace"); + for (const auto ¯o : Style.NamespaceMacros) { + foundEdgeCase |= Expected.contains(macro); + } + } + + // ObjC doesn't have using statements + if (Style.SortUsingDeclarations) { + foundEdgeCase |= Expected.contains("using"); + } + + return !foundEdgeCase; +} + class FormatTest : public ::testing::Test { protected: enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck }; @@ -70,12 +90,14 @@ EXPECT_EQ(Expected.str(), format(Expected, Style)) << "Expected code is not stable"; EXPECT_EQ(Expected.str(), format(Code, Style)); - if (Style.Language == FormatStyle::LK_Cpp) { - // Objective-C++ is a superset of C++, so everything checked for C++ - // needs to be checked for Objective-C++ as well. + if (Style.Language == FormatStyle::LK_Cpp && + canRunCppTestAsObjCTest(Expected, Style)) { + // Objective-C++ is generally a superset of C++, so most things checked + // for C++ needs to be checked for Objective-C++ as well FormatStyle ObjCStyle = Style; ObjCStyle.Language = FormatStyle::LK_ObjC; - EXPECT_EQ(Expected.str(), format(test::messUp(Code), ObjCStyle)); + EXPECT_EQ(Expected.str(), format(Code, ObjCStyle)) + << "Expected code works on C++ but fails on ObjC"; } } @@ -130,22 +152,22 @@ //===----------------------------------------------------------------------===// TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { - EXPECT_EQ(";", format(";")); + 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) { @@ -164,211 +186,211 @@ } 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" - "}")); + verifyFormat("if (a) {\n" + " f();\n" + "}", + "if (a) {\n" + " f();\n" + "}"); EXPECT_EQ(0, ReplacementCount); - EXPECT_EQ("/*\r\n" - "\r\n" - "*/\r\n", - format("/*\r\n" - "\r\n" - "*/\r\n")); + verifyFormat("/*\r\n" + "\r\n" + "*/\r\n", + "/*\r\n" + "\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()); // ...but do keep inlining and removing empty lines for non-block extern "C" // functions. verifyFormat("extern \"C\" int f() { return 42; }", getGoogleStyle()); - 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" - "}", - getLLVMStyle())); - 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" + "}", + getLLVMStyle()); + 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")); + verifyFormat("namespace {\n" + "int i;\n" + "\n" + "}", + "namespace {\n" + "int i;\n" + "\n" + "}", + LLVMWithNoNamespaceFix); + verifyFormat("namespace {\n" + "int i;\n" + "}", + "namespace {\n" + "int i;\n" + "}", + LLVMWithNoNamespaceFix); + verifyFormat("namespace {\n" + "int i;\n" + "\n" + "};", + "namespace {\n" + "int i;\n" + "\n" + "};", + LLVMWithNoNamespaceFix); + verifyFormat("namespace {\n" + "int i;\n" + "};", + "namespace {\n" + "int i;\n" + "};", + LLVMWithNoNamespaceFix); + verifyFormat("namespace {\n" + "int i;\n" + "\n" + "}", + "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; @@ -378,21 +400,21 @@ 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) { @@ -743,16 +765,16 @@ Style.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Always; Style.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_WithoutElse; Style.BreakBeforeBraces = FormatStyle::BS_Allman; - EXPECT_EQ("#define A \\\n" - " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" - " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" - "X;", - format("#define A \\\n" - " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" - " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" - " }\n" - "X;", - Style)); + verifyFormat("#define A \\\n" + " if (HANDLEwernufrnuLwrmviferuvnierv) \\\n" + " { RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; }\n" + "X;", + "#define A \\\n" + " if (HANDLEwernufrnuLwrmviferuvnierv) { \\\n" + " RET_ERR1_ANUIREUINERUIFNIOAerwfwrvnuier; \\\n" + " }\n" + "X;", + Style); } TEST_F(FormatTest, ParseIfElse) { @@ -1149,47 +1171,47 @@ " 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" - "});", - getLLVMStyle())); - 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" - "}", - getLLVMStyle())); + verifyFormat("DEBUG({\n" + " switch (x) {\n" + " case A:\n" + " f();\n" + " break;\n" + " // On B:\n" + " case B:\n" + " g();\n" + " break;\n" + " }\n" + "});", + "DEBUG({\n" + " switch (x) {\n" + " case A:\n" + " f();\n" + " break;\n" + " // On B:\n" + " case B:\n" + " g();\n" + " break;\n" + " }\n" + "});", + getLLVMStyle()); + 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" + "}", + getLLVMStyle()); verifyFormat("switch (a) {\n" "case (b):\n" " return;\n" @@ -1208,99 +1230,99 @@ 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)); + 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; - 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" + " {\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; - 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" + " {\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) { @@ -1369,60 +1391,60 @@ " // 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" @@ -1459,44 +1481,44 @@ 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) { @@ -1552,158 +1574,155 @@ Style.BraceWrapping.AfterControlStatement = FormatStyle::BWACS_MultiLine; Style.ColumnLimit = 20; // 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. - 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("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("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("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); } TEST_F(FormatTest, BeforeWhile) { @@ -1789,61 +1808,61 @@ } 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) { @@ -1953,21 +1972,21 @@ 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" @@ -2224,58 +2243,58 @@ "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" + "}", + "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); 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); } TEST_F(FormatTest, NamespaceMacros) { @@ -2338,51 +2357,51 @@ "}} // 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", + "namespace out {\n" + "TESTSUITE(in) {\n" + "} // TESTSUITE(in)\n" + "} // namespace out", + Style); + verifyFormat("TESTSUITE(out) {\n" + "namespace in {\n" + "} // namespace in\n" + "} // TESTSUITE(out)", + "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)", + "TESTSUITE(out) {\n" + "FOOBAR(in) {\n" + "} // FOOBAR(in)\n" + "} // TESTSUITE(out)", + Style); } TEST_F(FormatTest, FormatsCompactNamespaces) { @@ -2394,131 +2413,131 @@ "}} // 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)); - - 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)); - - EXPECT_EQ("namespace A { namespace B { namespace C {\n" - "}}} // namespace A::B::C\n", - format("namespace A { namespace B {\n" - "namespace C {\n" - "}} // namespace B::C\n" - "} // namespace A\n", - Style)); + verifyFormat("namespace out {\n" + "namespace in1 {\n" + "} // namespace in1\n" + "namespace in2 {\n" + "} // namespace in2\n" + "} // namespace out", + "namespace out {\n" + "namespace in1 {\n" + "} // namespace in1\n" + "namespace in2 {\n" + "} // namespace in2\n" + "} // 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); + + verifyFormat("namespace A { namespace B { namespace C {\n" + "}}} // namespace A::B::C\n", + "namespace A { namespace B {\n" + "namespace C {\n" + "}} // namespace B::C\n" + "} // namespace A\n", + 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); 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); } TEST_F(FormatTest, FormatsExternC) { @@ -2604,7 +2623,7 @@ " \"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" @@ -2612,31 +2631,31 @@ " 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" + "}"); + verifyFormat("_asm {\n" + " xor eax, eax;\n" + " cpuid;\n" + "}", + "_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;"); } TEST_F(FormatTest, FormatTryCatch) { @@ -2784,14 +2803,14 @@ // 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 @@ -2909,48 +2928,47 @@ 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) { - EXPECT_EQ("#;", format("#;")); + verifyFormat("#;", "#;"); verifyFormat("#\n;\n;\n;"); } TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { - EXPECT_EQ("#line 42 \"test\"\n", - format("# \\\n line \\\n 42 \\\n \"test\"\n")); - EXPECT_EQ("#define A B\n", format("# \\\n define \\\n A \\\n B\n", - getLLVMStyleWithColumns(12))); + verifyFormat("#line 42 \"test\"\n", + "# \\\n line \\\n 42 \\\n \"test\"\n"); + verifyFormat("#define A B\n", "# \\\n define \\\n A \\\n B\n", + 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\\"); @@ -2972,44 +2990,44 @@ } TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { - EXPECT_EQ("// somecomment\n" - "#include \"a.h\"\n" - "#define A( \\\n" - " A, B)\n" - "#include \"b.h\"\n" - "// somecomment\n", - format(" // somecomment\n" - " #include \"a.h\"\n" - "#define A(A,\\\n" - " B)\n" - " #include \"b.h\"\n" - " // somecomment\n", - getLLVMStyleWithColumns(13))); -} - -TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); } + verifyFormat("// somecomment\n" + "#include \"a.h\"\n" + "#define A( \\\n" + " A, B)\n" + "#include \"b.h\"\n" + "// somecomment\n", + " // somecomment\n" + " #include \"a.h\"\n" + "#define A(A,\\\n" + " B)\n" + " #include \"b.h\"\n" + " // somecomment\n", + getLLVMStyleWithColumns(13)); +} + +TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;", "#\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) { EXPECT_EQ("{}", format("{}")); } +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) { - EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle())); + verifyFormat("#define A(c) L#c", "#define A(c) L#c", getLLVMStyle()); verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); verifyFormat("#define A \\\n" " { \\\n" @@ -3029,8 +3047,8 @@ } TEST_F(FormatTest, RespectWhitespaceInMacroDefinitions) { - EXPECT_EQ("#define A (x)", format("#define A (x)")); - EXPECT_EQ("#define A(x)", format("#define A(x)")); + verifyFormat("#define A (x)", "#define A (x)"); + verifyFormat("#define A(x)", "#define A(x)"); FormatStyle Style = getLLVMStyle(); Style.SpaceBeforeParens = FormatStyle::SBPO_Never; @@ -3040,28 +3058,29 @@ } 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)); + verifyFormat("#define A \\\n" + " \\\n" + " a; \\\n" + " b;", + "#define A \\\n" + " \\\n" + " a; \\\n" + " b;", + getLLVMStyleWithColumns(11)); + verifyFormat("#define A \\\n" + " a; \\\n" + " \\\n" + " b;", + "#define A \\\n" + " a; \\\n" + " \\\n" + " b;", + getLLVMStyleWithColumns(11)); } TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) { @@ -3105,206 +3124,205 @@ 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();")); - EXPECT_EQ("MACRO\nf();", format("MACRO\nf();")); + verifyFormat("HTTP f();", "HTTP\nf();"); + verifyFormat("MACRO\nf();", "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))\n", getChromiumStyle(FormatStyle::LK_Cpp)); // See PR41483 - EXPECT_EQ("/**/ FOO(a)\n" - "FOO(b)", - format("/**/ FOO(a)\n" - "FOO(b)")); + verifyFormat("/**/ FOO(a)\n" + "FOO(b)", + "/**/ 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" - "}\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" - "}\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" - "}\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" - "}\n")); - EXPECT_EQ("class A {\n" - " A() : t(0) {}\n" - " A(int i) noexcept() : {}\n" - " A(X x)\n" // FIXME: function-level try blocks are broken. - " try : t(0) {\n" - " } catch (...) {\n" - " }\n" - "};", - format("class A {\n" - " A()\n : t(0) {}\n" - " A(int i)\n noexcept() : {}\n" - " A(X x)\n" - " try : t(0) {} 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" + "}\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" + "}\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" + "}\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" + "}\n"); + verifyFormat("class A {\n" + " A() : t(0) {}\n" + " A(int i) noexcept() : {}\n" + " A(X x)\n" // FIXME: function-level try blocks are broken. + " try : t(0) {\n" + " } catch (...) {\n" + " }\n" + "};", + "class A {\n" + " A()\n : t(0) {}\n" + " A(int i)\n noexcept() : {}\n" + " A(X x)\n" + " try : t(0) {} catch (...) {}\n" + "};"); FormatStyle Style = getLLVMStyle(); 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(>)"); @@ -3478,50 +3496,50 @@ // #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. @@ -3732,70 +3750,70 @@ } 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 */")); - EXPECT_EQ("", format("")); + 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 */"); + verifyFormat("", ""); FormatStyle AlignLeft = getLLVMStyle(); AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left; - EXPECT_EQ("#define MACRO(x) \\\n" - "private: \\\n" - " int x(int a);\n", - format("#define MACRO(x) \\\n" - "private: \\\n" - " int x(int a);\n", - AlignLeft)); + verifyFormat("#define MACRO(x) \\\n" + "private: \\\n" + " int x(int a);\n", + "#define MACRO(x) \\\n" + "private: \\\n" + " int x(int a);\n", + 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 */")); - EXPECT_EQ("", format("")); - EXPECT_EQ("#define MACRO(x) \\\r\n" - "private: \\\r\n" - " int x(int a);\r\n", - format("#define MACRO(x) \\\r\n" - "private: \\\r\n" - " int x(int a);\r\n", - AlignLeft)); + 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 */"); + verifyFormat("", ""); + verifyFormat("#define MACRO(x) \\\r\n" + "private: \\\r\n" + " int x(int a);\r\n", + "#define MACRO(x) \\\r\n" + "private: \\\r\n" + " int x(int a);\r\n", + 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)); + verifyFormat("#define A \\\n" + " class Foo { \\\n" + " void bar(); \\\n" + "\\\n" + "\\\n" + "\\\n" + " public: \\\n" + " void baz(); \\\n" + " };", + "#define A \\\n" + " class Foo { \\\n" + " void bar(); \\\n" + "\\\n" + "\\\n" + "\\\n" + " public: \\\n" + " void baz(); \\\n" + " };", + DontAlign); } TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { @@ -3807,26 +3825,25 @@ } TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { - EXPECT_EQ( - "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" - " \\\n" - " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" - "\n" - "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" - " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", - format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" - "\\\n" - "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" - " \n" - " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" - " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); + verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" + " \\\n" + " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" + "\n" + "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" + " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", + " #define ALooooooooooooooooooooooooooooooooooooooongMacro(" + "\\\n" + "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" + " \n" + " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" + " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n"); } 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" @@ -3963,9 +3980,9 @@ } 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) { @@ -3991,37 +4008,37 @@ " },\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" @@ -4046,35 +4063,35 @@ } 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) { - EXPECT_EQ("{}", format("{}")); + verifyFormat("{}", "{}"); verifyFormat("enum E {};"); 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); } TEST_F(FormatTest, FormatBeginBlockEndMacros) { @@ -4362,13 +4379,13 @@ 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) { @@ -4682,12 +4699,12 @@ " 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) { @@ -4993,13 +5010,13 @@ " bbbbbbbbbbbbbbbbbbbbbbbb(b) {}", OnePerLine); - EXPECT_EQ("Constructor() :\n" - " // Comment forcing unwanted break.\n" - " aaaa(aaaa) {}", - format("Constructor() :\n" - " // Comment forcing unwanted break.\n" - " aaaa(aaaa) {}", - Style)); + verifyFormat("Constructor() :\n" + " // Comment forcing unwanted break.\n" + " aaaa(aaaa) {}", + "Constructor() :\n" + " // Comment forcing unwanted break.\n" + " aaaa(aaaa) {}", + Style); Style.ColumnLimit = 0; verifyFormat("SomeClass::Constructor() :\n" @@ -5260,13 +5277,13 @@ 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) { @@ -5506,12 +5523,12 @@ " }\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( @@ -5620,25 +5637,25 @@ TEST_F(FormatTest, AdaptiveOnePerLineFormatting) { FormatStyle Style = getLLVMStyleWithColumns(15); Style.ExperimentalAutoDetectBinPacking = true; - EXPECT_EQ("aaa(aaaa,\n" - " aaaa,\n" - " aaaa);\n" - "aaa(aaaa,\n" - " aaaa,\n" - " aaaa);", - format("aaa(aaaa,\n" // one-per-line - " aaaa,\n" - " aaaa );\n" - "aaa(aaaa, aaaa, aaaa);", // inconclusive - Style)); - EXPECT_EQ("aaa(aaaa, aaaa,\n" - " aaaa);\n" - "aaa(aaaa, aaaa,\n" - " aaaa);", - format("aaa(aaaa, aaaa,\n" // bin-packed - " aaaa );\n" - "aaa(aaaa, aaaa, aaaa);", // inconclusive - Style)); + verifyFormat("aaa(aaaa,\n" + " aaaa,\n" + " aaaa);\n" + "aaa(aaaa,\n" + " aaaa,\n" + " aaaa);", + "aaa(aaaa,\n" // one-per-line + " aaaa,\n" + " aaaa );\n" + "aaa(aaaa, aaaa, aaaa);", // inconclusive + Style); + verifyFormat("aaa(aaaa, aaaa,\n" + " aaaa);\n" + "aaa(aaaa, aaaa,\n" + " aaaa);", + "aaa(aaaa, aaaa,\n" // bin-packed + " aaaa );\n" + "aaa(aaaa, aaaa, aaaa);", // inconclusive + Style); } TEST_F(FormatTest, FormatsBuilderPattern) { @@ -6527,19 +6544,19 @@ verifyFormat("someFunction(\"Always break between multi-line\"\n" " \" string literals\",\n" " and, 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\";"); @@ -6871,28 +6888,28 @@ 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)); + verifyFormat("x = \"a\\\n" + "b\\\n" + "c\";", + "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); @@ -6964,9 +6981,9 @@ " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); // Incomplete string literal. - EXPECT_EQ("llvm::errs() << \"\n" - " << a;", - format("llvm::errs() << \"\n<cccccc)\n" @@ -7214,14 +7231,14 @@ " 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);"); @@ -7278,52 +7295,51 @@ 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)); - - EXPECT_EQ("template <\n" - " typename aaaaaaaaaa::value> // trailing\n" - "void f() {}", - format("template <\n" - " typename aaaaaaaaaa::value> // trailing\n" - "void f() {}", - Style)); + verifyFormat("// Baseline - no comments.\n" + "template <\n" + " typename aaaaaaaaaaaaaaaaaaaaaa::value>\n" + "void f() {}", + "// Baseline - no comments.\n" + "template <\n" + " typename aaaaaaaaaaaaaaaaaaaaaa::value>\n" + "void f() {}", + Style); - EXPECT_EQ( + verifyFormat("template <\n" + " typename aaaaaaaaaa::value> // trailing\n" + "void f() {}", + "template <\n" + " typename aaaaaaaaaa::value> // trailing\n" + "void f() {}", + Style); + + 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) { @@ -7422,12 +7438,12 @@ 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)); @@ -7473,15 +7489,15 @@ } 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) { @@ -7982,15 +7998,15 @@ 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)); // FIXME: We cannot handle this case yet; we might be able to figure out that // foo d > v; doesn't make sense. @@ -8103,28 +8119,28 @@ " //...\n" "}"); - verifyFormat("MACRO;\n" + verifyFormat("MACRO;\n" + "[[nodiscard]] int foo() {\n" + " //...\n" + "}"); + + verifyFormat("MACRO\n\n" + "__attribute__((maybe_unused)) int foo() {\n" + " //...\n" + "}", + "MACRO\n\n" + "__attribute__((maybe_unused)) int foo() {\n" + " //...\n" + "}"); + + verifyFormat("MACRO\n\n" + "[[nodiscard]] int foo() {\n" + " //...\n" + "}", + "MACRO\n\n" "[[nodiscard]] int foo() {\n" " //...\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" - "}")); } TEST_F(FormatTest, AttributePenaltyBreaking) { @@ -8151,36 +8167,36 @@ } 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) { @@ -8534,8 +8550,8 @@ "#include \"some very long include path\"\n" "#include \n", 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 "); @@ -8600,21 +8616,21 @@ } 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) { @@ -8689,21 +8705,21 @@ } TEST_F(FormatTest, IncorrectCodeErrorDetection) { - EXPECT_EQ("{\n {}\n", format("{\n{\n}\n")); - EXPECT_EQ("{\n {}\n", format("{\n {\n}\n")); - EXPECT_EQ("{\n {}\n", format("{\n {\n }\n")); - EXPECT_EQ("{\n {}\n}\n}\n", format("{\n {\n }\n }\n}\n")); - - EXPECT_EQ("{\n" - " {\n" - " breakme(\n" - " qwe);\n" - " }\n", - format("{\n" - " {\n" - " breakme(qwe);\n" - "}\n", - getLLVMStyleWithColumns(10))); + verifyFormat("{\n {}\n", "{\n{\n}\n"); + verifyFormat("{\n {}\n", "{\n {\n}\n"); + verifyFormat("{\n {}\n", "{\n {\n }\n"); + verifyFormat("{\n {}\n}\n}\n", "{\n {\n }\n }\n}\n"); + + verifyFormat("{\n" + " {\n" + " breakme(\n" + " qwe);\n" + " }\n", + "{\n" + " {\n" + " breakme(qwe);\n" + "}\n", + getLLVMStyleWithColumns(10)); } TEST_F(FormatTest, LayoutCallsInsideBraceInitializers) { @@ -8840,14 +8856,14 @@ 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. @@ -8859,34 +8875,34 @@ // 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 @@ -8986,20 +9002,20 @@ " 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.SpaceInEmptyParentheses = true; verifyFormat("vector< int > x{ };", SpaceBetweenBraces); @@ -9223,27 +9239,27 @@ FormatStyle NoColumnLimit = getLLVMStyle(); NoColumnLimit.ColumnLimit = 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 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" @@ -9697,13 +9713,12 @@ } TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) { - EXPECT_EQ("#error Leave all white!!!!! space* alone!\n", - format("#error Leave all white!!!!! space* alone!\n")); - EXPECT_EQ( - "#warning Leave all white!!!!! space* alone!\n", - format("#warning Leave all white!!!!! space* alone!\n")); - EXPECT_EQ("#error 1", format(" # error 1")); - EXPECT_EQ("#warning 1", format(" # warning 1")); + verifyFormat("#error Leave all white!!!!! space* alone!\n", + "#error Leave all white!!!!! space* alone!\n"); + verifyFormat("#warning Leave all white!!!!! space* alone!\n", + "#warning Leave all white!!!!! space* alone!\n"); + verifyFormat("#error 1", " # error 1"); + verifyFormat("#warning 1", " # warning 1"); } TEST_F(FormatTest, FormatHashIfExpressions) { @@ -9727,8 +9742,7 @@ 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" @@ -9833,22 +9847,22 @@ 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;")); - EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", - format("- (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("- (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;", + "- (id)Method6:(id)A:(id)B:(id)C:(id)D;"); + verifyFormat("- (void)sendAction:(SEL)aSelector to:(id)anObject " + "forAllCells:(BOOL)flag;", + "- (void)sendAction:(SEL)aSelector to:(id)anObject " + "forAllCells:(BOOL)flag;"); // Very long objectiveC method declaration. verifyFormat("- (void)aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:\n" @@ -9900,89 +9914,84 @@ } TEST_F(FormatTest, BreaksStringLiterals) { - EXPECT_EQ("\"some text \"\n" - "\"other\";", - format("\"some text other\";", getLLVMStyleWithColumns(12))); - 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))); - - EXPECT_EQ("\"some text\"", - format("\"some text\"", getLLVMStyleWithColumns(1))); - EXPECT_EQ("\"some text\"", - format("\"some text\"", getLLVMStyleWithColumns(11))); - EXPECT_EQ("\"some \"\n" - "\"text\"", - format("\"some text\"", getLLVMStyleWithColumns(10))); - EXPECT_EQ("\"some \"\n" - "\"text\"", - format("\"some text\"", getLLVMStyleWithColumns(7))); - EXPECT_EQ("\"some\"\n" - "\" tex\"\n" - "\"t\"", - format("\"some text\"", getLLVMStyleWithColumns(6))); - EXPECT_EQ("\"some\"\n" - "\" tex\"\n" - "\" and\"", - format("\"some tex and\"", getLLVMStyleWithColumns(6))); - 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))); - - EXPECT_EQ("variable = f(\n" - " \"long string \"\n" - " \"literal\",\n" - " short,\n" - " loooooooooooooooooooong);", - format("variable = f(\"long string literal\", short, " - "loooooooooooooooooooong);", - getLLVMStyleWithColumns(20))); + verifyFormat("\"some text \"\n" + "\"other\";", + "\"some text other\";", getLLVMStyleWithColumns(12)); + verifyFormat("\"some text \"\n" + "\"other\";", + "\\\n\"some 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\"", "\"some text\"", getLLVMStyleWithColumns(1)); + verifyFormat("\"some text\"", "\"some text\"", getLLVMStyleWithColumns(11)); + verifyFormat("\"some \"\n" + "\"text\"", + "\"some text\"", getLLVMStyleWithColumns(10)); + verifyFormat("\"some \"\n" + "\"text\"", + "\"some text\"", getLLVMStyleWithColumns(7)); + verifyFormat("\"some\"\n" + "\" tex\"\n" + "\"t\"", + "\"some text\"", getLLVMStyleWithColumns(6)); + verifyFormat("\"some\"\n" + "\" tex\"\n" + "\" and\"", + "\"some tex and\"", getLLVMStyleWithColumns(6)); + verifyFormat("\"some\"\n" + "\"/tex\"\n" + "\"/and\"", + "\"some/tex/and\"", getLLVMStyleWithColumns(6)); + + verifyFormat("variable =\n" + " \"long string \"\n" + " \"literal\";", + "variable = \"long string literal\";", + 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("variable = f(\n" + " \"long string \"\n" + " \"literal\",\n" + " short,\n" + " loooooooooooooooooooong);", + "variable = f(\"long string literal\", short, " + "loooooooooooooooooooong);", + getLLVMStyleWithColumns(20)); + + 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( @@ -9990,117 +9999,114 @@ " aaaaaaaaaaaaaaaaaaaa,\n" " aaaaaa(\"aaa aaaaa aaa aaa aaaaa aaa aaaaa aaa aaa aaaaaa\"));"); - EXPECT_EQ("\"splitmea\"\n" - "\"trandomp\"\n" - "\"oint\"", - format("\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10))); - - EXPECT_EQ("\"split/\"\n" - "\"pathat/\"\n" - "\"slashes\"", - format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); - - EXPECT_EQ("\"split/\"\n" - "\"pathat/\"\n" - "\"slashes\"", - format("\"split/pathat/slashes\"", getLLVMStyleWithColumns(10))); - EXPECT_EQ("\"split at \"\n" - "\"spaces/at/\"\n" - "\"slashes.at.any$\"\n" - "\"non-alphanumeric%\"\n" - "\"1111111111characte\"\n" - "\"rs\"", - format("\"split at " - "spaces/at/" - "slashes.at." - "any$non-" - "alphanumeric%" - "1111111111characte" - "rs\"", - getLLVMStyleWithColumns(20))); + verifyFormat("\"splitmea\"\n" + "\"trandomp\"\n" + "\"oint\"", + "\"splitmeatrandompoint\"", getLLVMStyleWithColumns(10)); + + verifyFormat("\"split/\"\n" + "\"pathat/\"\n" + "\"slashes\"", + "\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)); + + verifyFormat("\"split/\"\n" + "\"pathat/\"\n" + "\"slashes\"", + "\"split/pathat/slashes\"", getLLVMStyleWithColumns(10)); + verifyFormat("\"split at \"\n" + "\"spaces/at/\"\n" + "\"slashes.at.any$\"\n" + "\"non-alphanumeric%\"\n" + "\"1111111111characte\"\n" + "\"rs\"", + "\"split at " + "spaces/at/" + "slashes.at." + "any$non-" + "alphanumeric%" + "1111111111characte" + "rs\"", + getLLVMStyleWithColumns(20)); // 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; - EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style)); + verifyFormat("\"some text other\";", "\"some text other\";", Style); 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) { - EXPECT_EQ( + verifyFormat( "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "(\n" " \"x\t\");", - format("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" - "aaaaaaa(" - "\"x\t\");")); + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaa(" + "\"x\t\");"); } TEST_F(FormatTest, BreaksWideAndNSStringLiterals) { - EXPECT_EQ( - "u8\"utf8 string \"\n" - "u8\"literal\";", - format("u8\"utf8 string literal\";", getGoogleStyleWithColumns(16))); - EXPECT_EQ( - "u\"utf16 string \"\n" - "u\"literal\";", - format("u\"utf16 string literal\";", getGoogleStyleWithColumns(16))); - EXPECT_EQ( - "U\"utf32 string \"\n" - "U\"literal\";", - format("U\"utf32 string literal\";", getGoogleStyleWithColumns(16))); - 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("u8\"utf8 string \"\n" + "u8\"literal\";", + "u8\"utf8 string literal\";", getGoogleStyleWithColumns(16)); + verifyFormat("u\"utf16 string \"\n" + "u\"literal\";", + "u\"utf16 string literal\";", getGoogleStyleWithColumns(16)); + verifyFormat("U\"utf32 string \"\n" + "U\"literal\";", + "U\"utf32 string literal\";", getGoogleStyleWithColumns(16)); + verifyFormat("L\"wide string \"\n" + "L\"literal\";", + "L\"wide string literal\";", getGoogleStyleWithColumns(16)); + 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 @@ -10112,213 +10118,207 @@ TEST_F(FormatTest, DoesNotBreakRawStringLiterals) { FormatStyle Style = getGoogleStyleWithColumns(15); - EXPECT_EQ("R\"x(raw literal)x\";", format("R\"x(raw literal)x\";", Style)); - EXPECT_EQ("uR\"x(raw literal)x\";", format("uR\"x(raw literal)x\";", Style)); - EXPECT_EQ("LR\"x(raw literal)x\";", format("LR\"x(raw literal)x\";", Style)); - EXPECT_EQ("UR\"x(raw literal)x\";", format("UR\"x(raw literal)x\";", Style)); - EXPECT_EQ("u8R\"x(raw literal)x\";", - format("u8R\"x(raw literal)x\";", Style)); + verifyFormat("R\"x(raw literal)x\";", "R\"x(raw literal)x\";", Style); + verifyFormat("uR\"x(raw literal)x\";", "uR\"x(raw literal)x\";", Style); + verifyFormat("LR\"x(raw literal)x\";", "LR\"x(raw literal)x\";", Style); + verifyFormat("UR\"x(raw literal)x\";", "UR\"x(raw literal)x\";", Style); + verifyFormat("u8R\"x(raw literal)x\";", "u8R\"x(raw literal)x\";", Style); } TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) { FormatStyle Style = getLLVMStyleWithColumns(20); - 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("_T(\"aaaaaaaaaaaaaa\")\n" + "_T(\"aaaaaaaaaaaaaa\")\n" + "_T(\"aaaaaaaaaaaa\")", + " _T(\"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\")", 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" + // verifyFormat("_T(\"aaaaaaaaaaaaa\")\n" // "_T(\"aaaaaaaaaaaaa\")\n" // "_T(\"aaaaaaaaaaaaa\")\n" // "_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\"));")); + // " _T ( \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" )", + // getLLVMStyleWithColumns(20)); + 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\"));"); } 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); } 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)); + + verifyFormat("fffffffffff(R\"x(\n" + "multiline raw string literal xxxxxxxxxxxxxx\n" + ")x\");", + "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) { @@ -10337,85 +10337,85 @@ 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) { - EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3))); - EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2))); - EXPECT_EQ("\"test\"\n" - "\"\\n\"", - format("\"test\\n\"", getLLVMStyleWithColumns(7))); - EXPECT_EQ("\"tes\\\\\"\n" - "\"n\"", - format("\"tes\\\\n\"", getLLVMStyleWithColumns(7))); - EXPECT_EQ("\"\\\\\\\\\"\n" - "\"\\n\"", - format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7))); - EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7))); - EXPECT_EQ("\"\\uff01\"\n" - "\"test\"", - format("\"\\uff01test\"", getLLVMStyleWithColumns(8))); - EXPECT_EQ("\"\\Uff01ff02\"", - format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11))); - EXPECT_EQ("\"\\x000000000001\"\n" - "\"next\"", - format("\"\\x000000000001next\"", getLLVMStyleWithColumns(16))); - EXPECT_EQ("\"\\x000000000001next\"", - format("\"\\x000000000001next\"", getLLVMStyleWithColumns(15))); - EXPECT_EQ("\"\\x000000000001\"", - format("\"\\x000000000001\"", getLLVMStyleWithColumns(7))); - EXPECT_EQ("\"test\"\n" - "\"\\000000\"\n" - "\"000001\"", - format("\"test\\000000000001\"", getLLVMStyleWithColumns(9))); - EXPECT_EQ("\"test\\000\"\n" - "\"00000000\"\n" - "\"1\"", - format("\"test\\000000000001\"", getLLVMStyleWithColumns(10))); + verifyFormat("\"\\a\"", "\"\\a\"", getLLVMStyleWithColumns(3)); + verifyFormat("\"\\\"", "\"\\\"", getLLVMStyleWithColumns(2)); + verifyFormat("\"test\"\n" + "\"\\n\"", + "\"test\\n\"", getLLVMStyleWithColumns(7)); + verifyFormat("\"tes\\\\\"\n" + "\"n\"", + "\"tes\\\\n\"", getLLVMStyleWithColumns(7)); + verifyFormat("\"\\\\\\\\\"\n" + "\"\\n\"", + "\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)); + verifyFormat("\"\\uff01\"", "\"\\uff01\"", getLLVMStyleWithColumns(7)); + verifyFormat("\"\\uff01\"\n" + "\"test\"", + "\"\\uff01test\"", getLLVMStyleWithColumns(8)); + verifyFormat("\"\\Uff01ff02\"", "\"\\Uff01ff02\"", + getLLVMStyleWithColumns(11)); + verifyFormat("\"\\x000000000001\"\n" + "\"next\"", + "\"\\x000000000001next\"", getLLVMStyleWithColumns(16)); + verifyFormat("\"\\x000000000001next\"", "\"\\x000000000001next\"", + getLLVMStyleWithColumns(15)); + verifyFormat("\"\\x000000000001\"", "\"\\x000000000001\"", + getLLVMStyleWithColumns(7)); + verifyFormat("\"test\"\n" + "\"\\000000\"\n" + "\"000001\"", + "\"test\\000000000001\"", getLLVMStyleWithColumns(9)); + verifyFormat("\"test\\000\"\n" + "\"00000000\"\n" + "\"1\"", + "\"test\\000000000001\"", getLLVMStyleWithColumns(10)); } TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { @@ -10474,18 +10474,18 @@ 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" @@ -10537,15 +10537,15 @@ 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); Tab.UseTab = FormatStyle::UT_ForIndentation; verifyFormat("{\n" @@ -10563,13 +10563,13 @@ "\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" @@ -10587,130 +10587,130 @@ "\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); + verifyFormat("{\n" + "\t/*\n" + "\n" + "\t*/\n" + "}", + "{\n" + "\t/*\n" + "\n" + "\t*/\n" + "}", + Tab); + verifyFormat("{\n" + "\t/*\n" + " asdf\n" + "\t*/\n" + "}", + "{\n" + "\t/*\n" + " asdf\n" + "\t*/\n" + "}", + Tab); 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); + + 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; - 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" @@ -10754,15 +10754,15 @@ 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" @@ -10778,13 +10778,13 @@ "\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" @@ -10802,125 +10802,125 @@ "\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); + verifyFormat("{\n" + "\t/*\n" + "\n" + "\t*/\n" + "}", + "{\n" + "\t/*\n" + "\n" + "\t*/\n" + "}", + Tab); + verifyFormat("{\n" + "\t/*\n" + " asdf\n" + "\t*/\n" + "}", + "{\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 = true; Tab.AlignConsecutiveDeclarations = true; Tab.TabWidth = 4; @@ -10938,18 +10938,18 @@ 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" @@ -10991,175 +10991,175 @@ " }\n" "};", 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)); + Tab.TabWidth = 8; + Tab.IndentWidth = 8; + 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" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" + "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" + "};", + Tab); + verifyFormat("enum AA {\n" + "\ta1, // Force multiple lines\n" + "\ta2,\n" + "\ta3\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" + "\t\t parameter2);\n" + "\t}\n" + "};", + Tab); + verifyFormat("{\n" + "\tQ(\n" + "\t {\n" + "\t\t int a;\n" + "\t\t someFunction(aaaaaaaa,\n" + "\t\t bbbbbbb);\n" + "\t },\n" + "\t p);\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); + verifyFormat("{\n" + "\t/*\n" + "\n" + "\t*/\n" + "}", + "{\n" + "\t/*\n" + "\n" + "\t*/\n" + "}", + Tab); verifyFormat("{\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" - "\taaaaaaaaaaaaaaaaaaaaaaaaaaaa();\n" - "};", + "\t/*\n" + " asdf\n" + "\t*/\n" + "}", + "{\n" + "\t/*\n" + " asdf\n" + "\t*/\n" + "}", Tab); - verifyFormat("enum AA {\n" - "\ta1, // Force multiple lines\n" - "\ta2,\n" - "\ta3\n" - "};", + verifyFormat("/* some\n" + " comment */", + " \t \t /* some\n" + " \t \t comment */", Tab); - EXPECT_EQ("if (aaaaaaaa && // q\n" - " bb) // w\n" - "\t;", - format("if (aaaaaaaa &&// q\n" - "bb)// w\n" - ";", - Tab)); - verifyFormat("class X {\n" - "\tvoid f() {\n" - "\t\tsomeFunction(parameter1,\n" - "\t\t parameter2);\n" - "\t}\n" - "};", + 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" - "\tQ(\n" - "\t {\n" - "\t\t int a;\n" - "\t\t someFunction(aaaaaaaa,\n" - "\t\t bbbbbbb);\n" - "\t },\n" - "\t p);\n" + "\t/*\n" + "\t * Comment\n" + "\t */\n" + "\tint i;\n" + "}", + "{\n" + "\t/*\n" + "\t * Comment\n" + "\t */\n" + "\t int i;\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)); 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 = true; Tab.AlignConsecutiveDeclarations = true; Tab.TabWidth = 4; @@ -11180,122 +11180,122 @@ 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)); - - 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// 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)); - - 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// 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)); - - 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// 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)); - - 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// 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)); - - 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// 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 */", - getLLVMStyle())); - EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" - "/* some\n" - " comment */", - format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" - " /* some\n" - " comment */", - getLLVMStyle())); - EXPECT_EQ("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" - "qqq\n" - "/* some\n" - " comment */", - format("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" - "qqq\n" - " /* some\n" - " comment */", - getLLVMStyle())); - EXPECT_EQ("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" - "wwww; /* some\n" - " comment */", - format(" inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" - "wwww; /* some\n" - " comment */", - getLLVMStyle())); + verifyFormat("\"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "q\"; /* some\n" + " comment */", + " \"qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "q\"; /* some\n" + " comment */", + getLLVMStyle()); + verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" + "/* some\n" + " comment */", + "// qqqqqqqqqqqqqqqqqqqqqqqqqq\n" + " /* some\n" + " comment */", + getLLVMStyle()); + verifyFormat("// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "qqq\n" + "/* some\n" + " comment */", + "// qqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "qqq\n" + " /* some\n" + " comment */", + getLLVMStyle()); + verifyFormat("inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "wwww; /* some\n" + " comment */", + " inttt qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq\\\n" + "wwww; /* some\n" + " comment */", + getLLVMStyle()); } TEST_F(FormatTest, ConfigurableSpaceBeforeParens) { @@ -11931,33 +11931,33 @@ "};\n", 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" @@ -12069,13 +12069,13 @@ Alignment.ReflowComments = true; Alignment.ColumnLimit = 50; - EXPECT_EQ("int x = 0;\n" - "int yy = 1; /// specificlennospace\n" - "int zzz = 2;\n", - format("int x = 0;\n" - "int yy = 1; ///specificlennospace\n" - "int zzz = 2;\n", - Alignment)); + verifyFormat("int x = 0;\n" + "int yy = 1; /// specificlennospace\n" + "int zzz = 2;\n", + "int x = 0;\n" + "int yy = 1; ///specificlennospace\n" + "int zzz = 2;\n", + Alignment); } TEST_F(FormatTest, AlignConsecutiveBitFields) { @@ -12159,33 +12159,33 @@ verifyFormat("int oneTwoThree{0}; // comment\n" "unsigned oneTwo; // comment", 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();", @@ -12197,25 +12197,25 @@ // 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; @@ -12268,29 +12268,29 @@ " double bar();\n" "};\n", Alignment); - 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)); + 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); Alignment.AlignConsecutiveAssignments = false; Alignment.AlignEscapedNewlines = FormatStyle::ENAS_DontAlign; verifyFormat("#define A \\\n" @@ -12460,9 +12460,9 @@ // See PR37175 FormatStyle Style = getMozillaStyle(); Style.AlignConsecutiveDeclarations = 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); } TEST_F(FormatTest, LinuxBraceBreaking) { @@ -12617,17 +12617,17 @@ FormatStyle AllmanBraceStyle = getLLVMStyle(); AllmanBraceStyle.BreakBeforeBraces = FormatStyle::BS_Allman; - EXPECT_EQ("namespace a\n" - "{\n" - "void f();\n" - "void g();\n" - "} // namespace a\n", - format("namespace a\n" - "{\n" - "void f();\n" - "void g();\n" - "}\n", - AllmanBraceStyle)); + verifyFormat("namespace a\n" + "{\n" + "void f();\n" + "void g();\n" + "} // namespace a\n", + "namespace a\n" + "{\n" + "void f();\n" + "void g();\n" + "}\n", + AllmanBraceStyle); verifyFormat("namespace a\n" "{\n" @@ -13272,16 +13272,16 @@ 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)."); } 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, OptimizeBreakPenaltyVsExcess) { @@ -13289,125 +13289,124 @@ Style.ColumnLimit = 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)); + verifyFormat("int a; /* first line\n" + " * second\n" + " * line third\n" + " * line\n" + " */", + "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); + verifyFormat("int a; /* first line\n" + " * second line\n" + " * third line\n" + " */", + "int a; /* first line\n" + " * second line\n" + " * third line\n" + " */", + Style); + verifyFormat("int a; // first line\n" + " // second line\n" + " // third line", + "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)); - - EXPECT_EQ("// foo bar baz bazfoo\n" - "// foo bar foo bar\n", - format("// foo bar baz bazfoo\n" - "// foo bar foo bar\n", - Style)); - EXPECT_EQ("// foo bar baz bazfoo\n" - "// foo bar foo bar\n", - format("// foo bar baz bazfoo\n" - "// foo bar foo bar\n", - Style)); + verifyFormat("int a; /* first line\n" + " * second line\n" + " * third line\n" + " */", + "int a; /* first line second line third line" + "\n*/", + Style); + + verifyFormat("// foo bar baz bazfoo\n" + "// foo bar foo bar\n", + "// foo bar baz bazfoo\n" + "// foo bar foo bar\n", + Style); + verifyFormat("// foo bar baz bazfoo\n" + "// foo bar foo bar\n", + "// foo bar baz bazfoo\n" + "// foo bar foo bar\n", + 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\n", - format("// foo bar baz bazfoo bar\n" - "// foo bar\n", - Style)); - - EXPECT_EQ("// foo bar baz bazfoo\n" - "// foo bar baz bazfoo\n" - "// bar foo bar\n", - format("// foo bar baz bazfoo\n" - "// foo bar baz bazfoo bar\n" - "// foo bar\n", - Style)); - - EXPECT_EQ("// foo bar baz bazfoo\n" - "// foo bar baz bazfoo\n" - "// bar foo bar\n", - format("// foo bar baz bazfoo\n" - "// foo bar baz bazfoo bar\n" - "// foo bar\n", - Style)); + verifyFormat("// foo bar baz bazfoo\n" + "// bar foo bar\n", + "// foo bar baz bazfoo bar\n" + "// foo bar\n", + Style); + + verifyFormat("// foo bar baz bazfoo\n" + "// foo bar baz bazfoo\n" + "// bar foo bar\n", + "// foo bar baz bazfoo\n" + "// foo bar baz bazfoo bar\n" + "// foo bar\n", + Style); + + verifyFormat("// foo bar baz bazfoo\n" + "// foo bar baz bazfoo\n" + "// bar foo bar\n", + "// foo bar baz bazfoo\n" + "// foo bar baz bazfoo bar\n" + "// foo bar\n", + Style); // 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\n", - format("// foo foo foo foo foo foo foo foo foo foo foo foo\n", Style)); + verifyFormat("// foo foo foo foo\n" + "// foo foo foo foo\n" + "// foo foo foo foo\n", + "// foo foo foo foo foo foo foo foo foo foo foo foo\n", 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); } #define EXPECT_ALL_STYLES_EQUAL(Styles) \ @@ -14109,22 +14108,22 @@ } TEST_F(FormatTest, WorksFor8bitEncodings) { - 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" - "\"\xef\xee\xf0\xf3...\"", - format("\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " - "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " - "\xef\xee\xf0\xf3...\"", - getLLVMStyleWithColumns(12))); + verifyFormat("\"\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" + "\"\xef\xee\xf0\xf3...\"", + "\"\xce\xe4\xed\xe0\xe6\xe4\xfb \xe2 " + "\xf1\xf2\xf3\xe4\xb8\xed\xf3\xfe \xe7\xe8\xec\xed\xfe\xfe " + "\xef\xee\xf0\xf3...\"", + getLLVMStyleWithColumns(12)); } TEST_F(FormatTest, HandlesUTF8BOM) { - EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf")); - EXPECT_EQ("\xef\xbb\xbf#include ", - format("\xef\xbb\xbf#include ")); - EXPECT_EQ("\xef\xbb\xbf\n#include ", - format("\xef\xbb\xbf\n#include ")); + verifyFormat("\xef\xbb\xbf", "\xef\xbb\xbf"); + verifyFormat("\xef\xbb\xbf#include ", + "\xef\xbb\xbf#include "); + verifyFormat("\xef\xbb\xbf\n#include ", + "\xef\xbb\xbf\n#include "); } // FIXME: Encode Cyrillic and CJK characters below to appease MS compilers. @@ -14149,87 +14148,85 @@ // 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. - EXPECT_EQ("\"aaaaÄ\"\n" - "\"\xc2\x8d\";", - format("\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); - EXPECT_EQ("\"aaaaaaaÄ\"\n" - "\"\xc2\x8d\";", - format("\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10))); - EXPECT_EQ("\"Однажды, в \"\n" - "\"студёную \"\n" - "\"зимнюю \"\n" - "\"пору,\"", - format("\"Однажды, в студёную зимнюю пору,\"", - getLLVMStyleWithColumns(13))); - EXPECT_EQ( - "\"一 二 三 \"\n" - "\"四 五六 \"\n" - "\"七 八 九 \"\n" - "\"十\"", - format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); - EXPECT_EQ("\"一\t\"\n" - "\"二 \t\"\n" - "\"三 四 \"\n" - "\"五\t\"\n" - "\"六 \t\"\n" - "\"七 \"\n" - "\"八九十\tqq\"", - format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", - getLLVMStyleWithColumns(11))); + verifyFormat("\"aaaaÄ\"\n" + "\"\xc2\x8d\";", + "\"aaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)); + verifyFormat("\"aaaaaaaÄ\"\n" + "\"\xc2\x8d\";", + "\"aaaaaaaÄ\xc2\x8d\";", getLLVMStyleWithColumns(10)); + verifyFormat("\"Однажды, в \"\n" + "\"студёную \"\n" + "\"зимнюю \"\n" + "\"пору,\"", + "\"Однажды, в студёную зимнюю пору,\"", + getLLVMStyleWithColumns(13)); + verifyFormat("\"一 二 三 \"\n" + "\"四 五六 \"\n" + "\"七 八 九 \"\n" + "\"十\"", + "\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11)); + verifyFormat("\"一\t\"\n" + "\"二 \t\"\n" + "\"三 四 \"\n" + "\"五\t\"\n" + "\"六 \t\"\n" + "\"七 \"\n" + "\"八九十\tqq\"", + "\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", + getLLVMStyleWithColumns(11)); // UTF8 character in an escape sequence. - EXPECT_EQ("\"aaaaaa\"\n" - "\"\\\xC2\x8D\"", - format("\"aaaaaa\\\xC2\x8D\"", getLLVMStyleWithColumns(10))); + verifyFormat("\"aaaaaa\"\n" + "\"\\\xC2\x8D\"", + "\"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) { - EXPECT_EQ("// aaaaÄ\xc2\x8d", - format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10))); - EXPECT_EQ("// Я из лесу\n" - "// вышел; был\n" - "// сильный\n" - "// мороз.", - format("// Я из лесу вышел; был сильный мороз.", - getLLVMStyleWithColumns(13))); - EXPECT_EQ("// 一二三\n" - "// 四五六七\n" - "// 八 九\n" - "// 十", - format("// 一二三 四五六七 八 九 十", getLLVMStyleWithColumns(9))); + verifyFormat("// aaaaÄ\xc2\x8d", "// aaaaÄ\xc2\x8d", + getLLVMStyleWithColumns(10)); + 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 @@ -14384,32 +14381,33 @@ 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. @@ -14426,11 +14424,11 @@ "{\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" @@ -14459,69 +14457,69 @@ 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) { @@ -14561,14 +14559,14 @@ 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" "}"); @@ -15305,23 +15303,23 @@ " }\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" @@ -15350,153 +15348,153 @@ 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;\r\n", - format("int a;\r\n" - " int b;\r\n" - " int c;\r\n", - getLLVMStyle())); - 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", - getLLVMStyle())); - EXPECT_EQ("int a;\n" - "int b;\n" - "int c;\n", - format("int a;\r\n" - " int b;\n" - " int c;\n", - getLLVMStyle())); - 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;\r\n", - format("#define A \\\r\n" - " b; \\\r\n" - " c; d; \r\n", - getGoogleStyle())); - - EXPECT_EQ("/*\r\n" - "multi line block comments\r\n" - "should not introduce\r\n" - "an extra carriage return\r\n" - "*/\r\n", - format("/*\r\n" - "multi line block comments\r\n" - "should not introduce\r\n" - "an extra carriage return\r\n" - "*/\r\n")); - EXPECT_EQ("/*\r\n" - "\r\n" - "*/", - format("/*\r\n" - " \r\r\r\n" - "*/")); + verifyFormat("int a;\r\n" + "int b;\r\n" + "int c;\r\n", + "int a;\r\n" + " int b;\r\n" + " int c;\r\n", + getLLVMStyle()); + verifyFormat("int a;\r\n" + "int b;\r\n" + "int c;\r\n", + "int a;\r\n" + " int b;\n" + " int c;\r\n", + getLLVMStyle()); + verifyFormat("int a;\n" + "int b;\n" + "int c;\n", + "int a;\r\n" + " int b;\n" + " int c;\n", + getLLVMStyle()); + verifyFormat("\"aaaaaaa \"\r\n" + "\"bbbbbbb\";\r\n", + "\"aaaaaaa bbbbbbb\";\r\n", getLLVMStyleWithColumns(10)); + verifyFormat("#define A \\\r\n" + " b; \\\r\n" + " c; \\\r\n" + " d;\r\n", + "#define A \\\r\n" + " b; \\\r\n" + " c; d; \r\n", + getGoogleStyle()); + + verifyFormat("/*\r\n" + "multi line block comments\r\n" + "should not introduce\r\n" + "an extra carriage return\r\n" + "*/\r\n", + "/*\r\n" + "multi line block comments\r\n" + "should not introduce\r\n" + "an extra carriage return\r\n" + "*/\r\n"); + verifyFormat("/*\r\n" + "\r\n" + "*/", + "/*\r\n" + " \r\r\r\n" + "*/"); FormatStyle style = getLLVMStyle(); style.DeriveLineEnding = true; style.UseCRLF = false; - 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.UseCRLF = true; - 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.DeriveLineEnding = false; style.UseCRLF = false; - 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.UseCRLF = true; - 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.DeriveLineEnding = true; style.UseCRLF = false; - 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.UseCRLF = true; - 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) { @@ -15510,18 +15508,18 @@ 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) { @@ -15578,10 +15576,10 @@ 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" @@ -15590,7 +15588,7 @@ TEST_F(FormatTest, MergeLessLessAtEnd) { verifyFormat("<<"); - EXPECT_EQ("< < <", format("\\\n<<<")); + verifyFormat("< < <", "\\\n<<<"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" "aaallvm::outs() <<"); verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" @@ -15613,136 +15611,136 @@ 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);\n", - 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);\n")); + 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);\n", + "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);\n"); // 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);\n", - 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);\n")); - - 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);\n", + "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);\n"); + + verifyFormat("<<<<<<<\n" + "|||||||\n" + "=======\n" + ">>>>>>>", + "<<<<<<<\n" + "|||||||\n" + "=======\n" + ">>>>>>>"); + + verifyFormat("<<<<<<<\n" + "|||||||\n" + "int i;\n" + "=======\n" + ">>>>>>>", + "<<<<<<<\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;\n", - format("#define Macro \\\n" - "<<<<<<<\n" - " Something \\\n" - "|||||||\n" - " Else \\\n" - "=======\n" - " Other \\\n" - ">>>>>>>\n" - " End\n" - "int i;\n")); + verifyFormat("#define Macro \\\n" + "<<<<<<<\n" + "Something \\\n" + "|||||||\n" + "Else \\\n" + "=======\n" + "Other \\\n" + ">>>>>>>\n" + " End int i;\n", + "#define Macro \\\n" + "<<<<<<<\n" + " Something \\\n" + "|||||||\n" + " Else \\\n" + "=======\n" + " Other \\\n" + ">>>>>>>\n" + " End\n" + "int i;\n"); } 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 */\n", - 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 */\n", - 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 */\n", + "// 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 */\n", + getLLVMStyleWithColumns(20)); } TEST_F(FormatTest, DoNotCrashOnInvalidInput) { @@ -15757,23 +15755,23 @@ } 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();"); } @@ -15928,58 +15926,53 @@ } 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) { format::FormatStyle Style = format::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) { // u8'a' is a C++17 feature, utf8 literal character, LS_Cpp11 covers // all modes, including C++11, C++14 and C++17 - EXPECT_EQ("auto c = u8'a';", format("auto c = u8'a';")); + verifyFormat("auto c = u8'a';", "auto c = u8'a';"); } TEST_F(FormatTest, DoNotFormatLikelyXml) { - EXPECT_EQ("", format("", getGoogleStyle())); - EXPECT_EQ(" ", format(" ", getGoogleStyle())); + verifyFormat("", "", getGoogleStyle()); + verifyFormat(" ", " ", getGoogleStyle()); } 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(); @@ -15997,24 +15990,20 @@ verifyFormat("auto const &&[a2, b]{A * i};", getLLVMStyle()); 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};"); format::FormatStyle Spaces = format::getLLVMStyle(); Spaces.SpacesInSquareBrackets = true; @@ -16434,17 +16423,17 @@ "} // 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" @@ -16454,16 +16443,16 @@ " // " "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" @@ -16473,16 +16462,16 @@ " // " "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) {