1 //===- unittest/Format/FormatTestObjC.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 10 #include "clang/Format/Format.h" 11 12 #include "../Tooling/ReplacementTest.h" 13 #include "FormatTestUtils.h" 14 15 #include "clang/Frontend/TextDiagnosticPrinter.h" 16 #include "llvm/Support/Debug.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "gtest/gtest.h" 19 20 #define DEBUG_TYPE "format-test" 21 22 using clang::tooling::ReplacementTest; 23 24 namespace clang { 25 namespace format { 26 namespace { 27 28 class FormatTestObjC : public ::testing::Test { 29 protected: 30 FormatTestObjC() { 31 Style = getLLVMStyle(); 32 Style.Language = FormatStyle::LK_ObjC; 33 } 34 35 enum StatusCheck { 36 SC_ExpectComplete, 37 SC_ExpectIncomplete, 38 SC_DoNotCheck 39 }; 40 41 std::string format(llvm::StringRef Code, 42 StatusCheck CheckComplete = SC_ExpectComplete) { 43 LLVM_DEBUG(llvm::errs() << "---\n"); 44 LLVM_DEBUG(llvm::errs() << Code << "\n\n"); 45 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size())); 46 FormattingAttemptStatus Status; 47 tooling::Replacements Replaces = 48 reformat(Style, Code, Ranges, "<stdin>", &Status); 49 if (CheckComplete != SC_DoNotCheck) { 50 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete; 51 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete) 52 << Code << "\n\n"; 53 } 54 auto Result = applyAllReplacements(Code, Replaces); 55 EXPECT_TRUE(static_cast<bool>(Result)); 56 LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); 57 return *Result; 58 } 59 60 void verifyFormat(StringRef Code) { 61 EXPECT_EQ(Code.str(), format(Code)) << "Expected code is not stable"; 62 EXPECT_EQ(Code.str(), format(test::messUp(Code))); 63 } 64 65 void verifyIncompleteFormat(StringRef Code) { 66 EXPECT_EQ(Code.str(), format(test::messUp(Code), SC_ExpectIncomplete)); 67 } 68 69 FormatStyle Style; 70 }; 71 72 TEST(FormatTestObjCStyle, DetectsObjCInHeaders) { 73 auto Style = getStyle("LLVM", "a.h", "none", "@interface\n" 74 "- (id)init;"); 75 ASSERT_TRUE((bool)Style); 76 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 77 78 Style = getStyle("LLVM", "a.h", "none", "@interface\n" 79 "+ (id)init;"); 80 ASSERT_TRUE((bool)Style); 81 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 82 83 Style = getStyle("LLVM", "a.h", "none", "@interface\n" 84 "@end\n" 85 "//comment"); 86 ASSERT_TRUE((bool)Style); 87 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 88 89 Style = getStyle("LLVM", "a.h", "none", "@interface\n" 90 "@end //comment"); 91 ASSERT_TRUE((bool)Style); 92 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 93 94 // No recognizable ObjC. 95 Style = getStyle("LLVM", "a.h", "none", "void f() {}"); 96 ASSERT_TRUE((bool)Style); 97 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language); 98 99 Style = getStyle("{}", "a.h", "none", "@interface Foo\n@end\n"); 100 ASSERT_TRUE((bool)Style); 101 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 102 103 Style = getStyle("{}", "a.h", "none", 104 "const int interface = 1;\nconst int end = 2;\n"); 105 ASSERT_TRUE((bool)Style); 106 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language); 107 108 Style = getStyle("{}", "a.h", "none", "@protocol Foo\n@end\n"); 109 ASSERT_TRUE((bool)Style); 110 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 111 112 Style = getStyle("{}", "a.h", "none", 113 "const int protocol = 1;\nconst int end = 2;\n"); 114 ASSERT_TRUE((bool)Style); 115 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language); 116 117 Style = 118 getStyle("{}", "a.h", "none", "typedef NS_ENUM(NSInteger, Foo) {};\n"); 119 ASSERT_TRUE((bool)Style); 120 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 121 122 Style = getStyle("{}", "a.h", "none", "enum Foo {};"); 123 ASSERT_TRUE((bool)Style); 124 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language); 125 126 Style = 127 getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }\n"); 128 ASSERT_TRUE((bool)Style); 129 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 130 131 Style = 132 getStyle("{}", "a.h", "none", "inline void Foo() { Log(\"Foo\"); }\n"); 133 ASSERT_TRUE((bool)Style); 134 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language); 135 136 Style = 137 getStyle("{}", "a.h", "none", "inline void Foo() { id = @[1, 2, 3]; }\n"); 138 ASSERT_TRUE((bool)Style); 139 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 140 141 Style = getStyle("{}", "a.h", "none", 142 "inline void Foo() { id foo = @{1: 2, 3: 4, 5: 6}; }\n"); 143 ASSERT_TRUE((bool)Style); 144 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 145 146 Style = getStyle("{}", "a.h", "none", 147 "inline void Foo() { int foo[] = {1, 2, 3}; }\n"); 148 ASSERT_TRUE((bool)Style); 149 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language); 150 151 // ObjC characteristic types. 152 Style = getStyle("{}", "a.h", "none", "extern NSString *kFoo;\n"); 153 ASSERT_TRUE((bool)Style); 154 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 155 156 Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n"); 157 ASSERT_TRUE((bool)Style); 158 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 159 160 Style = getStyle("{}", "a.h", "none", "NSObject *Foo();\n"); 161 ASSERT_TRUE((bool)Style); 162 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 163 164 Style = getStyle("{}", "a.h", "none", "NSSet *Foo();\n"); 165 ASSERT_TRUE((bool)Style); 166 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language); 167 } 168 169 TEST_F(FormatTestObjC, FormatObjCTryCatch) { 170 verifyFormat("@try {\n" 171 " f();\n" 172 "} @catch (NSException e) {\n" 173 " @throw;\n" 174 "} @finally {\n" 175 " exit(42);\n" 176 "}"); 177 verifyFormat("DEBUG({\n" 178 " @try {\n" 179 " } @finally {\n" 180 " }\n" 181 "});\n"); 182 } 183 184 TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) { 185 verifyFormat("@autoreleasepool {\n" 186 " f();\n" 187 "}\n" 188 "@autoreleasepool {\n" 189 " f();\n" 190 "}\n"); 191 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 192 Style.BraceWrapping.AfterControlStatement = true; 193 verifyFormat("@autoreleasepool\n" 194 "{\n" 195 " f();\n" 196 "}\n" 197 "@autoreleasepool\n" 198 "{\n" 199 " f();\n" 200 "}\n"); 201 } 202 203 TEST_F(FormatTestObjC, FormatObjCGenerics) { 204 Style.ColumnLimit = 40; 205 verifyFormat("int aaaaaaaaaaaaaaaa(\n" 206 " NSArray<aaaaaaaaaaaaaaaaaa *>\n" 207 " aaaaaaaaaaaaaaaaa);\n"); 208 verifyFormat("int aaaaaaaaaaaaaaaa(\n" 209 " NSArray<aaaaaaaaaaaaaaaaaaa<\n" 210 " aaaaaaaaaaaaaaaa *> *>\n" 211 " aaaaaaaaaaaaaaaaa);\n"); 212 } 213 214 TEST_F(FormatTestObjC, FormatObjCSynchronized) { 215 verifyFormat("@synchronized(self) {\n" 216 " f();\n" 217 "}\n" 218 "@synchronized(self) {\n" 219 " f();\n" 220 "}\n"); 221 Style.BreakBeforeBraces = FormatStyle::BS_Custom; 222 Style.BraceWrapping.AfterControlStatement = true; 223 verifyFormat("@synchronized(self)\n" 224 "{\n" 225 " f();\n" 226 "}\n" 227 "@synchronized(self)\n" 228 "{\n" 229 " f();\n" 230 "}\n"); 231 } 232 233 TEST_F(FormatTestObjC, FormatObjCInterface) { 234 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 235 "@public\n" 236 " int field1;\n" 237 "@protected\n" 238 " int field2;\n" 239 "@private\n" 240 " int field3;\n" 241 "@package\n" 242 " int field4;\n" 243 "}\n" 244 "+ (id)init;\n" 245 "@end"); 246 247 verifyFormat("@interface /* wait for it */ Foo\n" 248 "+ (id)init;\n" 249 "// Look, a comment!\n" 250 "- (int)answerWith:(int)i;\n" 251 "@end"); 252 253 verifyFormat("@interface Foo\n" 254 "@end\n" 255 "@interface Bar\n" 256 "@end"); 257 258 verifyFormat("@interface Foo : Bar\n" 259 "@property(assign, readwrite) NSInteger bar;\n" 260 "+ (id)init;\n" 261 "@end"); 262 263 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @interface Foo : Bar\n" 264 "@property(assign, readwrite) NSInteger bar;\n" 265 "+ (id)init;\n" 266 "@end"); 267 268 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n" 269 "+ (id)init;\n" 270 "@end"); 271 272 verifyFormat("@interface Foo (HackStuff)\n" 273 "+ (id)init;\n" 274 "@end"); 275 276 verifyFormat("@interface Foo ()\n" 277 "+ (id)init;\n" 278 "@end"); 279 280 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 281 "+ (id)init;\n" 282 "@end"); 283 284 verifyFormat("@interface Foo {\n" 285 " int _i;\n" 286 "}\n" 287 "+ (id)init;\n" 288 "@end"); 289 290 verifyFormat("@interface Foo : Bar {\n" 291 " int _i;\n" 292 "}\n" 293 "+ (id)init;\n" 294 "@end"); 295 296 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n" 297 " int _i;\n" 298 "}\n" 299 "+ (id)init;\n" 300 "@end"); 301 302 verifyFormat("@interface Foo<Baz : Blech> : Bar <Baz, Quux> {\n" 303 " int _i;\n" 304 "}\n" 305 "+ (id)init;\n" 306 "@end"); 307 308 verifyFormat("@interface Foo<Bar : Baz <Blech>> : Xyzzy <Corge> {\n" 309 " int _i;\n" 310 "}\n" 311 "+ (id)init;\n" 312 "@end"); 313 314 verifyFormat("@interface Foo (HackStuff) {\n" 315 " int _i;\n" 316 "}\n" 317 "+ (id)init;\n" 318 "@end"); 319 320 verifyFormat("@interface Foo () {\n" 321 " int _i;\n" 322 "}\n" 323 "+ (id)init;\n" 324 "@end"); 325 326 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n" 327 " int _i;\n" 328 "}\n" 329 "+ (id)init;\n" 330 "@end"); 331 verifyFormat("@interface Foo\n" 332 "- (void)foo {\n" 333 "}\n" 334 "@end\n" 335 "@implementation Bar\n" 336 "- (void)bar {\n" 337 "}\n" 338 "@end"); 339 Style.ColumnLimit = 40; 340 verifyFormat("@interface ccccccccccccc () <\n" 341 " ccccccccccccc, ccccccccccccc,\n" 342 " ccccccccccccc, ccccccccccccc> {\n" 343 "}"); 344 verifyFormat("@interface ccccccccccccc (ccccccccccc) <\n" 345 " ccccccccccccc> {\n" 346 "}"); 347 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never; 348 verifyFormat("@interface ddddddddddddd () <\n" 349 " ddddddddddddd,\n" 350 " ddddddddddddd,\n" 351 " ddddddddddddd,\n" 352 " ddddddddddddd> {\n" 353 "}"); 354 355 Style.BinPackParameters = false; 356 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto; 357 verifyFormat("@interface eeeeeeeeeeeee () <\n" 358 " eeeeeeeeeeeee,\n" 359 " eeeeeeeeeeeee,\n" 360 " eeeeeeeeeeeee,\n" 361 " eeeeeeeeeeeee> {\n" 362 "}"); 363 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Always; 364 verifyFormat("@interface fffffffffffff () <\n" 365 " fffffffffffff, fffffffffffff,\n" 366 " fffffffffffff, fffffffffffff> {\n" 367 "}"); 368 369 Style = getGoogleStyle(FormatStyle::LK_ObjC); 370 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 371 " @public\n" 372 " int field1;\n" 373 " @protected\n" 374 " int field2;\n" 375 " @private\n" 376 " int field3;\n" 377 " @package\n" 378 " int field4;\n" 379 "}\n" 380 "+ (id)init;\n" 381 "@end"); 382 verifyFormat("@interface Foo : Bar <Baz, Quux>\n" 383 "+ (id)init;\n" 384 "@end"); 385 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 386 "+ (id)init;\n" 387 "@end"); 388 Style.ColumnLimit = 40; 389 // BinPackParameters should be true by default. 390 verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n" 391 " int eeeee, int eeeee);\n"); 392 // ObjCBinPackProtocolList should be BPS_Never by default. 393 verifyFormat("@interface fffffffffffff () <\n" 394 " fffffffffffff,\n" 395 " fffffffffffff,\n" 396 " fffffffffffff,\n" 397 " fffffffffffff> {\n" 398 "}"); 399 } 400 401 TEST_F(FormatTestObjC, FormatObjCImplementation) { 402 verifyFormat("@implementation Foo : NSObject {\n" 403 "@public\n" 404 " int field1;\n" 405 "@protected\n" 406 " int field2;\n" 407 "@private\n" 408 " int field3;\n" 409 "@package\n" 410 " int field4;\n" 411 "}\n" 412 "+ (id)init {\n}\n" 413 "@end"); 414 415 verifyFormat("@implementation Foo\n" 416 "+ (id)init {\n" 417 " if (true)\n" 418 " return nil;\n" 419 "}\n" 420 "// Look, a comment!\n" 421 "- (int)answerWith:(int)i {\n" 422 " return i;\n" 423 "}\n" 424 "+ (int)answerWith:(int)i {\n" 425 " return i;\n" 426 "}\n" 427 "@end"); 428 429 verifyFormat("@implementation Foo\n" 430 "@end\n" 431 "@implementation Bar\n" 432 "@end"); 433 434 EXPECT_EQ("@implementation Foo : Bar\n" 435 "+ (id)init {\n}\n" 436 "- (void)foo {\n}\n" 437 "@end", 438 format("@implementation Foo : Bar\n" 439 "+(id)init{}\n" 440 "-(void)foo{}\n" 441 "@end")); 442 443 verifyFormat("@implementation Foo {\n" 444 " int _i;\n" 445 "}\n" 446 "+ (id)init {\n}\n" 447 "@end"); 448 449 verifyFormat("@implementation Foo : Bar {\n" 450 " int _i;\n" 451 "}\n" 452 "+ (id)init {\n}\n" 453 "@end"); 454 455 verifyFormat("@implementation Foo (HackStuff)\n" 456 "+ (id)init {\n}\n" 457 "@end"); 458 verifyFormat("@implementation ObjcClass\n" 459 "- (void)method;\n" 460 "{}\n" 461 "@end"); 462 463 Style = getGoogleStyle(FormatStyle::LK_ObjC); 464 verifyFormat("@implementation Foo : NSObject {\n" 465 " @public\n" 466 " int field1;\n" 467 " @protected\n" 468 " int field2;\n" 469 " @private\n" 470 " int field3;\n" 471 " @package\n" 472 " int field4;\n" 473 "}\n" 474 "+ (id)init {\n}\n" 475 "@end"); 476 } 477 478 TEST_F(FormatTestObjC, FormatObjCProtocol) { 479 verifyFormat("@protocol Foo\n" 480 "@property(weak) id delegate;\n" 481 "- (NSUInteger)numberOfThings;\n" 482 "@end"); 483 484 verifyFormat("@protocol MyProtocol <NSObject>\n" 485 "- (NSUInteger)numberOfThings;\n" 486 "@end"); 487 488 verifyFormat("@protocol Foo;\n" 489 "@protocol Bar;\n"); 490 491 verifyFormat("@protocol Foo\n" 492 "@end\n" 493 "@protocol Bar\n" 494 "@end"); 495 496 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n" 497 "@property(assign, readwrite) NSInteger bar;\n" 498 "@end"); 499 500 verifyFormat("@protocol myProtocol\n" 501 "- (void)mandatoryWithInt:(int)i;\n" 502 "@optional\n" 503 "- (void)optional;\n" 504 "@required\n" 505 "- (void)required;\n" 506 "@optional\n" 507 "@property(assign) int madProp;\n" 508 "@end\n"); 509 510 verifyFormat("@property(nonatomic, assign, readonly)\n" 511 " int *looooooooooooooooooooooooooooongNumber;\n" 512 "@property(nonatomic, assign, readonly)\n" 513 " NSString *looooooooooooooooooooooooooooongName;"); 514 515 verifyFormat("@implementation PR18406\n" 516 "}\n" 517 "@end"); 518 519 Style = getGoogleStyle(FormatStyle::LK_ObjC); 520 verifyFormat("@protocol MyProtocol <NSObject>\n" 521 "- (NSUInteger)numberOfThings;\n" 522 "@end"); 523 } 524 525 TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) { 526 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 527 " rect:(NSRect)theRect\n" 528 " interval:(float)theInterval {\n" 529 "}"); 530 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 531 " longKeyword:(NSRect)theRect\n" 532 " longerKeyword:(float)theInterval\n" 533 " error:(NSError **)theError {\n" 534 "}"); 535 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 536 " longKeyword:(NSRect)theRect\n" 537 " evenLongerKeyword:(float)theInterval\n" 538 " error:(NSError **)theError {\n" 539 "}"); 540 verifyFormat("+ (instancetype)new;\n"); 541 Style.ColumnLimit = 60; 542 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 543 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 544 " NS_DESIGNATED_INITIALIZER;"); 545 verifyFormat("- (void)drawRectOn:(id)surface\n" 546 " ofSize:(size_t)height\n" 547 " :(size_t)width;"); 548 Style.ColumnLimit = 40; 549 // Make sure selectors with 0, 1, or more arguments are indented when wrapped. 550 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"); 552 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 553 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); 554 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 555 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n" 556 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); 557 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 558 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n" 559 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); 560 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 561 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n" 562 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); 563 564 // Continuation indent width should win over aligning colons if the function 565 // name is long. 566 Style = getGoogleStyle(FormatStyle::LK_ObjC); 567 Style.ColumnLimit = 40; 568 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 569 " dontAlignNamef:(NSRect)theRect {\n" 570 "}"); 571 572 // Make sure we don't break aligning for short parameter names. 573 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 574 " aShortf:(NSRect)theRect {\n" 575 "}"); 576 577 // Format pairs correctly. 578 Style.ColumnLimit = 80; 579 verifyFormat("- (void)drawRectOn:(id)surface\n" 580 " ofSize:(aaaaaaaa)height\n" 581 " :(size_t)width\n" 582 " atOrigin:(size_t)x\n" 583 " :(size_t)y\n" 584 " aaaaa:(a)yyy\n" 585 " bbb:(d)cccc;"); 586 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;"); 587 } 588 589 TEST_F(FormatTestObjC, FormatObjCMethodExpr) { 590 verifyFormat("[foo bar:baz];"); 591 verifyFormat("return [foo bar:baz];"); 592 verifyFormat("return (a)[foo bar:baz];"); 593 verifyFormat("f([foo bar:baz]);"); 594 verifyFormat("f(2, [foo bar:baz]);"); 595 verifyFormat("f(2, a ? b : c);"); 596 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 597 598 // Unary operators. 599 verifyFormat("int a = +[foo bar:baz];"); 600 verifyFormat("int a = -[foo bar:baz];"); 601 verifyFormat("int a = ![foo bar:baz];"); 602 verifyFormat("int a = ~[foo bar:baz];"); 603 verifyFormat("int a = ++[foo bar:baz];"); 604 verifyFormat("int a = --[foo bar:baz];"); 605 verifyFormat("int a = sizeof [foo bar:baz];"); 606 verifyFormat("int a = alignof [foo bar:baz];"); 607 verifyFormat("int a = &[foo bar:baz];"); 608 verifyFormat("int a = *[foo bar:baz];"); 609 // FIXME: Make casts work, without breaking f()[4]. 610 // verifyFormat("int a = (int)[foo bar:baz];"); 611 // verifyFormat("return (int)[foo bar:baz];"); 612 // verifyFormat("(void)[foo bar:baz];"); 613 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 614 615 // Binary operators. 616 verifyFormat("[foo bar:baz], [foo bar:baz];"); 617 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 618 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 619 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 620 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 621 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 622 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 623 verifyFormat("[foo bar:baz] <<= [foo bar:baz];"); 624 verifyFormat("[foo bar:baz] >>= [foo bar:baz];"); 625 verifyFormat("[foo bar:baz] &= [foo bar:baz];"); 626 verifyFormat("[foo bar:baz] ^= [foo bar:baz];"); 627 verifyFormat("[foo bar:baz] |= [foo bar:baz];"); 628 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];"); 629 verifyFormat("[foo bar:baz] || [foo bar:baz];"); 630 verifyFormat("[foo bar:baz] && [foo bar:baz];"); 631 verifyFormat("[foo bar:baz] | [foo bar:baz];"); 632 verifyFormat("[foo bar:baz] ^ [foo bar:baz];"); 633 verifyFormat("[foo bar:baz] & [foo bar:baz];"); 634 verifyFormat("[foo bar:baz] == [foo bar:baz];"); 635 verifyFormat("[foo bar:baz] != [foo bar:baz];"); 636 verifyFormat("[foo bar:baz] >= [foo bar:baz];"); 637 verifyFormat("[foo bar:baz] <= [foo bar:baz];"); 638 verifyFormat("[foo bar:baz] > [foo bar:baz];"); 639 verifyFormat("[foo bar:baz] < [foo bar:baz];"); 640 verifyFormat("[foo bar:baz] >> [foo bar:baz];"); 641 verifyFormat("[foo bar:baz] << [foo bar:baz];"); 642 verifyFormat("[foo bar:baz] - [foo bar:baz];"); 643 verifyFormat("[foo bar:baz] + [foo bar:baz];"); 644 verifyFormat("[foo bar:baz] * [foo bar:baz];"); 645 verifyFormat("[foo bar:baz] / [foo bar:baz];"); 646 verifyFormat("[foo bar:baz] % [foo bar:baz];"); 647 // Whew! 648 649 verifyFormat("return in[42];"); 650 verifyFormat("for (auto v : in[1]) {\n}"); 651 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 652 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 653 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 654 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 655 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 656 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 657 "}"); 658 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 659 verifyFormat("[self aaaaa:MACRO(a, b:c:, d:e:)];"); 660 verifyFormat("[self aaaaa:MACRO(a, b:c:d:, e:f:g:)];"); 661 verifyFormat("int XYMyFoo(int a, int b) NS_SWIFT_NAME(foo(self:scale:));"); 662 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 663 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 664 665 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 666 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 667 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 668 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 669 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 670 verifyFormat("[button setAction:@selector(zoomOut:)];"); 671 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 672 673 verifyFormat("arr[[self indexForFoo:a]];"); 674 verifyFormat("throw [self errorFor:a];"); 675 verifyFormat("@throw [self errorFor:a];"); 676 677 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 678 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 679 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 680 681 // This tests that the formatter doesn't break after "backing" but before ":", 682 // which would be at 80 columns. 683 verifyFormat( 684 "void f() {\n" 685 " if ((self = [super initWithContentRect:contentRect\n" 686 " styleMask:styleMask ?: otherMask\n" 687 " backing:NSBackingStoreBuffered\n" 688 " defer:YES]))"); 689 690 verifyFormat( 691 "[foo checkThatBreakingAfterColonWorksOk:\n" 692 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 693 694 verifyFormat("[myObj short:arg1 // Force line break\n" 695 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 696 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 697 " error:arg4];"); 698 verifyFormat( 699 "void f() {\n" 700 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 701 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 702 " pos.width(), pos.height())\n" 703 " styleMask:NSBorderlessWindowMask\n" 704 " backing:NSBackingStoreBuffered\n" 705 " defer:NO]);\n" 706 "}"); 707 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 708 " with:contentsNativeView];"); 709 710 verifyFormat( 711 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 712 " owner:nillllll];"); 713 714 verifyFormat( 715 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 716 " forType:kBookmarkButtonDragType];"); 717 718 verifyFormat("[defaultCenter addObserver:self\n" 719 " selector:@selector(willEnterFullscreen)\n" 720 " name:kWillEnterFullscreenNotification\n" 721 " object:nil];"); 722 verifyFormat("[image_rep drawInRect:drawRect\n" 723 " fromRect:NSZeroRect\n" 724 " operation:NSCompositeCopy\n" 725 " fraction:1.0\n" 726 " respectFlipped:NO\n" 727 " hints:nil];"); 728 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 729 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 730 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 731 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 732 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 733 " aaaaaaaaaaaaaaaaaaaaaa];"); 734 735 verifyFormat( 736 "scoped_nsobject<NSTextField> message(\n" 737 " // The frame will be fixed up when |-setMessageText:| is called.\n" 738 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 739 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 740 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 741 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 742 " aaaa:bbb];"); 743 verifyFormat("[self param:function( //\n" 744 " parameter)]"); 745 verifyFormat( 746 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 747 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 748 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 749 750 // Variadic parameters. 751 verifyFormat( 752 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 753 verifyFormat( 754 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 755 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 756 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 757 verifyFormat("[self // break\n" 758 " a:a\n" 759 " aaa:aaa];"); 760 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 761 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 762 763 // Formats pair-parameters. 764 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];"); 765 verifyFormat("[I drawRectOn:surface //\n" 766 " ofSize:aa:bbb\n" 767 " atOrigin:cc:dd];"); 768 769 // Inline block as a first argument. 770 verifyFormat("[object justBlock:^{\n" 771 " a = 42;\n" 772 "}];"); 773 verifyFormat("[object\n" 774 " justBlock:^{\n" 775 " a = 42;\n" 776 " }\n" 777 " notBlock:42\n" 778 " a:42];"); 779 verifyFormat("[object\n" 780 " firstBlock:^{\n" 781 " a = 42;\n" 782 " }\n" 783 " blockWithLongerName:^{\n" 784 " a = 42;\n" 785 " }];"); 786 verifyFormat("[object\n" 787 " blockWithLongerName:^{\n" 788 " a = 42;\n" 789 " }\n" 790 " secondBlock:^{\n" 791 " a = 42;\n" 792 " }];"); 793 verifyFormat("[object\n" 794 " firstBlock:^{\n" 795 " a = 42;\n" 796 " }\n" 797 " notBlock:42\n" 798 " secondBlock:^{\n" 799 " a = 42;\n" 800 " }];"); 801 802 // Space between cast rparen and selector name component. 803 verifyFormat("[((Foo *)foo) bar];"); 804 verifyFormat("[((Foo *)foo) bar:1 blech:2];"); 805 806 Style.ColumnLimit = 70; 807 verifyFormat( 808 "void f() {\n" 809 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 810 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 811 " pos.width(), pos.height())\n" 812 " syeMask:NSBorderlessWindowMask\n" 813 " bking:NSBackingStoreBuffered\n" 814 " der:NO]);\n" 815 "}"); 816 817 Style.ColumnLimit = 60; 818 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 819 " .aaaaaaaa];"); // FIXME: Indentation seems off. 820 // FIXME: This violates the column limit. 821 verifyFormat( 822 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 823 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 824 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 825 826 Style = getChromiumStyle(FormatStyle::LK_ObjC); 827 Style.ColumnLimit = 80; 828 verifyFormat( 829 "void f() {\n" 830 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 831 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 832 " pos.width(), pos.height())\n" 833 " styleMask:NSBorderlessWindowMask\n" 834 " backing:NSBackingStoreBuffered\n" 835 " defer:NO]);\n" 836 "}"); 837 838 // Respect continuation indent and colon alignment (e.g. when object name is 839 // short, and first selector is the longest one) 840 Style = getLLVMStyle(); 841 Style.Language = FormatStyle::LK_ObjC; 842 Style.ContinuationIndentWidth = 8; 843 verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n" 844 " withObject:nil\n" 845 " waitUntilDone:false];"); 846 verifyFormat("[self performSelector:@selector(loadAccessories)\n" 847 " withObjectOnMainThread:nil\n" 848 " waitUntilDone:false];"); 849 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 850 " performSelectorOnMainThread:@selector(loadAccessories)\n" 851 " withObject:nil\n" 852 " waitUntilDone:false];"); 853 verifyFormat("[self // force wrapping\n" 854 " performSelectorOnMainThread:@selector(loadAccessories)\n" 855 " withObject:nil\n" 856 " waitUntilDone:false];"); 857 } 858 859 TEST_F(FormatTestObjC, ObjCAt) { 860 verifyFormat("@autoreleasepool"); 861 verifyFormat("@catch"); 862 verifyFormat("@class"); 863 verifyFormat("@compatibility_alias"); 864 verifyFormat("@defs"); 865 verifyFormat("@dynamic"); 866 verifyFormat("@encode"); 867 verifyFormat("@end"); 868 verifyFormat("@finally"); 869 verifyFormat("@implementation"); 870 verifyFormat("@import"); 871 verifyFormat("@interface"); 872 verifyFormat("@optional"); 873 verifyFormat("@package"); 874 verifyFormat("@private"); 875 verifyFormat("@property"); 876 verifyFormat("@protected"); 877 verifyFormat("@protocol"); 878 verifyFormat("@public"); 879 verifyFormat("@required"); 880 verifyFormat("@selector"); 881 verifyFormat("@synchronized"); 882 verifyFormat("@synthesize"); 883 verifyFormat("@throw"); 884 verifyFormat("@try"); 885 886 EXPECT_EQ("@interface", format("@ interface")); 887 888 // The precise formatting of this doesn't matter, nobody writes code like 889 // this. 890 verifyFormat("@ /*foo*/ interface"); 891 } 892 893 TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) { 894 verifyFormat("void DoStuffWithBlockType(int (^)(char));"); 895 verifyFormat("int (^foo)(char, float);"); 896 verifyFormat("int (^foo[10])(char, float);"); 897 verifyFormat("int (^foo[kNumEntries])(char, float);"); 898 verifyFormat("int (^foo[kNumEntries + 10])(char, float);"); 899 verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);"); 900 } 901 902 TEST_F(FormatTestObjC, ObjCSnippets) { 903 verifyFormat("@autoreleasepool {\n" 904 " foo();\n" 905 "}"); 906 verifyFormat("@class Foo, Bar;"); 907 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 908 verifyFormat("@dynamic textColor;"); 909 verifyFormat("char *buf1 = @encode(int *);"); 910 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 911 verifyFormat("char *buf1 = @encode(int **);"); 912 verifyFormat("Protocol *proto = @protocol(p1);"); 913 verifyFormat("SEL s = @selector(foo:);"); 914 verifyFormat("@synchronized(self) {\n" 915 " f();\n" 916 "}"); 917 918 verifyFormat("@import foo.bar;\n" 919 "@import baz;"); 920 921 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 922 923 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 924 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 925 926 Style = getMozillaStyle(); 927 verifyFormat("@property (assign, getter=isEditable) BOOL editable;"); 928 verifyFormat("@property BOOL editable;"); 929 930 Style = getWebKitStyle(); 931 verifyFormat("@property (assign, getter=isEditable) BOOL editable;"); 932 verifyFormat("@property BOOL editable;"); 933 934 Style = getGoogleStyle(FormatStyle::LK_ObjC); 935 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 936 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 937 } 938 939 TEST_F(FormatTestObjC, ObjCForIn) { 940 verifyFormat("- (void)test {\n" 941 " for (NSString *n in arrayOfStrings) {\n" 942 " foo(n);\n" 943 " }\n" 944 "}"); 945 verifyFormat("- (void)test {\n" 946 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 947 " foo(n);\n" 948 " }\n" 949 "}"); 950 verifyFormat("for (Foo *x in bar) {\n}"); 951 verifyFormat("for (Foo *x in [bar baz]) {\n}"); 952 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}"); 953 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}"); 954 verifyFormat("for (Foo *x in [bar baz:^{\n" 955 " [uh oh];\n" 956 " }]) {\n}"); 957 } 958 959 TEST_F(FormatTestObjC, ObjCCxxKeywords) { 960 verifyFormat("+ (instancetype)new {\n" 961 " return nil;\n" 962 "}\n"); 963 verifyFormat("+ (instancetype)myNew {\n" 964 " return [self new];\n" 965 "}\n"); 966 verifyFormat("SEL NewSelector(void) { return @selector(new); }\n"); 967 verifyFormat("SEL MacroSelector(void) { return MACRO(new); }\n"); 968 verifyFormat("+ (instancetype)delete {\n" 969 " return nil;\n" 970 "}\n"); 971 verifyFormat("+ (instancetype)myDelete {\n" 972 " return [self delete];\n" 973 "}\n"); 974 verifyFormat("SEL DeleteSelector(void) { return @selector(delete); }\n"); 975 verifyFormat("SEL MacroSelector(void) { return MACRO(delete); }\n"); 976 verifyFormat("MACRO(new:)\n"); 977 verifyFormat("MACRO(delete:)\n"); 978 verifyFormat("foo = @{MACRO(new:) : MACRO(delete:)}\n"); 979 verifyFormat("@implementation Foo\n" 980 "// Testing\n" 981 "- (Class)class {\n" 982 "}\n" 983 "- (void)foo {\n" 984 "}\n" 985 "@end\n"); 986 verifyFormat("@implementation Foo\n" 987 "- (Class)class {\n" 988 "}\n" 989 "- (void)foo {\n" 990 "}\n" 991 "@end"); 992 verifyFormat("@implementation Foo\n" 993 "+ (Class)class {\n" 994 "}\n" 995 "- (void)foo {\n" 996 "}\n" 997 "@end"); 998 verifyFormat("@implementation Foo\n" 999 "- (Class)class:(Class)klass {\n" 1000 "}\n" 1001 "- (void)foo {\n" 1002 "}\n" 1003 "@end"); 1004 verifyFormat("@implementation Foo\n" 1005 "+ (Class)class:(Class)klass {\n" 1006 "}\n" 1007 "- (void)foo {\n" 1008 "}\n" 1009 "@end"); 1010 1011 verifyFormat("@interface Foo\n" 1012 "// Testing\n" 1013 "- (Class)class;\n" 1014 "- (void)foo;\n" 1015 "@end\n"); 1016 verifyFormat("@interface Foo\n" 1017 "- (Class)class;\n" 1018 "- (void)foo;\n" 1019 "@end"); 1020 verifyFormat("@interface Foo\n" 1021 "+ (Class)class;\n" 1022 "- (void)foo;\n" 1023 "@end"); 1024 verifyFormat("@interface Foo\n" 1025 "- (Class)class:(Class)klass;\n" 1026 "- (void)foo;\n" 1027 "@end"); 1028 verifyFormat("@interface Foo\n" 1029 "+ (Class)class:(Class)klass;\n" 1030 "- (void)foo;\n" 1031 "@end"); 1032 } 1033 1034 TEST_F(FormatTestObjC, ObjCLiterals) { 1035 verifyFormat("@\"String\""); 1036 verifyFormat("@1"); 1037 verifyFormat("@+4.8"); 1038 verifyFormat("@-4"); 1039 verifyFormat("@1LL"); 1040 verifyFormat("@.5"); 1041 verifyFormat("@'c'"); 1042 verifyFormat("@true"); 1043 1044 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 1045 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 1046 verifyFormat("NSNumber *favoriteColor = @(Green);"); 1047 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 1048 1049 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 1050 } 1051 1052 TEST_F(FormatTestObjC, ObjCDictLiterals) { 1053 verifyFormat("@{"); 1054 verifyFormat("@{}"); 1055 verifyFormat("@{@\"one\" : @1}"); 1056 verifyFormat("return @{@\"one\" : @1;"); 1057 verifyFormat("@{@\"one\" : @1}"); 1058 1059 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 1060 verifyFormat("@{\n" 1061 " @\"one\" : @{@2 : @1},\n" 1062 "}"); 1063 1064 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 1065 verifyIncompleteFormat("[self setDict:@{}"); 1066 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 1067 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 1068 verifyFormat( 1069 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 1070 verifyFormat( 1071 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 1072 1073 verifyFormat("NSDictionary *d = @{\n" 1074 " @\"nam\" : NSUserNam(),\n" 1075 " @\"dte\" : [NSDate date],\n" 1076 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 1077 "};"); 1078 verifyFormat( 1079 "@{\n" 1080 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 1081 "regularFont,\n" 1082 "};"); 1083 verifyFormat( 1084 "@{\n" 1085 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 1086 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 1087 "};"); 1088 1089 // We should try to be robust in case someone forgets the "@". 1090 verifyFormat("NSDictionary *d = {\n" 1091 " @\"nam\" : NSUserNam(),\n" 1092 " @\"dte\" : [NSDate date],\n" 1093 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 1094 "};"); 1095 verifyFormat("NSMutableDictionary *dictionary =\n" 1096 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 1097 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 1098 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 1099 " cccccccccccccccc : ccccccccccccccc\n" 1100 " }];"); 1101 1102 // Ensure that casts before the key are kept on the same line as the key. 1103 verifyFormat( 1104 "NSDictionary *d = @{\n" 1105 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1106 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 1107 "};"); 1108 Style.ColumnLimit = 40; 1109 verifyFormat("int Foo() {\n" 1110 " a12345 = @{a12345 : a12345};\n" 1111 "}"); 1112 verifyFormat("int Foo() {\n" 1113 " a12345 = @{a12345 : @(a12345)};\n" 1114 "}"); 1115 verifyFormat("int Foo() {\n" 1116 " a12345 = @{(Foo *)a12345 : @(a12345)};\n" 1117 "}"); 1118 verifyFormat("int Foo() {\n" 1119 " a12345 = @{@(a12345) : a12345};\n" 1120 "}"); 1121 verifyFormat("int Foo() {\n" 1122 " a12345 = @{@(a12345) : @YES};\n" 1123 "}"); 1124 Style.SpacesInContainerLiterals = false; 1125 verifyFormat("int Foo() {\n" 1126 " b12345 = @{b12345: b12345};\n" 1127 "}"); 1128 verifyFormat("int Foo() {\n" 1129 " b12345 = @{(Foo *)b12345: @(b12345)};\n" 1130 "}"); 1131 Style.SpacesInContainerLiterals = true; 1132 1133 Style = getGoogleStyle(FormatStyle::LK_ObjC); 1134 verifyFormat( 1135 "@{\n" 1136 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 1137 "regularFont,\n" 1138 "};"); 1139 } 1140 1141 TEST_F(FormatTestObjC, ObjCArrayLiterals) { 1142 verifyIncompleteFormat("@["); 1143 verifyFormat("@[]"); 1144 verifyFormat( 1145 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 1146 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 1147 verifyFormat("NSArray *array = @[ [foo description] ];"); 1148 1149 verifyFormat( 1150 "NSArray *some_variable = @[\n" 1151 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 1152 " @\"aaaaaaaaaaaaaaaaa\",\n" 1153 " @\"aaaaaaaaaaaaaaaaa\",\n" 1154 " @\"aaaaaaaaaaaaaaaaa\",\n" 1155 "];"); 1156 verifyFormat( 1157 "NSArray *some_variable = @[\n" 1158 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 1159 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n" 1160 "];"); 1161 verifyFormat("NSArray *some_variable = @[\n" 1162 " @\"aaaaaaaaaaaaaaaaa\",\n" 1163 " @\"aaaaaaaaaaaaaaaaa\",\n" 1164 " @\"aaaaaaaaaaaaaaaaa\",\n" 1165 " @\"aaaaaaaaaaaaaaaaa\",\n" 1166 "];"); 1167 verifyFormat("NSArray *array = @[\n" 1168 " @\"a\",\n" 1169 " @\"a\",\n" // Trailing comma -> one per line. 1170 "];"); 1171 1172 // We should try to be robust in case someone forgets the "@". 1173 verifyFormat("NSArray *some_variable = [\n" 1174 " @\"aaaaaaaaaaaaaaaaa\",\n" 1175 " @\"aaaaaaaaaaaaaaaaa\",\n" 1176 " @\"aaaaaaaaaaaaaaaaa\",\n" 1177 " @\"aaaaaaaaaaaaaaaaa\",\n" 1178 "];"); 1179 verifyFormat( 1180 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 1181 " index:(NSUInteger)index\n" 1182 " nonDigitAttributes:\n" 1183 " (NSDictionary *)noDigitAttributes;"); 1184 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 1185 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 1186 "]];"); 1187 Style.ColumnLimit = 40; 1188 verifyFormat("int Foo() {\n" 1189 " a12345 = @[ a12345, a12345 ];\n" 1190 "}"); 1191 verifyFormat("int Foo() {\n" 1192 " a123 = @[ (Foo *)a12345, @(a12345) ];\n" 1193 "}"); 1194 Style.SpacesInContainerLiterals = false; 1195 verifyFormat("int Foo() {\n" 1196 " b12345 = @[b12345, b12345];\n" 1197 "}"); 1198 verifyFormat("int Foo() {\n" 1199 " b12345 = @[(Foo *)b12345, @(b12345)];\n" 1200 "}"); 1201 Style.SpacesInContainerLiterals = true; 1202 Style.ColumnLimit = 20; 1203 // We can't break string literals inside NSArray literals 1204 // (that raises -Wobjc-string-concatenation). 1205 verifyFormat("NSArray *foo = @[\n" 1206 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 1207 "];\n"); 1208 } 1209 1210 TEST_F(FormatTestObjC, BreaksCallStatementWhereSemiJustOverTheLimit) { 1211 Style.ColumnLimit = 60; 1212 // If the statement starting with 'a = ...' is put on a single line, the ';' 1213 // is at line 61. 1214 verifyFormat("int f(int a) {\n" 1215 " a = [self aaaaaaaaaa:bbbbbbbbb\n" 1216 " ccccccccc:dddddddd\n" 1217 " ee:fddd];\n" 1218 "}"); 1219 } 1220 1221 TEST_F(FormatTestObjC, AlwaysBreakBeforeMultilineStrings) { 1222 Style = getGoogleStyle(FormatStyle::LK_ObjC); 1223 Style.ColumnLimit = 40; 1224 verifyFormat("aaaa = @\"bbbb\"\n" 1225 " @\"cccc\";"); 1226 verifyFormat("aaaa(@\"bbbb\"\n" 1227 " @\"cccc\");"); 1228 verifyFormat("aaaa(qqq, @\"bbbb\"\n" 1229 " @\"cccc\");"); 1230 } 1231 1232 } // end namespace 1233 } // end namespace format 1234 } // end namespace clang 1235