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 unsigned PreviousColumnLimit = Style.ColumnLimit; 682 Style.ColumnLimit = 50; 683 // Instead of: 684 // bool a = 685 // ([object a:42] == 0 || [object a:42 686 // b:42] == 0); 687 verifyFormat("bool a = ([object a:42] == 0 ||\n" 688 " [object a:42 b:42] == 0);"); 689 Style.ColumnLimit = PreviousColumnLimit; 690 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 691 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 692 693 // This tests that the formatter doesn't break after "backing" but before ":", 694 // which would be at 80 columns. 695 verifyFormat( 696 "void f() {\n" 697 " if ((self = [super initWithContentRect:contentRect\n" 698 " styleMask:styleMask ?: otherMask\n" 699 " backing:NSBackingStoreBuffered\n" 700 " defer:YES]))"); 701 702 verifyFormat( 703 "[foo checkThatBreakingAfterColonWorksOk:\n" 704 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 705 706 verifyFormat("[myObj short:arg1 // Force line break\n" 707 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 708 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 709 " error:arg4];"); 710 verifyFormat( 711 "void f() {\n" 712 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 713 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 714 " pos.width(), pos.height())\n" 715 " styleMask:NSBorderlessWindowMask\n" 716 " backing:NSBackingStoreBuffered\n" 717 " defer:NO]);\n" 718 "}"); 719 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 720 " with:contentsNativeView];"); 721 722 verifyFormat( 723 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 724 " owner:nillllll];"); 725 726 verifyFormat( 727 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 728 " forType:kBookmarkButtonDragType];"); 729 730 verifyFormat("[defaultCenter addObserver:self\n" 731 " selector:@selector(willEnterFullscreen)\n" 732 " name:kWillEnterFullscreenNotification\n" 733 " object:nil];"); 734 verifyFormat("[image_rep drawInRect:drawRect\n" 735 " fromRect:NSZeroRect\n" 736 " operation:NSCompositeCopy\n" 737 " fraction:1.0\n" 738 " respectFlipped:NO\n" 739 " hints:nil];"); 740 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 741 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 742 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 743 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 744 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 745 " aaaaaaaaaaaaaaaaaaaaaa];"); 746 747 verifyFormat( 748 "scoped_nsobject<NSTextField> message(\n" 749 " // The frame will be fixed up when |-setMessageText:| is called.\n" 750 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 751 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 752 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 753 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 754 " aaaa:bbb];"); 755 verifyFormat("[self param:function( //\n" 756 " parameter)]"); 757 verifyFormat( 758 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 759 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 760 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 761 762 // Variadic parameters. 763 verifyFormat( 764 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 765 verifyFormat( 766 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 767 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 768 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 769 770 verifyFormat("[self // break\n" 771 " a:a\n" 772 " aaa:aaa];"); 773 774 // Formats pair-parameters. 775 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];"); 776 verifyFormat("[I drawRectOn:surface //\n" 777 " ofSize:aa:bbb\n" 778 " atOrigin:cc:dd];"); 779 780 // Inline block as a first argument. 781 verifyFormat("[object justBlock:^{\n" 782 " a = 42;\n" 783 "}];"); 784 verifyFormat("[object\n" 785 " justBlock:^{\n" 786 " a = 42;\n" 787 " }\n" 788 " notBlock:42\n" 789 " a:42];"); 790 verifyFormat("[object\n" 791 " firstBlock:^{\n" 792 " a = 42;\n" 793 " }\n" 794 " blockWithLongerName:^{\n" 795 " a = 42;\n" 796 " }];"); 797 verifyFormat("[object\n" 798 " blockWithLongerName:^{\n" 799 " a = 42;\n" 800 " }\n" 801 " secondBlock:^{\n" 802 " a = 42;\n" 803 " }];"); 804 verifyFormat("[object\n" 805 " firstBlock:^{\n" 806 " a = 42;\n" 807 " }\n" 808 " notBlock:42\n" 809 " secondBlock:^{\n" 810 " a = 42;\n" 811 " }];"); 812 813 // Space between cast rparen and selector name component. 814 verifyFormat("[((Foo *)foo) bar];"); 815 verifyFormat("[((Foo *)foo) bar:1 blech:2];"); 816 817 Style.ColumnLimit = 20; 818 verifyFormat("aaaaa = [a aa:aa\n" 819 " aa:aa];"); 820 verifyFormat("aaaaaa = [aa aa:aa\n" 821 " aa:aa];"); 822 823 // Message receiver taking multiple lines. 824 // Non-corner case. 825 verifyFormat("[[object block:^{\n" 826 " return 42;\n" 827 "}] a:42 b:42];"); 828 // Arguments just fit into one line. 829 verifyFormat("[[object block:^{\n" 830 " return 42;\n" 831 "}] aaaaaaa:42 b:42];"); 832 // Arguments just over a column limit. 833 verifyFormat("[[object block:^{\n" 834 " return 42;\n" 835 "}] aaaaaaa:42\n" 836 " bb:42];"); 837 // Arguments just fit into one line. 838 Style.ColumnLimit = 23; 839 verifyFormat("[[obj a:42\n" 840 " b:42\n" 841 " c:42\n" 842 " d:42] e:42 f:42];"); 843 844 // Arguments do not fit into one line with a receiver. 845 Style.ColumnLimit = 20; 846 verifyFormat("[[obj a:42] a:42\n" 847 " b:42];"); 848 verifyFormat("[[obj a:42] a:42\n" 849 " b:42\n" 850 " c:42];"); 851 verifyFormat("[[obj aaaaaa:42\n" 852 " b:42]\n" 853 " cc:42\n" 854 " d:42];"); 855 856 // Avoid breaking receiver expression. 857 Style.ColumnLimit = 30; 858 verifyFormat("fooooooo =\n" 859 " [[obj fooo] aaa:42\n" 860 " aaa:42];"); 861 verifyFormat("[[[obj foo] bar] aa:42\n" 862 " bb:42\n" 863 " cc:42];"); 864 865 Style.ColumnLimit = 70; 866 verifyFormat( 867 "void f() {\n" 868 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 869 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 870 " pos.width(), pos.height())\n" 871 " syeMask:NSBorderlessWindowMask\n" 872 " bking:NSBackingStoreBuffered\n" 873 " der:NO]);\n" 874 "}"); 875 876 Style.ColumnLimit = 60; 877 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 878 " .aaaaaaaa];"); // FIXME: Indentation seems off. 879 // FIXME: This violates the column limit. 880 verifyFormat( 881 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 882 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 883 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 884 885 Style = getChromiumStyle(FormatStyle::LK_ObjC); 886 Style.ColumnLimit = 80; 887 verifyFormat( 888 "void f() {\n" 889 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 890 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 891 " pos.width(), pos.height())\n" 892 " styleMask:NSBorderlessWindowMask\n" 893 " backing:NSBackingStoreBuffered\n" 894 " defer:NO]);\n" 895 "}"); 896 897 // Respect continuation indent and colon alignment (e.g. when object name is 898 // short, and first selector is the longest one) 899 Style = getLLVMStyle(); 900 Style.Language = FormatStyle::LK_ObjC; 901 Style.ContinuationIndentWidth = 8; 902 verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n" 903 " withObject:nil\n" 904 " waitUntilDone:false];"); 905 verifyFormat("[self performSelector:@selector(loadAccessories)\n" 906 " withObjectOnMainThread:nil\n" 907 " waitUntilDone:false];"); 908 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 909 " performSelectorOnMainThread:@selector(loadAccessories)\n" 910 " withObject:nil\n" 911 " waitUntilDone:false];"); 912 verifyFormat("[self // force wrapping\n" 913 " performSelectorOnMainThread:@selector(loadAccessories)\n" 914 " withObject:nil\n" 915 " waitUntilDone:false];"); 916 } 917 918 TEST_F(FormatTestObjC, ObjCAt) { 919 verifyFormat("@autoreleasepool"); 920 verifyFormat("@catch"); 921 verifyFormat("@class"); 922 verifyFormat("@compatibility_alias"); 923 verifyFormat("@defs"); 924 verifyFormat("@dynamic"); 925 verifyFormat("@encode"); 926 verifyFormat("@end"); 927 verifyFormat("@finally"); 928 verifyFormat("@implementation"); 929 verifyFormat("@import"); 930 verifyFormat("@interface"); 931 verifyFormat("@optional"); 932 verifyFormat("@package"); 933 verifyFormat("@private"); 934 verifyFormat("@property"); 935 verifyFormat("@protected"); 936 verifyFormat("@protocol"); 937 verifyFormat("@public"); 938 verifyFormat("@required"); 939 verifyFormat("@selector"); 940 verifyFormat("@synchronized"); 941 verifyFormat("@synthesize"); 942 verifyFormat("@throw"); 943 verifyFormat("@try"); 944 945 EXPECT_EQ("@interface", format("@ interface")); 946 947 // The precise formatting of this doesn't matter, nobody writes code like 948 // this. 949 verifyFormat("@ /*foo*/ interface"); 950 } 951 952 TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) { 953 verifyFormat("void DoStuffWithBlockType(int (^)(char));"); 954 verifyFormat("int (^foo)(char, float);"); 955 verifyFormat("int (^foo[10])(char, float);"); 956 verifyFormat("int (^foo[kNumEntries])(char, float);"); 957 verifyFormat("int (^foo[kNumEntries + 10])(char, float);"); 958 verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);"); 959 } 960 961 TEST_F(FormatTestObjC, ObjCSnippets) { 962 verifyFormat("@autoreleasepool {\n" 963 " foo();\n" 964 "}"); 965 verifyFormat("@class Foo, Bar;"); 966 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 967 verifyFormat("@dynamic textColor;"); 968 verifyFormat("char *buf1 = @encode(int *);"); 969 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 970 verifyFormat("char *buf1 = @encode(int **);"); 971 verifyFormat("Protocol *proto = @protocol(p1);"); 972 verifyFormat("SEL s = @selector(foo:);"); 973 verifyFormat("@synchronized(self) {\n" 974 " f();\n" 975 "}"); 976 977 verifyFormat("@import foo.bar;\n" 978 "@import baz;"); 979 980 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 981 982 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 983 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 984 985 Style.ColumnLimit = 50; 986 verifyFormat("@interface Foo\n" 987 "- (void)doStuffWithFoo:(id)name\n" 988 " bar:(id)bar\n" 989 " baz:(id)baz\n" 990 " NS_SWIFT_NAME(doStuff(withFoo:bar:baz:));\n" 991 "@end"); 992 993 Style = getMozillaStyle(); 994 verifyFormat("@property (assign, getter=isEditable) BOOL editable;"); 995 verifyFormat("@property BOOL editable;"); 996 997 Style = getWebKitStyle(); 998 verifyFormat("@property (assign, getter=isEditable) BOOL editable;"); 999 verifyFormat("@property BOOL editable;"); 1000 1001 Style = getGoogleStyle(FormatStyle::LK_ObjC); 1002 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 1003 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 1004 } 1005 1006 TEST_F(FormatTestObjC, ObjCForIn) { 1007 verifyFormat("- (void)test {\n" 1008 " for (NSString *n in arrayOfStrings) {\n" 1009 " foo(n);\n" 1010 " }\n" 1011 "}"); 1012 verifyFormat("- (void)test {\n" 1013 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 1014 " foo(n);\n" 1015 " }\n" 1016 "}"); 1017 verifyFormat("for (Foo *x in bar) {\n}"); 1018 verifyFormat("for (Foo *x in [bar baz]) {\n}"); 1019 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}"); 1020 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}"); 1021 verifyFormat("for (Foo *x in [bar baz:^{\n" 1022 " [uh oh];\n" 1023 " }]) {\n}"); 1024 } 1025 1026 TEST_F(FormatTestObjC, ObjCCxxKeywords) { 1027 verifyFormat("+ (instancetype)new {\n" 1028 " return nil;\n" 1029 "}\n"); 1030 verifyFormat("+ (instancetype)myNew {\n" 1031 " return [self new];\n" 1032 "}\n"); 1033 verifyFormat("SEL NewSelector(void) { return @selector(new); }\n"); 1034 verifyFormat("SEL MacroSelector(void) { return MACRO(new); }\n"); 1035 verifyFormat("+ (instancetype)delete {\n" 1036 " return nil;\n" 1037 "}\n"); 1038 verifyFormat("+ (instancetype)myDelete {\n" 1039 " return [self delete];\n" 1040 "}\n"); 1041 verifyFormat("SEL DeleteSelector(void) { return @selector(delete); }\n"); 1042 verifyFormat("SEL MacroSelector(void) { return MACRO(delete); }\n"); 1043 verifyFormat("MACRO(new:)\n"); 1044 verifyFormat("MACRO(delete:)\n"); 1045 verifyFormat("foo = @{MACRO(new:) : MACRO(delete:)}\n"); 1046 verifyFormat("@implementation Foo\n" 1047 "// Testing\n" 1048 "- (Class)class {\n" 1049 "}\n" 1050 "- (void)foo {\n" 1051 "}\n" 1052 "@end\n"); 1053 verifyFormat("@implementation Foo\n" 1054 "- (Class)class {\n" 1055 "}\n" 1056 "- (void)foo {\n" 1057 "}\n" 1058 "@end"); 1059 verifyFormat("@implementation Foo\n" 1060 "+ (Class)class {\n" 1061 "}\n" 1062 "- (void)foo {\n" 1063 "}\n" 1064 "@end"); 1065 verifyFormat("@implementation Foo\n" 1066 "- (Class)class:(Class)klass {\n" 1067 "}\n" 1068 "- (void)foo {\n" 1069 "}\n" 1070 "@end"); 1071 verifyFormat("@implementation Foo\n" 1072 "+ (Class)class:(Class)klass {\n" 1073 "}\n" 1074 "- (void)foo {\n" 1075 "}\n" 1076 "@end"); 1077 1078 verifyFormat("@interface Foo\n" 1079 "// Testing\n" 1080 "- (Class)class;\n" 1081 "- (void)foo;\n" 1082 "@end\n"); 1083 verifyFormat("@interface Foo\n" 1084 "- (Class)class;\n" 1085 "- (void)foo;\n" 1086 "@end"); 1087 verifyFormat("@interface Foo\n" 1088 "+ (Class)class;\n" 1089 "- (void)foo;\n" 1090 "@end"); 1091 verifyFormat("@interface Foo\n" 1092 "- (Class)class:(Class)klass;\n" 1093 "- (void)foo;\n" 1094 "@end"); 1095 verifyFormat("@interface Foo\n" 1096 "+ (Class)class:(Class)klass;\n" 1097 "- (void)foo;\n" 1098 "@end"); 1099 } 1100 1101 TEST_F(FormatTestObjC, ObjCLiterals) { 1102 verifyFormat("@\"String\""); 1103 verifyFormat("@1"); 1104 verifyFormat("@+4.8"); 1105 verifyFormat("@-4"); 1106 verifyFormat("@1LL"); 1107 verifyFormat("@.5"); 1108 verifyFormat("@'c'"); 1109 verifyFormat("@true"); 1110 1111 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 1112 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 1113 verifyFormat("NSNumber *favoriteColor = @(Green);"); 1114 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 1115 1116 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 1117 } 1118 1119 TEST_F(FormatTestObjC, ObjCDictLiterals) { 1120 verifyFormat("@{"); 1121 verifyFormat("@{}"); 1122 verifyFormat("@{@\"one\" : @1}"); 1123 verifyFormat("return @{@\"one\" : @1;"); 1124 verifyFormat("@{@\"one\" : @1}"); 1125 1126 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 1127 verifyFormat("@{\n" 1128 " @\"one\" : @{@2 : @1},\n" 1129 "}"); 1130 1131 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 1132 verifyIncompleteFormat("[self setDict:@{}"); 1133 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 1134 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 1135 verifyFormat( 1136 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 1137 verifyFormat( 1138 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 1139 1140 verifyFormat("NSDictionary *d = @{\n" 1141 " @\"nam\" : NSUserNam(),\n" 1142 " @\"dte\" : [NSDate date],\n" 1143 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 1144 "};"); 1145 verifyFormat( 1146 "@{\n" 1147 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 1148 "regularFont,\n" 1149 "};"); 1150 verifyFormat( 1151 "@{\n" 1152 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 1153 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 1154 "};"); 1155 1156 // We should try to be robust in case someone forgets the "@". 1157 verifyFormat("NSDictionary *d = {\n" 1158 " @\"nam\" : NSUserNam(),\n" 1159 " @\"dte\" : [NSDate date],\n" 1160 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 1161 "};"); 1162 verifyFormat("NSMutableDictionary *dictionary =\n" 1163 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 1164 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 1165 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 1166 " cccccccccccccccc : ccccccccccccccc\n" 1167 " }];"); 1168 1169 // Ensure that casts before the key are kept on the same line as the key. 1170 verifyFormat( 1171 "NSDictionary *d = @{\n" 1172 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1173 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 1174 "};"); 1175 Style.ColumnLimit = 40; 1176 verifyFormat("int Foo() {\n" 1177 " a12345 = @{a12345 : a12345};\n" 1178 "}"); 1179 verifyFormat("int Foo() {\n" 1180 " a12345 = @{a12345 : @(a12345)};\n" 1181 "}"); 1182 verifyFormat("int Foo() {\n" 1183 " a12345 = @{(Foo *)a12345 : @(a12345)};\n" 1184 "}"); 1185 verifyFormat("int Foo() {\n" 1186 " a12345 = @{@(a12345) : a12345};\n" 1187 "}"); 1188 verifyFormat("int Foo() {\n" 1189 " a12345 = @{@(a12345) : @YES};\n" 1190 "}"); 1191 Style.SpacesInContainerLiterals = false; 1192 verifyFormat("int Foo() {\n" 1193 " b12345 = @{b12345: b12345};\n" 1194 "}"); 1195 verifyFormat("int Foo() {\n" 1196 " b12345 = @{(Foo *)b12345: @(b12345)};\n" 1197 "}"); 1198 Style.SpacesInContainerLiterals = true; 1199 1200 Style = getGoogleStyle(FormatStyle::LK_ObjC); 1201 verifyFormat( 1202 "@{\n" 1203 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 1204 "regularFont,\n" 1205 "};"); 1206 } 1207 1208 TEST_F(FormatTestObjC, ObjCArrayLiterals) { 1209 verifyIncompleteFormat("@["); 1210 verifyFormat("@[]"); 1211 verifyFormat( 1212 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 1213 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 1214 verifyFormat("NSArray *array = @[ [foo description] ];"); 1215 1216 verifyFormat( 1217 "NSArray *some_variable = @[\n" 1218 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 1219 " @\"aaaaaaaaaaaaaaaaa\",\n" 1220 " @\"aaaaaaaaaaaaaaaaa\",\n" 1221 " @\"aaaaaaaaaaaaaaaaa\",\n" 1222 "];"); 1223 verifyFormat( 1224 "NSArray *some_variable = @[\n" 1225 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 1226 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n" 1227 "];"); 1228 verifyFormat("NSArray *some_variable = @[\n" 1229 " @\"aaaaaaaaaaaaaaaaa\",\n" 1230 " @\"aaaaaaaaaaaaaaaaa\",\n" 1231 " @\"aaaaaaaaaaaaaaaaa\",\n" 1232 " @\"aaaaaaaaaaaaaaaaa\",\n" 1233 "];"); 1234 verifyFormat("NSArray *array = @[\n" 1235 " @\"a\",\n" 1236 " @\"a\",\n" // Trailing comma -> one per line. 1237 "];"); 1238 1239 // We should try to be robust in case someone forgets the "@". 1240 verifyFormat("NSArray *some_variable = [\n" 1241 " @\"aaaaaaaaaaaaaaaaa\",\n" 1242 " @\"aaaaaaaaaaaaaaaaa\",\n" 1243 " @\"aaaaaaaaaaaaaaaaa\",\n" 1244 " @\"aaaaaaaaaaaaaaaaa\",\n" 1245 "];"); 1246 verifyFormat( 1247 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 1248 " index:(NSUInteger)index\n" 1249 " nonDigitAttributes:\n" 1250 " (NSDictionary *)noDigitAttributes;"); 1251 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 1252 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 1253 "]];"); 1254 Style.ColumnLimit = 40; 1255 verifyFormat("int Foo() {\n" 1256 " a12345 = @[ a12345, a12345 ];\n" 1257 "}"); 1258 verifyFormat("int Foo() {\n" 1259 " a123 = @[ (Foo *)a12345, @(a12345) ];\n" 1260 "}"); 1261 Style.SpacesInContainerLiterals = false; 1262 verifyFormat("int Foo() {\n" 1263 " b12345 = @[b12345, b12345];\n" 1264 "}"); 1265 verifyFormat("int Foo() {\n" 1266 " b12345 = @[(Foo *)b12345, @(b12345)];\n" 1267 "}"); 1268 Style.SpacesInContainerLiterals = true; 1269 Style.ColumnLimit = 20; 1270 // We can't break string literals inside NSArray literals 1271 // (that raises -Wobjc-string-concatenation). 1272 verifyFormat("NSArray *foo = @[\n" 1273 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 1274 "];\n"); 1275 } 1276 1277 TEST_F(FormatTestObjC, BreaksCallStatementWhereSemiJustOverTheLimit) { 1278 Style.ColumnLimit = 60; 1279 // If the statement starting with 'a = ...' is put on a single line, the ';' 1280 // is at line 61. 1281 verifyFormat("int f(int a) {\n" 1282 " a = [self aaaaaaaaaa:bbbbbbbbb\n" 1283 " ccccccccc:dddddddd\n" 1284 " ee:fddd];\n" 1285 "}"); 1286 } 1287 1288 TEST_F(FormatTestObjC, AlwaysBreakBeforeMultilineStrings) { 1289 Style = getGoogleStyle(FormatStyle::LK_ObjC); 1290 Style.ColumnLimit = 40; 1291 verifyFormat("aaaa = @\"bbbb\"\n" 1292 " @\"cccc\";"); 1293 verifyFormat("aaaa(@\"bbbb\"\n" 1294 " @\"cccc\");"); 1295 verifyFormat("aaaa(qqq, @\"bbbb\"\n" 1296 " @\"cccc\");"); 1297 verifyFormat("[aaaa qqqq:@\"bbbb\"\n" 1298 " @\"cccc\"];"); 1299 verifyFormat("aaaa = [aaaa qqqq:@\"bbbb\"\n" 1300 " @\"cccc\"];"); 1301 verifyFormat("[aaaa qqqq:@\"bbbb\"\n" 1302 " @\"cccc\"\n" 1303 " rr:42\n" 1304 " ssssss:@\"ee\"\n" 1305 " @\"fffff\"];"); 1306 } 1307 1308 } // end namespace 1309 } // end namespace format 1310 } // end namespace clang 1311