Skip to content

Commit

Permalink
[clang-format][NFC] Clean up unit tests
Browse files Browse the repository at this point in the history
This patch adds a verifyNoChange macro to verify code that won't
change after being formatted. (The code will not be messed up before
being formatted.) It then replaces EXPECT_EQ with verifyFormat
wherever applicable so that the code will be messed up before being
formatted. When the replacement fails the unit test, verifyFormat is
replaced with verifyNoChange.

Differential Revision: https://reviews.llvm.org/D153109
  • Loading branch information
owenca committed Jun 16, 2023
1 parent 12c12c5 commit 20b4df1
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 205 deletions.
143 changes: 58 additions & 85 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ TEST_F(FormatTest, LLVMStyleOverride) {
// Basic function tests.
//===----------------------------------------------------------------------===//

TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) {
EXPECT_EQ(";", format(";"));
}
TEST_F(FormatTest, DoesNotChangeCorrectlyFormattedCode) { verifyFormat(";"); }

TEST_F(FormatTest, FormatsGlobalStatementsAt0) {
EXPECT_EQ("int i;", format(" int i;"));
Expand Down Expand Up @@ -5159,7 +5157,7 @@ TEST_F(FormatTest, UnderstandsLinePPDirective) {
}

TEST_F(FormatTest, LayoutUnknownPPDirective) {
EXPECT_EQ("#;", format("#;"));
verifyFormat("#;");
verifyFormat("#\n;\n;\n;");
}

Expand Down Expand Up @@ -5492,7 +5490,7 @@ TEST_F(FormatTest, HandlePreprocessorDirectiveContext) {
getLLVMStyleWithColumns(13)));
}

TEST_F(FormatTest, LayoutSingleHash) { EXPECT_EQ("#\na;", format("#\na;")); }
TEST_F(FormatTest, LayoutSingleHash) { verifyFormat("#\na;"); }

TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
EXPECT_EQ("#define A \\\n"
Expand All @@ -5504,7 +5502,7 @@ TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
getLLVMStyleWithColumns(14)));
}

TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
TEST_F(FormatTest, LayoutRemainingTokens) { verifyFormat("{}"); }

