forked from qt-creator/qt-creator
ClangFormat: Add unit-tests
Test reported cases from bugreports. Change-Id: I9aeb42dc476cbfe98abb837f2e941d8e2685235a Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
@@ -56,7 +56,7 @@ class ClangFormat : public ::testing::Test
|
|||||||
protected:
|
protected:
|
||||||
void SetUp() final
|
void SetUp() final
|
||||||
{
|
{
|
||||||
indenter.setFileName(Utils::FileName::fromString(TESTDATA_DIR"/test.cpp"));
|
indenter.setFileName(Utils::FileName::fromString(TESTDATA_DIR "/clangformat/test.cpp"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void insertLines(const std::vector<QString> &lines)
|
void insertLines(const std::vector<QString> &lines)
|
||||||
@@ -88,7 +88,7 @@ protected:
|
|||||||
QTextCursor cursor{&doc};
|
QTextCursor cursor{&doc};
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(ClangFormat, SimpleIndent)
|
TEST_F(ClangFormat, IndentBasicFile)
|
||||||
{
|
{
|
||||||
insertLines({"int main()",
|
insertLines({"int main()",
|
||||||
"{",
|
"{",
|
||||||
@@ -103,4 +103,330 @@ TEST_F(ClangFormat, SimpleIndent)
|
|||||||
"}"));
|
"}"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentEmptyLine)
|
||||||
|
{
|
||||||
|
insertLines({"int main",
|
||||||
|
"{",
|
||||||
|
"",
|
||||||
|
"}"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("int main",
|
||||||
|
"{",
|
||||||
|
" ",
|
||||||
|
"}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentLambda)
|
||||||
|
{
|
||||||
|
insertLines({"int b = foo([](){",
|
||||||
|
"",
|
||||||
|
"});"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("int b = foo([](){",
|
||||||
|
" ",
|
||||||
|
"});"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentNestedIfElse)
|
||||||
|
{
|
||||||
|
insertLines({"if (a)",
|
||||||
|
"if (b)",
|
||||||
|
"foo();",
|
||||||
|
"else",
|
||||||
|
"bar();",
|
||||||
|
"else",
|
||||||
|
"baz();"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(),
|
||||||
|
ElementsAre("if (a)",
|
||||||
|
" if (b)",
|
||||||
|
" foo();",
|
||||||
|
" else",
|
||||||
|
" bar();",
|
||||||
|
"else",
|
||||||
|
" baz();"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentInitializerListInArguments)
|
||||||
|
{
|
||||||
|
insertLines({"foo(arg1,",
|
||||||
|
"args,",
|
||||||
|
"{1, 2});"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("foo(arg1,",
|
||||||
|
" args,",
|
||||||
|
" {1, 2});"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentLambdaWithReturnType)
|
||||||
|
{
|
||||||
|
insertLines({"{",
|
||||||
|
"auto lambda = []() -> bool {",
|
||||||
|
"",
|
||||||
|
"};",
|
||||||
|
"}"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(),
|
||||||
|
ElementsAre("{",
|
||||||
|
" auto lambda = []() -> bool {",
|
||||||
|
" ",
|
||||||
|
" };",
|
||||||
|
"}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentFunctionArgumentLambdaWithNextLineScope)
|
||||||
|
{
|
||||||
|
insertLines({"foo([]()",
|
||||||
|
"{",
|
||||||
|
"",
|
||||||
|
"});"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(),
|
||||||
|
ElementsAre("foo([]()",
|
||||||
|
" {",
|
||||||
|
" ",
|
||||||
|
" });"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentScopeAsFunctionArgument)
|
||||||
|
{
|
||||||
|
insertLines({"foo(",
|
||||||
|
"{",
|
||||||
|
"",
|
||||||
|
"});"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(),
|
||||||
|
ElementsAre("foo(",
|
||||||
|
" {",
|
||||||
|
" ",
|
||||||
|
" });"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentInsideStructuredBinding)
|
||||||
|
{
|
||||||
|
insertLines({"auto [a,",
|
||||||
|
"b] = c;"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("auto [a,",
|
||||||
|
" b] = c;"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentMacrosWithoutSemicolon)
|
||||||
|
{
|
||||||
|
insertLines({"void test()",
|
||||||
|
"{",
|
||||||
|
"ASSERT(1);",
|
||||||
|
"ASSERT(2)",
|
||||||
|
"ASSERT(3)",
|
||||||
|
"ASSERT(4);",
|
||||||
|
"ASSERT(5)",
|
||||||
|
"}"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("void test()",
|
||||||
|
"{",
|
||||||
|
" ASSERT(1);",
|
||||||
|
" ASSERT(2)",
|
||||||
|
" ASSERT(3)",
|
||||||
|
" ASSERT(4);",
|
||||||
|
" ASSERT(5)",
|
||||||
|
"}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentAfterSquareBracesInsideBraceInitialization)
|
||||||
|
{
|
||||||
|
insertLines({"int foo() {",
|
||||||
|
"char a = char{b[0]};",
|
||||||
|
"int c;",
|
||||||
|
"}"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("int foo() {",
|
||||||
|
" char a = char{b[0]};",
|
||||||
|
" int c;",
|
||||||
|
"}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentStringLiteralContinuation)
|
||||||
|
{
|
||||||
|
insertLines({"foo(bar, \"foo\"",
|
||||||
|
"\"bar\");"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("foo(bar, \"foo\"",
|
||||||
|
" \"bar\");"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, IndentTemplateparameters)
|
||||||
|
{
|
||||||
|
insertLines({"using Alias = Template<A,",
|
||||||
|
"B,",
|
||||||
|
"C>"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("using Alias = Template<A,",
|
||||||
|
" B,",
|
||||||
|
" C>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, NoExtraIndentAfterStatementInsideSquareBraces)
|
||||||
|
{
|
||||||
|
insertLines({"{",
|
||||||
|
" x[y=z];",
|
||||||
|
" int a;",
|
||||||
|
"}"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("{",
|
||||||
|
" x[y=z];",
|
||||||
|
" int a;",
|
||||||
|
"}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, NoExtraIndentAfterBraceInitialization)
|
||||||
|
{
|
||||||
|
insertLines({"int j{i?5:10};",
|
||||||
|
"return 0;"});
|
||||||
|
|
||||||
|
indenter.indent(cursor, QChar::Null, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("int j{i?5:10};",
|
||||||
|
"return 0;"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, FormatBasicFile)
|
||||||
|
{
|
||||||
|
insertLines({"int main()",
|
||||||
|
"{",
|
||||||
|
"int a;",
|
||||||
|
"}"});
|
||||||
|
|
||||||
|
indenter.format(cursor, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("int main()",
|
||||||
|
"{",
|
||||||
|
" int a;",
|
||||||
|
"}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, FormatEmptyLine)
|
||||||
|
{
|
||||||
|
insertLines({"int main()",
|
||||||
|
"{",
|
||||||
|
"",
|
||||||
|
"}"});
|
||||||
|
|
||||||
|
indenter.format(cursor, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("int main() {}"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, FormatLambda)
|
||||||
|
{
|
||||||
|
insertLines({"int b = foo([](){",
|
||||||
|
"",
|
||||||
|
"});"});
|
||||||
|
|
||||||
|
indenter.format(cursor, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("int b = foo([]() {",
|
||||||
|
"",
|
||||||
|
"});"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, FormatInitializerListInArguments)
|
||||||
|
{
|
||||||
|
insertLines({"foo(arg1,",
|
||||||
|
"args,",
|
||||||
|
"{1, 2});"});
|
||||||
|
|
||||||
|
indenter.format(cursor, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("foo(arg1, args, {1, 2});"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, FormatFunctionArgumentLambdaWithScope)
|
||||||
|
{
|
||||||
|
insertLines({"foo([]()",
|
||||||
|
"{",
|
||||||
|
"",
|
||||||
|
"});"});
|
||||||
|
|
||||||
|
indenter.format(cursor, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(),
|
||||||
|
ElementsAre("foo([]() {",
|
||||||
|
"",
|
||||||
|
"});"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, FormatScopeAsFunctionArgument)
|
||||||
|
{
|
||||||
|
insertLines({"foo(",
|
||||||
|
"{",
|
||||||
|
"",
|
||||||
|
"});"});
|
||||||
|
|
||||||
|
indenter.format(cursor, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(),
|
||||||
|
ElementsAre("foo({",
|
||||||
|
"",
|
||||||
|
"});"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, FormatStructuredBinding)
|
||||||
|
{
|
||||||
|
insertLines({"auto [a,",
|
||||||
|
"b] = c;"});
|
||||||
|
|
||||||
|
indenter.format(cursor, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("auto [a, b] = c;"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, FormatStringLiteralContinuation)
|
||||||
|
{
|
||||||
|
insertLines({"foo(bar, \"foo\"",
|
||||||
|
"\"bar\");"});
|
||||||
|
|
||||||
|
indenter.format(cursor, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("foo(bar,",
|
||||||
|
" \"foo\"",
|
||||||
|
" \"bar\");"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ClangFormat, FormatTemplateparameters)
|
||||||
|
{
|
||||||
|
insertLines({"using Alias = Template<A,",
|
||||||
|
"B,",
|
||||||
|
"C>"});
|
||||||
|
|
||||||
|
indenter.format(cursor, TextEditor::TabSettings());
|
||||||
|
|
||||||
|
ASSERT_THAT(documentLines(), ElementsAre("using Alias = Template<A, B, C>"));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,102 +0,0 @@
|
|||||||
---
|
|
||||||
Language: Cpp
|
|
||||||
AccessModifierOffset: -4
|
|
||||||
AlignAfterOpenBracket: Align
|
|
||||||
AlignConsecutiveAssignments: false
|
|
||||||
AlignConsecutiveDeclarations: false
|
|
||||||
AlignEscapedNewlines: DontAlign
|
|
||||||
AlignOperands: true
|
|
||||||
AlignTrailingComments: true
|
|
||||||
AllowAllParametersOfDeclarationOnNextLine: true
|
|
||||||
AllowShortBlocksOnASingleLine: false
|
|
||||||
AllowShortCaseLabelsOnASingleLine: false
|
|
||||||
AllowShortFunctionsOnASingleLine: Inline
|
|
||||||
AllowShortIfStatementsOnASingleLine: false
|
|
||||||
AllowShortLoopsOnASingleLine: false
|
|
||||||
AlwaysBreakAfterReturnType: None
|
|
||||||
AlwaysBreakBeforeMultilineStrings: false
|
|
||||||
AlwaysBreakTemplateDeclarations: true
|
|
||||||
BinPackArguments: false
|
|
||||||
BinPackParameters: false
|
|
||||||
BraceWrapping:
|
|
||||||
AfterClass: true
|
|
||||||
AfterControlStatement: false
|
|
||||||
AfterEnum: false
|
|
||||||
AfterFunction: true
|
|
||||||
AfterNamespace: false
|
|
||||||
AfterObjCDeclaration: false
|
|
||||||
AfterStruct: true
|
|
||||||
AfterUnion: false
|
|
||||||
BeforeCatch: false
|
|
||||||
BeforeElse: false
|
|
||||||
IndentBraces: false
|
|
||||||
SplitEmptyFunction: false
|
|
||||||
SplitEmptyRecord: false
|
|
||||||
SplitEmptyNamespace: false
|
|
||||||
BreakBeforeBinaryOperators: All
|
|
||||||
BreakBeforeBraces: Custom
|
|
||||||
BreakBeforeInheritanceComma: false
|
|
||||||
BreakBeforeTernaryOperators: true
|
|
||||||
BreakConstructorInitializersBeforeComma: false
|
|
||||||
BreakConstructorInitializers: BeforeComma
|
|
||||||
BreakAfterJavaFieldAnnotations: false
|
|
||||||
BreakStringLiterals: true
|
|
||||||
ColumnLimit: 100
|
|
||||||
CommentPragmas: '^ IWYU pragma:'
|
|
||||||
CompactNamespaces: false
|
|
||||||
ConstructorInitializerAllOnOneLineOrOnePerLine: false
|
|
||||||
ConstructorInitializerIndentWidth: 4
|
|
||||||
ContinuationIndentWidth: 4
|
|
||||||
Cpp11BracedListStyle: true
|
|
||||||
DerivePointerAlignment: false
|
|
||||||
DisableFormat: false
|
|
||||||
ExperimentalAutoDetectBinPacking: false
|
|
||||||
FixNamespaceComments: true
|
|
||||||
ForEachMacros:
|
|
||||||
- forever # avoids { wrapped to next line
|
|
||||||
- foreach
|
|
||||||
- Q_FOREACH
|
|
||||||
- BOOST_FOREACH
|
|
||||||
IncludeCategories:
|
|
||||||
- Regex: '^<Q.*'
|
|
||||||
Priority: 200
|
|
||||||
IncludeIsMainRegex: '(Test)?$'
|
|
||||||
IndentCaseLabels: false
|
|
||||||
IndentWidth: 4
|
|
||||||
IndentWrappedFunctionNames: false
|
|
||||||
JavaScriptQuotes: Leave
|
|
||||||
JavaScriptWrapImports: true
|
|
||||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
|
||||||
# Do not add QT_BEGIN_NAMESPACE/QT_END_NAMESPACE as this will indent lines in between.
|
|
||||||
MacroBlockBegin: ""
|
|
||||||
MacroBlockEnd: ""
|
|
||||||
MaxEmptyLinesToKeep: 1
|
|
||||||
NamespaceIndentation: None
|
|
||||||
ObjCBlockIndentWidth: 4
|
|
||||||
ObjCSpaceAfterProperty: false
|
|
||||||
ObjCSpaceBeforeProtocolList: true
|
|
||||||
PenaltyBreakAssignment: 150
|
|
||||||
PenaltyBreakBeforeFirstCallParameter: 300
|
|
||||||
PenaltyBreakComment: 500
|
|
||||||
PenaltyBreakFirstLessLess: 400
|
|
||||||
PenaltyBreakString: 600
|
|
||||||
PenaltyExcessCharacter: 50
|
|
||||||
PenaltyReturnTypeOnItsOwnLine: 300
|
|
||||||
PointerAlignment: Right
|
|
||||||
ReflowComments: false
|
|
||||||
SortIncludes: true
|
|
||||||
SortUsingDeclarations: true
|
|
||||||
SpaceAfterCStyleCast: true
|
|
||||||
SpaceAfterTemplateKeyword: false
|
|
||||||
SpaceBeforeAssignmentOperators: true
|
|
||||||
SpaceBeforeParens: ControlStatements
|
|
||||||
SpaceInEmptyParentheses: false
|
|
||||||
SpacesBeforeTrailingComments: 1
|
|
||||||
SpacesInAngles: false
|
|
||||||
SpacesInContainerLiterals: false
|
|
||||||
SpacesInCStyleCastParentheses: false
|
|
||||||
SpacesInParentheses: false
|
|
||||||
SpacesInSquareBrackets: false
|
|
||||||
Standard: Cpp11
|
|
||||||
TabWidth: 4
|
|
||||||
UseTab: Never
|
|
Reference in New Issue
Block a user