Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1 | //===- unittest/Format/FormatTest.cpp - Formatting unit tests -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chandler Carruth | 1050e8b | 2012-12-04 09:45:34 +0000 | [diff] [blame] | 10 | #include "clang/Format/Format.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 11 | #include "../Tooling/RewriterTestContext.h" |
| 12 | #include "clang/Lex/Lexer.h" |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | namespace clang { |
| 16 | namespace format { |
| 17 | |
| 18 | class FormatTest : public ::testing::Test { |
| 19 | protected: |
| 20 | std::string format(llvm::StringRef Code, unsigned Offset, unsigned Length, |
| 21 | const FormatStyle &Style) { |
| 22 | RewriterTestContext Context; |
| 23 | FileID ID = Context.createInMemoryFile("input.cc", Code); |
| 24 | SourceLocation Start = |
| 25 | Context.Sources.getLocForStartOfFile(ID).getLocWithOffset(Offset); |
| 26 | std::vector<CharSourceRange> Ranges( |
| 27 | 1, |
| 28 | CharSourceRange::getCharRange(Start, Start.getLocWithOffset(Length))); |
| 29 | LangOptions LangOpts; |
| 30 | LangOpts.CPlusPlus = 1; |
| 31 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts); |
| 32 | tooling::Replacements Replace = |
| 33 | reformat(Style, Lex, Context.Sources, Ranges); |
| 34 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
| 35 | return Context.getRewrittenText(ID); |
| 36 | } |
| 37 | |
| 38 | std::string format(llvm::StringRef Code, |
| 39 | const FormatStyle &Style = getLLVMStyle()) { |
| 40 | return format(Code, 0, Code.size(), Style); |
| 41 | } |
| 42 | |
| 43 | void verifyFormat(llvm::StringRef Code) { |
| 44 | std::string WithoutFormat(Code.str()); |
| 45 | for (unsigned i = 0, e = WithoutFormat.size(); i != e; ++i) { |
| 46 | if (WithoutFormat[i] == '\n') |
| 47 | WithoutFormat[i] = ' '; |
| 48 | } |
| 49 | EXPECT_EQ(Code.str(), format(WithoutFormat)); |
| 50 | } |
| 51 | |
| 52 | void verifyGoogleFormat(llvm::StringRef Code) { |
| 53 | std::string WithoutFormat(Code.str()); |
| 54 | for (unsigned i = 0, e = WithoutFormat.size(); i != e; ++i) { |
| 55 | if (WithoutFormat[i] == '\n') |
| 56 | WithoutFormat[i] = ' '; |
| 57 | } |
| 58 | EXPECT_EQ(Code.str(), format(WithoutFormat, getGoogleStyle())); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 63 | EXPECT_EQ(";", format(";")); |
| 64 | } |
| 65 | |
| 66 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 67 | EXPECT_EQ("int i;", format(" int i;")); |
| 68 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 69 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 70 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 71 | } |
| 72 | |
| 73 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 74 | EXPECT_EQ("int i;", format("int\ni;")); |
| 75 | } |
| 76 | |
| 77 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
| 78 | EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}")); |
| 79 | } |
| 80 | |
| 81 | TEST_F(FormatTest, FormatsForLoop) { |
| 82 | verifyFormat( |
| 83 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
| 84 | " ++VeryVeryLongLoopVariable);"); |
| 85 | } |
| 86 | |
| 87 | TEST_F(FormatTest, FormatsWhileLoop) { |
| 88 | verifyFormat("while (true) {\n}"); |
| 89 | } |
| 90 | |
| 91 | TEST_F(FormatTest, FormatsNestedCall) { |
| 92 | verifyFormat("Method(f1, f2(f3));"); |
| 93 | verifyFormat("Method(f1(f2, f3()));"); |
| 94 | } |
| 95 | |
| 96 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 97 | verifyFormat( |
| 98 | "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" |
| 99 | " parameter, parameter, parameter)), SecondLongCall(parameter));"); |
| 100 | } |
| 101 | |
| 102 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 103 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 104 | " int h, int j, int f,\n" |
| 105 | " int c, int ddddddddddddd) {\n" |
| 106 | "}"); |
| 107 | } |
| 108 | |
| 109 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
| 110 | verifyFormat("if (true)\n f();\ng();"); |
| 111 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
| 112 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
| 113 | } |
| 114 | |
| 115 | TEST_F(FormatTest, ParseIfThenElse) { |
| 116 | verifyFormat("if (true)\n" |
| 117 | " if (true)\n" |
| 118 | " if (true)\n" |
| 119 | " f();\n" |
| 120 | " else\n" |
| 121 | " g();\n" |
| 122 | " else\n" |
| 123 | " h();\n" |
| 124 | "else\n" |
| 125 | " i();"); |
| 126 | verifyFormat("if (true)\n" |
| 127 | " if (true)\n" |
| 128 | " if (true) {\n" |
| 129 | " if (true)\n" |
| 130 | " f();\n" |
| 131 | " } else {\n" |
| 132 | " g();\n" |
| 133 | " }\n" |
| 134 | " else\n" |
| 135 | " h();\n" |
| 136 | "else {\n" |
| 137 | " i();\n" |
| 138 | "}"); |
| 139 | } |
| 140 | |
| 141 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
| 142 | EXPECT_EQ("// line 1\n// line 2\nvoid f() {\n}\n", |
| 143 | format("// line 1\n// line 2\nvoid f() {}\n")); |
| 144 | |
| 145 | EXPECT_EQ("void f() {\n // Doesn't do anything\n}", |
| 146 | format("void f() {\n// Doesn't do anything\n}")); |
| 147 | |
| 148 | EXPECT_EQ("int i // This is a fancy variable\n = 5;", |
| 149 | format("int i // This is a fancy variable\n= 5;")); |
| 150 | |
| 151 | verifyFormat("f(/*test=*/ true);"); |
| 152 | } |
| 153 | |
| 154 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
| 155 | verifyFormat("class A {\n};"); |
| 156 | } |
| 157 | |
| 158 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 159 | verifyFormat( |
| 160 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 161 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 162 | " f();"); |
| 163 | } |
| 164 | |
| 165 | TEST_F(FormatTest, ElseIf) { |
| 166 | verifyFormat("if (a) {\n" |
| 167 | "} else if (b) {\n" |
| 168 | "}"); |
| 169 | verifyFormat("if (a)\n" |
| 170 | " f();\n" |
| 171 | "else if (b)\n" |
| 172 | " g();\n" |
| 173 | "else\n" |
| 174 | " h();"); |
| 175 | } |
| 176 | |
| 177 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 178 | verifyFormat("class A {\n" |
| 179 | "public:\n" |
| 180 | "protected:\n" |
| 181 | "private:\n" |
| 182 | " void f() {\n" |
| 183 | " }\n" |
| 184 | "};"); |
| 185 | verifyGoogleFormat("class A {\n" |
| 186 | " public:\n" |
| 187 | " protected:\n" |
| 188 | " private:\n" |
| 189 | " void f() {\n" |
| 190 | " }\n" |
| 191 | "};"); |
| 192 | } |
| 193 | |
| 194 | TEST_F(FormatTest, SwitchStatement) { |
| 195 | verifyFormat("switch (x) {\n" |
| 196 | "case 1:\n" |
| 197 | " f();\n" |
| 198 | " break;\n" |
| 199 | "case kFoo:\n" |
| 200 | "case ns::kBar:\n" |
| 201 | "case kBaz:\n" |
| 202 | " break;\n" |
| 203 | "default:\n" |
| 204 | " g();\n" |
| 205 | " break;\n" |
| 206 | "}"); |
| 207 | verifyFormat("switch (x) {\n" |
| 208 | "case 1: {\n" |
| 209 | " f();\n" |
| 210 | " break;\n" |
| 211 | "}\n" |
| 212 | "}"); |
| 213 | verifyFormat("switch (test)\n" |
| 214 | " ;"); |
| 215 | } |
| 216 | |
| 217 | TEST_F(FormatTest, Labels) { |
| 218 | verifyFormat("void f() {\n" |
| 219 | " some_code();\n" |
| 220 | "test_label:\n" |
| 221 | " some_other_code();\n" |
| 222 | " {\n" |
| 223 | " some_more_code();\n" |
| 224 | " another_label:\n" |
| 225 | " some_more_code();\n" |
| 226 | " }\n" |
| 227 | "}"); |
| 228 | verifyFormat("some_code();\n" |
| 229 | "test_label:\n" |
| 230 | "some_other_code();"); |
| 231 | } |
| 232 | |
| 233 | TEST_F(FormatTest, DerivedClass) { |
| 234 | verifyFormat("class A : public B {\n" |
| 235 | "};"); |
| 236 | } |
| 237 | |
| 238 | TEST_F(FormatTest, DoWhile) { |
| 239 | verifyFormat("do {\n" |
| 240 | " do_something();\n" |
| 241 | "} while (something());"); |
| 242 | verifyFormat("do\n" |
| 243 | " do_something();\n" |
| 244 | "while (something());"); |
| 245 | } |
| 246 | |
| 247 | TEST_F(FormatTest, BreaksDesireably) { |
| 248 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 249 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 250 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};"); |
| 251 | |
| 252 | verifyFormat( |
| 253 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 254 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 255 | |
| 256 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 257 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 258 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 259 | |
| 260 | verifyFormat( |
| 261 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 262 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 263 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 264 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 268 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 269 | " \"short literal\");"); |
| 270 | verifyFormat( |
| 271 | "looooooooooooooooooooooooongFunction(\n" |
| 272 | " \"short literal\"\n" |
| 273 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 274 | } |
| 275 | |
| 276 | TEST_F(FormatTest, UnderstandsEquals) { |
| 277 | verifyFormat( |
| 278 | "aaaaaaaaaaaaaaaaa =\n" |
| 279 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 280 | verifyFormat( |
| 281 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 282 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 283 | "}"); |
| 284 | verifyFormat( |
| 285 | "if (a) {\n" |
| 286 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 287 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 288 | "}"); |
| 289 | |
| 290 | verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 291 | " 100000000 + 100000000) {\n}"); |
| 292 | } |
| 293 | |
| 294 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 295 | verifyFormat("A<int> a;"); |
| 296 | verifyFormat("A<A<A<int> > > a;"); |
| 297 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 298 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 299 | verifyFormat("bool x = 5 < f<int>();"); |
| 300 | verifyFormat("bool x = f<int>() > 5;"); |
| 301 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 302 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 303 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 304 | |
| 305 | verifyGoogleFormat("A<A<int>> a;"); |
| 306 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 307 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 308 | |
| 309 | verifyFormat("test >> a >> b;"); |
| 310 | verifyFormat("test << a >> b;"); |
| 311 | |
| 312 | verifyFormat("f<int>();"); |
| 313 | verifyFormat("template <typename T> void f() {\n}"); |
| 314 | } |
| 315 | |
| 316 | TEST_F(FormatTest, UndestandsUnaryOperators) { |
| 317 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 318 | verifyFormat("f(-1, -2, -3);"); |
| 319 | verifyFormat("a[-1] = 5;"); |
| 320 | verifyFormat("int a = 5 + -2;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
| 324 | verifyFormat("bool operator<() {\n}"); |
| 325 | } |
| 326 | |
| 327 | TEST_F(FormatTest, UnderstandsUsesOfStar) { |
| 328 | verifyFormat("int *f(int *a) {\n}"); |
| 329 | verifyFormat("f(a, *a);"); |
| 330 | verifyFormat("f(*a);"); |
| 331 | verifyFormat("int a = b * 10;"); |
| 332 | verifyFormat("int a = 10 * b;"); |
| 333 | // verifyFormat("int a = b * c;"); |
| 334 | verifyFormat("int a = *b;"); |
| 335 | // verifyFormat("int a = *b * c;"); |
| 336 | // verifyFormat("int a = b * *c;"); |
| 337 | } |
| 338 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 339 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 340 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
| 341 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 342 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 343 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 344 | } |
| 345 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 346 | //TEST_F(FormatTest, IncorrectDerivedClass) { |
| 347 | // verifyFormat("public B {\n" |
| 348 | // "};"); |
| 349 | //} |
| 350 | |
| 351 | } // end namespace tooling |
| 352 | } // end namespace clang |