TEST_F(FormatTest, MacroDefinitionInsideStatement) {
EXPECT_EQ("int x,\n"
Expand All @@ -5514,14 +5512,14 @@ TEST_F(FormatTest, MacroDefinitionInsideStatement) {
}

TEST_F(FormatTest, HashInMacroDefinition) {
EXPECT_EQ("#define A(c) L#c", format("#define A(c) L#c", getLLVMStyle()));
EXPECT_EQ("#define A(c) u#c", format("#define A(c) u#c", getLLVMStyle()));
EXPECT_EQ("#define A(c) U#c", format("#define A(c) U#c", getLLVMStyle()));
EXPECT_EQ("#define A(c) u8#c", format("#define A(c) u8#c", getLLVMStyle()));
EXPECT_EQ("#define A(c) LR#c", format("#define A(c) LR#c", getLLVMStyle()));
EXPECT_EQ("#define A(c) uR#c", format("#define A(c) uR#c", getLLVMStyle()));
EXPECT_EQ("#define A(c) UR#c", format("#define A(c) UR#c", getLLVMStyle()));
EXPECT_EQ("#define A(c) u8R#c", format("#define A(c) u8R#c", getLLVMStyle()));
verifyFormat("#define A(c) L#c", getLLVMStyle());
verifyFormat("#define A(c) u#c", getLLVMStyle());
verifyFormat("#define A(c) U#c", getLLVMStyle());
verifyFormat("#define A(c) u8#c", getLLVMStyle());
verifyFormat("#define A(c) LR#c", getLLVMStyle());
verifyFormat("#define A(c) uR#c", getLLVMStyle());
verifyFormat("#define A(c) UR#c", getLLVMStyle());
verifyFormat("#define A(c) u8R#c", getLLVMStyle());
verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11));
verifyFormat("#define A \\\n"
" { \\\n"
Expand All @@ -5541,8 +5539,8 @@ TEST_F(FormatTest, HashInMacroDefinition) {
}

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)");
verifyFormat("#define A(x)");

FormatStyle Style = getLLVMStyle();
Style.SpaceBeforeParens = FormatStyle::SBPO_Never;
Expand Down Expand Up @@ -5669,7 +5667,7 @@ TEST_F(FormatTest, MacrosWithoutTrailingSemicolon) {
"} // namespace"));
// Only if the identifier contains at least 5 characters.
EXPECT_EQ("HTTP f();", format("HTTP\nf();"));
EXPECT_EQ("MACRO\nf();", format("MACRO\nf();"));
verifyNoChange("MACRO\nf();");
// Only if everything is upper case.
EXPECT_EQ("class A : public QObject {\n"
" Q_Object A() {}\n"
Expand Down Expand Up @@ -6099,7 +6097,7 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
"# define C 0\n"
"#endif";
EXPECT_EQ(Expected, format(ToFormat, Style));
EXPECT_EQ(Expected, format(Expected, Style));
verifyFormat(Expected, Style);
}
// Keep block quotes aligned.
{
Expand All @@ -6126,7 +6124,7 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
"# define C 0\n"
"#endif";
EXPECT_EQ(Expected, format(ToFormat, Style));
EXPECT_EQ(Expected, format(Expected, Style));
verifyNoChange(Expected, Style);
}
// Keep comments aligned with un-indented directives.
{
Expand All @@ -6149,7 +6147,7 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
" // Code. Not aligned with #\n"
"#define C 0\n";
EXPECT_EQ(Expected, format(ToFormat, Style));
EXPECT_EQ(Expected, format(Expected, Style));
verifyFormat(Expected, Style);
}
// Test AfterHash with tabs.
{
Expand Down Expand Up @@ -6236,7 +6234,7 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
"#endif\n"
"}";
EXPECT_EQ(Expected, format(ToFormat, Style));
EXPECT_EQ(Expected, format(Expected, Style));
verifyFormat(Expected, Style);
}
{
const char *Expected = "void f() {\n"
Expand Down Expand Up @@ -6266,7 +6264,7 @@ TEST_F(FormatTest, IndentPreprocessorDirectives) {
"#endif\n"
"}";
EXPECT_EQ(Expected, format(ToFormat, Style));
EXPECT_EQ(Expected, format(Expected, Style));
verifyNoChange(Expected, Style);
}

// Test single comment before preprocessor
Expand Down Expand Up @@ -6432,7 +6430,7 @@ TEST_F(FormatTest, EscapedNewlines) {
EXPECT_EQ("#define A\n\nint i;", format("#define A \\\n\n int i;"));
EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
EXPECT_EQ("/* \\ \\ \\\n */", format("\\\n/* \\ \\ \\\n */"));
EXPECT_EQ("<a\n\\\\\n>", format("<a\n\\\\\n>"));
verifyNoChange("<a\n\\\\\n>");

FormatStyle AlignLeft = getLLVMStyle();
AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
Expand All @@ -6450,7 +6448,7 @@ TEST_F(FormatTest, EscapedNewlines) {
EXPECT_EQ("#define A\r\n\r\nint i;", format("#define A \\\r\n\r\n int i;"));
EXPECT_EQ("template <class T> f();", format("\\\ntemplate <class T> f();"));
EXPECT_EQ("/* \\ \\ \\\r\n */", format("\\\r\n/* \\ \\ \\\r\n */"));
EXPECT_EQ("<a\r\n\\\\\r\n>", format("<a\r\n\\\\\r\n>"));
verifyNoChange("<a\r\n\\\\\r\n>");
EXPECT_EQ("#define MACRO(x) \\\r\n"
"private: \\\r\n"
" int x(int a);\r\n",
Expand Down Expand Up @@ -6791,7 +6789,7 @@ TEST_F(FormatTest, FormatNestedBlocksInMacros) {
}

TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
EXPECT_EQ("{}", format("{}"));
verifyFormat("{}");
verifyFormat("enum E {};");
verifyFormat("enum E {}");
FormatStyle Style = getLLVMStyle();
Expand Down Expand Up @@ -12358,14 +12356,6 @@ TEST_F(FormatTest, FormatsAccessModifiers) {
"};\n",
Style);
verifyFormat("struct foo { /* comment */\n"
"private:\n"
" int i;\n"
" // comment\n"
"\n"
"private:\n"
" int j;\n"
"};\n",
"struct foo { /* comment */\n"
"private:\n"
" int i;\n"
" // comment\n"
Expand Down Expand Up @@ -14504,8 +14494,7 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) {
}

TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
EXPECT_EQ("#error Leave all white!!!!! space* alone!\n",
format("#error Leave all white!!!!! space* alone!\n"));
verifyNoChange("#error Leave all white!!!!! space* alone!\n");
EXPECT_EQ(
"#warning Leave all white!!!!! space* alone!\n",
format("#warning Leave all white!!!!! space* alone!\n"));
Expand Down Expand Up @@ -14656,8 +14645,7 @@ TEST_F(FormatTest, FormatForObjectiveCMethodDecls) {
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;"));
verifyFormat("- (id)Method6:(id)A:(id)B:(id)C:(id)D;");
EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject "
"forAllCells:(BOOL)flag;",
format("- (void)sendAction:(SEL)aSelector to:(id)anObject "
Expand Down Expand Up @@ -14732,10 +14720,8 @@ TEST_F(FormatTest, BreaksStringLiterals) {
" \"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)));
verifyFormat("\"some text\"", getLLVMStyleWithColumns(1));
verifyFormat("\"some text\"", getLLVMStyleWithColumns(11));
EXPECT_EQ("\"some \"\n"
"\"text\"",
format("\"some text\"", getLLVMStyleWithColumns(10)));
Expand Down Expand Up @@ -14861,7 +14847,7 @@ TEST_F(FormatTest, BreaksStringLiterals) {

FormatStyle Style = getLLVMStyleWithColumns(12);
Style.BreakStringLiterals = false;
EXPECT_EQ("\"some text other\";", format("\"some text other\";", Style));
verifyFormat("\"some text other\";", Style);

FormatStyle AlignLeft = getLLVMStyleWithColumns(12);
AlignLeft.AlignEscapedNewlines = FormatStyle::ENAS_Left;
Expand Down Expand Up @@ -14925,12 +14911,11 @@ TEST_F(FormatTest, BreaksWideAndNSStringLiterals) {

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\";", Style);
verifyFormat("uR\"x(raw literal)x\";", Style);
verifyFormat("LR\"x(raw literal)x\";", Style);
verifyFormat("UR\"x(raw literal)x\";", Style);
verifyFormat("u8R\"x(raw literal)x\";", Style);
}

TEST_F(FormatTest, BreaksStringLiteralsWithin_TMacro) {
Expand Down Expand Up @@ -15210,8 +15195,8 @@ TEST_F(FormatTest, BreakStringLiteralsBeforeUnbreakableTokenSequence) {
}

TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
EXPECT_EQ("\"\\a\"", format("\"\\a\"", getLLVMStyleWithColumns(3)));
EXPECT_EQ("\"\\\"", format("\"\\\"", getLLVMStyleWithColumns(2)));
verifyFormat("\"\\a\"", getLLVMStyleWithColumns(3));
verifyFormat("\"\\\"", getLLVMStyleWithColumns(2));
EXPECT_EQ("\"test\"\n"
"\"\\n\"",
format("\"test\\n\"", getLLVMStyleWithColumns(7)));
Expand All @@ -15221,19 +15206,16 @@ TEST_F(FormatTest, DoNotBreakStringLiteralsInEscapeSequence) {
EXPECT_EQ("\"\\\\\\\\\"\n"
"\"\\n\"",
format("\"\\\\\\\\\\n\"", getLLVMStyleWithColumns(7)));
EXPECT_EQ("\"\\uff01\"", format("\"\\uff01\"", getLLVMStyleWithColumns(7)));
verifyFormat("\"\\uff01\"", getLLVMStyleWithColumns(7));
EXPECT_EQ("\"\\uff01\"\n"
"\"test\"",
format("\"\\uff01test\"", getLLVMStyleWithColumns(8)));
EXPECT_EQ("\"\\Uff01ff02\"",
format("\"\\Uff01ff02\"", getLLVMStyleWithColumns(11)));
verifyFormat("\"\\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)));
verifyFormat("\"\\x000000000001next\"", getLLVMStyleWithColumns(15));
verifyFormat("\"\\x000000000001\"", getLLVMStyleWithColumns(7));
EXPECT_EQ("\"test\"\n"
"\"\\000000\"\n"
"\"000001\"",
Expand Down Expand Up @@ -21054,11 +21036,9 @@ TEST_F(FormatTest, WorksFor8bitEncodings) {
}

TEST_F(FormatTest, HandlesUTF8BOM) {
EXPECT_EQ("\xef\xbb\xbf", format("\xef\xbb\xbf"));
EXPECT_EQ("\xef\xbb\xbf#include <iostream>",
format("\xef\xbb\xbf#include <iostream>"));
EXPECT_EQ("\xef\xbb\xbf\n#include <iostream>",
format("\xef\xbb\xbf\n#include <iostream>"));
verifyFormat("\xef\xbb\xbf");
verifyFormat("\xef\xbb\xbf#include <iostream>");
verifyFormat("\xef\xbb\xbf\n#include <iostream>");
}

// FIXME: Encode Cyrillic and CJK characters below to appease MS compilers.
Expand Down Expand Up @@ -21127,8 +21107,7 @@ TEST_F(FormatTest, HandlesDoubleWidthCharsInMultiLineStrings) {
}

TEST_F(FormatTest, SplitsUTF8LineComments) {
EXPECT_EQ("// aaaaÄ\xc2\x8d",
format("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10)));
verifyFormat("// aaaaÄ\xc2\x8d", getLLVMStyleWithColumns(10));
EXPECT_EQ("// Я из лесу\n"
"// вышел; был\n"
"// сильный\n"
Expand Down Expand Up @@ -21985,12 +21964,9 @@ TEST_F(FormatTest, FormatsLambdas) {
" return b;\n"
"};",
"auto c = []() { return b; };", MergeInline);
verifyFormat("function([]() { return b; })", "function([]() { return b; })",
MergeInline);
verifyFormat("function([]() { return b; }, a)",
"function([]() { return b; }, a)", MergeInline);
verifyFormat("function(a, []() { return b; })",
"function(a, []() { return b; })", MergeInline);
verifyFormat("function([]() { return b; })", MergeInline);
verifyFormat("function([]() { return b; }, a)", MergeInline);
verifyFormat("function(a, []() { return b; })", MergeInline);

// Check option "BraceWrapping.BeforeLambdaBody" and different state of
// AllowShortLambdasOnASingleLine
Expand Down Expand Up @@ -22925,7 +22901,7 @@ TEST_F(FormatTest, HandleUnbalancedImplicitBracesAcrossPPBranches) {
"#if C\n"
"#else\n"
"#endif\n";
EXPECT_EQ(code, format(code));
verifyFormat(code);
}

TEST_F(FormatTest, HandleConflictMarkers) {
Expand Down Expand Up @@ -23143,12 +23119,12 @@ TEST_F(FormatTest, UTF8CharacterLiteralCpp03) {
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';");
}

TEST_F(FormatTest, DoNotFormatLikelyXml) {
EXPECT_EQ("<!-- ;> -->", format("<!-- ;> -->", getGoogleStyle()));
EXPECT_EQ(" <!-- >; -->", format(" <!-- >; -->", getGoogleStyle()));
verifyFormat("<!-- ;> -->", getGoogleStyle());
verifyNoChange(" <!-- >; -->", getGoogleStyle());
}

TEST_F(FormatTest, StructuredBindings) {
Expand Down Expand Up @@ -23771,13 +23747,11 @@ TEST_F(FormatTest, WhitespaceSensitiveMacros) {

// Don't use the helpers here, since 'mess up' will change the whitespace
// and these are all whitespace sensitive by definition
EXPECT_EQ("FOO(String-ized&Messy+But(: :Still)=Intentional);",
format("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style));
verifyFormat("FOO(String-ized&Messy+But(: :Still)=Intentional);", Style);
EXPECT_EQ(
"FOO(String-ized&Messy+But\\(: :Still)=Intentional);",
format("FOO(String-ized&Messy+But\\(: :Still)=Intentional);", Style));
EXPECT_EQ("FOO(String-ized&Messy+But,: :Still=Intentional);",
format("FOO(String-ized&Messy+But,: :Still=Intentional);", Style));
verifyFormat("FOO(String-ized&Messy+But,: :Still=Intentional);", Style);
EXPECT_EQ("FOO(String-ized&Messy+But,: :\n"
" Still=Intentional);",
format("FOO(String-ized&Messy+But,: :\n"
Expand All @@ -23791,8 +23765,7 @@ TEST_F(FormatTest, WhitespaceSensitiveMacros) {
Style));

Style.ColumnLimit = 21;
EXPECT_EQ("FOO(String-ized&Messy+But: :Still=Intentional);",
format("FOO(String-ized&Messy+But: :Still=Intentional);", Style));
verifyFormat("FOO(String-ized&Messy+But: :Still=Intentional);", Style);
}

TEST_F(FormatTest, VeryLongNamespaceCommentSplit) {
Expand Down Expand Up @@ -24914,7 +24887,7 @@ TEST_F(FormatTest, StatementAttributeLikeMacros) {
" Q_EMIT signal(MyChar);\n"
"}";

EXPECT_EQ(Source, format(Source, Style));
verifyFormat(Source, Style);

Style.AlignConsecutiveDeclarations.Enabled = true;
EXPECT_EQ("void Foo::slot() {\n"
Expand All @@ -24925,7 +24898,7 @@ TEST_F(FormatTest, StatementAttributeLikeMacros) {
format(Source, Style));

Style.StatementAttributeLikeMacros.push_back("emit");
EXPECT_EQ(Source, format(Source, Style));
verifyFormat(Source, Style);

Style.StatementAttributeLikeMacros = {};
EXPECT_EQ("void Foo::slot() {\n"
Expand Down Expand Up @@ -25020,7 +24993,7 @@ TEST_F(FormatTest, LimitlessStringsAndComments) {
" // Small line comment\n"
" return String.size() > SmallString.size();\n"
"}";
EXPECT_EQ(Code, format(Code, Style));
verifyNoChange(Code, Style);
}

TEST_F(FormatTest, FormatDecayCopy) {
Expand Down Expand Up @@ -25717,7 +25690,7 @@ TEST_F(FormatTest, BreakAfterAttributes) {
Code, Style);

Style.BreakAfterAttributes = FormatStyle::ABS_Leave;
EXPECT_EQ(Code, format(Code, Style));
verifyNoChange(Code, Style);
}

TEST_F(FormatTest, InsertNewlineAtEOF) {
Expand All @@ -25733,7 +25706,7 @@ TEST_F(FormatTest, KeepEmptyLinesAtEOF) {
Style.KeepEmptyLinesAtEOF = true;

const StringRef Code{"int i;\n\n"};
verifyFormat(Code, Code, Style);
verifyNoChange(Code, Style);
verifyFormat(Code, "int i;\n\n\n", Style);
}

Expand Down

0 comments on commit 20b4df1

Please sign in to comment.