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