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 332 Style.ColumnLimit = 40; 333 verifyFormat("@interface ccccccccccccc () <\n" 334 " ccccccccccccc, ccccccccccccc,\n" 335 " ccccccccccccc, ccccccccccccc> {\n" 336 "}"); 337 verifyFormat("@interface ccccccccccccc (ccccccccccc) <\n" 338 " ccccccccccccc> {\n" 339 "}"); 340 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never; 341 verifyFormat("@interface ddddddddddddd () <\n" 342 " ddddddddddddd,\n" 343 " ddddddddddddd,\n" 344 " ddddddddddddd,\n" 345 " ddddddddddddd> {\n" 346 "}"); 347 348 Style.BinPackParameters = false; 349 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto; 350 verifyFormat("@interface eeeeeeeeeeeee () <\n" 351 " eeeeeeeeeeeee,\n" 352 " eeeeeeeeeeeee,\n" 353 " eeeeeeeeeeeee,\n" 354 " eeeeeeeeeeeee> {\n" 355 "}"); 356 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Always; 357 verifyFormat("@interface fffffffffffff () <\n" 358 " fffffffffffff, fffffffffffff,\n" 359 " fffffffffffff, fffffffffffff> {\n" 360 "}"); 361 362 Style = getGoogleStyle(FormatStyle::LK_ObjC); 363 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n" 364 " @public\n" 365 " int field1;\n" 366 " @protected\n" 367 " int field2;\n" 368 " @private\n" 369 " int field3;\n" 370 " @package\n" 371 " int field4;\n" 372 "}\n" 373 "+ (id)init;\n" 374 "@end"); 375 verifyFormat("@interface Foo : Bar <Baz, Quux>\n" 376 "+ (id)init;\n" 377 "@end"); 378 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n" 379 "+ (id)init;\n" 380 "@end"); 381 Style.ColumnLimit = 40; 382 // BinPackParameters should be true by default. 383 verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n" 384 " int eeeee, int eeeee);\n"); 385 // ObjCBinPackProtocolList should be BPS_Never by default. 386 verifyFormat("@interface fffffffffffff () <\n" 387 " fffffffffffff,\n" 388 " fffffffffffff,\n" 389 " fffffffffffff,\n" 390 " fffffffffffff> {\n" 391 "}"); 392 } 393 394 TEST_F(FormatTestObjC, FormatObjCImplementation) { 395 verifyFormat("@implementation Foo : NSObject {\n" 396 "@public\n" 397 " int field1;\n" 398 "@protected\n" 399 " int field2;\n" 400 "@private\n" 401 " int field3;\n" 402 "@package\n" 403 " int field4;\n" 404 "}\n" 405 "+ (id)init {\n}\n" 406 "@end"); 407 408 verifyFormat("@implementation Foo\n" 409 "+ (id)init {\n" 410 " if (true)\n" 411 " return nil;\n" 412 "}\n" 413 "// Look, a comment!\n" 414 "- (int)answerWith:(int)i {\n" 415 " return i;\n" 416 "}\n" 417 "+ (int)answerWith:(int)i {\n" 418 " return i;\n" 419 "}\n" 420 "@end"); 421 422 verifyFormat("@implementation Foo\n" 423 "@end\n" 424 "@implementation Bar\n" 425 "@end"); 426 427 EXPECT_EQ("@implementation Foo : Bar\n" 428 "+ (id)init {\n}\n" 429 "- (void)foo {\n}\n" 430 "@end", 431 format("@implementation Foo : Bar\n" 432 "+(id)init{}\n" 433 "-(void)foo{}\n" 434 "@end")); 435 436 verifyFormat("@implementation Foo {\n" 437 " int _i;\n" 438 "}\n" 439 "+ (id)init {\n}\n" 440 "@end"); 441 442 verifyFormat("@implementation Foo : Bar {\n" 443 " int _i;\n" 444 "}\n" 445 "+ (id)init {\n}\n" 446 "@end"); 447 448 verifyFormat("@implementation Foo (HackStuff)\n" 449 "+ (id)init {\n}\n" 450 "@end"); 451 verifyFormat("@implementation ObjcClass\n" 452 "- (void)method;\n" 453 "{}\n" 454 "@end"); 455 456 Style = getGoogleStyle(FormatStyle::LK_ObjC); 457 verifyFormat("@implementation Foo : NSObject {\n" 458 " @public\n" 459 " int field1;\n" 460 " @protected\n" 461 " int field2;\n" 462 " @private\n" 463 " int field3;\n" 464 " @package\n" 465 " int field4;\n" 466 "}\n" 467 "+ (id)init {\n}\n" 468 "@end"); 469 } 470 471 TEST_F(FormatTestObjC, FormatObjCProtocol) { 472 verifyFormat("@protocol Foo\n" 473 "@property(weak) id delegate;\n" 474 "- (NSUInteger)numberOfThings;\n" 475 "@end"); 476 477 verifyFormat("@protocol MyProtocol <NSObject>\n" 478 "- (NSUInteger)numberOfThings;\n" 479 "@end"); 480 481 verifyFormat("@protocol Foo;\n" 482 "@protocol Bar;\n"); 483 484 verifyFormat("@protocol Foo\n" 485 "@end\n" 486 "@protocol Bar\n" 487 "@end"); 488 489 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n" 490 "@property(assign, readwrite) NSInteger bar;\n" 491 "@end"); 492 493 verifyFormat("@protocol myProtocol\n" 494 "- (void)mandatoryWithInt:(int)i;\n" 495 "@optional\n" 496 "- (void)optional;\n" 497 "@required\n" 498 "- (void)required;\n" 499 "@optional\n" 500 "@property(assign) int madProp;\n" 501 "@end\n"); 502 503 verifyFormat("@property(nonatomic, assign, readonly)\n" 504 " int *looooooooooooooooooooooooooooongNumber;\n" 505 "@property(nonatomic, assign, readonly)\n" 506 " NSString *looooooooooooooooooooooooooooongName;"); 507 508 verifyFormat("@implementation PR18406\n" 509 "}\n" 510 "@end"); 511 512 Style = getGoogleStyle(FormatStyle::LK_ObjC); 513 verifyFormat("@protocol MyProtocol <NSObject>\n" 514 "- (NSUInteger)numberOfThings;\n" 515 "@end"); 516 } 517 518 TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) { 519 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n" 520 " rect:(NSRect)theRect\n" 521 " interval:(float)theInterval {\n" 522 "}"); 523 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 524 " longKeyword:(NSRect)theRect\n" 525 " longerKeyword:(float)theInterval\n" 526 " error:(NSError **)theError {\n" 527 "}"); 528 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 529 " longKeyword:(NSRect)theRect\n" 530 " evenLongerKeyword:(float)theInterval\n" 531 " error:(NSError **)theError {\n" 532 "}"); 533 verifyFormat("+ (instancetype)new;\n"); 534 Style.ColumnLimit = 60; 535 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n" 536 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n" 537 " NS_DESIGNATED_INITIALIZER;"); 538 verifyFormat("- (void)drawRectOn:(id)surface\n" 539 " ofSize:(size_t)height\n" 540 " :(size_t)width;"); 541 Style.ColumnLimit = 40; 542 // Make sure selectors with 0, 1, or more arguments are indented when wrapped. 543 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 544 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n"); 545 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); 547 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 548 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n" 549 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); 550 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 551 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n" 552 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); 553 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" 554 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n" 555 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n"); 556 557 // Continuation indent width should win over aligning colons if the function 558 // name is long. 559 Style = getGoogleStyle(FormatStyle::LK_ObjC); 560 Style.ColumnLimit = 40; 561 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 562 " dontAlignNamef:(NSRect)theRect {\n" 563 "}"); 564 565 // Make sure we don't break aligning for short parameter names. 566 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n" 567 " aShortf:(NSRect)theRect {\n" 568 "}"); 569 570 // Format pairs correctly. 571 Style.ColumnLimit = 80; 572 verifyFormat("- (void)drawRectOn:(id)surface\n" 573 " ofSize:(aaaaaaaa)height\n" 574 " :(size_t)width\n" 575 " atOrigin:(size_t)x\n" 576 " :(size_t)y\n" 577 " aaaaa:(a)yyy\n" 578 " bbb:(d)cccc;"); 579 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;"); 580 } 581 582 TEST_F(FormatTestObjC, FormatObjCMethodExpr) { 583 verifyFormat("[foo bar:baz];"); 584 verifyFormat("return [foo bar:baz];"); 585 verifyFormat("return (a)[foo bar:baz];"); 586 verifyFormat("f([foo bar:baz]);"); 587 verifyFormat("f(2, [foo bar:baz]);"); 588 verifyFormat("f(2, a ? b : c);"); 589 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];"); 590 591 // Unary operators. 592 verifyFormat("int a = +[foo bar:baz];"); 593 verifyFormat("int a = -[foo bar:baz];"); 594 verifyFormat("int a = ![foo bar:baz];"); 595 verifyFormat("int a = ~[foo bar:baz];"); 596 verifyFormat("int a = ++[foo bar:baz];"); 597 verifyFormat("int a = --[foo bar:baz];"); 598 verifyFormat("int a = sizeof [foo bar:baz];"); 599 verifyFormat("int a = alignof [foo bar:baz];"); 600 verifyFormat("int a = &[foo bar:baz];"); 601 verifyFormat("int a = *[foo bar:baz];"); 602 // FIXME: Make casts work, without breaking f()[4]. 603 // verifyFormat("int a = (int)[foo bar:baz];"); 604 // verifyFormat("return (int)[foo bar:baz];"); 605 // verifyFormat("(void)[foo bar:baz];"); 606 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];"); 607 608 // Binary operators. 609 verifyFormat("[foo bar:baz], [foo bar:baz];"); 610 verifyFormat("[foo bar:baz] = [foo bar:baz];"); 611 verifyFormat("[foo bar:baz] *= [foo bar:baz];"); 612 verifyFormat("[foo bar:baz] /= [foo bar:baz];"); 613 verifyFormat("[foo bar:baz] %= [foo bar:baz];"); 614 verifyFormat("[foo bar:baz] += [foo bar:baz];"); 615 verifyFormat("[foo bar:baz] -= [foo bar:baz];"); 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] : [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];"); 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 // Whew! 641 642 verifyFormat("return in[42];"); 643 verifyFormat("for (auto v : in[1]) {\n}"); 644 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}"); 645 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}"); 646 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}"); 647 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}"); 648 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}"); 649 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n" 650 "}"); 651 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];"); 652 verifyFormat("[self aaaaa:MACRO(a, b:c:, d:e:)];"); 653 verifyFormat("[self aaaaa:MACRO(a, b:c:d:, e:f:g:)];"); 654 verifyFormat("int XYMyFoo(int a, int b) NS_SWIFT_NAME(foo(self:scale:));"); 655 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];"); 656 verifyFormat("[self aaaaa:(Type)a bbbbb:3];"); 657 658 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];"); 659 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];"); 660 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];"); 661 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];"); 662 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]"); 663 verifyFormat("[button setAction:@selector(zoomOut:)];"); 664 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];"); 665 666 verifyFormat("arr[[self indexForFoo:a]];"); 667 verifyFormat("throw [self errorFor:a];"); 668 verifyFormat("@throw [self errorFor:a];"); 669 670 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];"); 671 verifyFormat("[(id)foo bar:(id) ? baz : quux];"); 672 verifyFormat("4 > 4 ? (id)a : (id)baz;"); 673 674 // This tests that the formatter doesn't break after "backing" but before ":", 675 // which would be at 80 columns. 676 verifyFormat( 677 "void f() {\n" 678 " if ((self = [super initWithContentRect:contentRect\n" 679 " styleMask:styleMask ?: otherMask\n" 680 " backing:NSBackingStoreBuffered\n" 681 " defer:YES]))"); 682 683 verifyFormat( 684 "[foo checkThatBreakingAfterColonWorksOk:\n" 685 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];"); 686 687 verifyFormat("[myObj short:arg1 // Force line break\n" 688 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n" 689 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n" 690 " error:arg4];"); 691 verifyFormat( 692 "void f() {\n" 693 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 694 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 695 " pos.width(), pos.height())\n" 696 " styleMask:NSBorderlessWindowMask\n" 697 " backing:NSBackingStoreBuffered\n" 698 " defer:NO]);\n" 699 "}"); 700 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n" 701 " with:contentsNativeView];"); 702 703 verifyFormat( 704 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n" 705 " owner:nillllll];"); 706 707 verifyFormat( 708 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n" 709 " forType:kBookmarkButtonDragType];"); 710 711 verifyFormat("[defaultCenter addObserver:self\n" 712 " selector:@selector(willEnterFullscreen)\n" 713 " name:kWillEnterFullscreenNotification\n" 714 " object:nil];"); 715 verifyFormat("[image_rep drawInRect:drawRect\n" 716 " fromRect:NSZeroRect\n" 717 " operation:NSCompositeCopy\n" 718 " fraction:1.0\n" 719 " respectFlipped:NO\n" 720 " hints:nil];"); 721 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" 722 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 723 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n" 724 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 725 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" 726 " aaaaaaaaaaaaaaaaaaaaaa];"); 727 728 verifyFormat( 729 "scoped_nsobject<NSTextField> message(\n" 730 " // The frame will be fixed up when |-setMessageText:| is called.\n" 731 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);"); 732 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n" 733 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n" 734 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n" 735 " aaaa:bbb];"); 736 verifyFormat("[self param:function( //\n" 737 " parameter)]"); 738 verifyFormat( 739 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 740 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n" 741 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];"); 742 743 // Variadic parameters. 744 verifyFormat( 745 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];"); 746 verifyFormat( 747 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 748 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n" 749 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];"); 750 verifyFormat("[self // break\n" 751 " a:a\n" 752 " aaa:aaa];"); 753 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n" 754 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);"); 755 756 // Formats pair-parameters. 757 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];"); 758 verifyFormat("[I drawRectOn:surface //\n" 759 " ofSize:aa:bbb\n" 760 " atOrigin:cc:dd];"); 761 762 // Inline block as a first argument. 763 verifyFormat("[object justBlock:^{\n" 764 " a = 42;\n" 765 "}];"); 766 verifyFormat("[object\n" 767 " justBlock:^{\n" 768 " a = 42;\n" 769 " }\n" 770 " notBlock:42\n" 771 " a:42];"); 772 verifyFormat("[object\n" 773 " firstBlock:^{\n" 774 " a = 42;\n" 775 " }\n" 776 " blockWithLongerName:^{\n" 777 " a = 42;\n" 778 " }];"); 779 verifyFormat("[object\n" 780 " blockWithLongerName:^{\n" 781 " a = 42;\n" 782 " }\n" 783 " secondBlock:^{\n" 784 " a = 42;\n" 785 " }];"); 786 verifyFormat("[object\n" 787 " firstBlock:^{\n" 788 " a = 42;\n" 789 " }\n" 790 " notBlock:42\n" 791 " secondBlock:^{\n" 792 " a = 42;\n" 793 " }];"); 794 795 // Space between cast rparen and selector name component. 796 verifyFormat("[((Foo *)foo) bar];"); 797 verifyFormat("[((Foo *)foo) bar:1 blech:2];"); 798 799 // Message receiver taking multiple lines. 800 Style.ColumnLimit = 20; 801 // Non-corner case. 802 verifyFormat("[[object block:^{\n" 803 " return 42;\n" 804 "}] a:42 b:42];"); 805 // Arguments just fit into one line. 806 verifyFormat("[[object block:^{\n" 807 " return 42;\n" 808 "}] aaaaaaa:42 b:42];"); 809 // Arguments just over a column limit. 810 verifyFormat("[[object block:^{\n" 811 " return 42;\n" 812 "}] aaaaaaa:42\n" 813 " bb:42];"); 814 // Arguments just fit into one line. 815 Style.ColumnLimit = 23; 816 verifyFormat("[[obj a:42\n" 817 " b:42\n" 818 " c:42\n" 819 " d:42] e:42 f:42];"); 820 821 // Arguments do not fit into one line with a receiver. 822 Style.ColumnLimit = 20; 823 verifyFormat("[[obj a:42] a:42\n" 824 " b:42];"); 825 verifyFormat("[[obj a:42] a:42\n" 826 " b:42\n" 827 " c:42];"); 828 verifyFormat("[[obj aaaaaa:42\n" 829 " b:42]\n" 830 " cc:42\n" 831 " d:42];"); 832 833 834 Style.ColumnLimit = 70; 835 verifyFormat( 836 "void f() {\n" 837 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n" 838 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n" 839 " pos.width(), pos.height())\n" 840 " syeMask:NSBorderlessWindowMask\n" 841 " bking:NSBackingStoreBuffered\n" 842 " der:NO]);\n" 843 "}"); 844 845 Style.ColumnLimit = 60; 846 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" 847 " .aaaaaaaa];"); // FIXME: Indentation seems off. 848 // FIXME: This violates the column limit. 849 verifyFormat( 850 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 851 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n" 852 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); 853 854 Style = getChromiumStyle(FormatStyle::LK_ObjC); 855 Style.ColumnLimit = 80; 856 verifyFormat( 857 "void f() {\n" 858 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n" 859 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n" 860 " pos.width(), pos.height())\n" 861 " styleMask:NSBorderlessWindowMask\n" 862 " backing:NSBackingStoreBuffered\n" 863 " defer:NO]);\n" 864 "}"); 865 866 // Respect continuation indent and colon alignment (e.g. when object name is 867 // short, and first selector is the longest one) 868 Style = getLLVMStyle(); 869 Style.Language = FormatStyle::LK_ObjC; 870 Style.ContinuationIndentWidth = 8; 871 verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n" 872 " withObject:nil\n" 873 " waitUntilDone:false];"); 874 verifyFormat("[self performSelector:@selector(loadAccessories)\n" 875 " withObjectOnMainThread:nil\n" 876 " waitUntilDone:false];"); 877 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaa\n" 878 " performSelectorOnMainThread:@selector(loadAccessories)\n" 879 " withObject:nil\n" 880 " waitUntilDone:false];"); 881 verifyFormat("[self // force wrapping\n" 882 " performSelectorOnMainThread:@selector(loadAccessories)\n" 883 " withObject:nil\n" 884 " waitUntilDone:false];"); 885 } 886 887 TEST_F(FormatTestObjC, ObjCAt) { 888 verifyFormat("@autoreleasepool"); 889 verifyFormat("@catch"); 890 verifyFormat("@class"); 891 verifyFormat("@compatibility_alias"); 892 verifyFormat("@defs"); 893 verifyFormat("@dynamic"); 894 verifyFormat("@encode"); 895 verifyFormat("@end"); 896 verifyFormat("@finally"); 897 verifyFormat("@implementation"); 898 verifyFormat("@import"); 899 verifyFormat("@interface"); 900 verifyFormat("@optional"); 901 verifyFormat("@package"); 902 verifyFormat("@private"); 903 verifyFormat("@property"); 904 verifyFormat("@protected"); 905 verifyFormat("@protocol"); 906 verifyFormat("@public"); 907 verifyFormat("@required"); 908 verifyFormat("@selector"); 909 verifyFormat("@synchronized"); 910 verifyFormat("@synthesize"); 911 verifyFormat("@throw"); 912 verifyFormat("@try"); 913 914 EXPECT_EQ("@interface", format("@ interface")); 915 916 // The precise formatting of this doesn't matter, nobody writes code like 917 // this. 918 verifyFormat("@ /*foo*/ interface"); 919 } 920 921 TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) { 922 verifyFormat("void DoStuffWithBlockType(int (^)(char));"); 923 verifyFormat("int (^foo)(char, float);"); 924 verifyFormat("int (^foo[10])(char, float);"); 925 verifyFormat("int (^foo[kNumEntries])(char, float);"); 926 verifyFormat("int (^foo[kNumEntries + 10])(char, float);"); 927 verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);"); 928 } 929 930 TEST_F(FormatTestObjC, ObjCSnippets) { 931 verifyFormat("@autoreleasepool {\n" 932 " foo();\n" 933 "}"); 934 verifyFormat("@class Foo, Bar;"); 935 verifyFormat("@compatibility_alias AliasName ExistingClass;"); 936 verifyFormat("@dynamic textColor;"); 937 verifyFormat("char *buf1 = @encode(int *);"); 938 verifyFormat("char *buf1 = @encode(typeof(4 * 5));"); 939 verifyFormat("char *buf1 = @encode(int **);"); 940 verifyFormat("Protocol *proto = @protocol(p1);"); 941 verifyFormat("SEL s = @selector(foo:);"); 942 verifyFormat("@synchronized(self) {\n" 943 " f();\n" 944 "}"); 945 946 verifyFormat("@import foo.bar;\n" 947 "@import baz;"); 948 949 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 950 951 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;"); 952 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 953 954 Style = getMozillaStyle(); 955 verifyFormat("@property (assign, getter=isEditable) BOOL editable;"); 956 verifyFormat("@property BOOL editable;"); 957 958 Style = getWebKitStyle(); 959 verifyFormat("@property (assign, getter=isEditable) BOOL editable;"); 960 verifyFormat("@property BOOL editable;"); 961 962 Style = getGoogleStyle(FormatStyle::LK_ObjC); 963 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;"); 964 verifyFormat("@property(assign, getter=isEditable) BOOL editable;"); 965 } 966 967 TEST_F(FormatTestObjC, ObjCForIn) { 968 verifyFormat("- (void)test {\n" 969 " for (NSString *n in arrayOfStrings) {\n" 970 " foo(n);\n" 971 " }\n" 972 "}"); 973 verifyFormat("- (void)test {\n" 974 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n" 975 " foo(n);\n" 976 " }\n" 977 "}"); 978 verifyFormat("for (Foo *x in bar) {\n}"); 979 verifyFormat("for (Foo *x in [bar baz]) {\n}"); 980 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}"); 981 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}"); 982 verifyFormat("for (Foo *x in [bar baz:^{\n" 983 " [uh oh];\n" 984 " }]) {\n}"); 985 } 986 987 TEST_F(FormatTestObjC, ObjCCxxKeywords) { 988 verifyFormat("+ (instancetype)new {\n" 989 " return nil;\n" 990 "}\n"); 991 verifyFormat("+ (instancetype)myNew {\n" 992 " return [self new];\n" 993 "}\n"); 994 verifyFormat("SEL NewSelector(void) { return @selector(new); }\n"); 995 verifyFormat("SEL MacroSelector(void) { return MACRO(new); }\n"); 996 verifyFormat("+ (instancetype)delete {\n" 997 " return nil;\n" 998 "}\n"); 999 verifyFormat("+ (instancetype)myDelete {\n" 1000 " return [self delete];\n" 1001 "}\n"); 1002 verifyFormat("SEL DeleteSelector(void) { return @selector(delete); }\n"); 1003 verifyFormat("SEL MacroSelector(void) { return MACRO(delete); }\n"); 1004 verifyFormat("MACRO(new:)\n"); 1005 verifyFormat("MACRO(delete:)\n"); 1006 verifyFormat("foo = @{MACRO(new:) : MACRO(delete:)}\n"); 1007 } 1008 1009 TEST_F(FormatTestObjC, ObjCLiterals) { 1010 verifyFormat("@\"String\""); 1011 verifyFormat("@1"); 1012 verifyFormat("@+4.8"); 1013 verifyFormat("@-4"); 1014 verifyFormat("@1LL"); 1015 verifyFormat("@.5"); 1016 verifyFormat("@'c'"); 1017 verifyFormat("@true"); 1018 1019 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);"); 1020 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);"); 1021 verifyFormat("NSNumber *favoriteColor = @(Green);"); 1022 verifyFormat("NSString *path = @(getenv(\"PATH\"));"); 1023 1024 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];"); 1025 } 1026 1027 TEST_F(FormatTestObjC, ObjCDictLiterals) { 1028 verifyFormat("@{"); 1029 verifyFormat("@{}"); 1030 verifyFormat("@{@\"one\" : @1}"); 1031 verifyFormat("return @{@\"one\" : @1;"); 1032 verifyFormat("@{@\"one\" : @1}"); 1033 1034 verifyFormat("@{@\"one\" : @{@2 : @1}}"); 1035 verifyFormat("@{\n" 1036 " @\"one\" : @{@2 : @1},\n" 1037 "}"); 1038 1039 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}"); 1040 verifyIncompleteFormat("[self setDict:@{}"); 1041 verifyIncompleteFormat("[self setDict:@{@1 : @2}"); 1042 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);"); 1043 verifyFormat( 1044 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};"); 1045 verifyFormat( 1046 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};"); 1047 1048 verifyFormat("NSDictionary *d = @{\n" 1049 " @\"nam\" : NSUserNam(),\n" 1050 " @\"dte\" : [NSDate date],\n" 1051 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 1052 "};"); 1053 verifyFormat( 1054 "@{\n" 1055 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 1056 "regularFont,\n" 1057 "};"); 1058 verifyFormat( 1059 "@{\n" 1060 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n" 1061 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n" 1062 "};"); 1063 1064 // We should try to be robust in case someone forgets the "@". 1065 verifyFormat("NSDictionary *d = {\n" 1066 " @\"nam\" : NSUserNam(),\n" 1067 " @\"dte\" : [NSDate date],\n" 1068 " @\"processInfo\" : [NSProcessInfo processInfo]\n" 1069 "};"); 1070 verifyFormat("NSMutableDictionary *dictionary =\n" 1071 " [NSMutableDictionary dictionaryWithDictionary:@{\n" 1072 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n" 1073 " bbbbbbbbbbbbbbbbbb : bbbbb,\n" 1074 " cccccccccccccccc : ccccccccccccccc\n" 1075 " }];"); 1076 1077 // Ensure that casts before the key are kept on the same line as the key. 1078 verifyFormat( 1079 "NSDictionary *d = @{\n" 1080 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n" 1081 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n" 1082 "};"); 1083 Style.ColumnLimit = 40; 1084 verifyFormat("int Foo() {\n" 1085 " a12345 = @{a12345 : a12345};\n" 1086 "}"); 1087 verifyFormat("int Foo() {\n" 1088 " a12345 = @{a12345 : @(a12345)};\n" 1089 "}"); 1090 verifyFormat("int Foo() {\n" 1091 " a12345 = @{(Foo *)a12345 : @(a12345)};\n" 1092 "}"); 1093 verifyFormat("int Foo() {\n" 1094 " a12345 = @{@(a12345) : a12345};\n" 1095 "}"); 1096 verifyFormat("int Foo() {\n" 1097 " a12345 = @{@(a12345) : @YES};\n" 1098 "}"); 1099 Style.SpacesInContainerLiterals = false; 1100 verifyFormat("int Foo() {\n" 1101 " b12345 = @{b12345: b12345};\n" 1102 "}"); 1103 verifyFormat("int Foo() {\n" 1104 " b12345 = @{(Foo *)b12345: @(b12345)};\n" 1105 "}"); 1106 Style.SpacesInContainerLiterals = true; 1107 1108 Style = getGoogleStyle(FormatStyle::LK_ObjC); 1109 verifyFormat( 1110 "@{\n" 1111 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : " 1112 "regularFont,\n" 1113 "};"); 1114 } 1115 1116 TEST_F(FormatTestObjC, ObjCArrayLiterals) { 1117 verifyIncompleteFormat("@["); 1118 verifyFormat("@[]"); 1119 verifyFormat( 1120 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];"); 1121 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];"); 1122 verifyFormat("NSArray *array = @[ [foo description] ];"); 1123 1124 verifyFormat( 1125 "NSArray *some_variable = @[\n" 1126 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 1127 " @\"aaaaaaaaaaaaaaaaa\",\n" 1128 " @\"aaaaaaaaaaaaaaaaa\",\n" 1129 " @\"aaaaaaaaaaaaaaaaa\",\n" 1130 "];"); 1131 verifyFormat( 1132 "NSArray *some_variable = @[\n" 1133 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n" 1134 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n" 1135 "];"); 1136 verifyFormat("NSArray *some_variable = @[\n" 1137 " @\"aaaaaaaaaaaaaaaaa\",\n" 1138 " @\"aaaaaaaaaaaaaaaaa\",\n" 1139 " @\"aaaaaaaaaaaaaaaaa\",\n" 1140 " @\"aaaaaaaaaaaaaaaaa\",\n" 1141 "];"); 1142 verifyFormat("NSArray *array = @[\n" 1143 " @\"a\",\n" 1144 " @\"a\",\n" // Trailing comma -> one per line. 1145 "];"); 1146 1147 // We should try to be robust in case someone forgets the "@". 1148 verifyFormat("NSArray *some_variable = [\n" 1149 " @\"aaaaaaaaaaaaaaaaa\",\n" 1150 " @\"aaaaaaaaaaaaaaaaa\",\n" 1151 " @\"aaaaaaaaaaaaaaaaa\",\n" 1152 " @\"aaaaaaaaaaaaaaaaa\",\n" 1153 "];"); 1154 verifyFormat( 1155 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n" 1156 " index:(NSUInteger)index\n" 1157 " nonDigitAttributes:\n" 1158 " (NSDictionary *)noDigitAttributes;"); 1159 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n" 1160 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n" 1161 "]];"); 1162 Style.ColumnLimit = 40; 1163 verifyFormat("int Foo() {\n" 1164 " a12345 = @[ a12345, a12345 ];\n" 1165 "}"); 1166 verifyFormat("int Foo() {\n" 1167 " a123 = @[ (Foo *)a12345, @(a12345) ];\n" 1168 "}"); 1169 Style.SpacesInContainerLiterals = false; 1170 verifyFormat("int Foo() {\n" 1171 " b12345 = @[b12345, b12345];\n" 1172 "}"); 1173 verifyFormat("int Foo() {\n" 1174 " b12345 = @[(Foo *)b12345, @(b12345)];\n" 1175 "}"); 1176 Style.SpacesInContainerLiterals = true; 1177 Style.ColumnLimit = 20; 1178 // We can't break string literals inside NSArray literals 1179 // (that raises -Wobjc-string-concatenation). 1180 verifyFormat("NSArray *foo = @[\n" 1181 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n" 1182 "];\n"); 1183 } 1184 1185 TEST_F(FormatTestObjC, BreaksCallStatementWhereSemiJustOverTheLimit) { 1186 Style.ColumnLimit = 60; 1187 // If the statement starting with 'a = ...' is put on a single line, the ';' 1188 // is at line 61. 1189 verifyFormat("int f(int a) {\n" 1190 " a = [self aaaaaaaaaa:bbbbbbbbb\n" 1191 " ccccccccc:dddddddd\n" 1192 " ee:fddd];\n" 1193 "}"); 1194 } 1195 1196 } // end namespace 1197 } // end namespace format 1198 } // end namespace clang 1199