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