summaryrefslogtreecommitdiffstats
path: root/unittests
diff options
context:
space:
mode:
Diffstat (limited to 'unittests')
-rw-r--r--unittests/ASTMatchers/ASTMatchersTest.h1
-rw-r--r--unittests/Format/CMakeLists.txt1
-rw-r--r--unittests/Format/FormatTest.cpp526
-rw-r--r--unittests/Format/FormatTestJS.cpp90
-rw-r--r--unittests/Format/FormatTestJava.cpp6
-rw-r--r--unittests/Format/FormatTestSelective.cpp441
-rw-r--r--unittests/Tooling/RewriterTest.cpp13
7 files changed, 621 insertions, 457 deletions
diff --git a/unittests/ASTMatchers/ASTMatchersTest.h b/unittests/ASTMatchers/ASTMatchersTest.h
index e555e63..17b69da 100644
--- a/unittests/ASTMatchers/ASTMatchersTest.h
+++ b/unittests/ASTMatchers/ASTMatchersTest.h
@@ -80,6 +80,7 @@ testing::AssertionResult matchesConditionally(
Args.push_back("-frtti");
Args.push_back("-fexceptions");
if (!runToolOnCodeWithArgs(Factory->create(), Code, Args, Filename,
+ std::make_shared<RawPCHContainerOperations>(),
VirtualMappedFiles)) {
return testing::AssertionFailure() << "Parsing error in \"" << Code << "\"";
}
diff --git a/unittests/Format/CMakeLists.txt b/unittests/Format/CMakeLists.txt
index 4a7ab79..6d48cf8 100644
--- a/unittests/Format/CMakeLists.txt
+++ b/unittests/Format/CMakeLists.txt
@@ -7,6 +7,7 @@ add_clang_unittest(FormatTests
FormatTestJava.cpp
FormatTestJS.cpp
FormatTestProto.cpp
+ FormatTestSelective.cpp
)
target_link_libraries(FormatTests
diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp
index c4aa712..ed2658b 100644
--- a/unittests/Format/FormatTest.cpp
+++ b/unittests/Format/FormatTest.cpp
@@ -16,6 +16,7 @@
namespace clang {
namespace format {
+namespace {
FormatStyle getGoogleStyle() { return getGoogleStyle(FormatStyle::LK_Cpp); }
@@ -27,12 +28,12 @@ protected:
IC_DoNotCheck
};
- std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length,
- const FormatStyle &Style,
+ std::string format(llvm::StringRef Code,
+ const FormatStyle &Style = getLLVMStyle(),
IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
DEBUG(llvm::errs() << "---\n");
DEBUG(llvm::errs() << Code << "\n\n");
- std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
+ std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
bool IncompleteFormat = false;
tooling::Replacements Replaces =
reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
@@ -47,12 +48,6 @@ protected:
return Result;
}
- std::string format(llvm::StringRef Code,
- const FormatStyle &Style = getLLVMStyle(),
- IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
- return format(Code, 0, Code.size(), Style, CheckIncomplete);
- }
-
FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
FormatStyle Style = getLLVMStyle();
Style.ColumnLimit = ColumnLimit;
@@ -151,54 +146,13 @@ TEST_F(FormatTest, OnlyGeneratesNecessaryReplacements) {
" f();\n"
"}"));
EXPECT_EQ(0, ReplacementCount);
-}
-
-TEST_F(FormatTest, RemovesTrailingWhitespaceOfFormattedLine) {
- EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0, getLLVMStyle()));
- EXPECT_EQ("int a;", format("int a; "));
- EXPECT_EQ("int a;\n", format("int a; \n \n \n "));
- EXPECT_EQ("int a;\nint b; ",
- format("int a; \nint b; ", 0, 0, getLLVMStyle()));
-}
-
-TEST_F(FormatTest, FormatsCorrectRegionForLeadingWhitespace) {
- EXPECT_EQ("int b;\nint a;",
- format("int b;\n int a;", 7, 0, getLLVMStyle()));
- EXPECT_EQ("int b;\n int a;",
- format("int b;\n int a;", 6, 0, getLLVMStyle()));
-
- EXPECT_EQ("#define A \\\n"
- " int a; \\\n"
- " int b;",
- format("#define A \\\n"
- " int a; \\\n"
- " int b;",
- 26, 0, getLLVMStyleWithColumns(12)));
- EXPECT_EQ("#define A \\\n"
- " int a; \\\n"
- " int b;",
- format("#define A \\\n"
- " int a; \\\n"
- " int b;",
- 25, 0, getLLVMStyleWithColumns(12)));
-}
-
-TEST_F(FormatTest, FormatLineWhenInvokedOnTrailingNewline) {
- EXPECT_EQ("int b;\n\nint a;",
- format("int b;\n\nint a;", 8, 0, getLLVMStyle()));
- EXPECT_EQ("int b;\n\nint a;",
- format("int b;\n\nint a;", 7, 0, getLLVMStyle()));
-
- // This might not strictly be correct, but is likely good in all practical
- // cases.
- EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0, getLLVMStyle()));
-}
-
-TEST_F(FormatTest, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
- EXPECT_EQ("int a;\n\n int b;",
- format("int a;\n \n\n int b;", 8, 0, getLLVMStyle()));
- EXPECT_EQ("int a;\n\n int b;",
- format("int a;\n \n\n int b;", 9, 0, getLLVMStyle()));
+ EXPECT_EQ("/*\r\n"
+ "\r\n"
+ "*/\r\n",
+ format("/*\r\n"
+ "\r\n"
+ "*/\r\n"));
+ EXPECT_EQ(0, ReplacementCount);
}
TEST_F(FormatTest, RemovesEmptyLines) {
@@ -316,19 +270,6 @@ TEST_F(FormatTest, RemovesEmptyLines) {
"} // namespace"));
}
-TEST_F(FormatTest, ReformatsMovedLines) {
- EXPECT_EQ(
- "template <typename T> T *getFETokenInfo() const {\n"
- " return static_cast<T *>(FETokenInfo);\n"
- "}\n"
- " int a; // <- Should not be formatted",
- format(
- "template<typename T>\n"
- "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
- " int a; // <- Should not be formatted",
- 9, 5, getLLVMStyle()));
-}
-
TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {
verifyFormat("x = (a) and (b);");
verifyFormat("x = (a) or (b);");
@@ -377,10 +318,6 @@ TEST_F(FormatTest, FormatIfWithoutCompoundStatement) {
"}",
AllowsMergedIf);
- EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1, AllowsMergedIf));
- EXPECT_EQ("if (a) return; // comment",
- format("if(a)\nreturn; // comment", 20, 1, AllowsMergedIf));
-
AllowsMergedIf.ColumnLimit = 14;
verifyFormat("if (a) return;", AllowsMergedIf);
verifyFormat("if (aaaaaaaaa)\n"
@@ -616,6 +553,18 @@ TEST_F(FormatTest, ForEachLoops) {
" BOOST_FOREACH (Item *item, itemlist) {}\n"
" UNKNOWN_FORACH(Item * item, itemlist) {}\n"
"}");
+
+ // As function-like macros.
+ verifyFormat("#define foreach(x, y)\n"
+ "#define Q_FOREACH(x, y)\n"
+ "#define BOOST_FOREACH(x, y)\n"
+ "#define UNKNOWN_FOREACH(x, y)\n");
+
+ // Not as function-like macros.
+ verifyFormat("#define foreach (x, y)\n"
+ "#define Q_FOREACH (x, y)\n"
+ "#define BOOST_FOREACH (x, y)\n"
+ "#define UNKNOWN_FOREACH (x, y)\n");
}
TEST_F(FormatTest, FormatsWhileLoop) {
@@ -1121,65 +1070,6 @@ TEST_F(FormatTest, KeepsParameterWithTrailingCommentsOnTheirOwnLine) {
" c); // comment"));
}
-TEST_F(FormatTest, CanFormatCommentsLocally) {
- EXPECT_EQ("int a; // comment\n"
- "int b; // comment",
- format("int a; // comment\n"
- "int b; // comment",
- 0, 0, getLLVMStyle()));
- EXPECT_EQ("int a; // comment\n"
- " // line 2\n"
- "int b;",
- format("int a; // comment\n"
- " // line 2\n"
- "int b;",
- 28, 0, getLLVMStyle()));
- EXPECT_EQ("int aaaaaa; // comment\n"
- "int b;\n"
- "int c; // unrelated comment",
- format("int aaaaaa; // comment\n"
- "int b;\n"
- "int c; // unrelated comment",
- 31, 0, getLLVMStyle()));
-
- EXPECT_EQ("int a; // This\n"
- " // is\n"
- " // a",
- format("int a; // This\n"
- " // is\n"
- " // a",
- 0, 0, getLLVMStyle()));
- EXPECT_EQ("int a; // This\n"
- " // is\n"
- " // a\n"
- "// This is b\n"
- "int b;",
- format("int a; // This\n"
- " // is\n"
- " // a\n"
- "// This is b\n"
- "int b;",
- 0, 0, getLLVMStyle()));
- EXPECT_EQ("int a; // This\n"
- " // is\n"
- " // a\n"
- "\n"
- " // This is unrelated",
- format("int a; // This\n"
- " // is\n"
- " // a\n"
- "\n"
- " // This is unrelated",
- 0, 0, getLLVMStyle()));
- EXPECT_EQ("int a;\n"
- "// This is\n"
- "// not formatted. ",
- format("int a;\n"
- "// This is\n"
- "// not formatted. ",
- 0, 0, getLLVMStyle()));
-}
-
TEST_F(FormatTest, RemovesTrailingWhitespaceOfComments) {
EXPECT_EQ("// comment", format("// comment "));
EXPECT_EQ("int aaaaaaa, bbbbbbb; // comment",
@@ -2106,7 +1996,6 @@ TEST_F(FormatTest, FormatsEnum) {
verifyFormat("enum X E {} d;");
verifyFormat("enum __attribute__((...)) E {} d;");
verifyFormat("enum __declspec__((...)) E {} d;");
- verifyFormat("enum X f() {\n a();\n return 42;\n}");
verifyFormat("enum {\n"
" Bar = Foo<int, int>::value\n"
"};",
@@ -2135,6 +2024,23 @@ TEST_F(FormatTest, FormatsEnum) {
" TWO\n"
"};\n"
"int i;");
+ // Not enums.
+ verifyFormat("enum X f() {\n"
+ " a();\n"
+ " return 42;\n"
+ "}");
+ verifyFormat("enum X Type::f() {\n"
+ " a();\n"
+ " return 42;\n"
+ "}");
+ verifyFormat("enum ::X f() {\n"
+ " a();\n"
+ " return 42;\n"
+ "}");
+ verifyFormat("enum ns::X f() {\n"
+ " a();\n"
+ " return 42;\n"
+ "}");
}
TEST_F(FormatTest, FormatsEnumsWithErrors) {
@@ -2699,31 +2605,6 @@ TEST_F(FormatTest, LayoutCodeInMacroDefinitions) {
TEST_F(FormatTest, LayoutRemainingTokens) { EXPECT_EQ("{}", format("{}")); }
-TEST_F(FormatTest, AlwaysFormatsEntireMacroDefinitions) {
- EXPECT_EQ("int i;\n"
- "#define A \\\n"
- " int i; \\\n"
- " int j\n"
- "int k;",
- format("int i;\n"
- "#define A \\\n"
- " int i ; \\\n"
- " int j\n"
- "int k;",
- 8, 0, getGoogleStyle())); // 8: position of "#define".
- EXPECT_EQ("int i;\n"
- "#define A \\\n"
- " int i; \\\n"
- " int j\n"
- "int k;",
- format("int i;\n"
- "#define A \\\n"
- " int i ; \\\n"
- " int j\n"
- "int k;",
- 45, 0, getGoogleStyle())); // 45: position of "j".
-}
-
TEST_F(FormatTest, MacroDefinitionInsideStatement) {
EXPECT_EQ("int x,\n"
"#define A\n"
@@ -2799,7 +2680,8 @@ TEST_F(FormatTest, MacroDefinitionsWithIncompleteCode) {
"#define b \\\n"
" } \\\n"
" a\n"
- "a", getLLVMStyleWithColumns(15));
+ "a",
+ getLLVMStyleWithColumns(15));
verifyFormat("#define A \\\n"
" { \\\n"
" {\n"
@@ -3027,8 +2909,7 @@ TEST_F(FormatTest, EscapedNewlines) {
EXPECT_EQ(
"#define A \\\n int i; \\\n int j;",
format("#define A \\\nint i;\\\n int j;", getLLVMStyleWithColumns(11)));
- EXPECT_EQ(
- "#define A\n\nint i;", format("#define A \\\n\n int i;"));
+ 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>"));
@@ -3205,30 +3086,6 @@ TEST_F(FormatTest, LayoutBlockInsideParens) {
" if (a)\n"
" f();\n"
"});");
- EXPECT_EQ("int longlongname; // comment\n"
- "int x = f({\n"
- " int x; // comment\n"
- " int y; // comment\n"
- "});",
- format("int longlongname; // comment\n"
- "int x = f({\n"
- " int x; // comment\n"
- " int y; // comment\n"
- "});",
- 65, 0, getLLVMStyle()));
- EXPECT_EQ("int s = f({\n"
- " class X {\n"
- " public:\n"
- " void f();\n"
- " };\n"
- "});",
- format("int s = f({\n"
- " class X {\n"
- " public:\n"
- " void f();\n"
- " };\n"
- "});",
- 0, 0, getLLVMStyle()));
}
TEST_F(FormatTest, LayoutBlockInsideStatement) {
@@ -3330,90 +3187,6 @@ TEST_F(FormatTest, FormatNestedBlocksInMacros) {
getGoogleStyle()));
}
-TEST_F(FormatTest, IndividualStatementsOfNestedBlocks) {
- EXPECT_EQ("DEBUG({\n"
- " int i;\n"
- " int j;\n"
- "});",
- format("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- 20, 1, getLLVMStyle()));
- EXPECT_EQ("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- format("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- 41, 1, getLLVMStyle()));
- EXPECT_EQ("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- format("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- 41, 1, getLLVMStyle()));
- EXPECT_EQ("DEBUG({\n"
- " int i;\n"
- " int j;\n"
- "});",
- format("DEBUG( {\n"
- " int i;\n"
- " int j;\n"
- "} ) ;",
- 20, 1, getLLVMStyle()));
-
- EXPECT_EQ("Debug({\n"
- " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
- " return;\n"
- " },\n"
- " a);",
- format("Debug({\n"
- " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
- " return;\n"
- " },\n"
- " a);",
- 50, 1, getLLVMStyle()));
- EXPECT_EQ("DEBUG({\n"
- " DEBUG({\n"
- " int a;\n"
- " int b;\n"
- " }) ;\n"
- "});",
- format("DEBUG({\n"
- " DEBUG({\n"
- " int a;\n"
- " int b;\n" // Format this line only.
- " }) ;\n" // Don't touch this line.
- "});",
- 35, 0, getLLVMStyle()));
- EXPECT_EQ("DEBUG({\n"
- " int a; //\n"
- "});",
- format("DEBUG({\n"
- " int a; //\n"
- "});",
- 0, 0, getLLVMStyle()));
- EXPECT_EQ("someFunction(\n"
- " [] {\n"
- " // Only with this comment.\n"
- " int i; // invoke formatting here.\n"
- " }, // force line break\n"
- " aaa);",
- format("someFunction(\n"
- " [] {\n"
- " // Only with this comment.\n"
- " int i; // invoke formatting here.\n"
- " }, // force line break\n"
- " aaa);",
- 63, 1, getLLVMStyle()));
-}
-
TEST_F(FormatTest, PutEmptyBlocksIntoOneLine) {
EXPECT_EQ("{}", format("{}"));
verifyFormat("enum E {};");
@@ -4886,6 +4659,13 @@ TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
" L\"bbbb\"\n"
" L\"cccc\");",
Break);
+ verifyFormat("aaaaa(aaaaaa, aaaaaaa(\"aaaa\"\n"
+ " \"bbbb\"));",
+ Break);
+ verifyFormat("string s = someFunction(\n"
+ " \"abc\"\n"
+ " \"abc\");",
+ Break);
// As we break before unary operators, breaking right after them is bad.
verifyFormat("string foo = abc ? \"x\"\n"
@@ -4906,11 +4686,11 @@ TEST_F(FormatTest, AlwaysBreakBeforeMultilineStrings) {
"b\\\n"
"c\";",
NoBreak));
- EXPECT_EQ("x =\n"
+ EXPECT_EQ("xxxx =\n"
" \"a\\\n"
"b\\\n"
"c\";",
- format("x = \"a\\\n"
+ format("xxxx = \"a\\\n"
"b\\\n"
"c\";",
Break));
@@ -5806,6 +5586,7 @@ TEST_F(FormatTest, FormatsCasts) {
verifyFormat("my_int a = (const my_int *)-1;");
verifyFormat("my_int a = (my_int)(my_int)-1;");
verifyFormat("my_int a = (ns::my_int)-2;");
+ verifyFormat("case (my_int)ONE:");
// FIXME: single value wrapped with paren will be treated as cast.
verifyFormat("void f(int i = (kValue)*kMask) {}");
@@ -6357,20 +6138,19 @@ TEST_F(FormatTest, LayoutCxx11BraceInitializers) {
"std::this_thread::sleep_for(\n"
" std::chrono::nanoseconds{ std::chrono::seconds{ 1 } } / 5);",
ExtraSpaces);
- verifyFormat(
- "std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
- " aaaaaaa,\n"
- " aaaaaaaaaa,\n"
- " aaaaa,\n"
- " aaaaaaaaaaaaaaa,\n"
- " aaa,\n"
- " aaaaaaaaaa,\n"
- " a,\n"
- " aaaaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaaaaaaa,\n"
- " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
- " aaaaaaa,\n"
- " a};");
+ verifyFormat("std::vector<MyValues> aaaaaaaaaaaaaaaaaaa{\n"
+ " aaaaaaa,\n"
+ " aaaaaaaaaa,\n"
+ " aaaaa,\n"
+ " aaaaaaaaaaaaaaa,\n"
+ " aaa,\n"
+ " aaaaaaaaaa,\n"
+ " a,\n"
+ " aaaaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaa,\n"
+ " aaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaa,\n"
+ " aaaaaaa,\n"
+ " a};");
verifyFormat("vector<int> foo = { ::SomeGlobalFunction() };", ExtraSpaces);
}
@@ -7613,114 +7393,6 @@ TEST_F(FormatTest, ObjCArrayLiterals) {
"]];");
}
-TEST_F(FormatTest, ReformatRegionAdjustsIndent) {
- EXPECT_EQ("{\n"
- "{\n"
- "a;\n"
- "b;\n"
- "}\n"
- "}",
- format("{\n"
- "{\n"
- "a;\n"
- " b;\n"
- "}\n"
- "}",
- 13, 2, getLLVMStyle()));
- EXPECT_EQ("{\n"
- "{\n"
- " a;\n"
- "b;\n"
- "}\n"
- "}",
- format("{\n"
- "{\n"
- " a;\n"
- "b;\n"
- "}\n"
- "}",
- 9, 2, getLLVMStyle()));
- EXPECT_EQ("{\n"
- "{\n"
- "public:\n"
- " b;\n"
- "}\n"
- "}",
- format("{\n"
- "{\n"
- "public:\n"
- " b;\n"
- "}\n"
- "}",
- 17, 2, getLLVMStyle()));
- EXPECT_EQ("{\n"
- "{\n"
- "a;\n"
- "}\n"
- "{\n"
- " b; //\n"
- "}\n"
- "}",
- format("{\n"
- "{\n"
- "a;\n"
- "}\n"
- "{\n"
- " b; //\n"
- "}\n"
- "}",
- 22, 2, getLLVMStyle()));
- EXPECT_EQ(" {\n"
- " a; //\n"
- " }",
- format(" {\n"
- "a; //\n"
- " }",
- 4, 2, getLLVMStyle()));
- EXPECT_EQ("void f() {}\n"
- "void g() {}",
- format("void f() {}\n"
- "void g() {}",
- 13, 0, getLLVMStyle()));
- EXPECT_EQ("int a; // comment\n"
- " // line 2\n"
- "int b;",
- format("int a; // comment\n"
- " // line 2\n"
- " int b;",
- 35, 0, getLLVMStyle()));
- EXPECT_EQ(" int a;\n"
- " void\n"
- " ffffff() {\n"
- " }",
- format(" int a;\n"
- "void ffffff() {}",
- 11, 0, getLLVMStyleWithColumns(11)));
-
- EXPECT_EQ(" void f() {\n"
- "#define A 1\n"
- " }",
- format(" void f() {\n"
- " #define A 1\n" // Format this line.
- " }",
- 20, 0, getLLVMStyle()));
- EXPECT_EQ(" void f() {\n"
- " int i;\n"
- "#define A \\\n"
- " int i; \\\n"
- " int j;\n"
- " int k;\n"
- " }",
- format(" void f() {\n"
- " int i;\n"
- "#define A \\\n"
- " int i; \\\n"
- " int j;\n"
- " int k;\n" // Format this line.
- " }",
- 67, 0, getLLVMStyle()));
-}
-
TEST_F(FormatTest, BreaksStringLiterals) {
EXPECT_EQ("\"some text \"\n"
"\"other\";",
@@ -7843,13 +7515,14 @@ TEST_F(FormatTest, BreaksStringLiterals) {
// Verify that splitting the strings understands
// Style::AlwaysBreakBeforeMultilineStrings.
- EXPECT_EQ("aaaaaaaaaaaa(aaaaaaaaaaaaa,\n"
- " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa \"\n"
- " \"aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa\");",
- format("aaaaaaaaaaaa(aaaaaaaaaaaaa, \"aaaaaaaaaaaaaaaaaaaaaa "
- "aaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaaaaa "
- "aaaaaaaaaaaaaaaaaaaaaa\");",
- getGoogleStyle()));
+ 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 "
@@ -8206,33 +7879,6 @@ TEST_F(FormatTest, ConfigurableUseOfTab) {
"\t\t parameter2); \\\n"
"\t}",
Tab);
- EXPECT_EQ("void f() {\n"
- "\tf();\n"
- "\tg();\n"
- "}",
- format("void f() {\n"
- "\tf();\n"
- "\tg();\n"
- "}",
- 0, 0, Tab));
- EXPECT_EQ("void f() {\n"
- "\tf();\n"
- "\tg();\n"
- "}",
- format("void f() {\n"
- "\tf();\n"
- "\tg();\n"
- "}",
- 16, 0, Tab));
- EXPECT_EQ("void f() {\n"
- " \tf();\n"
- "\tg();\n"
- "}",
- format("void f() {\n"
- " \tf();\n"
- " \tg();\n"
- "}",
- 21, 0, Tab));
Tab.TabWidth = 4;
Tab.IndentWidth = 8;
@@ -9322,13 +8968,15 @@ TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
Styles[2].Language = FormatStyle::LK_JavaScript;
EXPECT_EQ(0, parseConfiguration("Language: JavaScript\n"
"BasedOnStyle: Google",
- &Styles[2]).value());
+ &Styles[2])
+ .value());
Styles[3] = getLLVMStyle();
Styles[3].Language = FormatStyle::LK_JavaScript;
EXPECT_EQ(0, parseConfiguration("BasedOnStyle: Google\n"
"Language: JavaScript",
- &Styles[3]).value());
+ &Styles[3])
+ .value());
Styles[4] = getLLVMStyle();
Styles[4].Language = FormatStyle::LK_JavaScript;
@@ -9338,7 +8986,8 @@ TEST_F(FormatTest, GetsCorrectBasedOnStyle) {
"---\n"
"BasedOnStyle: Google\n"
"Language: JavaScript",
- &Styles[4]).value());
+ &Styles[4])
+ .value());
EXPECT_ALL_STYLES_EQUAL(Styles);
}
@@ -9637,7 +9286,8 @@ TEST_F(FormatTest, UsesLanguageForBasedOnStyle) {
"Language: JavaScript\n"
"IndentWidth: 76\n"
"...\n",
- &Style).value());
+ &Style)
+ .value());
EXPECT_FALSE(Style.BreakBeforeTernaryOperators);
EXPECT_EQ(76u, Style.IndentWidth);
EXPECT_EQ(FormatStyle::LK_JavaScript, Style.Language);
@@ -10085,6 +9735,7 @@ TEST_F(FormatTest, FormatsLambdas) {
// Lambdas with return types.
verifyFormat("int c = []() -> int { return 2; }();\n");
+ verifyFormat("int c = []() -> int * { return 2; }();\n");
verifyFormat("int c = []() -> vector<int> { return {2}; }();\n");
verifyFormat("Foo([]() -> std::vector<int> { return {2}; }());");
verifyGoogleFormat("auto a = [&b, c](D* d) -> D* {};");
@@ -10309,8 +9960,7 @@ TEST_F(FormatTest, FormatsBlocksWithZeroColumnWidth) {
ZeroColumn.AllowShortBlocksOnASingleLine = true;
EXPECT_EQ("void (^largeBlock)(void) = ^{ int i; };",
- format("void (^largeBlock)(void) = ^{ int i; };",
- ZeroColumn));
+ format("void (^largeBlock)(void) = ^{ int i; };", ZeroColumn));
ZeroColumn.AllowShortBlocksOnASingleLine = false;
EXPECT_EQ("void (^largeBlock)(void) = ^{\n"
" int i;\n"
@@ -10564,7 +10214,11 @@ TEST_F(FormatTest, DisableRegions) {
" int k;"));
}
-TEST_F(FormatTest, DoNotCrashOnInvalidInput) { format("? ) ="); }
+TEST_F(FormatTest, DoNotCrashOnInvalidInput) {
+ format("? ) =");
+ verifyNoCrash("#define a\\\n /**/}");
+}
-} // end namespace tooling
+} // end namespace
+} // end namespace format
} // end namespace clang
diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp
index efa845c..15d62eb 100644
--- a/unittests/Format/FormatTestJS.cpp
+++ b/unittests/Format/FormatTestJS.cpp
@@ -24,7 +24,10 @@ protected:
DEBUG(llvm::errs() << "---\n");
DEBUG(llvm::errs() << Code << "\n\n");
std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
- tooling::Replacements Replaces = reformat(Style, Code, Ranges);
+ bool IncompleteFormat = false;
+ tooling::Replacements Replaces =
+ reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
+ EXPECT_FALSE(IncompleteFormat);
std::string Result = applyAllReplacements(Code, Replaces);
EXPECT_NE("", Result);
DEBUG(llvm::errs() << "\n" << Result << "\n\n");
@@ -146,6 +149,10 @@ TEST_F(FormatTestJS, ContainerLiterals) {
// Enum style top level assignment.
verifyFormat("X = {\n a: 123\n};");
verifyFormat("X.Y = {\n a: 123\n};");
+ // But only on the top level, otherwise its a plain object literal assignment.
+ verifyFormat("function x() {\n"
+ " y = {z: 1};\n"
+ "}");
verifyFormat("x = foo && {a: 123};");
// Arrow functions in object literals.
@@ -245,12 +252,12 @@ TEST_F(FormatTestJS, FormatsFreestandingFunctions) {
" function inner2(a, b) { return a; }\n"
" inner2(a, b);\n"
"}");
+ verifyFormat("function f() {}");
}
TEST_F(FormatTestJS, ArrayLiterals) {
- verifyFormat(
- "var aaaaa: List<SomeThing> =\n"
- " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
+ verifyFormat("var aaaaa: List<SomeThing> =\n"
+ " [new SomeThingAAAAAAAAAAAA(), new SomeThingBBBBBBBBB()];");
verifyFormat("return [\n"
" aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
" bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
@@ -270,6 +277,13 @@ TEST_F(FormatTestJS, ArrayLiterals) {
" bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
" ccccccccccccccccccccccccccc\n"
"]);");
+ verifyFormat("var someVariable = SomeFuntion(aaaa,\n"
+ " [\n"
+ " aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
+ " bbbbbbbbbbbbbbbbbbbbbbbbbbb,\n"
+ " ccccccccccccccccccccccccccc\n"
+ " ],\n"
+ " aaaa);");
verifyFormat("someFunction([], {a: a});");
}
@@ -280,6 +294,10 @@ TEST_F(FormatTestJS, FunctionLiterals) {
verifyFormat("var func = function() {\n"
" return 1;\n"
"};");
+ verifyFormat("var func = //\n"
+ " function() {\n"
+ " return 1;\n"
+ "};");
verifyFormat("return {\n"
" body: {\n"
" setAttribute: function(key, val) { this[key] = val; },\n"
@@ -513,6 +531,18 @@ TEST_F(FormatTestJS, ReturnStatements) {
"}");
}
+TEST_F(FormatTestJS, AutomaticSemicolonInsertion) {
+ // The following statements must not wrap, as otherwise the program meaning
+ // would change due to automatic semicolon insertion.
+ // See http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1.
+ verifyFormat("return aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("continue aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("break aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("throw aaaaa;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("aaaaaaaaa++;", getGoogleJSStyleWithColumns(10));
+ verifyFormat("aaaaaaaaa--;", getGoogleJSStyleWithColumns(10));
+}
+
TEST_F(FormatTestJS, ClosureStyleCasts) {
verifyFormat("var x = /** @type {foo} */ (bar);");
}
@@ -657,7 +687,24 @@ TEST_F(FormatTestJS, ClassDeclarations) {
TEST_F(FormatTestJS, InterfaceDeclarations) {
verifyFormat("interface I {\n"
" x: string;\n"
+ "}\n"
+ "var y;");
+}
+
+TEST_F(FormatTestJS, EnumDeclarations) {
+ verifyFormat("enum Foo {\n"
+ " A = 1,\n"
+ " B\n"
"}");
+ verifyFormat("export /* somecomment*/ enum Foo {\n"
+ " A = 1,\n"
+ " B\n"
+ "}");
+ verifyFormat("enum Foo {\n"
+ " A = 1, // comment\n"
+ " B\n"
+ "}\n"
+ "var x = 1;");
}
TEST_F(FormatTestJS, MetadataAnnotations) {
@@ -700,12 +747,9 @@ TEST_F(FormatTestJS, Modules) {
verifyFormat("export function fn() {\n"
" return 'fn';\n"
"}");
- verifyFormat("export function A() {\n"
- "}\n"
- "export default function B() {\n"
- "}\n"
- "export function C() {\n"
- "}");
+ verifyFormat("export function A() {}\n"
+ "export default function B() {}\n"
+ "export function C() {}");
verifyFormat("export const x = 12;");
verifyFormat("export default class X {}");
verifyFormat("export {X, Y} from 'some/module.js';");
@@ -721,11 +765,19 @@ TEST_F(FormatTestJS, Modules) {
verifyFormat("export default class X { y: number }");
verifyFormat("export default function() {\n return 1;\n}");
verifyFormat("export var x = 12;");
+ verifyFormat("class C {}\n"
+ "export function f() {}\n"
+ "var v;");
verifyFormat("export var x: number = 12;");
verifyFormat("export const y = {\n"
" a: 1,\n"
" b: 2\n"
"};");
+ verifyFormat("export enum Foo {\n"
+ " BAR,\n"
+ " // adsdasd\n"
+ " BAZ\n"
+ "}");
}
TEST_F(FormatTestJS, TemplateStrings) {
@@ -783,6 +835,11 @@ TEST_F(FormatTestJS, TemplateStrings) {
"var y;",
format("var x =\n `/*a`;\n"
"var y;"));
+ // Unterminated string literals in a template string.
+ verifyFormat("var x = `'`; // comment with matching quote '\n"
+ "var y;");
+ verifyFormat("var x = `\"`; // comment with matching quote \"\n"
+ "var y;");
// Backticks in a comment - not a template string.
EXPECT_EQ("var x = 1 // `/*a`;\n"
" ;",
@@ -802,9 +859,7 @@ TEST_F(FormatTestJS, TemplateStrings) {
"var y;"));
}
-TEST_F(FormatTestJS, CastSyntax) {
- verifyFormat("var x = <type>foo;");
-}
+TEST_F(FormatTestJS, CastSyntax) { verifyFormat("var x = <type>foo;"); }
TEST_F(FormatTestJS, TypeArguments) {
verifyFormat("class X<Y> {}");
@@ -812,12 +867,12 @@ TEST_F(FormatTestJS, TypeArguments) {
verifyFormat("foo<Y>(a);");
verifyFormat("var x: X<Y>[];");
verifyFormat("class C extends D<E> implements F<G>, H<I> {}");
- verifyFormat("function f(a: List<any> = null) {\n}");
- verifyFormat("function f(): List<any> {\n}");
+ verifyFormat("function f(a: List<any> = null) {}");
+ verifyFormat("function f(): List<any> {}");
}
TEST_F(FormatTestJS, OptionalTypes) {
- verifyFormat("function x(a?: b, c?, d?) {\n}");
+ verifyFormat("function x(a?: b, c?, d?) {}");
verifyFormat("class X {\n"
" y?: z;\n"
" z?;\n"
@@ -831,8 +886,7 @@ TEST_F(FormatTestJS, OptionalTypes) {
" aaaaaaaa?: string,\n"
" aaaaaaaaaaaaaaa?: boolean,\n"
" aaaaaa?: List<string>\n"
- "}) {\n"
- "}");
+ "}) {}");
}
TEST_F(FormatTestJS, IndexSignature) {
diff --git a/unittests/Format/FormatTestJava.cpp b/unittests/Format/FormatTestJava.cpp
index 631a3dc..4c161e0 100644
--- a/unittests/Format/FormatTestJava.cpp
+++ b/unittests/Format/FormatTestJava.cpp
@@ -31,9 +31,9 @@ protected:
return Result;
}
- static std::string format(
- llvm::StringRef Code,
- const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
+ static std::string
+ format(llvm::StringRef Code,
+ const FormatStyle &Style = getGoogleStyle(FormatStyle::LK_Java)) {
return format(Code, 0, Code.size(), Style);
}
diff --git a/unittests/Format/FormatTestSelective.cpp b/unittests/Format/FormatTestSelective.cpp
new file mode 100644
index 0000000..8d2cb5a
--- /dev/null
+++ b/unittests/Format/FormatTestSelective.cpp
@@ -0,0 +1,441 @@
+//===- unittest/Format/FormatTestSelective.cpp - Formatting unit tests ----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "FormatTestUtils.h"
+#include "clang/Format/Format.h"
+#include "llvm/Support/Debug.h"
+#include "gtest/gtest.h"
+
+#define DEBUG_TYPE "format-test"
+
+namespace clang {
+namespace format {
+namespace {
+
+class FormatTestSelective : public ::testing::Test {
+protected:
+ std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length) {
+ DEBUG(llvm::errs() << "---\n");
+ DEBUG(llvm::errs() << Code << "\n\n");
+ std::vector<tooling::Range> Ranges(1, tooling::Range(Offset, Length));
+ bool IncompleteFormat = false;
+ tooling::Replacements Replaces =
+ reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
+ EXPECT_FALSE(IncompleteFormat) << Code << "\n\n";
+ std::string Result = applyAllReplacements(Code, Replaces);
+ EXPECT_NE("", Result);
+ DEBUG(llvm::errs() << "\n" << Result << "\n\n");
+ return Result;
+ }
+
+ FormatStyle Style = getLLVMStyle();
+};
+
+TEST_F(FormatTestSelective, RemovesTrailingWhitespaceOfFormattedLine) {
+ EXPECT_EQ("int a;\nint b;", format("int a; \nint b;", 0, 0));
+ EXPECT_EQ("int a;", format("int a; ", 0, 0));
+ EXPECT_EQ("int a;\n", format("int a; \n \n \n ", 0, 0));
+ EXPECT_EQ("int a;\nint b; ", format("int a; \nint b; ", 0, 0));
+}
+
+TEST_F(FormatTestSelective, FormatsCorrectRegionForLeadingWhitespace) {
+ EXPECT_EQ("int b;\nint a;", format("int b;\n int a;", 7, 0));
+ EXPECT_EQ("int b;\n int a;", format("int b;\n int a;", 6, 0));
+
+ Style.ColumnLimit = 12;
+ EXPECT_EQ("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ format("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ 26, 0));
+ EXPECT_EQ("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ format("#define A \\\n"
+ " int a; \\\n"
+ " int b;",
+ 25, 0));
+}
+
+TEST_F(FormatTestSelective, FormatLineWhenInvokedOnTrailingNewline) {
+ EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 8, 0));
+ EXPECT_EQ("int b;\n\nint a;", format("int b;\n\nint a;", 7, 0));
+
+ // This might not strictly be correct, but is likely good in all practical
+ // cases.
+ EXPECT_EQ("int b;\nint a;", format("int b;int a;", 7, 0));
+}
+
+TEST_F(FormatTestSelective, RemovesWhitespaceWhenTriggeredOnEmptyLine) {
+ EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 8, 0));
+ EXPECT_EQ("int a;\n\n int b;", format("int a;\n \n\n int b;", 9, 0));
+}
+
+TEST_F(FormatTestSelective, ReformatsMovedLines) {
+ EXPECT_EQ(
+ "template <typename T> T *getFETokenInfo() const {\n"
+ " return static_cast<T *>(FETokenInfo);\n"
+ "}\n"
+ " int a; // <- Should not be formatted",
+ format(
+ "template<typename T>\n"
+ "T *getFETokenInfo() const { return static_cast<T*>(FETokenInfo); }\n"
+ " int a; // <- Should not be formatted",
+ 9, 5));
+}
+
+TEST_F(FormatTestSelective, FormatsIfWithoutCompoundStatement) {
+ Style.AllowShortIfStatementsOnASingleLine = true;
+ EXPECT_EQ("if (a) return;", format("if(a)\nreturn;", 7, 1));
+ EXPECT_EQ("if (a) return; // comment",
+ format("if(a)\nreturn; // comment", 20, 1));
+}
+
+TEST_F(FormatTestSelective, FormatsCommentsLocally) {
+ EXPECT_EQ("int a; // comment\n"
+ "int b; // comment",
+ format("int a; // comment\n"
+ "int b; // comment",
+ 0, 0));
+ EXPECT_EQ("int a; // comment\n"
+ " // line 2\n"
+ "int b;",
+ format("int a; // comment\n"
+ " // line 2\n"
+ "int b;",
+ 28, 0));
+ EXPECT_EQ("int aaaaaa; // comment\n"
+ "int b;\n"
+ "int c; // unrelated comment",
+ format("int aaaaaa; // comment\n"
+ "int b;\n"
+ "int c; // unrelated comment",
+ 31, 0));
+
+ EXPECT_EQ("int a; // This\n"
+ " // is\n"
+ " // a",
+ format("int a; // This\n"
+ " // is\n"
+ " // a",
+ 0, 0));
+ EXPECT_EQ("int a; // This\n"
+ " // is\n"
+ " // a\n"
+ "// This is b\n"
+ "int b;",
+ format("int a; // This\n"
+ " // is\n"
+ " // a\n"
+ "// This is b\n"
+ "int b;",
+ 0, 0));
+ EXPECT_EQ("int a; // This\n"
+ " // is\n"
+ " // a\n"
+ "\n"
+ " // This is unrelated",
+ format("int a; // This\n"
+ " // is\n"
+ " // a\n"
+ "\n"
+ " // This is unrelated",
+ 0, 0));
+ EXPECT_EQ("int a;\n"
+ "// This is\n"
+ "// not formatted. ",
+ format("int a;\n"
+ "// This is\n"
+ "// not formatted. ",
+ 0, 0));
+}
+
+TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {
+ EXPECT_EQ("DEBUG({\n"
+ " int i;\n"
+ " int j;\n"
+ "});",
+ format("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ 20, 1));
+ EXPECT_EQ("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ format("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ 41, 1));
+ EXPECT_EQ("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ format("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ 41, 1));
+ EXPECT_EQ("DEBUG({\n"
+ " int i;\n"
+ " int j;\n"
+ "});",
+ format("DEBUG( {\n"
+ " int i;\n"
+ " int j;\n"
+ "} ) ;",
+ 20, 1));
+
+ EXPECT_EQ("Debug({\n"
+ " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
+ " return;\n"
+ " },\n"
+ " a);",
+ format("Debug({\n"
+ " if (aaaaaaaaaaaaaaaaaaaaaaaa)\n"
+ " return;\n"
+ " },\n"
+ " a);",
+ 50, 1));
+ EXPECT_EQ("DEBUG({\n"
+ " DEBUG({\n"
+ " int a;\n"
+ " int b;\n"
+ " }) ;\n"
+ "});",
+ format("DEBUG({\n"
+ " DEBUG({\n"
+ " int a;\n"
+ " int b;\n" // Format this line only.
+ " }) ;\n" // Don't touch this line.
+ "});",
+ 35, 0));
+ EXPECT_EQ("DEBUG({\n"
+ " int a; //\n"
+ "});",
+ format("DEBUG({\n"
+ " int a; //\n"
+ "});",
+ 0, 0));
+ EXPECT_EQ("someFunction(\n"
+ " [] {\n"
+ " // Only with this comment.\n"
+ " int i; // invoke formatting here.\n"
+ " }, // force line break\n"
+ " aaa);",
+ format("someFunction(\n"
+ " [] {\n"
+ " // Only with this comment.\n"
+ " int i; // invoke formatting here.\n"
+ " }, // force line break\n"
+ " aaa);",
+ 63, 1));
+
+ EXPECT_EQ("int longlongname; // comment\n"
+ "int x = f({\n"
+ " int x; // comment\n"
+ " int y; // comment\n"
+ "});",
+ format("int longlongname; // comment\n"
+ "int x = f({\n"
+ " int x; // comment\n"
+ " int y; // comment\n"
+ "});",
+ 65, 0));
+ EXPECT_EQ("int s = f({\n"
+ " class X {\n"
+ " public:\n"
+ " void f();\n"
+ " };\n"
+ "});",
+ format("int s = f({\n"
+ " class X {\n"
+ " public:\n"
+ " void f();\n"
+ " };\n"
+ "});",
+ 0, 0));
+}
+
+TEST_F(FormatTestSelective, AlwaysFormatsEntireMacroDefinitions) {
+ Style.AlignEscapedNewlinesLeft = true;
+ EXPECT_EQ("int i;\n"
+ "#define A \\\n"
+ " int i; \\\n"
+ " int j\n"
+ "int k;",
+ format("int i;\n"
+ "#define A \\\n"
+ " int i ; \\\n"
+ " int j\n"
+ "int k;",
+ 8, 0)); // 8: position of "#define".
+ EXPECT_EQ("int i;\n"
+ "#define A \\\n"
+ " int i; \\\n"
+ " int j\n"
+ "int k;",
+ format("int i;\n"
+ "#define A \\\n"
+ " int i ; \\\n"
+ " int j\n"
+ "int k;",
+ 45, 0)); // 45: position of "j".
+}
+
+TEST_F(FormatTestSelective, ReformatRegionAdjustsIndent) {
+ EXPECT_EQ("{\n"
+ "{\n"
+ "a;\n"
+ "b;\n"
+ "}\n"
+ "}",
+ format("{\n"
+ "{\n"
+ "a;\n"
+ " b;\n"
+ "}\n"
+ "}",
+ 13, 2));
+ EXPECT_EQ("{\n"
+ "{\n"
+ " a;\n"
+ "b;\n"
+ "}\n"
+ "}",
+ format("{\n"
+ "{\n"
+ " a;\n"
+ "b;\n"
+ "}\n"
+ "}",
+ 9, 2));
+ EXPECT_EQ("{\n"
+ "{\n"
+ "public:\n"
+ " b;\n"
+ "}\n"
+ "}",
+ format("{\n"
+ "{\n"
+ "public:\n"
+ " b;\n"
+ "}\n"
+ "}",
+ 17, 2));
+ EXPECT_EQ("{\n"
+ "{\n"
+ "a;\n"
+ "}\n"
+ "{\n"
+ " b; //\n"
+ "}\n"
+ "}",
+ format("{\n"
+ "{\n"
+ "a;\n"
+ "}\n"
+ "{\n"
+ " b; //\n"
+ "}\n"
+ "}",
+ 22, 2));
+ EXPECT_EQ(" {\n"
+ " a; //\n"
+ " }",
+ format(" {\n"
+ "a; //\n"
+ " }",
+ 4, 2));
+ EXPECT_EQ("void f() {}\n"
+ "void g() {}",
+ format("void f() {}\n"
+ "void g() {}",
+ 13, 0));
+ EXPECT_EQ("int a; // comment\n"
+ " // line 2\n"
+ "int b;",
+ format("int a; // comment\n"
+ " // line 2\n"
+ " int b;",
+ 35, 0));
+
+ EXPECT_EQ(" void f() {\n"
+ "#define A 1\n"
+ " }",
+ format(" void f() {\n"
+ " #define A 1\n" // Format this line.
+ " }",
+ 20, 0));
+ EXPECT_EQ(" void f() {\n"
+ " int i;\n"
+ "#define A \\\n"
+ " int i; \\\n"
+ " int j;\n"
+ " int k;\n"
+ " }",
+ format(" void f() {\n"
+ " int i;\n"
+ "#define A \\\n"
+ " int i; \\\n"
+ " int j;\n"
+ " int k;\n" // Format this line.
+ " }",
+ 67, 0));
+
+ Style.ColumnLimit = 11;
+ EXPECT_EQ(" int a;\n"
+ " void\n"
+ " ffffff() {\n"
+ " }",
+ format(" int a;\n"
+ "void ffffff() {}",
+ 11, 0));
+}
+
+TEST_F(FormatTestSelective, UnderstandsTabs) {
+ Style.IndentWidth = 8;
+ Style.UseTab = FormatStyle::UT_Always;
+ Style.AlignEscapedNewlinesLeft = true;
+ EXPECT_EQ("void f() {\n"
+ "\tf();\n"
+ "\tg();\n"
+ "}",
+ format("void f() {\n"
+ "\tf();\n"
+ "\tg();\n"
+ "}",
+ 0, 0));
+ EXPECT_EQ("void f() {\n"
+ "\tf();\n"
+ "\tg();\n"
+ "}",
+ format("void f() {\n"
+ "\tf();\n"
+ "\tg();\n"
+ "}",
+ 16, 0));
+ EXPECT_EQ("void f() {\n"
+ " \tf();\n"
+ "\tg();\n"
+ "}",
+ format("void f() {\n"
+ " \tf();\n"
+ " \tg();\n"
+ "}",
+ 21, 0));
+}
+
+} // end namespace
+} // end namespace format
+} // end namespace clang
diff --git a/unittests/Tooling/RewriterTest.cpp b/unittests/Tooling/RewriterTest.cpp
index c53e50a..93f69eb 100644
--- a/unittests/Tooling/RewriterTest.cpp
+++ b/unittests/Tooling/RewriterTest.cpp
@@ -8,9 +8,12 @@
//===----------------------------------------------------------------------===//
#include "RewriterTestContext.h"
+#include "clang/Tooling/Core/Replacement.h"
#include "gtest/gtest.h"
namespace clang {
+namespace tooling {
+namespace {
TEST(Rewriter, OverwritesChangedFiles) {
RewriterTestContext Context;
@@ -34,4 +37,14 @@ TEST(Rewriter, ContinuesOverwritingFilesOnError) {
Context.getFileContentFromDisk("working.cpp"));
}
+TEST(Rewriter, AdjacentInsertAndDelete) {
+ Replacements Replaces;
+ Replaces.insert(Replacement("<file>", 6, 6, ""));
+ Replaces.insert(Replacement("<file>", 6, 0, "replaced\n"));
+ EXPECT_EQ("line1\nreplaced\nline3\nline4",
+ applyAllReplacements("line1\nline2\nline3\nline4", Replaces));
+}
+
+} // end namespace
+} // end namespace tooling
} // end namespace clang
OpenPOWER on IntegriCloud