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