1 //===- unittest/Format/FormatTestObjC.cpp - Formatting unit tests----------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "FormatTestBase.h"
11 #define DEBUG_TYPE "format-test-objc"
18 class FormatTestObjC
: public FormatTestBase
{
21 Style
= getLLVMStyle();
22 Style
.Language
= FormatStyle::LK_ObjC
;
25 FormatStyle
getDefaultStyle() const override
{ return Style
; }
30 #define verifyIncompleteFormat(...) \
31 _verifyIncompleteFormat(__FILE__, __LINE__, __VA_ARGS__)
32 #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__)
34 TEST(FormatTestObjCStyle
, DetectsObjCInStdin
) {
35 auto Style
= getStyle("LLVM", "<stdin>", "none",
38 ASSERT_TRUE((bool)Style
);
39 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
42 TEST(FormatTestObjCStyle
, DetectsObjCInHeaders
) {
43 auto Style
= getStyle("LLVM", "a.h", "none",
46 ASSERT_TRUE((bool)Style
);
47 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
49 Style
= getStyle("LLVM", "a.h", "none",
52 ASSERT_TRUE((bool)Style
);
53 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
55 Style
= getStyle("LLVM", "a.h", "none",
59 ASSERT_TRUE((bool)Style
);
60 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
62 Style
= getStyle("LLVM", "a.h", "none",
65 ASSERT_TRUE((bool)Style
);
66 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
68 // No recognizable ObjC.
69 Style
= getStyle("LLVM", "a.h", "none", "void f() {}");
70 ASSERT_TRUE((bool)Style
);
71 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
73 Style
= getStyle("{}", "a.h", "none", "@interface Foo\n@end");
74 ASSERT_TRUE((bool)Style
);
75 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
77 Style
= getStyle("{}", "a.h", "none",
78 "const int interface = 1;\nconst int end = 2;");
79 ASSERT_TRUE((bool)Style
);
80 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
82 Style
= getStyle("{}", "a.h", "none", "@protocol Foo\n@end");
83 ASSERT_TRUE((bool)Style
);
84 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
86 Style
= getStyle("{}", "a.h", "none",
87 "const int protocol = 1;\nconst int end = 2;");
88 ASSERT_TRUE((bool)Style
);
89 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
91 Style
= getStyle("{}", "a.h", "none", "typedef NS_ENUM(int, Foo) {};");
92 ASSERT_TRUE((bool)Style
);
93 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
95 Style
= getStyle("{}", "a.h", "none", "typedef NS_CLOSED_ENUM(int, Foo) {};");
96 ASSERT_TRUE((bool)Style
);
97 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
99 Style
= getStyle("{}", "a.h", "none", "typedef NS_ERROR_ENUM(int, Foo) {};");
100 ASSERT_TRUE((bool)Style
);
101 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
103 Style
= getStyle("{}", "a.h", "none", R
"objc(
104 NS_ASSUME_NONNULL_BEGIN
106 NS_ASSUME_NONNULL_END
108 ASSERT_TRUE((bool)Style
);
109 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
111 Style
= getStyle("{}", "a.h", "none", R
"objc(
112 FOUNDATION_EXTERN void DoStuff(void);
114 ASSERT_TRUE((bool)Style
);
115 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
117 Style
= getStyle("{}", "a.h", "none", R
"objc(
118 FOUNDATION_EXPORT void DoStuff(void);
120 ASSERT_TRUE((bool)Style
);
121 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
123 Style
= getStyle("{}", "a.h", "none", "enum Foo {};");
124 ASSERT_TRUE((bool)Style
);
125 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
127 Style
= getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }");
128 ASSERT_TRUE((bool)Style
);
129 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
131 Style
= getStyle("{}", "a.h", "none", "inline void Foo() { Log(\"Foo\"); }");
132 ASSERT_TRUE((bool)Style
);
133 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
136 getStyle("{}", "a.h", "none", "inline void Foo() { id = @[1, 2, 3]; }");
137 ASSERT_TRUE((bool)Style
);
138 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
140 Style
= getStyle("{}", "a.h", "none",
141 "inline void Foo() { id foo = @{1: 2, 3: 4, 5: 6}; }");
142 ASSERT_TRUE((bool)Style
);
143 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
145 Style
= getStyle("{}", "a.h", "none",
146 "inline void Foo() { int foo[] = {1, 2, 3}; }");
147 ASSERT_TRUE((bool)Style
);
148 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
150 // ObjC characteristic types.
151 Style
= getStyle("{}", "a.h", "none", "extern NSString *kFoo;");
152 ASSERT_TRUE((bool)Style
);
153 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
155 Style
= getStyle("{}", "a.h", "none", "extern NSInteger Foo();");
156 ASSERT_TRUE((bool)Style
);
157 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
159 Style
= getStyle("{}", "a.h", "none", "NSObject *Foo();");
160 ASSERT_TRUE((bool)Style
);
161 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
163 Style
= getStyle("{}", "a.h", "none", "NSSet *Foo();");
164 ASSERT_TRUE((bool)Style
);
165 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
168 TEST(FormatTestObjCStyle
, AvoidDetectingDesignatedInitializersAsObjCInHeaders
) {
169 auto Style
= getStyle("LLVM", "a.h", "none",
170 "static const char *names[] = {[0] = \"foo\",\n"
171 "[kBar] = \"bar\"};");
172 ASSERT_TRUE((bool)Style
);
173 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
175 Style
= getStyle("LLVM", "a.h", "none",
176 "static const char *names[] = {[0] EQ \"foo\",\n"
177 "[kBar] EQ \"bar\"};");
178 ASSERT_TRUE((bool)Style
);
179 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
182 TEST_F(FormatTestObjC
, FormatObjCTryCatch
) {
183 verifyFormat("@try {\n"
185 "} @catch (NSException e) {\n"
190 verifyFormat("DEBUG({\n"
197 TEST_F(FormatTestObjC
, FormatObjCAutoreleasepool
) {
198 verifyFormat("@autoreleasepool {\n"
201 "@autoreleasepool {\n"
204 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
205 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
206 verifyFormat("@autoreleasepool\n"
216 TEST_F(FormatTestObjC
, FormatObjCGenerics
) {
217 Style
.ColumnLimit
= 40;
218 verifyFormat("int aaaaaaaaaaaaaaaa(\n"
219 " NSArray<aaaaaaaaaaaaaaaaaa *>\n"
220 " aaaaaaaaaaaaaaaaa);");
221 verifyFormat("int aaaaaaaaaaaaaaaa(\n"
222 " NSArray<aaaaaaaaaaaaaaaaaaa<\n"
223 " aaaaaaaaaaaaaaaa *> *>\n"
224 " aaaaaaaaaaaaaaaaa);");
227 TEST_F(FormatTestObjC
, FormatObjCSynchronized
) {
228 verifyFormat("@synchronized(self) {\n"
231 "@synchronized(self) {\n"
234 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
235 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
236 verifyFormat("@synchronized(self)\n"
240 "@synchronized(self)\n"
246 TEST_F(FormatTestObjC
, FormatObjCInterface
) {
247 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
260 verifyFormat("@interface /* wait for it */ Foo\n"
262 "// Look, a comment!\n"
263 "- (int)answerWith:(int)i;\n"
266 verifyFormat("@interface Foo\n"
271 verifyFormat("@interface Foo : Bar\n"
272 "@property(assign, readwrite) NSInteger bar;\n"
276 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @interface Foo : Bar\n"
277 "@property(assign, readwrite) NSInteger bar;\n"
281 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
285 verifyFormat("@interface Foo (HackStuff)\n"
289 verifyFormat("@interface Foo ()\n"
293 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
297 verifyFormat("@interface Foo {\n"
303 verifyFormat("@interface Foo : Bar {\n"
309 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
315 verifyFormat("@interface Foo<Baz : Blech> : Bar <Baz, Quux> {\n"
321 verifyFormat("@interface Foo<Bar : Baz <Blech>> : Xyzzy <Corge> {\n"
327 verifyFormat("@interface Foo<Bar : Baz <Blech>> : Xyzzy <Corge> <Quux> {\n"
333 verifyFormat("@interface Foo : Bar <Baz> <Blech>\n"
336 verifyFormat("@interface Foo : Bar <Baz> <Blech, Xyzzy, Corge>\n"
339 verifyFormat("@interface Foo (HackStuff) {\n"
345 verifyFormat("@interface Foo () {\n"
351 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
356 verifyFormat("@interface Foo\n"
360 "@implementation Bar\n"
364 Style
.ColumnLimit
= 40;
365 verifyFormat("@interface ccccccccccccc () <\n"
366 " ccccccccccccc, ccccccccccccc,\n"
367 " ccccccccccccc, ccccccccccccc> {\n"
369 verifyFormat("@interface ccccccccccccc (ccccccccccc) <\n"
370 " ccccccccccccc> {\n"
372 Style
.ObjCBinPackProtocolList
= FormatStyle::BPS_Never
;
373 verifyFormat("@interface ddddddddddddd () <\n"
377 " ddddddddddddd> {\n"
380 Style
.BinPackParameters
= FormatStyle::BPPS_OnePerLine
;
381 Style
.ObjCBinPackProtocolList
= FormatStyle::BPS_Auto
;
382 verifyFormat("@interface eeeeeeeeeeeee () <\n"
386 " eeeeeeeeeeeee> {\n"
388 Style
.ObjCBinPackProtocolList
= FormatStyle::BPS_Always
;
389 verifyFormat("@interface fffffffffffff () <\n"
390 " fffffffffffff, fffffffffffff,\n"
391 " fffffffffffff, fffffffffffff> {\n"
394 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
395 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
407 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
410 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
413 Style
.ColumnLimit
= 40;
414 // BinPackParameters should be BPPS_BinPack by default.
415 verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n"
416 " int eeeee, int eeeee);");
417 // ObjCBinPackProtocolList should be BPS_Never by default.
418 verifyFormat("@interface fffffffffffff () <\n"
422 " fffffffffffff> {\n"
424 verifyFormat("@interface ggggggggggggg\n"
425 " : ggggggggggggg <ggggggggggggg>\n"
430 TEST_F(FormatTestObjC
, FormatObjCImplementation
) {
431 verifyFormat("@implementation Foo : NSObject {\n"
444 verifyFormat("@implementation Foo\n"
449 "// Look, a comment!\n"
450 "- (int)answerWith:(int)i {\n"
453 "+ (int)answerWith:(int)i {\n"
458 verifyFormat("@implementation Foo\n"
460 "@implementation Bar\n"
463 EXPECT_EQ("@implementation Foo : Bar\n"
467 format("@implementation Foo : Bar\n"
472 verifyFormat("@implementation Foo {\n"
478 verifyFormat("@implementation Foo : Bar {\n"
484 verifyFormat("@implementation Foo (HackStuff)\n"
487 verifyFormat("@implementation ObjcClass\n"
492 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
493 verifyFormat("@implementation Foo : NSObject {\n"
507 TEST_F(FormatTestObjC
, FormatObjCProtocol
) {
508 verifyFormat("@protocol Foo\n"
509 "@property(weak) id delegate;\n"
510 "- (NSUInteger)numberOfThings;\n"
513 verifyFormat("@protocol MyProtocol <NSObject>\n"
514 "- (NSUInteger)numberOfThings;\n"
517 verifyFormat("@protocol Foo;\n"
520 verifyFormat("@protocol Foo\n"
525 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n"
526 "@property(assign, readwrite) NSInteger bar;\n"
529 verifyFormat("@protocol myProtocol\n"
530 "- (void)mandatoryWithInt:(int)i;\n"
532 "- (void)optional;\n"
534 "- (void)required;\n"
536 "@property(assign) int madProp;\n"
539 verifyFormat("@property(nonatomic, assign, readonly)\n"
540 " int *looooooooooooooooooooooooooooongNumber;\n"
541 "@property(nonatomic, assign, readonly)\n"
542 " NSString *looooooooooooooooooooooooooooongName;");
544 verifyFormat("@implementation PR18406\n"
548 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
549 verifyFormat("@protocol MyProtocol <NSObject>\n"
550 "- (NSUInteger)numberOfThings;\n"
554 TEST_F(FormatTestObjC
, FormatObjCMethodDeclarations
) {
555 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
556 " rect:(NSRect)theRect\n"
557 " interval:(float)theInterval {\n"
559 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
560 " longKeyword:(NSRect)theRect\n"
561 " longerKeyword:(float)theInterval\n"
562 " error:(NSError **)theError {\n"
564 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
565 " longKeyword:(NSRect)theRect\n"
566 " evenLongerKeyword:(float)theInterval\n"
567 " error:(NSError **)theError {\n"
569 verifyFormat("+ (instancetype)new;");
570 Style
.ColumnLimit
= 60;
571 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
572 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
573 " NS_DESIGNATED_INITIALIZER;");
574 verifyFormat("- (void)drawRectOn:(id)surface\n"
575 " ofSize:(size_t)height\n"
577 Style
.ColumnLimit
= 40;
578 // Make sure selectors with 0, 1, or more arguments are indented when wrapped.
579 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
581 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;");
583 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
584 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
585 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;");
586 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
587 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
588 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;");
589 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
590 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
591 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;");
593 // Continuation indent width should win over aligning colons if the function
595 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
596 Style
.ColumnLimit
= 40;
597 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
598 " dontAlignNamef:(NSRect)theRect {\n"
601 // Make sure we don't break aligning for short parameter names.
602 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
603 " aShortf:(NSRect)theRect {\n"
606 // Format pairs correctly.
607 Style
.ColumnLimit
= 80;
608 verifyFormat("- (void)drawRectOn:(id)surface\n"
609 " ofSize:(aaaaaaaa)height\n"
611 " atOrigin:(size_t)x\n"
615 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
617 // BraceWrapping AfterFunction is respected for ObjC methods
618 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
619 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
620 Style
.BraceWrapping
.AfterFunction
= true;
621 verifyFormat("@implementation Foo\n"
622 "- (void)foo:(id)bar\n"
628 TEST_F(FormatTestObjC
, FormatObjCMethodExpr
) {
629 verifyFormat("[foo bar:baz];");
630 verifyFormat("[foo bar]->baz;");
631 verifyFormat("return [foo bar:baz];");
632 verifyFormat("return (a)[foo bar:baz];");
633 verifyFormat("f([foo bar:baz]);");
634 verifyFormat("f(2, [foo bar:baz]);");
635 verifyFormat("f(2, a ? b : c);");
636 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
639 verifyFormat("int a = +[foo bar:baz];");
640 verifyFormat("int a = -[foo bar:baz];");
641 verifyFormat("int a = ![foo bar:baz];");
642 verifyFormat("int a = ~[foo bar:baz];");
643 verifyFormat("int a = ++[foo bar:baz];");
644 verifyFormat("int a = --[foo bar:baz];");
645 verifyFormat("int a = sizeof [foo bar:baz];");
646 verifyFormat("int a = alignof [foo bar:baz];");
647 verifyFormat("int a = &[foo bar:baz];");
648 verifyFormat("int a = *[foo bar:baz];");
649 // FIXME: Make casts work, without breaking f()[4].
650 // verifyFormat("int a = (int)[foo bar:baz];");
651 // verifyFormat("return (int)[foo bar:baz];");
652 // verifyFormat("(void)[foo bar:baz];");
653 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
656 verifyFormat("[foo bar:baz], [foo bar:baz];");
657 verifyFormat("[foo bar:baz] = [foo bar:baz];");
658 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
659 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
660 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
661 verifyFormat("[foo bar:baz] += [foo bar:baz];");
662 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
663 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
664 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
665 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
666 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
667 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
668 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
669 verifyFormat("[foo bar:baz] || [foo bar:baz];");
670 verifyFormat("[foo bar:baz] && [foo bar:baz];");
671 verifyFormat("[foo bar:baz] | [foo bar:baz];");
672 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
673 verifyFormat("[foo bar:baz] & [foo bar:baz];");
674 verifyFormat("[foo bar:baz] == [foo bar:baz];");
675 verifyFormat("[foo bar:baz] != [foo bar:baz];");
676 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
677 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
678 verifyFormat("[foo bar:baz] > [foo bar:baz];");
679 verifyFormat("[foo bar:baz] < [foo bar:baz];");
680 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
681 verifyFormat("[foo bar:baz] << [foo bar:baz];");
682 verifyFormat("[foo bar:baz] - [foo bar:baz];");
683 verifyFormat("[foo bar:baz] + [foo bar:baz];");
684 verifyFormat("[foo bar:baz] * [foo bar:baz];");
685 verifyFormat("[foo bar:baz] / [foo bar:baz];");
686 verifyFormat("[foo bar:baz] % [foo bar:baz];");
689 verifyFormat("return in[42];");
690 verifyFormat("for (auto v : in[1]) {\n}");
691 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
692 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
693 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
694 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
695 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
696 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
698 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
699 verifyFormat("[self aaaaa:MACRO(a, b:c:, d:e:)];");
700 verifyFormat("[self aaaaa:MACRO(a, b:c:d:, e:f:g:)];");
701 verifyFormat("int XYMyFoo(int a, int b) NS_SWIFT_NAME(foo(self:scale:));");
702 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
703 verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
705 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
706 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
707 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
708 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
709 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
710 verifyFormat("[button setAction:@selector(zoomOut:)];");
711 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
713 verifyFormat("arr[[self indexForFoo:a]];");
714 verifyFormat("throw [self errorFor:a];");
715 verifyFormat("@throw [self errorFor:a];");
717 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
718 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
719 verifyFormat("4 > 4 ? (id)a : (id)baz;");
721 unsigned PreviousColumnLimit
= Style
.ColumnLimit
;
722 Style
.ColumnLimit
= 50;
725 // ([object a:42] == 0 || [object a:42
727 verifyFormat("bool a = ([object a:42] == 0 ||\n"
728 " [object a:42 b:42] == 0);");
729 Style
.ColumnLimit
= PreviousColumnLimit
;
730 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
731 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
733 // This tests that the formatter doesn't break after "backing" but before ":",
734 // which would be at 80 columns.
737 " if ((self = [super initWithContentRect:contentRect\n"
738 " styleMask:styleMask ?: otherMask\n"
739 " backing:NSBackingStoreBuffered\n"
743 "[foo checkThatBreakingAfterColonWorksOk:\n"
744 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
746 verifyFormat("[myObj short:arg1 // Force line break\n"
747 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
748 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
752 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
753 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
754 " pos.width(), pos.height())\n"
755 " styleMask:NSBorderlessWindowMask\n"
756 " backing:NSBackingStoreBuffered\n"
759 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
760 " with:contentsNativeView];");
763 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
764 " owner:nillllll];");
767 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
768 " forType:kBookmarkButtonDragType];");
770 verifyFormat("[defaultCenter addObserver:self\n"
771 " selector:@selector(willEnterFullscreen)\n"
772 " name:kWillEnterFullscreenNotification\n"
774 verifyFormat("[image_rep drawInRect:drawRect\n"
775 " fromRect:NSZeroRect\n"
776 " operation:NSCompositeCopy\n"
778 " respectFlipped:NO\n"
780 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
781 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
782 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
783 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
784 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
785 " aaaaaaaaaaaaaaaaaaaaaa];");
788 "scoped_nsobject<NSTextField> message(\n"
789 " // The frame will be fixed up when |-setMessageText:| is called.\n"
790 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
791 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
792 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
793 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
795 verifyFormat("[self param:function( //\n"
798 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
799 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
800 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
802 // Variadic parameters.
804 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
806 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
807 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
808 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
810 verifyFormat("[self // break\n"
814 // Formats pair-parameters.
815 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
816 verifyFormat("[I drawRectOn:surface //\n"
818 " atOrigin:cc:dd];");
820 // Inline block as a first argument.
821 verifyFormat("[object justBlock:^{\n"
824 verifyFormat("[object\n"
830 verifyFormat("[object\n"
834 " blockWithLongerName:^{\n"
837 verifyFormat("[object\n"
838 " blockWithLongerName:^{\n"
844 verifyFormat("[object\n"
853 // Space between cast rparen and selector name component.
854 verifyFormat("[((Foo *)foo) bar];");
855 verifyFormat("[((Foo *)foo) bar:1 blech:2];");
857 Style
.ColumnLimit
= 20;
858 verifyFormat("aaaaa = [a aa:aa\n"
860 verifyFormat("aaaaaa = [aa aa:aa\n"
863 // Message receiver taking multiple lines.
865 verifyFormat("[[object block:^{\n"
868 // Arguments just fit into one line.
869 verifyFormat("[[object block:^{\n"
871 "}] aaaaaaa:42 b:42];");
872 // Arguments just over a column limit.
873 verifyFormat("[[object block:^{\n"
877 // Arguments just fit into one line.
878 Style
.ColumnLimit
= 23;
879 verifyFormat("[[obj a:42\n"
882 " d:42] e:42 f:42];");
884 // Arguments do not fit into one line with a receiver.
885 Style
.ColumnLimit
= 20;
886 verifyFormat("[[obj a:42] a:42\n"
888 verifyFormat("[[obj a:42] a:42\n"
891 verifyFormat("[[obj aaaaaa:42\n"
896 // Avoid breaking receiver expression.
897 Style
.ColumnLimit
= 30;
898 verifyFormat("fooooooo =\n"
899 " [[obj fooo] aaa:42\n"
901 verifyFormat("[[[obj foo] bar] aa:42\n"
905 // Avoid breaking between unary operators and ObjC method expressions.
906 Style
.ColumnLimit
= 45;
907 verifyFormat("if (a012345678901234567890123 &&\n"
910 verifyFormat("if (a012345678901234567890123 &&\n"
913 verifyFormat("if (a012345678901234567890123 &&\n"
917 Style
.ColumnLimit
= 70;
920 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
921 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
922 " pos.width(), pos.height())\n"
923 " syeMask:NSBorderlessWindowMask\n"
924 " bking:NSBackingStoreBuffered\n"
928 Style
.ColumnLimit
= 60;
929 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
930 " .aaaaaaaa];"); // FIXME: Indentation seems off.
931 // FIXME: This violates the column limit.
933 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
934 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
935 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
937 Style
= getChromiumStyle(FormatStyle::LK_ObjC
);
938 Style
.ColumnLimit
= 80;
941 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
942 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
943 " pos.width(), pos.height())\n"
944 " styleMask:NSBorderlessWindowMask\n"
945 " backing:NSBackingStoreBuffered\n"
949 // Respect continuation indent and colon alignment (e.g. when object name is
950 // short, and first selector is the longest one)
951 Style
= getLLVMStyle();
952 Style
.Language
= FormatStyle::LK_ObjC
;
953 Style
.ContinuationIndentWidth
= 8;
954 verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
956 " waitUntilDone:false];");
957 verifyFormat("[self performSelector:@selector(loadAccessories)\n"
958 " withObjectOnMainThread:nil\n"
959 " waitUntilDone:false];");
961 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
962 " performSelectorOnMainThread:@selector(loadAccessories)\n"
964 " waitUntilDone:false];");
966 "[self // force wrapping\n"
967 " performSelectorOnMainThread:@selector(loadAccessories)\n"
969 " waitUntilDone:false];");
971 // The appropriate indentation is used after a block statement.
972 Style
.ContinuationIndentWidth
= 4;
974 "void aaaaaaaaaaaaaaaaaaaaa(int c) {\n"
978 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd\n"
979 " eeeeeeeeeeeeeeeeeeeeeeeeeeeee:^(fffffffffffffff gggggggg) {\n"
985 TEST_F(FormatTestObjC
, ObjCAt
) {
986 verifyFormat("@autoreleasepool");
987 verifyFormat("@catch");
988 verifyFormat("@class");
989 verifyFormat("@compatibility_alias");
990 verifyFormat("@defs");
991 verifyFormat("@dynamic");
992 verifyFormat("@encode");
993 verifyFormat("@end");
994 verifyFormat("@finally");
995 verifyFormat("@implementation");
996 verifyFormat("@import");
997 verifyFormat("@interface");
998 verifyFormat("@optional");
999 verifyFormat("@package");
1000 verifyFormat("@private");
1001 verifyFormat("@property");
1002 verifyFormat("@protected");
1003 verifyFormat("@protocol");
1004 verifyFormat("@public");
1005 verifyFormat("@required");
1006 verifyFormat("@selector");
1007 verifyFormat("@synchronized");
1008 verifyFormat("@synthesize");
1009 verifyFormat("@throw");
1010 verifyFormat("@try");
1012 EXPECT_EQ("@interface", format("@ interface"));
1014 // The precise formatting of this doesn't matter, nobody writes code like
1016 verifyFormat("@ /*foo*/ interface");
1019 TEST_F(FormatTestObjC
, ObjCBlockTypesAndVariables
) {
1020 verifyFormat("void DoStuffWithBlockType(int (^)(char));");
1021 verifyFormat("int (^foo)(char, float);");
1022 verifyFormat("int (^foo[10])(char, float);");
1023 verifyFormat("int (^foo[kNumEntries])(char, float);");
1024 verifyFormat("int (^foo[kNumEntries + 10])(char, float);");
1025 verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);");
1027 verifyFormat("int *p = ^int *() { //\n"
1028 " return nullptr;\n"
1031 verifyFormat("int * (^p)(void) = ^int *(void) { //\n"
1032 " return nullptr;\n"
1035 // WebKit forces function braces onto a newline, but blocks should not.
1036 verifyFormat("int* p = ^int*() { //\n"
1037 " return nullptr;\n"
1042 TEST_F(FormatTestObjC
, ObjCSnippets
) {
1043 verifyFormat("@autoreleasepool {\n"
1046 verifyFormat("@class Foo, Bar;");
1047 verifyFormat("@compatibility_alias AliasName ExistingClass;");
1048 verifyFormat("@dynamic textColor;");
1049 verifyFormat("char *buf1 = @encode(int *);");
1050 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
1051 verifyFormat("char *buf1 = @encode(int **);");
1052 verifyFormat("Protocol *proto = @protocol(p1);");
1053 verifyFormat("SEL s = @selector(foo:);");
1054 verifyFormat("@synchronized(self) {\n"
1058 verifyFormat("@import foo.bar;\n"
1061 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1063 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
1064 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1066 verifyFormat("extern UIWindow *MainWindow(void) "
1067 "NS_SWIFT_NAME(getter:MyHelper.mainWindow());");
1069 verifyFormat("extern UIWindow *MainWindow(void) "
1070 "CF_SWIFT_NAME(getter:MyHelper.mainWindow());");
1072 Style
.ColumnLimit
= 50;
1073 verifyFormat("@interface Foo\n"
1074 "- (void)doStuffWithFoo:(id)name\n"
1077 " NS_SWIFT_NAME(doStuff(withFoo:bar:baz:));\n"
1080 Style
= getMozillaStyle();
1081 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
1082 verifyFormat("@property BOOL editable;");
1084 Style
= getWebKitStyle();
1085 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
1086 verifyFormat("@property BOOL editable;");
1088 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
1089 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1090 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1093 TEST_F(FormatTestObjC
, ObjCForIn
) {
1094 verifyFormat("- (void)test {\n"
1095 " for (NSString *n in arrayOfStrings) {\n"
1099 verifyFormat("- (void)test {\n"
1100 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
1104 verifyFormat("for (Foo *x in bar) {\n}");
1105 verifyFormat("for (Foo *x in [bar baz]) {\n}");
1106 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
1107 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
1108 verifyFormat("for (Foo *x in [bar baz:^{\n"
1113 TEST_F(FormatTestObjC
, ObjCCxxKeywords
) {
1114 verifyFormat("+ (instancetype)new {\n"
1117 verifyFormat("+ (instancetype)myNew {\n"
1118 " return [self new];\n"
1120 verifyFormat("SEL NewSelector(void) { return @selector(new); }");
1121 verifyFormat("SEL MacroSelector(void) { return MACRO(new); }");
1122 verifyFormat("+ (instancetype)delete {\n"
1125 verifyFormat("+ (instancetype)myDelete {\n"
1126 " return [self delete];\n"
1128 verifyFormat("SEL DeleteSelector(void) { return @selector(delete); }");
1129 verifyFormat("SEL MacroSelector(void) { return MACRO(delete); }");
1130 verifyFormat("MACRO(new:)");
1131 verifyFormat("MACRO(delete:)");
1132 verifyFormat("foo = @{MACRO(new:) : MACRO(delete:)}");
1133 verifyFormat("@implementation Foo\n"
1135 "- (Class)class {\n"
1140 verifyFormat("@implementation Foo\n"
1141 "- (Class)class {\n"
1146 verifyFormat("@implementation Foo\n"
1147 "+ (Class)class {\n"
1152 verifyFormat("@implementation Foo\n"
1153 "- (Class)class:(Class)klass {\n"
1158 verifyFormat("@implementation Foo\n"
1159 "+ (Class)class:(Class)klass {\n"
1165 verifyFormat("@interface Foo\n"
1170 verifyFormat("@interface Foo\n"
1174 verifyFormat("@interface Foo\n"
1178 verifyFormat("@interface Foo\n"
1179 "- (Class)class:(Class)klass;\n"
1182 verifyFormat("@interface Foo\n"
1183 "+ (Class)class:(Class)klass;\n"
1188 TEST_F(FormatTestObjC
, ObjCLiterals
) {
1189 verifyFormat("@\"String\"");
1191 verifyFormat("@+4.8");
1192 verifyFormat("@-4");
1193 verifyFormat("@1LL");
1194 verifyFormat("@.5");
1195 verifyFormat("@'c'");
1196 verifyFormat("@true");
1198 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
1199 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
1200 verifyFormat("NSNumber *favoriteColor = @(Green);");
1201 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
1203 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
1206 TEST_F(FormatTestObjC
, ObjCDictLiterals
) {
1208 verifyFormat("@{}");
1209 verifyFormat("@{@\"one\" : @1}");
1210 verifyFormat("return @{@\"one\" : @1;");
1211 verifyFormat("@{@\"one\" : @1}");
1213 verifyFormat("@{@\"one\" : @{@2 : @1}}");
1215 " @\"one\" : @{@2 : @1},\n"
1218 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
1219 verifyIncompleteFormat("[self setDict:@{}");
1220 verifyIncompleteFormat("[self setDict:@{@1 : @2}");
1221 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
1223 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
1225 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
1227 verifyFormat("NSDictionary *d = @{\n"
1228 " @\"nam\" : NSUserNam(),\n"
1229 " @\"dte\" : [NSDate date],\n"
1230 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
1234 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1239 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
1240 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
1243 // We should try to be robust in case someone forgets the "@".
1244 verifyFormat("NSDictionary *d = {\n"
1245 " @\"nam\" : NSUserNam(),\n"
1246 " @\"dte\" : [NSDate date],\n"
1247 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
1249 verifyFormat("NSMutableDictionary *dictionary =\n"
1250 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
1251 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
1252 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
1253 " cccccccccccccccc : ccccccccccccccc\n"
1256 // Ensure that casts before the key are kept on the same line as the key.
1258 "NSDictionary *d = @{\n"
1259 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1260 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
1262 Style
.ColumnLimit
= 40;
1263 verifyFormat("int Foo() {\n"
1264 " a12345 = @{a12345 : a12345};\n"
1266 verifyFormat("int Foo() {\n"
1267 " a12345 = @{a12345 : @(a12345)};\n"
1269 verifyFormat("int Foo() {\n"
1270 " a12345 = @{(Foo *)a12345 : @(a12345)};\n"
1272 verifyFormat("int Foo() {\n"
1273 " a12345 = @{@(a12345) : a12345};\n"
1275 verifyFormat("int Foo() {\n"
1276 " a12345 = @{@(a12345) : @YES};\n"
1278 Style
.SpacesInContainerLiterals
= false;
1279 verifyFormat("int Foo() {\n"
1280 " b12345 = @{b12345: b12345};\n"
1282 verifyFormat("int Foo() {\n"
1283 " b12345 = @{(Foo *)b12345: @(b12345)};\n"
1285 Style
.SpacesInContainerLiterals
= true;
1287 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
1290 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1295 TEST_F(FormatTestObjC
, ObjCArrayLiterals
) {
1296 verifyIncompleteFormat("@[");
1297 verifyFormat("@[]");
1299 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
1300 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
1301 verifyFormat("NSArray *array = @[ [foo description] ];");
1304 "NSArray *some_variable = @[\n"
1305 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1306 " @\"aaaaaaaaaaaaaaaaa\",\n"
1307 " @\"aaaaaaaaaaaaaaaaa\",\n"
1308 " @\"aaaaaaaaaaaaaaaaa\",\n"
1311 "NSArray *some_variable = @[\n"
1312 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1313 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
1315 verifyFormat("NSArray *some_variable = @[\n"
1316 " @\"aaaaaaaaaaaaaaaaa\",\n"
1317 " @\"aaaaaaaaaaaaaaaaa\",\n"
1318 " @\"aaaaaaaaaaaaaaaaa\",\n"
1319 " @\"aaaaaaaaaaaaaaaaa\",\n"
1321 verifyFormat("NSArray *array = @[\n"
1323 " @\"a\",\n" // Trailing comma -> one per line.
1326 // We should try to be robust in case someone forgets the "@".
1327 verifyFormat("NSArray *some_variable = [\n"
1328 " @\"aaaaaaaaaaaaaaaaa\",\n"
1329 " @\"aaaaaaaaaaaaaaaaa\",\n"
1330 " @\"aaaaaaaaaaaaaaaaa\",\n"
1331 " @\"aaaaaaaaaaaaaaaaa\",\n"
1334 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
1335 " index:(NSUInteger)index\n"
1336 " nonDigitAttributes:\n"
1337 " (NSDictionary *)noDigitAttributes;");
1338 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
1339 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
1341 Style
.ColumnLimit
= 40;
1342 verifyFormat("int Foo() {\n"
1343 " a12345 = @[ a12345, a12345 ];\n"
1345 verifyFormat("int Foo() {\n"
1346 " a123 = @[ (Foo *)a12345, @(a12345) ];\n"
1348 Style
.SpacesInContainerLiterals
= false;
1349 verifyFormat("int Foo() {\n"
1350 " b12345 = @[b12345, b12345];\n"
1352 verifyFormat("int Foo() {\n"
1353 " b12345 = @[(Foo *)b12345, @(b12345)];\n"
1355 Style
.SpacesInContainerLiterals
= true;
1356 Style
.ColumnLimit
= 20;
1357 // We can't break string literals inside NSArray literals
1358 // (that raises -Wobjc-string-concatenation).
1359 verifyFormat("NSArray *foo = @[\n"
1360 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1364 TEST_F(FormatTestObjC
, BreaksCallStatementWhereSemiJustOverTheLimit
) {
1365 Style
.ColumnLimit
= 60;
1366 // If the statement starting with 'a = ...' is put on a single line, the ';'
1368 verifyFormat("int f(int a) {\n"
1369 " a = [self aaaaaaaaaa:bbbbbbbbb\n"
1370 " ccccccccc:dddddddd\n"
1375 TEST_F(FormatTestObjC
, AlwaysBreakBeforeMultilineStrings
) {
1376 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
1377 Style
.ColumnLimit
= 40;
1378 verifyFormat("aaaa = @\"bbbb\"\n"
1380 verifyFormat("aaaa(@\"bbbb\"\n"
1382 verifyFormat("aaaa(qqq, @\"bbbb\"\n"
1384 verifyFormat("[aaaa qqqq:@\"bbbb\"\n"
1386 verifyFormat("aaaa = [aaaa qqqq:@\"bbbb\"\n"
1388 verifyFormat("[aaaa qqqq:@\"bbbb\"\n"
1395 TEST_F(FormatTestObjC
, DisambiguatesCallsFromCppLambdas
) {
1396 verifyFormat("x = ([a foo:bar] && b->c == 'd');");
1397 verifyFormat("x = ([a foo:bar] + b->c == 'd');");
1398 verifyFormat("x = ([a foo:bar] + !b->c == 'd');");
1399 verifyFormat("x = ([a foo:bar] + ~b->c == 'd');");
1400 verifyFormat("x = ([a foo:bar] - b->c == 'd');");
1401 verifyFormat("x = ([a foo:bar] / b->c == 'd');");
1402 verifyFormat("x = ([a foo:bar] % b->c == 'd');");
1403 verifyFormat("x = ([a foo:bar] | b->c == 'd');");
1404 verifyFormat("x = ([a foo:bar] || b->c == 'd');");
1405 verifyFormat("x = ([a foo:bar] && b->c == 'd');");
1406 verifyFormat("x = ([a foo:bar] == b->c == 'd');");
1407 verifyFormat("x = ([a foo:bar] != b->c == 'd');");
1408 verifyFormat("x = ([a foo:bar] <= b->c == 'd');");
1409 verifyFormat("x = ([a foo:bar] >= b->c == 'd');");
1410 verifyFormat("x = ([a foo:bar] << b->c == 'd');");
1411 verifyFormat("x = ([a foo:bar] ? b->c == 'd' : 'e');");
1412 // FIXME: The following are wrongly classified as C++ lambda expressions.
1413 // For example this code:
1414 // x = ([a foo:bar] & b->c == 'd');
1416 // x = ([a foo:bar] & b -> c == 'd');
1417 // verifyFormat("x = ([a foo:bar] & b->c == 'd');");
1418 // verifyFormat("x = ([a foo:bar] > b->c == 'd');");
1419 // verifyFormat("x = ([a foo:bar] < b->c == 'd');");
1420 // verifyFormat("x = ([a foo:bar] >> b->c == 'd');");
1423 TEST_F(FormatTestObjC
, DisambiguatesCallsFromStructuredBindings
) {
1424 verifyFormat("int f() {\n"
1425 " if (a && [f arg])\n"
1428 verifyFormat("int f() {\n"
1429 " if (a & [f arg])\n"
1432 verifyFormat("int f() {\n"
1433 " for (auto &[elem] : list)\n"
1436 verifyFormat("int f() {\n"
1437 " for (auto &&[elem] : list)\n"
1442 " for (auto /**/ const /**/ volatile /**/ && /**/ [elem] : list)\n"
1447 TEST_F(FormatTestObjC
, BreakLineBeforeNestedBlockParam
) {
1448 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
1449 Style
.ObjCBreakBeforeNestedBlockParam
= false;
1450 Style
.ColumnLimit
= 0;
1452 verifyFormat("[self.test1 t:self callback:^(typeof(self) self, NSNumber *u, "
1457 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1458 "NSNumber *u, NSNumber *v) {\n"
1462 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1463 "NSNumber *u, NSNumber *v) {\n"
1465 "} w:self callback2:^(typeof(self) self, NSNumber *a, NSNumber "
1466 "*b, NSNumber *c) {\n"
1469 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1470 "NSNumber *u, NSNumber *v) {\n"
1474 Style
.ColumnLimit
= 80;
1476 "[self.test_method a:self b:self\n"
1477 " callback:^(typeof(self) self, NSNumber *u, NSNumber *v) {\n"
1481 verifyFormat("[self block:^(void) {\n"
1483 "} completionHandler:^(void) {\n"
1485 " [self block:^(void) {\n"
1487 " } completionHandler:^(void) {\n"
1492 Style
.ColumnLimit
= 0;
1493 verifyFormat("[[SessionService sharedService] "
1494 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
1496 " [self windowDidLoad:window];\n"
1498 " [self errorLoadingWindow];\n"
1501 verifyFormat("[controller test:^{\n"
1503 "} withTimeout:5 completionHandler:^{\n"
1507 "[self setupTextFieldSignals:@[\n"
1508 " self.documentWidthField,\n"
1509 " self.documentHeightField,\n"
1510 "] solver:^(NSTextField *textField) {\n"
1511 " return [self.representedObject solveEquationForTextField:textField];\n"
1515 TEST_F(FormatTestObjC
, IfNotUnlikely
) {
1516 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
1518 verifyFormat("if (argc < 5) [obj func:arg];");
1519 verifyFormat("if (argc < 5) [[obj1 method1:arg1] method2:arg2];");
1520 verifyFormat("if (argc < 5) [[foo bar] baz:i[0]];");
1521 verifyFormat("if (argc < 5) [[foo bar] baz:i[0]][1];");
1523 verifyFormat("if (argc < 5)\n"
1524 " [obj func:arg];\n"
1526 " [obj func:arg2];");
1528 verifyFormat("if (argc < 5) [[unlikely]]\n"
1529 " [obj func:arg];\n"
1531 " [obj func:arg2];");
1534 TEST_F(FormatTestObjC
, AttributesOnObjCDecl
) {
1535 Style
.AttributeMacros
.push_back("ATTRIBUTE_MACRO");
1537 // Check '__attribute__' macro directly.
1538 verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1541 verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1544 verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1545 "@implementation Foo\n"
1548 // Check AttributeMacro gets treated the same, with or without parentheses.
1549 verifyFormat("ATTRIBUTE_MACRO\n"
1552 verifyFormat("ATTRIBUTE_MACRO(X)\n"
1556 // Indenter also needs to understand multiple attribute macros.
1557 // Try each of the three kinds paired with each of the other kind.
1559 // Column limit, but no reflow.
1560 verifyFormat("ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO\n"
1563 verifyFormat("ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X)\n"
1566 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO\n"
1569 verifyFormat("ATTRIBUTE_MACRO __attribute__((X))\n"
1572 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO(X)\n"
1575 verifyFormat("ATTRIBUTE_MACRO(X) __attribute__((X))\n"
1579 // Column limit that requires reflow.
1580 Style
.ColumnLimit
= 30;
1581 verifyFormat("ATTRIBUTE_MACRO(X)\n"
1585 verifyFormat("ATTRIBUTE_MACRO\n"
1586 "ATTRIBUTE_MACRO(X)\n"
1589 verifyFormat("__attribute__((X))\n"
1593 verifyFormat("ATTRIBUTE_MACRO\n"
1594 "__attribute__((X))\n"
1597 verifyFormat("__attribute__((X))\n"
1598 "ATTRIBUTE_MACRO(X)\n"
1601 verifyFormat("ATTRIBUTE_MACRO(X)\n"
1602 "__attribute__((X))\n"
1607 Style
.ColumnLimit
= 0;
1608 verifyFormat("ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO\n"
1611 verifyFormat("ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X)\n"
1614 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO\n"
1617 verifyFormat("ATTRIBUTE_MACRO __attribute__((X))\n"
1620 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO(X)\n"
1623 verifyFormat("ATTRIBUTE_MACRO(X) __attribute__((X))\n"
1628 TEST_F(FormatTestObjC
, AttributesOnObjCMethodDecl
) {
1629 Style
.AttributeMacros
.push_back("ATTRIBUTE_MACRO");
1631 // Check '__attribute__' macro directly.
1632 verifyFormat("- (id)init __attribute__((objc_designated_initializer));");
1634 // Check AttributeMacro gets treated the same, with or without parentheses.
1635 verifyFormat("- (id)init ATTRIBUTE_MACRO;");
1636 verifyFormat("- (id)init ATTRIBUTE_MACRO(X);");
1638 // Indenter also needs to understand multiple attribute macros.
1640 // Column limit (default), but no reflow.
1641 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1642 verifyFormat("- (id)init ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1643 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO;");
1644 verifyFormat("- (id)init ATTRIBUTE_MACRO __attribute__((X));");
1645 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO(X);");
1646 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) __attribute__((X));");
1648 // Column limit that requires reflow.
1649 Style
.ColumnLimit
= 30;
1651 // Reflow after method name.
1652 verifyFormat("- (id)initWithReallyLongName\n"
1653 " __attribute__((X))\n"
1654 " ATTRIBUTE_MACRO;");
1655 verifyFormat("- (id)initWithReallyLongName\n"
1656 " ATTRIBUTE_MACRO(X)\n"
1657 " ATTRIBUTE_MACRO;");
1658 verifyFormat("- (id)initWithReallyLongName\n"
1659 " ATTRIBUTE_MACRO\n"
1660 " ATTRIBUTE_MACRO;");
1661 // Reflow after first macro.
1662 // FIXME: these should indent but don't.
1664 verifyFormat("- (id)init ATTRIBUTE_MACRO(X)\n"
1665 " ATTRIBUTE_MACRO;");
1666 verifyFormat("- (id)init ATTRIBUTE_MACRO\n"
1667 " ATTRIBUTE_MACRO(X);");
1668 verifyFormat("- (id)init __attribute__((X))\n"
1669 " ATTRIBUTE_MACRO;");
1670 verifyFormat("- (id)init ATTRIBUTE_MACRO\n"
1671 " __attribute__((X));");
1672 verifyFormat("- (id)init __attribute__((X))\n"
1673 " ATTRIBUTE_MACRO(X);");
1674 verifyFormat("- (id)init ATTRIBUTE_MACRO(X)\n"
1675 " __attribute__((X));");
1679 Style
.ColumnLimit
= 0;
1680 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1681 verifyFormat("- (id)init ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1682 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO;");
1683 verifyFormat("- (id)init ATTRIBUTE_MACRO __attribute__((X));");
1684 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO(X);");
1685 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) __attribute__((X));");
1688 TEST_F(FormatTestObjC
, AttributesOnObjCProperty
) {
1689 Style
.AttributeMacros
.push_back("ATTRIBUTE_MACRO");
1691 // Check '__attribute__' macro directly.
1692 verifyFormat("@property(weak) id delegate "
1693 "__attribute__((objc_designated_initializer));");
1695 // Check AttributeMacro gets treated the same, with or without parentheses.
1696 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO;");
1697 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO(X);");
1699 // Indenter also needs to understand multiple attribute macros.
1701 // Column limit (default), but no reflow.
1703 "@property(weak) id delegate ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1705 "@property(weak) id delegate ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1707 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO;");
1709 "@property(weak) id delegate ATTRIBUTE_MACRO __attribute__((X));");
1711 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO(X);");
1713 "@property(weak) id delegate ATTRIBUTE_MACRO(X) __attribute__((X));");
1715 // Column limit that requires reflow.
1716 Style
.ColumnLimit
= 50;
1718 // Reflow after method name.
1719 verifyFormat("@property(weak) id delegateWithLongName\n"
1720 " __attribute__((X)) ATTRIBUTE_MACRO;");
1721 verifyFormat("@property(weak) id delegateWithLongName\n"
1722 " ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1723 verifyFormat("@property(weak) id delegateWithLongName\n"
1724 " ATTRIBUTE_MACRO ATTRIBUTE_MACRO;");
1725 // Reflow after first macro.
1726 // FIXME: these should indent but don't.
1728 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO(X)\n"
1729 " ATTRIBUTE_MACRO;");
1730 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO\n"
1731 " ATTRIBUTE_MACRO(X);");
1732 verifyFormat("@property(weak) id delegate __attribute__((X))\n"
1733 " ATTRIBUTE_MACRO;");
1734 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO\n"
1735 " __attribute__((X));");
1736 verifyFormat("@property(weak) id delegate __attribute__((X))\n"
1737 " ATTRIBUTE_MACRO(X);");
1738 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO(X)\n"
1739 " __attribute__((X));");
1743 Style
.ColumnLimit
= 0;
1745 "@property(weak) id delegate ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1747 "@property(weak) id delegate ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1749 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO;");
1751 "@property(weak) id delegate ATTRIBUTE_MACRO __attribute__((X));");
1753 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO(X);");
1755 "@property(weak) id delegate ATTRIBUTE_MACRO(X) __attribute__((X));");
1760 } // end namespace format
1761 } // end namespace clang