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; |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 31 | LangOpts.CPlusPlus11 = 1; |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 32 | Lexer Lex(ID, Context.Sources.getBuffer(ID), Context.Sources, LangOpts); |
| 33 | tooling::Replacements Replace = |
| 34 | reformat(Style, Lex, Context.Sources, Ranges); |
| 35 | EXPECT_TRUE(applyAllReplacements(Replace, Context.Rewrite)); |
| 36 | return Context.getRewrittenText(ID); |
| 37 | } |
| 38 | |
| 39 | std::string format(llvm::StringRef Code, |
| 40 | const FormatStyle &Style = getLLVMStyle()) { |
| 41 | return format(Code, 0, Code.size(), Style); |
| 42 | } |
| 43 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 44 | std::string messUp(llvm::StringRef Code) { |
| 45 | std::string MessedUp(Code.str()); |
| 46 | bool InComment = false; |
| 47 | bool JustReplacedNewline = false; |
| 48 | for (unsigned i = 0, e = MessedUp.size() - 1; i != e; ++i) { |
| 49 | if (MessedUp[i] == '/' && MessedUp[i + 1] == '/') { |
| 50 | if (JustReplacedNewline) |
| 51 | MessedUp[i - 1] = '\n'; |
| 52 | InComment = true; |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 53 | } else if (MessedUp[i] == '\\' && MessedUp[i + 1] == '\n') { |
| 54 | MessedUp[i] = ' '; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 55 | } else if (MessedUp[i] == '\n') { |
| 56 | if (InComment) { |
| 57 | InComment = false; |
| 58 | } else { |
| 59 | JustReplacedNewline = true; |
| 60 | MessedUp[i] = ' '; |
| 61 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 62 | } else if (MessedUp[i] != ' ') { |
| 63 | JustReplacedNewline = false; |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 64 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 65 | } |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 66 | return MessedUp; |
| 67 | } |
| 68 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 69 | FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) { |
| 70 | FormatStyle Style = getLLVMStyle(); |
| 71 | Style.ColumnLimit = ColumnLimit; |
| 72 | return Style; |
| 73 | } |
| 74 | |
| 75 | void verifyFormat(llvm::StringRef Code, |
| 76 | const FormatStyle &Style = getLLVMStyle()) { |
| 77 | EXPECT_EQ(Code.str(), format(messUp(Code), Style)); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void verifyGoogleFormat(llvm::StringRef Code) { |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 81 | verifyFormat(Code, getGoogleStyle()); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 82 | } |
| 83 | }; |
| 84 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 85 | //===----------------------------------------------------------------------===// |
| 86 | // Basic function tests. |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 89 | TEST_F(FormatTest, DoesNotChangeCorrectlyFormatedCode) { |
| 90 | EXPECT_EQ(";", format(";")); |
| 91 | } |
| 92 | |
| 93 | TEST_F(FormatTest, FormatsGlobalStatementsAt0) { |
| 94 | EXPECT_EQ("int i;", format(" int i;")); |
| 95 | EXPECT_EQ("\nint i;", format(" \n\t \r int i;")); |
| 96 | EXPECT_EQ("int i;\nint j;", format(" int i; int j;")); |
| 97 | EXPECT_EQ("int i;\nint j;", format(" int i;\n int j;")); |
| 98 | } |
| 99 | |
| 100 | TEST_F(FormatTest, FormatsUnwrappedLinesAtFirstFormat) { |
| 101 | EXPECT_EQ("int i;", format("int\ni;")); |
| 102 | } |
| 103 | |
| 104 | TEST_F(FormatTest, FormatsNestedBlockStatements) { |
| 105 | EXPECT_EQ("{\n {\n {\n }\n }\n}", format("{{{}}}")); |
| 106 | } |
| 107 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 108 | TEST_F(FormatTest, FormatsNestedCall) { |
| 109 | verifyFormat("Method(f1, f2(f3));"); |
| 110 | verifyFormat("Method(f1(f2, f3()));"); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | //===----------------------------------------------------------------------===// |
| 115 | // Tests for control statements. |
| 116 | //===----------------------------------------------------------------------===// |
| 117 | |
| 118 | TEST_F(FormatTest, FormatIfWithoutCompountStatement) { |
| 119 | verifyFormat("if (true)\n f();\ng();"); |
| 120 | verifyFormat("if (a)\n if (b)\n if (c)\n g();\nh();"); |
| 121 | verifyFormat("if (a)\n if (b) {\n f();\n }\ng();"); |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 122 | verifyFormat("if (a)\n" |
| 123 | " // comment\n" |
| 124 | " f();"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | TEST_F(FormatTest, ParseIfElse) { |
| 128 | verifyFormat("if (true)\n" |
| 129 | " if (true)\n" |
| 130 | " if (true)\n" |
| 131 | " f();\n" |
| 132 | " else\n" |
| 133 | " g();\n" |
| 134 | " else\n" |
| 135 | " h();\n" |
| 136 | "else\n" |
| 137 | " i();"); |
| 138 | verifyFormat("if (true)\n" |
| 139 | " if (true)\n" |
| 140 | " if (true) {\n" |
| 141 | " if (true)\n" |
| 142 | " f();\n" |
| 143 | " } else {\n" |
| 144 | " g();\n" |
| 145 | " }\n" |
| 146 | " else\n" |
| 147 | " h();\n" |
| 148 | "else {\n" |
| 149 | " i();\n" |
| 150 | "}"); |
| 151 | } |
| 152 | |
| 153 | TEST_F(FormatTest, ElseIf) { |
| 154 | verifyFormat("if (a) {\n" |
| 155 | "} else if (b) {\n" |
| 156 | "}"); |
| 157 | verifyFormat("if (a)\n" |
| 158 | " f();\n" |
| 159 | "else if (b)\n" |
| 160 | " g();\n" |
| 161 | "else\n" |
| 162 | " h();"); |
| 163 | } |
| 164 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 165 | TEST_F(FormatTest, FormatsForLoop) { |
| 166 | verifyFormat( |
| 167 | "for (int VeryVeryLongLoopVariable = 0; VeryVeryLongLoopVariable < 10;\n" |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 168 | " ++VeryVeryLongLoopVariable)\n" |
| 169 | " ;"); |
| 170 | verifyFormat("for (;;)\n" |
| 171 | " f();"); |
| 172 | verifyFormat("for (;;) {\n" |
| 173 | "}"); |
| 174 | verifyFormat("for (;;) {\n" |
| 175 | " f();\n" |
| 176 | "}"); |
Daniel Jasper | a324a0e | 2012-12-21 14:37:20 +0000 | [diff] [blame] | 177 | |
| 178 | verifyFormat( |
| 179 | "for (std::vector<UnwrappedLine>::iterator I = UnwrappedLines.begin(),\n" |
| 180 | " E = UnwrappedLines.end();\n" |
| 181 | " I != E; ++I) {\n}"); |
| 182 | |
| 183 | verifyFormat( |
| 184 | "for (MachineFun::iterator IIII = PrevIt, EEEE = F.end(); IIII != EEEE;\n" |
| 185 | " ++IIIII) {\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | TEST_F(FormatTest, FormatsWhileLoop) { |
| 189 | verifyFormat("while (true) {\n}"); |
Alexander Kornienko | 2e97cfc | 2012-12-05 15:06:06 +0000 | [diff] [blame] | 190 | verifyFormat("while (true)\n" |
| 191 | " f();"); |
| 192 | verifyFormat("while () {\n" |
| 193 | "}"); |
| 194 | verifyFormat("while () {\n" |
| 195 | " f();\n" |
| 196 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 199 | TEST_F(FormatTest, FormatsDoWhile) { |
| 200 | verifyFormat("do {\n" |
| 201 | " do_something();\n" |
| 202 | "} while (something());"); |
| 203 | verifyFormat("do\n" |
| 204 | " do_something();\n" |
| 205 | "while (something());"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 208 | TEST_F(FormatTest, FormatsSwitchStatement) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 209 | verifyFormat("switch (x) {\n" |
| 210 | "case 1:\n" |
| 211 | " f();\n" |
| 212 | " break;\n" |
| 213 | "case kFoo:\n" |
| 214 | "case ns::kBar:\n" |
| 215 | "case kBaz:\n" |
| 216 | " break;\n" |
| 217 | "default:\n" |
| 218 | " g();\n" |
| 219 | " break;\n" |
| 220 | "}"); |
| 221 | verifyFormat("switch (x) {\n" |
| 222 | "case 1: {\n" |
| 223 | " f();\n" |
| 224 | " break;\n" |
| 225 | "}\n" |
| 226 | "}"); |
| 227 | verifyFormat("switch (test)\n" |
| 228 | " ;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 229 | verifyGoogleFormat("switch (x) {\n" |
| 230 | " case 1:\n" |
| 231 | " f();\n" |
| 232 | " break;\n" |
| 233 | " case kFoo:\n" |
| 234 | " case ns::kBar:\n" |
| 235 | " case kBaz:\n" |
| 236 | " break;\n" |
| 237 | " default:\n" |
| 238 | " g();\n" |
| 239 | " break;\n" |
| 240 | "}"); |
| 241 | verifyGoogleFormat("switch (x) {\n" |
| 242 | " case 1: {\n" |
| 243 | " f();\n" |
| 244 | " break;\n" |
| 245 | " }\n" |
| 246 | "}"); |
| 247 | verifyGoogleFormat("switch (test)\n" |
| 248 | " ;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 251 | TEST_F(FormatTest, FormatsLabels) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 252 | verifyFormat("void f() {\n" |
| 253 | " some_code();\n" |
| 254 | "test_label:\n" |
| 255 | " some_other_code();\n" |
| 256 | " {\n" |
| 257 | " some_more_code();\n" |
| 258 | " another_label:\n" |
| 259 | " some_more_code();\n" |
| 260 | " }\n" |
| 261 | "}"); |
| 262 | verifyFormat("some_code();\n" |
| 263 | "test_label:\n" |
| 264 | "some_other_code();"); |
| 265 | } |
| 266 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 267 | |
| 268 | //===----------------------------------------------------------------------===// |
| 269 | // Tests for comments. |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
| 272 | TEST_F(FormatTest, UnderstandsSingleLineComments) { |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 273 | verifyFormat("// line 1\n" |
| 274 | "// line 2\n" |
| 275 | "void f() {\n}\n"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 276 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 277 | verifyFormat("void f() {\n" |
| 278 | " // Doesn't do anything\n" |
| 279 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 280 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 281 | verifyFormat("int i // This is a fancy variable\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 282 | " = 5;"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 283 | |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 284 | verifyFormat("enum E {\n" |
| 285 | " // comment\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 286 | " VAL_A, // comment\n" |
Daniel Jasper | e35ec2b | 2012-12-18 19:56:56 +0000 | [diff] [blame] | 287 | " VAL_B\n" |
| 288 | "};"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 289 | |
| 290 | verifyFormat( |
| 291 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 292 | " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb; // Trailing comment"); |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 293 | |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 294 | EXPECT_EQ("int i; // single line trailing comment", |
Manuel Klimek | 6cf5814 | 2013-01-07 08:54:53 +0000 | [diff] [blame] | 295 | format("int i;\\\n// single line trailing comment")); |
Daniel Jasper | 7ad4eff | 2013-01-07 11:09:06 +0000 | [diff] [blame] | 296 | |
| 297 | verifyGoogleFormat("int a; // Trailing comment."); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | TEST_F(FormatTest, UnderstandsMultiLineComments) { |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 301 | verifyFormat("f(/*test=*/ true);"); |
| 302 | } |
| 303 | |
| 304 | |
| 305 | //===----------------------------------------------------------------------===// |
| 306 | // Tests for classes, namespaces, etc. |
| 307 | //===----------------------------------------------------------------------===// |
| 308 | |
| 309 | TEST_F(FormatTest, DoesNotBreakSemiAfterClassDecl) { |
| 310 | verifyFormat("class A {\n};"); |
| 311 | } |
| 312 | |
| 313 | TEST_F(FormatTest, UnderstandsAccessSpecifiers) { |
| 314 | verifyFormat("class A {\n" |
| 315 | "public:\n" |
| 316 | "protected:\n" |
| 317 | "private:\n" |
| 318 | " void f() {\n" |
| 319 | " }\n" |
| 320 | "};"); |
| 321 | verifyGoogleFormat("class A {\n" |
| 322 | " public:\n" |
| 323 | " protected:\n" |
| 324 | " private:\n" |
| 325 | " void f() {\n" |
| 326 | " }\n" |
| 327 | "};"); |
| 328 | } |
| 329 | |
| 330 | TEST_F(FormatTest, FormatsDerivedClass) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 331 | verifyFormat("class A : public B {\n" |
| 332 | "};"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 333 | verifyFormat("class A : public ::B {\n" |
| 334 | "};"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 337 | TEST_F(FormatTest, FormatsEnum) { |
Alexander Kornienko | a166e73 | 2012-12-04 14:46:19 +0000 | [diff] [blame] | 338 | verifyFormat("enum {\n" |
| 339 | " Zero,\n" |
| 340 | " One = 1,\n" |
| 341 | " Two = One + 1,\n" |
| 342 | " Three = (One + Two),\n" |
| 343 | " Four = (Zero && (One ^ Two)) | (One << Two),\n" |
| 344 | " Five = (One, Two, Three, Four, 5)\n" |
| 345 | "};"); |
| 346 | verifyFormat("enum Enum {\n" |
| 347 | "};"); |
| 348 | verifyFormat("enum {\n" |
| 349 | "};"); |
| 350 | } |
| 351 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 352 | TEST_F(FormatTest, FormatsNamespaces) { |
| 353 | verifyFormat("namespace some_namespace {\n" |
| 354 | "class A {\n" |
| 355 | "};\n" |
| 356 | "void f() {\n" |
| 357 | " f();\n" |
| 358 | "}\n" |
| 359 | "}"); |
| 360 | verifyFormat("namespace {\n" |
| 361 | "class A {\n" |
| 362 | "};\n" |
| 363 | "void f() {\n" |
| 364 | " f();\n" |
| 365 | "}\n" |
| 366 | "}"); |
Dmitri Gribenko | 1f94f2b | 2012-12-30 21:27:25 +0000 | [diff] [blame] | 367 | verifyFormat("inline namespace X {\n" |
| 368 | "class A {\n" |
| 369 | "};\n" |
| 370 | "void f() {\n" |
| 371 | " f();\n" |
| 372 | "}\n" |
| 373 | "}"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 374 | verifyFormat("using namespace some_namespace;\n" |
| 375 | "class A {\n" |
| 376 | "};\n" |
| 377 | "void f() {\n" |
| 378 | " f();\n" |
| 379 | "}"); |
| 380 | } |
| 381 | |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 382 | TEST_F(FormatTest, StaticInitializers) { |
| 383 | verifyFormat("static SomeClass SC = { 1, 'a' };"); |
| 384 | |
| 385 | // FIXME: Format like enums if the static initializer does not fit on a line. |
| 386 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame^] | 387 | "static SomeClass WithALoooooooooooooooooooongName = {\n" |
| 388 | " 100000000, \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\" };"); |
Daniel Jasper | 05b1ac8 | 2012-12-17 11:29:41 +0000 | [diff] [blame] | 389 | } |
| 390 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 391 | TEST_F(FormatTest, FormatsSmallMacroDefinitionsInSingleLine) { |
| 392 | verifyFormat("#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 393 | " \\\n" |
| 394 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)"); |
| 395 | } |
| 396 | |
Daniel Jasper | 7160751 | 2013-01-07 10:48:50 +0000 | [diff] [blame] | 397 | TEST_F(FormatTest, DoesNotBreakPureVirtualFunctionDefinition) { |
| 398 | verifyFormat( |
| 399 | "virtual void write(ELFWriter *writerrr,\n" |
| 400 | " OwningPtr<FileOutputBuffer> &buffer) = 0;"); |
| 401 | } |
| 402 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 403 | TEST_F(FormatTest, BreaksOnHashWhenDirectiveIsInvalid) { |
| 404 | EXPECT_EQ("#\n;", format("#;")); |
Manuel Klimek | dd5b101 | 2013-01-07 10:03:37 +0000 | [diff] [blame] | 405 | verifyFormat("#\n;\n;\n;"); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | TEST_F(FormatTest, UnescapedEndOfLineEndsPPDirective) { |
| 409 | EXPECT_EQ("#line 42 \"test\"\n", |
| 410 | format("# \\\n line \\\n 42 \\\n \"test\"\n")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 411 | EXPECT_EQ("#define A \\\n B\n", |
| 412 | format("# \\\n define \\\n A \\\n B\n", |
| 413 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | TEST_F(FormatTest, EndOfFileEndsPPDirective) { |
| 417 | EXPECT_EQ("#line 42 \"test\"", |
| 418 | format("# \\\n line \\\n 42 \\\n \"test\"")); |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 419 | EXPECT_EQ("#define A \\\n B", |
| 420 | format("# \\\n define \\\n A \\\n B", |
| 421 | getLLVMStyleWithColumns(12))); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 424 | TEST_F(FormatTest, IndentsPPDirectiveInReducedSpace) { |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 425 | // If the macro fits in one line, we still do not get the full |
| 426 | // line, as only the next line decides whether we need an escaped newline and |
| 427 | // thus use the last column. |
| 428 | verifyFormat("#define A(B)", getLLVMStyleWithColumns(13)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 429 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 430 | verifyFormat("#define A( \\\n B)", getLLVMStyleWithColumns(12)); |
| 431 | verifyFormat("#define AA(\\\n B)", getLLVMStyleWithColumns(12)); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 432 | verifyFormat("#define A( \\\n A, B)", getLLVMStyleWithColumns(12)); |
| 433 | } |
| 434 | |
| 435 | TEST_F(FormatTest, HandlePreprocessorDirectiveContext) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 436 | EXPECT_EQ("// some comment\n" |
| 437 | "#include \"a.h\"\n" |
| 438 | "#define A(A,\\\n" |
| 439 | " B)\n" |
| 440 | "#include \"b.h\"\n" |
| 441 | "// some comment\n", |
| 442 | format(" // some comment\n" |
| 443 | " #include \"a.h\"\n" |
| 444 | "#define A(A,\\\n" |
| 445 | " B)\n" |
| 446 | " #include \"b.h\"\n" |
| 447 | " // some comment\n", getLLVMStyleWithColumns(13))); |
Manuel Klimek | 060143e | 2013-01-02 18:33:23 +0000 | [diff] [blame] | 448 | } |
| 449 | |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 450 | TEST_F(FormatTest, LayoutSingleHash) { |
| 451 | EXPECT_EQ("#\na;", format("#\na;")); |
| 452 | } |
| 453 | |
| 454 | TEST_F(FormatTest, LayoutCodeInMacroDefinitions) { |
| 455 | EXPECT_EQ("#define A \\\n" |
| 456 | " c; \\\n" |
| 457 | " e;\n" |
| 458 | "f;", format("#define A c; e;\n" |
| 459 | "f;", getLLVMStyleWithColumns(14))); |
| 460 | } |
| 461 | |
| 462 | TEST_F(FormatTest, LayoutRemainingTokens) { |
| 463 | EXPECT_EQ("{\n}", format("{}")); |
| 464 | } |
| 465 | |
| 466 | TEST_F(FormatTest, LayoutSingleUnwrappedLineInMacro) { |
| 467 | EXPECT_EQ("#define A \\\n b;", |
| 468 | format("#define A b;", 10, 2, getLLVMStyleWithColumns(11))); |
| 469 | } |
| 470 | |
| 471 | TEST_F(FormatTest, MacroDefinitionInsideStatement) { |
| 472 | EXPECT_EQ("int x,\n#define A\ny;", format("int x,\n#define A\ny;")); |
| 473 | } |
| 474 | |
Manuel Klimek | 6f8424b | 2013-01-05 21:34:55 +0000 | [diff] [blame] | 475 | TEST_F(FormatTest, HashInMacroDefinition) { |
| 476 | verifyFormat("#define A \\\n b #c;", getLLVMStyleWithColumns(11)); |
| 477 | verifyFormat("#define A \\\n" |
| 478 | " { \\\n" |
| 479 | " f(#c);\\\n" |
| 480 | " }", getLLVMStyleWithColumns(11)); |
| 481 | } |
| 482 | |
Manuel Klimek | c37b4d6 | 2013-01-05 22:14:16 +0000 | [diff] [blame] | 483 | TEST_F(FormatTest, IndentPreprocessorDirectivesAtZero) { |
| 484 | EXPECT_EQ("{\n {\n#define A\n }\n}", format("{{\n#define A\n}}")); |
| 485 | } |
| 486 | |
Manuel Klimek | f6fd00b | 2013-01-05 22:56:06 +0000 | [diff] [blame] | 487 | TEST_F(FormatTest, FormatHashIfNotAtStartOfLine) { |
| 488 | verifyFormat("{\n {\n a #c;\n }\n}"); |
| 489 | } |
| 490 | |
Manuel Klimek | a5342db | 2013-01-06 20:07:31 +0000 | [diff] [blame] | 491 | TEST_F(FormatTest, FormatUnbalancedStructuralElements) { |
| 492 | EXPECT_EQ("#define A \\\n { \\\n {\nint i;", |
| 493 | format("#define A { {\nint i;", getLLVMStyleWithColumns(11))); |
| 494 | EXPECT_EQ("#define A \\\n } \\\n }\nint i;", |
| 495 | format("#define A } }\nint i;", getLLVMStyleWithColumns(11))); |
| 496 | } |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 497 | |
Manuel Klimek | 9541938 | 2013-01-07 07:56:50 +0000 | [diff] [blame] | 498 | TEST_F(FormatTest, EscapedNewlineAtStartOfTokenInMacroDefinition) { |
| 499 | EXPECT_EQ("#define A \\\n int i; \\\n int j;", |
| 500 | format("#define A \\\nint i;\\\n int j;", |
| 501 | getLLVMStyleWithColumns(11))); |
| 502 | } |
| 503 | |
Manuel Klimek | d544c57 | 2013-01-07 09:24:17 +0000 | [diff] [blame] | 504 | TEST_F(FormatTest, CalculateSpaceOnConsecutiveLinesInMacro) { |
| 505 | verifyFormat("#define A \\\n" |
| 506 | " int v( \\\n" |
| 507 | " a); \\\n" |
| 508 | " int i;", getLLVMStyleWithColumns(11)); |
| 509 | } |
| 510 | |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 511 | TEST_F(FormatTest, MixingPreprocessorDirectivesAndNormalCode) { |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 512 | EXPECT_EQ( |
| 513 | "#define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 514 | " \\\n" |
| 515 | " aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 516 | "\n" |
| 517 | "AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 518 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n", |
| 519 | format(" #define ALooooooooooooooooooooooooooooooooooooooongMacro(" |
| 520 | "\\\n" |
| 521 | "aLoooooooooooooooooooooooongFuuuuuuuuuuuuuunctiooooooooo)\n" |
| 522 | " \n" |
| 523 | " AlooooooooooooooooooooooooooooooooooooooongCaaaaaaaaaal(\n" |
| 524 | " aLooooooooooooooooooooooonPaaaaaaaaaaaaaaaaaaaaarmmmm);\n")); |
Manuel Klimek | a080a18 | 2013-01-02 16:30:12 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 527 | //===----------------------------------------------------------------------===// |
| 528 | // Line break tests. |
| 529 | //===----------------------------------------------------------------------===// |
| 530 | |
| 531 | TEST_F(FormatTest, FormatsFunctionDefinition) { |
| 532 | verifyFormat("void f(int a, int b, int c, int d, int e, int f, int g," |
| 533 | " int h, int j, int f,\n" |
| 534 | " int c, int ddddddddddddd) {\n" |
| 535 | "}"); |
| 536 | } |
| 537 | |
| 538 | TEST_F(FormatTest, FormatsAwesomeMethodCall) { |
| 539 | verifyFormat( |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 540 | "SomeLongMethodName(SomeReallyLongMethod(\n" |
| 541 | " CallOtherReallyLongMethod(parameter, parameter, parameter)),\n" |
| 542 | " SecondLongCall(parameter));"); |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 545 | TEST_F(FormatTest, ConstructorInitializers) { |
| 546 | verifyFormat("Constructor() : Initializer(FitsOnTheLine) {\n}"); |
| 547 | |
| 548 | verifyFormat( |
| 549 | "SomeClass::Constructor()\n" |
| 550 | " : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 551 | "}"); |
| 552 | |
| 553 | verifyFormat( |
| 554 | "SomeClass::Constructor()\n" |
| 555 | " : aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 556 | " aaaaaaaaaaaaaaa(aaaaaaaaaaaa) {\n" |
| 557 | "}"); |
| 558 | |
| 559 | verifyFormat("Constructor()\n" |
| 560 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 561 | " aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 562 | " aaaaaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 563 | " aaaaaaaaaaaaaaaaaaaaaaa() {\n" |
| 564 | "}"); |
| 565 | |
| 566 | // Here a line could be saved by splitting the second initializer onto two |
| 567 | // lines, but that is not desireable. |
| 568 | verifyFormat("Constructor()\n" |
| 569 | " : aaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaa),\n" |
| 570 | " aaaaaaaaaaa(aaaaaaaaaaa),\n" |
| 571 | " aaaaaaaaaaaaaaaaaaaaat(aaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 572 | "}"); |
| 573 | |
| 574 | verifyGoogleFormat("MyClass::MyClass(int var)\n" |
| 575 | " : some_var_(var), // 4 space indent\n" |
| 576 | " some_other_var_(var + 1) { // lined up\n" |
| 577 | "}"); |
| 578 | } |
| 579 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 580 | TEST_F(FormatTest, BreaksAsHighAsPossible) { |
| 581 | verifyFormat( |
| 582 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa && aaaaaaaaaaaaaaaaaaaaaaaaaa) ||\n" |
| 583 | " (bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb && bbbbbbbbbbbbbbbbbbbbbbbbbb))\n" |
| 584 | " f();"); |
| 585 | } |
| 586 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 587 | TEST_F(FormatTest, BreaksDesireably) { |
| 588 | verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 589 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" |
| 590 | " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa)) {\n};"); |
| 591 | |
| 592 | verifyFormat( |
| 593 | "aaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 594 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n}"); |
| 595 | |
| 596 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 597 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 598 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa));"); |
Daniel Jasper | a88bb45 | 2012-12-04 10:50:12 +0000 | [diff] [blame] | 599 | |
| 600 | verifyFormat( |
| 601 | "aaaaaaaa(aaaaaaaaaaaaa, aaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 602 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)),\n" |
| 603 | " aaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" |
| 604 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)));"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 605 | |
| 606 | verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 607 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 608 | |
Daniel Jasper | 723f030 | 2013-01-02 14:40:02 +0000 | [diff] [blame] | 609 | verifyFormat( |
| 610 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa &&\n" |
| 611 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 612 | |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 613 | // This test case breaks on an incorrect memoization, i.e. an optimization not |
| 614 | // taking into account the StopAt value. |
| 615 | verifyFormat( |
| 616 | "return aaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 617 | " aaaaaaaaaaa(aaaaaaaaa) || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 618 | " aaaaaaaaaaaaaaaaaaaaaaaaa || aaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 619 | " (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | a4974cf | 2012-12-24 16:43:00 +0000 | [diff] [blame] | 620 | |
| 621 | verifyFormat( |
| 622 | "{\n {\n {\n" |
| 623 | " Annotation.SpaceRequiredBefore =\n" |
| 624 | " Line.Tokens[i - 1].Tok.isNot(tok::l_paren) &&\n" |
| 625 | " Line.Tokens[i - 1].Tok.isNot(tok::l_square);\n" |
| 626 | " }\n }\n}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 627 | } |
| 628 | |
Daniel Jasper | e2c7acf | 2012-12-24 00:13:23 +0000 | [diff] [blame] | 629 | TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { |
| 630 | verifyFormat( |
| 631 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa ||\n" |
| 632 | " bbbbbbbbbbbbbbbbbbbbbbbbb && ccccccccccccccccccccccccc) {\n}"); |
| 633 | verifyFormat( |
| 634 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa && bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 635 | " ccccccccccccccccccccccccc) {\n}"); |
| 636 | verifyFormat( |
| 637 | "if (aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb ||\n" |
| 638 | " ccccccccccccccccccccccccc) {\n}"); |
| 639 | verifyFormat( |
| 640 | "if ((aaaaaaaaaaaaaaaaaaaaaaaaa || bbbbbbbbbbbbbbbbbbbbbbbbb) &&\n" |
| 641 | " ccccccccccccccccccccccccc) {\n}"); |
| 642 | } |
| 643 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame^] | 644 | TEST_F(FormatTest, PrefersNotToBreakAfterAssignments) { |
| 645 | verifyFormat( |
| 646 | "unsigned Cost = TTI.getMemoryOpCost(I->getOpcode(), VectorTy,\n" |
| 647 | " SI->getAlignment(),\n" |
| 648 | " SI->getPointerAddressSpaceee());\n"); |
| 649 | verifyFormat( |
| 650 | "CharSourceRange LineRange = CharSourceRange::getTokenRange(\n" |
| 651 | " Line.Tokens.front().Tok.getLocation(),\n" |
| 652 | " Line.Tokens.back().Tok.getLocation());"); |
| 653 | } |
| 654 | |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 655 | TEST_F(FormatTest, AlignsAfterAssignments) { |
| 656 | verifyFormat( |
| 657 | "int Result = aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame^] | 658 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 659 | verifyFormat( |
| 660 | "Result += aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame^] | 661 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 662 | verifyFormat( |
| 663 | "Result >>= aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame^] | 664 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 665 | verifyFormat( |
| 666 | "int Result = (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame^] | 667 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 668 | verifyFormat( |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame^] | 669 | "double LooooooooooooooooooooooooongResult = aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 670 | " aaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 671 | " aaaaaaaaaaaaaaaaaaaaaaaa;"); |
Daniel Jasper | cf225b6 | 2012-12-24 13:43:52 +0000 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | TEST_F(FormatTest, AlignsAfterReturn) { |
| 675 | verifyFormat( |
| 676 | "return aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 677 | " aaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 678 | verifyFormat( |
| 679 | "return (aaaaaaaaaaaaaaaaaaaaaaaaa + aaaaaaaaaaaaaaaaaaaaaaaaa +\n" |
| 680 | " aaaaaaaaaaaaaaaaaaaaaaaaa);"); |
| 681 | } |
| 682 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 683 | TEST_F(FormatTest, AlignsStringLiterals) { |
| 684 | verifyFormat("loooooooooooooooooooooooooongFunction(\"short literal \"\n" |
| 685 | " \"short literal\");"); |
| 686 | verifyFormat( |
| 687 | "looooooooooooooooooooooooongFunction(\n" |
| 688 | " \"short literal\"\n" |
| 689 | " \"looooooooooooooooooooooooooooooooooooooooooooooooong literal\");"); |
| 690 | } |
| 691 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 692 | TEST_F(FormatTest, AlignsPipes) { |
| 693 | verifyFormat( |
| 694 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 695 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 696 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 697 | verifyFormat( |
| 698 | "aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaa\n" |
| 699 | " << aaaaaaaaaaaaaaaaaaaa;"); |
| 700 | verifyFormat( |
| 701 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 702 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 703 | verifyFormat( |
| 704 | "llvm::outs() << \"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" |
| 705 | " \"bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb\"\n" |
| 706 | " << \"ccccccccccccccccccccccccccccccccccccccccccccccccc\";"); |
| 707 | verifyFormat( |
| 708 | "aaaaaaaa << (aaaaaaaaaaaaaaaaaaa << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" |
| 709 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" |
| 710 | " << aaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 711 | } |
| 712 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 713 | TEST_F(FormatTest, UnderstandsEquals) { |
| 714 | verifyFormat( |
| 715 | "aaaaaaaaaaaaaaaaa =\n" |
| 716 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;"); |
| 717 | verifyFormat( |
| 718 | "if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 719 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 720 | "}"); |
| 721 | verifyFormat( |
| 722 | "if (a) {\n" |
| 723 | "} else if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa =\n" |
| 724 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 725 | "}"); |
| 726 | |
Daniel Jasper | 9cda800 | 2013-01-07 13:08:40 +0000 | [diff] [blame^] | 727 | verifyFormat( |
| 728 | "if (int aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa = 100000000 +\n" |
| 729 | " 10000000) {\n" |
| 730 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 733 | TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 734 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 735 | " .looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 736 | |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 737 | verifyFormat("LoooooooooooooooooooooooooooooooooooooongObject\n" |
| 738 | " ->looooooooooooooooooooooooooooooooooooooongFunction();"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 739 | |
| 740 | verifyFormat( |
| 741 | "LooooooooooooooooooooooooooooooooongObject->shortFunction(Parameter1,\n" |
| 742 | " Parameter2);"); |
| 743 | |
| 744 | verifyFormat( |
| 745 | "ShortObject->shortFunction(\n" |
| 746 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter1,\n" |
| 747 | " LooooooooooooooooooooooooooooooooooooooooooooooongParameter2);"); |
| 748 | |
| 749 | verifyFormat("loooooooooooooongFunction(\n" |
| 750 | " LoooooooooooooongObject->looooooooooooooooongFunction());"); |
| 751 | |
| 752 | verifyFormat( |
| 753 | "function(LoooooooooooooooooooooooooooooooooooongObject\n" |
| 754 | " ->loooooooooooooooooooooooooooooooooooooooongFunction());"); |
| 755 | |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 756 | // Here, it is not necessary to wrap at "." or "->". |
Daniel Jasper | 1321eb5 | 2012-12-18 21:05:13 +0000 | [diff] [blame] | 757 | verifyFormat("if (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaa) ||\n" |
| 758 | " aaaa.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) {\n" |
| 759 | "}"); |
Daniel Jasper | 46a46a2 | 2013-01-07 07:13:20 +0000 | [diff] [blame] | 760 | verifyFormat( |
| 761 | "aaaaaaaaaaa->aaaaaaaaa(\n" |
| 762 | " aaaaaaaaaaaaaaaaaaaaaaaaa,\n" |
| 763 | " aaaaaaaaaaaaaaaaaa->aaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaa));\n"); |
Daniel Jasper | 9a0b494 | 2012-12-17 14:34:14 +0000 | [diff] [blame] | 764 | } |
| 765 | |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 766 | TEST_F(FormatTest, WrapsTemplateDeclarations) { |
| 767 | verifyFormat("template <typename T>\n" |
| 768 | "virtual void loooooooooooongFunction(int Param1, int Param2);"); |
| 769 | verifyFormat( |
| 770 | "template <typename T> void f(int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram1,\n" |
| 771 | " int Paaaaaaaaaaaaaaaaaaaaaaaaaaaaaaram2);"); |
| 772 | verifyFormat( |
| 773 | "template <typename T>\n" |
| 774 | "void looooooooooooooooooooongFunction(int Paaaaaaaaaaaaaaaaaaaaram1,\n" |
| 775 | " int Paaaaaaaaaaaaaaaaaaaaram2);"); |
Daniel Jasper | 5eda31e | 2013-01-02 18:30:06 +0000 | [diff] [blame] | 776 | verifyFormat( |
| 777 | "template <typename T>\n" |
| 778 | "aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaa,\n" |
| 779 | " aaaaaaaaaaaaaaaaaaaaaaaaaa<T>::aaaaaaaaaa,\n" |
| 780 | " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);"); |
Daniel Jasper | 9a64fb5 | 2013-01-02 15:08:56 +0000 | [diff] [blame] | 781 | |
| 782 | } |
| 783 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 784 | TEST_F(FormatTest, UnderstandsTemplateParameters) { |
| 785 | verifyFormat("A<int> a;"); |
| 786 | verifyFormat("A<A<A<int> > > a;"); |
| 787 | verifyFormat("A<A<A<int, 2>, 3>, 4> a;"); |
| 788 | verifyFormat("bool x = a < 1 || 2 > a;"); |
| 789 | verifyFormat("bool x = 5 < f<int>();"); |
| 790 | verifyFormat("bool x = f<int>() > 5;"); |
| 791 | verifyFormat("bool x = 5 < a<int>::x;"); |
| 792 | verifyFormat("bool x = a < 4 ? a > 2 : false;"); |
| 793 | verifyFormat("bool x = f() ? a < 2 : a > 2;"); |
| 794 | |
| 795 | verifyGoogleFormat("A<A<int>> a;"); |
| 796 | verifyGoogleFormat("A<A<A<int>>> a;"); |
| 797 | verifyGoogleFormat("A<A<A<A<int>>>> a;"); |
| 798 | |
| 799 | verifyFormat("test >> a >> b;"); |
| 800 | verifyFormat("test << a >> b;"); |
| 801 | |
| 802 | verifyFormat("f<int>();"); |
| 803 | verifyFormat("template <typename T> void f() {\n}"); |
| 804 | } |
| 805 | |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 806 | TEST_F(FormatTest, UnderstandsUnaryOperators) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 807 | verifyFormat("int a = -2;"); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 808 | verifyFormat("f(-1, -2, -3);"); |
| 809 | verifyFormat("a[-1] = 5;"); |
| 810 | verifyFormat("int a = 5 + -2;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 811 | verifyFormat("if (i == -1) {\n}"); |
| 812 | verifyFormat("if (i != -1) {\n}"); |
| 813 | verifyFormat("if (i > -1) {\n}"); |
| 814 | verifyFormat("if (i < -1) {\n}"); |
Daniel Jasper | d56a737 | 2012-12-06 13:16:39 +0000 | [diff] [blame] | 815 | verifyFormat("++(a->f());"); |
| 816 | verifyFormat("--(a->f());"); |
| 817 | verifyFormat("if (!(a->f())) {\n}"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 818 | |
| 819 | verifyFormat("a-- > b;"); |
| 820 | verifyFormat("b ? -a : c;"); |
| 821 | verifyFormat("n * sizeof char16;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 822 | verifyFormat("n * alignof char16;"); |
Daniel Jasper | 98e6b4a | 2012-12-21 09:41:31 +0000 | [diff] [blame] | 823 | verifyFormat("sizeof(char);"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 824 | verifyFormat("alignof(char);"); |
Daniel Jasper | 1f0754b | 2013-01-02 15:26:16 +0000 | [diff] [blame] | 825 | |
| 826 | verifyFormat("return -1;"); |
| 827 | verifyFormat("switch (a) {\n" |
| 828 | "case -1:\n" |
| 829 | " break;\n" |
| 830 | "}"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | TEST_F(FormatTest, UndestandsOverloadedOperators) { |
Daniel Jasper | f6aef6a | 2012-12-24 10:56:04 +0000 | [diff] [blame] | 834 | verifyFormat("bool operator<();"); |
| 835 | verifyFormat("bool operator>();"); |
| 836 | verifyFormat("bool operator=();"); |
| 837 | verifyFormat("bool operator==();"); |
| 838 | verifyFormat("bool operator!=();"); |
| 839 | verifyFormat("int operator+();"); |
| 840 | verifyFormat("int operator++();"); |
| 841 | verifyFormat("bool operator();"); |
| 842 | verifyFormat("bool operator()();"); |
| 843 | verifyFormat("bool operator[]();"); |
| 844 | verifyFormat("operator bool();"); |
| 845 | verifyFormat("operator SomeType<int>();"); |
| 846 | verifyFormat("void *operator new(std::size_t size);"); |
| 847 | verifyFormat("void *operator new[](std::size_t size);"); |
| 848 | verifyFormat("void operator delete(void *ptr);"); |
| 849 | verifyFormat("void operator delete[](void *ptr);"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 850 | } |
| 851 | |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 852 | TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) { |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 853 | verifyFormat("int *f(int *a) {\n}"); |
| 854 | verifyFormat("f(a, *a);"); |
| 855 | verifyFormat("f(*a);"); |
| 856 | verifyFormat("int a = b * 10;"); |
| 857 | verifyFormat("int a = 10 * b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 858 | verifyFormat("int a = b * c;"); |
Daniel Jasper | 33182dd | 2012-12-05 14:57:28 +0000 | [diff] [blame] | 859 | verifyFormat("int a += b * c;"); |
| 860 | verifyFormat("int a -= b * c;"); |
| 861 | verifyFormat("int a *= b * c;"); |
| 862 | verifyFormat("int a /= b * c;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 863 | verifyFormat("int a = *b;"); |
Daniel Jasper | 112fb27 | 2012-12-05 07:51:39 +0000 | [diff] [blame] | 864 | verifyFormat("int a = *b * c;"); |
| 865 | verifyFormat("int a = b * *c;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 866 | verifyFormat("int main(int argc, char **argv) {\n}"); |
Nico Weber | 00d5a04 | 2012-12-23 01:07:46 +0000 | [diff] [blame] | 867 | verifyFormat("return 10 * b;"); |
| 868 | verifyFormat("return *b * *c;"); |
| 869 | verifyFormat("return a & ~b;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 870 | verifyFormat("f(b ? *c : *d);"); |
| 871 | verifyFormat("int a = b ? *c : *d;"); |
Daniel Jasper | ba3d307 | 2013-01-02 17:21:36 +0000 | [diff] [blame] | 872 | verifyFormat("*b = a;"); |
| 873 | verifyFormat("a * ~b;"); |
| 874 | verifyFormat("a * !b;"); |
| 875 | verifyFormat("a * +b;"); |
| 876 | verifyFormat("a * -b;"); |
| 877 | verifyFormat("a * ++b;"); |
| 878 | verifyFormat("a * --b;"); |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 879 | |
Daniel Jasper | 9bb0d28 | 2013-01-04 20:46:38 +0000 | [diff] [blame] | 880 | verifyFormat("InvalidRegions[*R] = 0;"); |
| 881 | |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 882 | // FIXME: Is this desired for LLVM? Fix if not. |
| 883 | verifyFormat("A<int *> a;"); |
| 884 | verifyFormat("A<int **> a;"); |
| 885 | verifyFormat("A<int *, int *> a;"); |
| 886 | verifyFormat("A<int **, int **> a;"); |
Daniel Jasper | ef5b9c3 | 2013-01-02 15:46:59 +0000 | [diff] [blame] | 887 | verifyFormat("Type *A = static_cast<Type *>(P);"); |
| 888 | verifyFormat("Type *A = (Type *) P;"); |
| 889 | verifyFormat("Type *A = (vector<Type *, int *>) P;"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 890 | |
Daniel Jasper | c74e279 | 2012-12-07 09:52:15 +0000 | [diff] [blame] | 891 | verifyGoogleFormat("int main(int argc, char** argv) {\n}"); |
Daniel Jasper | 8b39c66 | 2012-12-10 18:59:13 +0000 | [diff] [blame] | 892 | verifyGoogleFormat("A<int*> a;"); |
| 893 | verifyGoogleFormat("A<int**> a;"); |
| 894 | verifyGoogleFormat("A<int*, int*> a;"); |
| 895 | verifyGoogleFormat("A<int**, int**> a;"); |
Daniel Jasper | 5d33440 | 2013-01-02 08:57:10 +0000 | [diff] [blame] | 896 | verifyGoogleFormat("f(b ? *c : *d);"); |
| 897 | verifyGoogleFormat("int a = b ? *c : *d;"); |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 898 | } |
| 899 | |
Daniel Jasper | 4dc41de | 2013-01-02 08:44:14 +0000 | [diff] [blame] | 900 | TEST_F(FormatTest, DoesNotBreakBeforePointerOrReference) { |
| 901 | verifyFormat( |
| 902 | "int *someFunction(int LoooooooooooooooongParam1,\n" |
| 903 | " int LoooooooooooooooongParam2) {\n}"); |
| 904 | verifyFormat( |
| 905 | "TypeSpecDecl *TypeSpecDecl::Create(ASTContext &C, DeclContext *DC,\n" |
| 906 | " SourceLocation L, IdentifierIn *II,\n" |
| 907 | " Type *T) {\n}"); |
| 908 | } |
| 909 | |
Daniel Jasper | 3b5943f | 2012-12-06 09:56:08 +0000 | [diff] [blame] | 910 | TEST_F(FormatTest, LineStartsWithSpecialCharacter) { |
| 911 | verifyFormat("(a)->b();"); |
| 912 | verifyFormat("--a;"); |
| 913 | } |
| 914 | |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 915 | TEST_F(FormatTest, HandlesIncludeDirectives) { |
| 916 | EXPECT_EQ("#include <string>\n", format("#include <string>\n")); |
Daniel Jasper | cd1a32b | 2012-12-21 17:58:39 +0000 | [diff] [blame] | 917 | EXPECT_EQ("#include <a/b/c.h>\n", format("#include <a/b/c.h>\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 918 | EXPECT_EQ("#include \"a/b/string\"\n", format("#include \"a/b/string\"\n")); |
| 919 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
| 920 | EXPECT_EQ("#include \"string.h\"\n", format("#include \"string.h\"\n")); |
Nico Weber | b23ae0c | 2012-12-21 18:21:56 +0000 | [diff] [blame] | 921 | |
| 922 | EXPECT_EQ("#import <string>\n", format("#import <string>\n")); |
| 923 | EXPECT_EQ("#import <a/b/c.h>\n", format("#import <a/b/c.h>\n")); |
| 924 | EXPECT_EQ("#import \"a/b/string\"\n", format("#import \"a/b/string\"\n")); |
| 925 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
| 926 | EXPECT_EQ("#import \"string.h\"\n", format("#import \"string.h\"\n")); |
Daniel Jasper | 8822d3a | 2012-12-04 13:02:32 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Alexander Kornienko | 1575731 | 2012-12-06 18:03:27 +0000 | [diff] [blame] | 929 | |
| 930 | //===----------------------------------------------------------------------===// |
| 931 | // Error recovery tests. |
| 932 | //===----------------------------------------------------------------------===// |
| 933 | |
Alexander Kornienko | 56e49c5 | 2012-12-10 16:34:48 +0000 | [diff] [blame] | 934 | TEST_F(FormatTest, IncorrectAccessSpecifier) { |
| 935 | verifyFormat("public:"); |
| 936 | verifyFormat("class A {\n" |
| 937 | "public\n" |
| 938 | " void f() {\n" |
| 939 | " }\n" |
| 940 | "};"); |
| 941 | verifyFormat("public\n" |
| 942 | "int qwerty;"); |
| 943 | verifyFormat("public\n" |
| 944 | "B {\n" |
| 945 | "};"); |
| 946 | verifyFormat("public\n" |
| 947 | "{\n" |
| 948 | "};"); |
| 949 | verifyFormat("public\n" |
| 950 | "B {\n" |
| 951 | " int x;\n" |
| 952 | "};"); |
| 953 | } |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 954 | |
Alexander Kornienko | 393b008 | 2012-12-04 15:40:36 +0000 | [diff] [blame] | 955 | TEST_F(FormatTest, IncorrectCodeUnbalancedBraces) { |
| 956 | verifyFormat("{"); |
| 957 | } |
| 958 | |
| 959 | TEST_F(FormatTest, IncorrectCodeDoNoWhile) { |
| 960 | verifyFormat("do {\n" |
| 961 | "};"); |
| 962 | verifyFormat("do {\n" |
| 963 | "};\n" |
| 964 | "f();"); |
| 965 | verifyFormat("do {\n" |
| 966 | "}\n" |
| 967 | "wheeee(fun);"); |
| 968 | verifyFormat("do {\n" |
| 969 | " f();\n" |
| 970 | "};"); |
| 971 | } |
| 972 | |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 973 | TEST_F(FormatTest, DoesNotTouchUnwrappedLinesWithErrors) { |
| 974 | verifyFormat("namespace {\n" |
Manuel Klimek | d4397b9 | 2013-01-04 23:34:14 +0000 | [diff] [blame] | 975 | "class Foo { Foo ( }; } // comment"); |
Daniel Jasper | 1f42f11 | 2013-01-04 18:52:56 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 978 | TEST_F(FormatTest, IncorrectCodeErrorDetection) { |
| 979 | EXPECT_EQ("{\n{\n}\n", format("{\n{\n}\n")); |
| 980 | EXPECT_EQ("{\n {\n}\n", format("{\n {\n}\n")); |
| 981 | EXPECT_EQ("{\n {\n }\n", format("{\n {\n }\n")); |
Alexander Kornienko | a3a2b3a | 2012-12-06 17:49:17 +0000 | [diff] [blame] | 982 | EXPECT_EQ("{\n {\n }\n }\n}\n", format("{\n {\n }\n }\n}\n")); |
Alexander Kornienko | cff563c | 2012-12-04 17:27:50 +0000 | [diff] [blame] | 983 | |
| 984 | FormatStyle Style = getLLVMStyle(); |
| 985 | Style.ColumnLimit = 10; |
| 986 | EXPECT_EQ("{\n" |
| 987 | " {\n" |
| 988 | " breakme(\n" |
| 989 | " qwe);\n" |
| 990 | "}\n", format("{\n" |
| 991 | " {\n" |
| 992 | " breakme(qwe);\n" |
| 993 | "}\n", Style)); |
| 994 | |
| 995 | } |
| 996 | |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 997 | TEST_F(FormatTest, FormatForObjectiveCMethodDecls) { |
| 998 | verifyFormat("- (void)sendAction:(SEL)aSelector to:(BOOL)anObject;"); |
| 999 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject;", |
| 1000 | format("-(NSUInteger)indexOfObject:(id)anObject;")); |
| 1001 | EXPECT_EQ("- (NSInteger)Mthod1;", |
| 1002 | format("-(NSInteger)Mthod1;")); |
| 1003 | EXPECT_EQ("+ (id)Mthod2;", format("+(id)Mthod2;")); |
| 1004 | EXPECT_EQ("- (NSInteger)Method3:(id)anObject;", |
| 1005 | format("-(NSInteger)Method3:(id)anObject;")); |
| 1006 | EXPECT_EQ("- (NSInteger)Method4:(id)anObject;", |
| 1007 | format("-(NSInteger)Method4:(id)anObject;")); |
| 1008 | EXPECT_EQ("- (NSInteger)Method5:(id)anObject:(id)AnotherObject;", |
| 1009 | format("-(NSInteger)Method5:(id)anObject:(id)AnotherObject;")); |
| 1010 | EXPECT_EQ("- (id)Method6:(id)A:(id)B:(id)C:(id)D;", |
| 1011 | format("- (id)Method6:(id)A:(id)B:(id)C:(id)D;")); |
| 1012 | EXPECT_EQ("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;", |
| 1013 | format("- (void)sendAction:(SEL)aSelector to:(id)anObject forAllCells:(BOOL)flag;")); |
Fariborz Jahanian | 9b3f02c | 2012-12-21 22:51:18 +0000 | [diff] [blame] | 1014 | |
| 1015 | // Very long objectiveC method declaration. |
| 1016 | EXPECT_EQ("- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range\n " |
| 1017 | "outRange:(NSRange)out_range outRange1:(NSRange)out_range1\n " |
| 1018 | "outRange2:(NSRange)out_range2 outRange3:(NSRange)out_range3\n " |
| 1019 | "outRange4:(NSRange)out_range4 outRange5:(NSRange)out_range5\n " |
| 1020 | "outRange6:(NSRange)out_range6 outRange7:(NSRange)out_range7\n " |
| 1021 | "outRange8:(NSRange)out_range8 outRange9:(NSRange)out_range9;", |
| 1022 | |
| 1023 | format("- (NSUInteger)indexOfObject:(id)anObject inRange:(NSRange)range " |
| 1024 | "outRange:(NSRange) out_range outRange1:(NSRange) out_range1 " |
| 1025 | "outRange2:(NSRange) out_range2 outRange3:(NSRange) out_range3 " |
| 1026 | "outRange4:(NSRange) out_range4 outRange5:(NSRange) out_range5 " |
| 1027 | "outRange6:(NSRange) out_range6 outRange7:(NSRange) out_range7 " |
| 1028 | "outRange8:(NSRange) out_range8 outRange9:(NSRange) out_range9;")); |
Fariborz Jahanian | 154120c | 2012-12-20 19:54:13 +0000 | [diff] [blame] | 1029 | } |
| 1030 | |
Daniel Jasper | bac016b | 2012-12-03 18:12:45 +0000 | [diff] [blame] | 1031 | } // end namespace tooling |
| 1032 | } // end namespace clang |