Changeset View
Standalone View
clang/unittests/Format/FormatTest.cpp
- This file is larger than 256 KB, so syntax highlighting is disabled by default.
Show First 20 Lines • Show All 7,438 Lines • ▼ Show 20 Lines | TEST_F(FormatTest, AllowAllArgumentsOnNextLineDontAlign) { | |||||||||||||||||
StringRef Input = "functionCall(paramA, paramB, paramC);\n" | StringRef Input = "functionCall(paramA, paramB, paramC);\n" | |||||||||||||||||
"void functionDecl(int A, int B, int C);"; | "void functionDecl(int A, int B, int C);"; | |||||||||||||||||
Style.AllowAllArgumentsOnNextLine = false; | Style.AllowAllArgumentsOnNextLine = false; | |||||||||||||||||
Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; | Style.AlignAfterOpenBracket = FormatStyle::BAS_DontAlign; | |||||||||||||||||
EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" | EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" | |||||||||||||||||
" paramC);\n" | " paramC);\n" | |||||||||||||||||
"void functionDecl(int A, int B,\n" | "void functionDecl(int A, int B,\n" | |||||||||||||||||
" int C);"), | " int C);"), | |||||||||||||||||
format(Input, Style)); | format(Input, Style)); | |||||||||||||||||
HazardyKnusperkeks: Sorry for the confusion, you can (and should?) keep the `Input` variable. There is a… | ||||||||||||||||||
Ok, I restored it in this function. I didn't add it to the other functions since they weren't using it already. The vast majority of the calls to verifyFormat are using the 3-argument version. It's unclear what is preferred. gedare: Ok, I restored it in this function. I didn't add it to the other functions since they weren't… | ||||||||||||||||||
That depends. Most of the times the version with one string is enough. It verifies that:
The version with 2 strings does:
The latter should be useful, if you want to verify what happens with your defined line breaks, that should not be stable, because https://github.com/llvm/llvm-project/blob/e989b8bb5fb36abac6e8f82809f06144dd762113/clang/unittests/Format/FormatTestUtils.h#L25 messup removes most of them. HazardyKnusperkeks: That depends. Most of the times the version with one string is enough. It verifies that:
* The… | ||||||||||||||||||
Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; | Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; | |||||||||||||||||
EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" | EXPECT_EQ(StringRef("functionCall(paramA, paramB,\n" | |||||||||||||||||
" paramC);\n" | " paramC);\n" | |||||||||||||||||
"void functionDecl(int A, int B,\n" | "void functionDecl(int A, int B,\n" | |||||||||||||||||
" int C);"), | " int C);"), | |||||||||||||||||
format(Input, Style)); | format(Input, Style)); | |||||||||||||||||
// However, BAS_AlwaysBreak should take precedence over | // However, BAS_AlwaysBreak and BAS_BlockIndent should take precedence over | |||||||||||||||||
// AllowAllArgumentsOnNextLine. | // AllowAllArgumentsOnNextLine. | |||||||||||||||||
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; | |||||||||||||||||
EXPECT_EQ(StringRef("functionCall(\n" | EXPECT_EQ(StringRef("functionCall(\n" | |||||||||||||||||
" paramA, paramB, paramC);\n" | " paramA, paramB, paramC);\n" | |||||||||||||||||
"void functionDecl(\n" | "void functionDecl(\n" | |||||||||||||||||
" int A, int B, int C);"), | " int A, int B, int C);"), | |||||||||||||||||
format(Input, Style)); | format(Input, Style)); | |||||||||||||||||
Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; | ||||||||||||||||||
verifyFormat("functionCall(\n" | ||||||||||||||||||
" paramA, paramB, paramC\n" | ||||||||||||||||||
");\n" | ||||||||||||||||||
"void functionDecl(\n" | ||||||||||||||||||
" int A, int B, int C\n" | ||||||||||||||||||
");", | ||||||||||||||||||
Input, Style); | ||||||||||||||||||
I know you copy what is there, but could you use verifyFormat? You can port the other checks also, but are not obligated to. HazardyKnusperkeks: I know you copy what is there, but could you use verifyFormat? You can port the other checks… | ||||||||||||||||||
// When AllowAllArgumentsOnNextLine is set, we prefer breaking before the | // When AllowAllArgumentsOnNextLine is set, we prefer breaking before the | |||||||||||||||||
// first argument. | // first argument. | |||||||||||||||||
Style.AllowAllArgumentsOnNextLine = true; | Style.AllowAllArgumentsOnNextLine = true; | |||||||||||||||||
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; | |||||||||||||||||
EXPECT_EQ(StringRef("functionCall(\n" | EXPECT_EQ(StringRef("functionCall(\n" | |||||||||||||||||
" paramA, paramB, paramC);\n" | " paramA, paramB, paramC);\n" | |||||||||||||||||
"void functionDecl(\n" | "void functionDecl(\n" | |||||||||||||||||
▲ Show 20 Lines • Show All 1,186 Lines • ▼ Show 20 Lines | verifyFormat( | |||||||||||||||||
" aaaaaaaaaaaaaaaa);", | " aaaaaaaaaaaaaaaa);", | |||||||||||||||||
Style); | Style); | |||||||||||||||||
verifyFormat( | verifyFormat( | |||||||||||||||||
"aaaaaaaaaaaaaaaaaaaaaaaa(\n" | "aaaaaaaaaaaaaaaaaaaaaaaa(\n" | |||||||||||||||||
" aaaaaaaaaaaaaaaaaaaaa(\n" | " aaaaaaaaaaaaaaaaaaaaa(\n" | |||||||||||||||||
" aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" | " aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)) &&\n" | |||||||||||||||||
" aaaaaaaaaaaaaaaa);", | " aaaaaaaaaaaaaaaa);", | |||||||||||||||||
Style); | Style); | |||||||||||||||||
Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; | ||||||||||||||||||
Style.BinPackArguments = false; | ||||||||||||||||||
Style.BinPackParameters = false; | ||||||||||||||||||
verifyFormat("void aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" | ||||||||||||||||||
" aaaaaaaaaaa aaaaaaaa,\n" | ||||||||||||||||||
" aaaaaaaaa aaaaaaa,\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" | ||||||||||||||||||
") {}", | ||||||||||||||||||
Style); | ||||||||||||||||||
verifyFormat("SomeLongVariableName->someVeryLongFunctionName(\n" | ||||||||||||||||||
" aaaaaaaaaaa aaaaaaaaa,\n" | ||||||||||||||||||
" aaaaaaaaaaa aaaaaaaaa,\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" | ||||||||||||||||||
");", | ||||||||||||||||||
Style); | ||||||||||||||||||
verifyFormat("SomeLongVariableName->someFunction(foooooooo(\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaa,\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaaaaaaa,\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" | ||||||||||||||||||
"));", | ||||||||||||||||||
Style); | ||||||||||||||||||
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa(\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" | ||||||||||||||||||
"));", | ||||||||||||||||||
Style); | ||||||||||||||||||
verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaa.aaaaaaaaaa(\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" | ||||||||||||||||||
"));", | ||||||||||||||||||
Style); | ||||||||||||||||||
verifyFormat( | ||||||||||||||||||
"aaaaaaaaaaaaaaaaaaaaaaaa(\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaaaaaaa(\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" | ||||||||||||||||||
" ),\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaa\n" | ||||||||||||||||||
");", | ||||||||||||||||||
Style); | ||||||||||||||||||
verifyFormat( | ||||||||||||||||||
"aaaaaaaaaaaaaaaaaaaaaaaa(\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaaaaaaa(\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaa)\n" | ||||||||||||||||||
" ) &&\n" | ||||||||||||||||||
" aaaaaaaaaaaaaaaa\n" | ||||||||||||||||||
");", | ||||||||||||||||||
Style); | ||||||||||||||||||
} | } | |||||||||||||||||
TEST_F(FormatTest, ParenthesesAndOperandAlignment) { | TEST_F(FormatTest, ParenthesesAndOperandAlignment) { | |||||||||||||||||
FormatStyle Style = getLLVMStyleWithColumns(40); | FormatStyle Style = getLLVMStyleWithColumns(40); | |||||||||||||||||
verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" | verifyFormat("int a = f(aaaaaaaaaaaaaaaaaaaaaa &&\n" | |||||||||||||||||
" bbbbbbbbbbbbbbbbbbbbbb);", | " bbbbbbbbbbbbbbbbbbbbbb);", | |||||||||||||||||
Style); | Style); | |||||||||||||||||
Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; | Style.AlignAfterOpenBracket = FormatStyle::BAS_Align; | |||||||||||||||||
▲ Show 20 Lines • Show All 5,903 Lines • ▼ Show 20 Lines | TEST_F(FormatTest, BreaksStringLiteralOperands) { | |||||||||||||||||
// the first must be broken with a line break before it. | // the first must be broken with a line break before it. | |||||||||||||||||
FormatStyle Style = getLLVMStyleWithColumns(25); | FormatStyle Style = getLLVMStyleWithColumns(25); | |||||||||||||||||
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; | Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak; | |||||||||||||||||
EXPECT_EQ("someFunction(\n" | EXPECT_EQ("someFunction(\n" | |||||||||||||||||
" \"long long long \"\n" | " \"long long long \"\n" | |||||||||||||||||
" \"long\",\n" | " \"long\",\n" | |||||||||||||||||
" a);", | " a);", | |||||||||||||||||
format("someFunction(\"long long long long\", a);", Style)); | format("someFunction(\"long long long long\", a);", Style)); | |||||||||||||||||
Style.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; | ||||||||||||||||||
verifyFormat("someFunction(\n" | ||||||||||||||||||
" \"long long long \"\n" | ||||||||||||||||||
" \"long\",\n" | ||||||||||||||||||
" a\n" | ||||||||||||||||||
");", | ||||||||||||||||||
Style); | ||||||||||||||||||
} | } | |||||||||||||||||
TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { | TEST_F(FormatTest, DontSplitStringLiteralsWithEscapedNewlines) { | |||||||||||||||||
EXPECT_EQ( | EXPECT_EQ( | |||||||||||||||||
"aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" | "aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" | |||||||||||||||||
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" | |||||||||||||||||
" aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\";", | |||||||||||||||||
format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" | format("aaaaaaaaaaa = \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\\\n" | |||||||||||||||||
▲ Show 20 Lines • Show All 1,707 Lines • ▼ Show 20 Lines | TEST_F(FormatTest, ConfigurableSpacesInParentheses) { | |||||||||||||||||
verifyFormat("void foo( ) {\n" | verifyFormat("void foo( ) {\n" | |||||||||||||||||
" size_t foo = (*(function))(\n" | " size_t foo = (*(function))(\n" | |||||||||||||||||
" Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " | " Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " | |||||||||||||||||
"BarrrrrrrrrrrrLong,\n" | "BarrrrrrrrrrrrLong,\n" | |||||||||||||||||
" FoooooooooLooooong);\n" | " FoooooooooLooooong);\n" | |||||||||||||||||
"}", | "}", | |||||||||||||||||
Spaces); | Spaces); | |||||||||||||||||
Spaces.AlignAfterOpenBracket = FormatStyle::BAS_BlockIndent; | ||||||||||||||||||
verifyFormat("void foo( ) {\n" | ||||||||||||||||||
" size_t foo = (*(function))(\n" | ||||||||||||||||||
" Foooo, Barrrrr, Foooo, Barrrr, FoooooooooLooooong, " | ||||||||||||||||||
"BarrrrrrrrrrrrLong,\n" | ||||||||||||||||||
" FoooooooooLooooong\n" | ||||||||||||||||||
" );\n" | ||||||||||||||||||
"}", | ||||||||||||||||||
Spaces); | ||||||||||||||||||
verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces); | ||||||||||||||||||
verifyFormat("size_t idx = (size_t)a;", Spaces); | ||||||||||||||||||
verifyFormat("size_t idx = (size_t)(a - 1);", Spaces); | ||||||||||||||||||
verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces); | ||||||||||||||||||
verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces); | ||||||||||||||||||
verifyFormat("size_t idx = (*foo)(a - 1);", Spaces); | ||||||||||||||||||
verifyFormat("size_t idx = (*(foo))(a - 1);", Spaces); | ||||||||||||||||||
} | } | |||||||||||||||||
TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { | TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) { | |||||||||||||||||
verifyFormat("int a[5];"); | verifyFormat("int a[5];"); | |||||||||||||||||
verifyFormat("a[3] += 42;"); | verifyFormat("a[3] += 42;"); | |||||||||||||||||
FormatStyle Spaces = getLLVMStyle(); | FormatStyle Spaces = getLLVMStyle(); | |||||||||||||||||
Spaces.SpacesInSquareBrackets = true; | Spaces.SpacesInSquareBrackets = true; | |||||||||||||||||
▲ Show 20 Lines • Show All 8,726 Lines • Show Last 20 Lines |
Sorry for the confusion, you can (and should?) keep the Input variable. There is a verifyFormat with 4 arguments. So that the input in the formatting doesn't have to be the expected.