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" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 84 | " ++VeryVeryLongLoopVariable)\n" |
| 85 | " ;"); |
| 86 | verifyFormat("for (;;)\n" |
| 87 | " f();"); |
| 88 | verifyFormat("for (;;) {\n" |
| 89 | "}"); |
| 90 | verifyFormat("for (;;) {\n" |
| 91 | " f();\n" |
| 92 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | TEST_F(FormatTest, FormatsWhileLoop) { |
| 96 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 97 | verifyFormat("while (true)\n" |
| 98 | " f();"); |
| 99 | verifyFormat("while () {\n" |
| 100 | "}"); |
| 101 | verifyFormat("while () {\n" |
| 102 | " f();\n" |
| 103 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | TEST_F(FormatTest, FormatsNestedCall) { |
| 107 | verifyFormat("Method(f1, f2(f3));"); |
| 108 | verifyFormat("Method(f1(f2, f3()));"); |
| 109 | } |
| 110 | |
| 111 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 112 | verifyFormat( |
| 113 | "SomeLongMethodName(SomeReallyLongMethod(CallOtherReallyLongMethod(\n" |
| 114 | " parameter, parameter, parameter)), SecondLongCall(parameter));"); |
| 115 | } |
| 116 | |
| 117 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 118 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 119 | " int h, int j, int f,\n" |
| 120 | " int c, int ddddddddddddd) {\n" |
| 121 | "}"); |
| 122 | } |
| 123 | |
| 124 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
| 125 | verifyFormat("if (true)\n f();\ng();"); |
| 126 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
| 127 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 128 | EXPECT_EQ("if (a)\n // comment\n f();", format("if(a)\n// comment\nf();")); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | TEST_F(FormatTest, ParseIfThenElse) { |
| 132 | verifyFormat("if (true)\n" |
| 133 | " if (true)\n" |
| 134 | " if (true)\n" |
| 135 | " f();\n" |
| 136 | " else\n" |
| 137 | " g();\n" |
| 138 | " else\n" |
| 139 | " h();\n" |
| 140 | "else\n" |
| 141 | " i();"); |
| 142 | verifyFormat("if (true)\n" |
| 143 | " if (true)\n" |
| 144 | " if (true) {\n" |
| 145 | " if (true)\n" |
| 146 | " f();\n" |
| 147 | " } else {\n" |
| 148 | " g();\n" |
| 149 | " }\n" |
| 150 | " else\n" |
| 151 | " h();\n" |
| 152 | "else {\n" |
| 153 | " i();\n" |
| 154 | "}"); |
| 155 | } |
| 156 | |
| 157 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
| 158 | EXPECT_EQ("// line 1\n// line 2\nvoid f() {\n}\n", |
| 159 | format("// line 1\n// line 2\nvoid f() {}\n")); |
| 160 | |
| 161 | EXPECT_EQ("void f() {\n // Doesn't do anything\n}", |
| 162 | format("void f() {\n// Doesn't do anything\n}")); |
| 163 | |
| 164 | EXPECT_EQ("int i // This is a fancy variable\n = 5;", |
| 165 | format("int i // This is a fancy variable\n= 5;")); |
| 166 | |
| 167 | verifyFormat("f(/*test=*/ true);"); |
| 168 | } |
| 169 | |
| 170 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
| 171 | verifyFormat("class A {\n};"); |
| 172 | } |
| 173 | |
| 174 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 175 | verifyFormat( |
| 176 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 177 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 178 | " f();"); |
| 179 | } |
| 180 | |
| 181 | TEST_F(FormatTest, ElseIf) { |
| 182 | verifyFormat("if (a) {\n" |
| 183 | "} else if (b) {\n" |
| 184 | "}"); |
| 185 | verifyFormat("if (a)\n" |
| 186 | " f();\n" |
| 187 | "else if (b)\n" |
| 188 | " g();\n" |
| 189 | "else\n" |
| 190 | " h();"); |
| 191 | } |
| 192 | |
| 193 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 194 | verifyFormat("class A {\n" |
| 195 | "public:\n" |
| 196 | "protected:\n" |
| 197 | "private:\n" |
| 198 | " void f() {\n" |
| 199 | " }\n" |
| 200 | "};"); |
| 201 | verifyGoogleFormat("class A {\n" |
| 202 | " public:\n" |
| 203 | " protected:\n" |
| 204 | " private:\n" |
| 205 | " void f() {\n" |
| 206 | " }\n" |
| 207 | "};"); |
| 208 | } |
| 209 | |
| 210 | TEST_F(FormatTest, SwitchStatement) { |
| 211 | verifyFormat("switch (x) {\n" |
| 212 | "case 1:\n" |
| 213 | " f();\n" |
| 214 | " break;\n" |
| 215 | "case kFoo:\n" |
| 216 | "case ns::kBar:\n" |
| 217 | "case kBaz:\n" |
| 218 | " break;\n" |
| 219 | "default:\n" |
| 220 | " g();\n" |
| 221 | " break;\n" |
| 222 | "}"); |
| 223 | verifyFormat("switch (x) {\n" |
| 224 | "case 1: {\n" |
| 225 | " f();\n" |
| 226 | " break;\n" |
| 227 | "}\n" |
| 228 | "}"); |
| 229 | verifyFormat("switch (test)\n" |
| 230 | " ;"); |
| 231 | } |
| 232 | |
| 233 | TEST_F(FormatTest, Labels) { |
| 234 | verifyFormat("void f() {\n" |
| 235 | " some_code();\n" |
| 236 | "test_label:\n" |
| 237 | " some_other_code();\n" |
| 238 | " {\n" |
| 239 | " some_more_code();\n" |
| 240 | " another_label:\n" |
| 241 | " some_more_code();\n" |
| 242 | " }\n" |
| 243 | "}"); |
| 244 | verifyFormat("some_code();\n" |
| 245 | "test_label:\n" |
| 246 | "some_other_code();"); |
| 247 | } |
| 248 | |
| 249 | TEST_F(FormatTest, DerivedClass) { |
| 250 | verifyFormat("class A : public B {\n" |
| 251 | "};"); |
| 252 | } |
| 253 | |
| 254 | TEST_F(FormatTest, DoWhile) { |
| 255 | verifyFormat("do {\n" |
| 256 | " do_something();\n" |
| 257 | "} while (something());"); |
| 258 | verifyFormat("do\n" |
| 259 | " do_something();\n" |
| 260 | "while (something());"); |
| 261 | } |
| 262 | |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 263 | TEST_F(FormatTest, Enum) { |
| 264 | verifyFormat("enum {\n" |
| 265 | " Zero,\n" |
| 266 | " One = 1,\n" |
| 267 | " Two = One + 1,\n" |
| 268 | " Three = (One + Two),\n" |
| 269 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 270 | " Five = (One, Two, Three, Four, 5)\n" |
| 271 | "};"); |
| 272 | verifyFormat("enum Enum {\n" |
| 273 | "};"); |
| 274 | verifyFormat("enum {\n" |
| 275 | "};"); |
| 276 | } |
| 277 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 278 | TEST_F(FormatTest, BreaksDesireably) { |
| 279 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 280 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 281 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};"); |
| 282 | |
| 283 | verifyFormat( |
| 284 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 285 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 286 | |
| 287 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 288 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 289 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 290 | |
| 291 | verifyFormat( |
| 292 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 293 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 294 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 295 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 296 | |
| 297 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 298 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 299 | |
| 300 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 301 | // taking into account the StopAt value. |
| 302 | verifyFormat( |
| 303 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 304 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 305 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 306 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 310 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 311 | " \"short literal\");"); |
| 312 | verifyFormat( |
| 313 | "looooooooooooooooooooooooongFunction(\n" |
| 314 | " \"short literal\"\n" |
| 315 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 316 | } |
| 317 | |
| 318 | TEST_F(FormatTest, UnderstandsEquals) { |
| 319 | verifyFormat( |
| 320 | "aaaaaaaaaaaaaaaaa =\n" |
| 321 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 322 | verifyFormat( |
| 323 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 324 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 325 | "}"); |
| 326 | verifyFormat( |
| 327 | "if (a) {\n" |
| 328 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 329 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 330 | "}"); |
| 331 | |
| 332 | verifyFormat("if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 333 | " 100000000 + 100000000) {\n}"); |
| 334 | } |
| 335 | |
| 336 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 337 | verifyFormat("A<int> a;"); |
| 338 | verifyFormat("A<A<A<int> > > a;"); |
| 339 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 340 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 341 | verifyFormat("bool x = 5 < f<int>();"); |
| 342 | verifyFormat("bool x = f<int>() > 5;"); |
| 343 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 344 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 345 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 346 | |
| 347 | verifyGoogleFormat("A<A<int>> a;"); |
| 348 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 349 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 350 | |
| 351 | verifyFormat("test >> a >> b;"); |
| 352 | verifyFormat("test << a >> b;"); |
| 353 | |
| 354 | verifyFormat("f<int>();"); |
| 355 | verifyFormat("template <typename T> void f() {\n}"); |
| 356 | } |
| 357 | |
| 358 | TEST_F(FormatTest, UndestandsUnaryOperators) { |
| 359 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 360 | verifyFormat("f(-1, -2, -3);"); |
| 361 | verifyFormat("a[-1] = 5;"); |
| 362 | verifyFormat("int a = 5 + -2;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 363 | verifyFormat("if (i == -1) {\n}"); |
| 364 | verifyFormat("if (i != -1) {\n}"); |
| 365 | verifyFormat("if (i > -1) {\n}"); |
| 366 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
| 370 | verifyFormat("bool operator<() {\n}"); |
| 371 | } |
| 372 | |
| 373 | TEST_F(FormatTest, UnderstandsUsesOfStar) { |
| 374 | verifyFormat("int *f(int *a) {\n}"); |
| 375 | verifyFormat("f(a, *a);"); |
| 376 | verifyFormat("f(*a);"); |
| 377 | verifyFormat("int a = b * 10;"); |
| 378 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 379 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 380 | verifyFormat("int a += b * c;"); |
| 381 | verifyFormat("int a -= b * c;"); |
| 382 | verifyFormat("int a *= b * c;"); |
| 383 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 384 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 385 | verifyFormat("int a = *b * c;"); |
| 386 | verifyFormat("int a = b * *c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 389 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 390 | verifyFormat("(a)->b();"); |
| 391 | verifyFormat("--a;"); |
| 392 | } |
| 393 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 394 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 395 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
| 396 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 397 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 398 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 399 | } |
| 400 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 401 | //TEST_F(FormatTest, IncorrectDerivedClass) { |
| 402 | // verifyFormat("public B {\n" |
| 403 | // "};"); |
| 404 | //} |
| 405 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 406 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 407 | verifyFormat("{"); |
| 408 | } |
| 409 | |
| 410 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
| 411 | verifyFormat("do {\n" |
| 412 | "};"); |
| 413 | verifyFormat("do {\n" |
| 414 | "};\n" |
| 415 | "f();"); |
| 416 | verifyFormat("do {\n" |
| 417 | "}\n" |
| 418 | "wheeee(fun);"); |
| 419 | verifyFormat("do {\n" |
| 420 | " f();\n" |
| 421 | "};"); |
| 422 | } |
| 423 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 424 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
| 425 | EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n")); |
| 426 | EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n")); |
| 427 | EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n")); |
| 428 | |
| 429 | FormatStyle Style = getLLVMStyle(); |
| 430 | Style.ColumnLimit = 10; |
| 431 | EXPECT_EQ("{\n" |
| 432 | " {\n" |
| 433 | " breakme(\n" |
| 434 | " qwe);\n" |
| 435 | "}\n", format("{\n" |
| 436 | " {\n" |
| 437 | " breakme(qwe);\n" |
| 438 | "}\n", Style)); |
| 439 | |
| 440 | } |
| 441 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 442 | TEST_F(FormatTest, AlignsPipes) { |
| 443 | verifyFormat( |
| 444 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 445 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 446 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 447 | verifyFormat( |
| 448 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 449 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 450 | verifyFormat( |
| 451 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 452 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 453 | verifyFormat( |
| 454 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 455 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 456 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 457 | verifyFormat( |
| 458 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 459 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 460 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 461 | } |
| 462 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 463 | } // end namespace tooling |
| 464 | } // end namespace clang |