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