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
, DetectsObjCInHeaders
) {
35 auto Style
= getStyle("LLVM", "a.h", "none",
38 ASSERT_TRUE((bool)Style
);
39 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
41 Style
= getStyle("LLVM", "a.h", "none",
44 ASSERT_TRUE((bool)Style
);
45 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
47 Style
= getStyle("LLVM", "a.h", "none",
51 ASSERT_TRUE((bool)Style
);
52 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
54 Style
= getStyle("LLVM", "a.h", "none",
57 ASSERT_TRUE((bool)Style
);
58 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
60 // No recognizable ObjC.
61 Style
= getStyle("LLVM", "a.h", "none", "void f() {}");
62 ASSERT_TRUE((bool)Style
);
63 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
65 Style
= getStyle("{}", "a.h", "none", "@interface Foo\n@end");
66 ASSERT_TRUE((bool)Style
);
67 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
69 Style
= getStyle("{}", "a.h", "none",
70 "const int interface = 1;\nconst int end = 2;");
71 ASSERT_TRUE((bool)Style
);
72 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
74 Style
= getStyle("{}", "a.h", "none", "@protocol Foo\n@end");
75 ASSERT_TRUE((bool)Style
);
76 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
78 Style
= getStyle("{}", "a.h", "none",
79 "const int protocol = 1;\nconst int end = 2;");
80 ASSERT_TRUE((bool)Style
);
81 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
83 Style
= getStyle("{}", "a.h", "none", "typedef NS_ENUM(int, Foo) {};");
84 ASSERT_TRUE((bool)Style
);
85 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
87 Style
= getStyle("{}", "a.h", "none", "typedef NS_CLOSED_ENUM(int, Foo) {};");
88 ASSERT_TRUE((bool)Style
);
89 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
91 Style
= getStyle("{}", "a.h", "none", "typedef NS_ERROR_ENUM(int, Foo) {};");
92 ASSERT_TRUE((bool)Style
);
93 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
95 Style
= getStyle("{}", "a.h", "none", R
"objc(
96 NS_ASSUME_NONNULL_BEGIN
100 ASSERT_TRUE((bool)Style
);
101 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
103 Style
= getStyle("{}", "a.h", "none", R
"objc(
104 FOUNDATION_EXTERN void DoStuff(void);
106 ASSERT_TRUE((bool)Style
);
107 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
109 Style
= getStyle("{}", "a.h", "none", R
"objc(
110 FOUNDATION_EXPORT void DoStuff(void);
112 ASSERT_TRUE((bool)Style
);
113 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
115 Style
= getStyle("{}", "a.h", "none", "enum Foo {};");
116 ASSERT_TRUE((bool)Style
);
117 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
119 Style
= getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }");
120 ASSERT_TRUE((bool)Style
);
121 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
123 Style
= getStyle("{}", "a.h", "none", "inline void Foo() { Log(\"Foo\"); }");
124 ASSERT_TRUE((bool)Style
);
125 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
128 getStyle("{}", "a.h", "none", "inline void Foo() { id = @[1, 2, 3]; }");
129 ASSERT_TRUE((bool)Style
);
130 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
132 Style
= getStyle("{}", "a.h", "none",
133 "inline void Foo() { id foo = @{1: 2, 3: 4, 5: 6}; }");
134 ASSERT_TRUE((bool)Style
);
135 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
137 Style
= getStyle("{}", "a.h", "none",
138 "inline void Foo() { int foo[] = {1, 2, 3}; }");
139 ASSERT_TRUE((bool)Style
);
140 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
142 // ObjC characteristic types.
143 Style
= getStyle("{}", "a.h", "none", "extern NSString *kFoo;");
144 ASSERT_TRUE((bool)Style
);
145 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
147 Style
= getStyle("{}", "a.h", "none", "extern NSInteger Foo();");
148 ASSERT_TRUE((bool)Style
);
149 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
151 Style
= getStyle("{}", "a.h", "none", "NSObject *Foo();");
152 ASSERT_TRUE((bool)Style
);
153 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
155 Style
= getStyle("{}", "a.h", "none", "NSSet *Foo();");
156 ASSERT_TRUE((bool)Style
);
157 EXPECT_EQ(FormatStyle::LK_ObjC
, Style
->Language
);
160 TEST(FormatTestObjCStyle
, AvoidDetectingDesignatedInitializersAsObjCInHeaders
) {
161 auto Style
= getStyle("LLVM", "a.h", "none",
162 "static const char *names[] = {[0] = \"foo\",\n"
163 "[kBar] = \"bar\"};");
164 ASSERT_TRUE((bool)Style
);
165 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
167 Style
= getStyle("LLVM", "a.h", "none",
168 "static const char *names[] = {[0] EQ \"foo\",\n"
169 "[kBar] EQ \"bar\"};");
170 ASSERT_TRUE((bool)Style
);
171 EXPECT_EQ(FormatStyle::LK_Cpp
, Style
->Language
);
174 TEST_F(FormatTestObjC
, FormatObjCTryCatch
) {
175 verifyFormat("@try {\n"
177 "} @catch (NSException e) {\n"
182 verifyFormat("DEBUG({\n"
189 TEST_F(FormatTestObjC
, FormatObjCAutoreleasepool
) {
190 verifyFormat("@autoreleasepool {\n"
193 "@autoreleasepool {\n"
196 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
197 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
198 verifyFormat("@autoreleasepool\n"
208 TEST_F(FormatTestObjC
, FormatObjCGenerics
) {
209 Style
.ColumnLimit
= 40;
210 verifyFormat("int aaaaaaaaaaaaaaaa(\n"
211 " NSArray<aaaaaaaaaaaaaaaaaa *>\n"
212 " aaaaaaaaaaaaaaaaa);");
213 verifyFormat("int aaaaaaaaaaaaaaaa(\n"
214 " NSArray<aaaaaaaaaaaaaaaaaaa<\n"
215 " aaaaaaaaaaaaaaaa *> *>\n"
216 " aaaaaaaaaaaaaaaaa);");
219 TEST_F(FormatTestObjC
, FormatObjCSynchronized
) {
220 verifyFormat("@synchronized(self) {\n"
223 "@synchronized(self) {\n"
226 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
227 Style
.BraceWrapping
.AfterControlStatement
= FormatStyle::BWACS_Always
;
228 verifyFormat("@synchronized(self)\n"
232 "@synchronized(self)\n"
238 TEST_F(FormatTestObjC
, FormatObjCInterface
) {
239 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
252 verifyFormat("@interface /* wait for it */ Foo\n"
254 "// Look, a comment!\n"
255 "- (int)answerWith:(int)i;\n"
258 verifyFormat("@interface Foo\n"
263 verifyFormat("@interface Foo : Bar\n"
264 "@property(assign, readwrite) NSInteger bar;\n"
268 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @interface Foo : Bar\n"
269 "@property(assign, readwrite) NSInteger bar;\n"
273 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
277 verifyFormat("@interface Foo (HackStuff)\n"
281 verifyFormat("@interface Foo ()\n"
285 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
289 verifyFormat("@interface Foo {\n"
295 verifyFormat("@interface Foo : Bar {\n"
301 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
307 verifyFormat("@interface Foo<Baz : Blech> : Bar <Baz, Quux> {\n"
313 verifyFormat("@interface Foo<Bar : Baz <Blech>> : Xyzzy <Corge> {\n"
319 verifyFormat("@interface Foo<Bar : Baz <Blech>> : Xyzzy <Corge> <Quux> {\n"
325 verifyFormat("@interface Foo : Bar <Baz> <Blech>\n"
328 verifyFormat("@interface Foo : Bar <Baz> <Blech, Xyzzy, Corge>\n"
331 verifyFormat("@interface Foo (HackStuff) {\n"
337 verifyFormat("@interface Foo () {\n"
343 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
348 verifyFormat("@interface Foo\n"
352 "@implementation Bar\n"
356 Style
.ColumnLimit
= 40;
357 verifyFormat("@interface ccccccccccccc () <\n"
358 " ccccccccccccc, ccccccccccccc,\n"
359 " ccccccccccccc, ccccccccccccc> {\n"
361 verifyFormat("@interface ccccccccccccc (ccccccccccc) <\n"
362 " ccccccccccccc> {\n"
364 Style
.ObjCBinPackProtocolList
= FormatStyle::BPS_Never
;
365 verifyFormat("@interface ddddddddddddd () <\n"
369 " ddddddddddddd> {\n"
372 Style
.BinPackParameters
= false;
373 Style
.ObjCBinPackProtocolList
= FormatStyle::BPS_Auto
;
374 verifyFormat("@interface eeeeeeeeeeeee () <\n"
378 " eeeeeeeeeeeee> {\n"
380 Style
.ObjCBinPackProtocolList
= FormatStyle::BPS_Always
;
381 verifyFormat("@interface fffffffffffff () <\n"
382 " fffffffffffff, fffffffffffff,\n"
383 " fffffffffffff, fffffffffffff> {\n"
386 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
387 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
399 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
402 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
405 Style
.ColumnLimit
= 40;
406 // BinPackParameters should be true by default.
407 verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n"
408 " int eeeee, int eeeee);");
409 // ObjCBinPackProtocolList should be BPS_Never by default.
410 verifyFormat("@interface fffffffffffff () <\n"
414 " fffffffffffff> {\n"
416 verifyFormat("@interface ggggggggggggg\n"
417 " : ggggggggggggg <ggggggggggggg>\n"
422 TEST_F(FormatTestObjC
, FormatObjCImplementation
) {
423 verifyFormat("@implementation Foo : NSObject {\n"
436 verifyFormat("@implementation Foo\n"
441 "// Look, a comment!\n"
442 "- (int)answerWith:(int)i {\n"
445 "+ (int)answerWith:(int)i {\n"
450 verifyFormat("@implementation Foo\n"
452 "@implementation Bar\n"
455 EXPECT_EQ("@implementation Foo : Bar\n"
459 format("@implementation Foo : Bar\n"
464 verifyFormat("@implementation Foo {\n"
470 verifyFormat("@implementation Foo : Bar {\n"
476 verifyFormat("@implementation Foo (HackStuff)\n"
479 verifyFormat("@implementation ObjcClass\n"
484 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
485 verifyFormat("@implementation Foo : NSObject {\n"
499 TEST_F(FormatTestObjC
, FormatObjCProtocol
) {
500 verifyFormat("@protocol Foo\n"
501 "@property(weak) id delegate;\n"
502 "- (NSUInteger)numberOfThings;\n"
505 verifyFormat("@protocol MyProtocol <NSObject>\n"
506 "- (NSUInteger)numberOfThings;\n"
509 verifyFormat("@protocol Foo;\n"
512 verifyFormat("@protocol Foo\n"
517 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n"
518 "@property(assign, readwrite) NSInteger bar;\n"
521 verifyFormat("@protocol myProtocol\n"
522 "- (void)mandatoryWithInt:(int)i;\n"
524 "- (void)optional;\n"
526 "- (void)required;\n"
528 "@property(assign) int madProp;\n"
531 verifyFormat("@property(nonatomic, assign, readonly)\n"
532 " int *looooooooooooooooooooooooooooongNumber;\n"
533 "@property(nonatomic, assign, readonly)\n"
534 " NSString *looooooooooooooooooooooooooooongName;");
536 verifyFormat("@implementation PR18406\n"
540 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
541 verifyFormat("@protocol MyProtocol <NSObject>\n"
542 "- (NSUInteger)numberOfThings;\n"
546 TEST_F(FormatTestObjC
, FormatObjCMethodDeclarations
) {
547 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
548 " rect:(NSRect)theRect\n"
549 " interval:(float)theInterval {\n"
551 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
552 " longKeyword:(NSRect)theRect\n"
553 " longerKeyword:(float)theInterval\n"
554 " error:(NSError **)theError {\n"
556 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
557 " longKeyword:(NSRect)theRect\n"
558 " evenLongerKeyword:(float)theInterval\n"
559 " error:(NSError **)theError {\n"
561 verifyFormat("+ (instancetype)new;");
562 Style
.ColumnLimit
= 60;
563 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
564 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
565 " NS_DESIGNATED_INITIALIZER;");
566 verifyFormat("- (void)drawRectOn:(id)surface\n"
567 " ofSize:(size_t)height\n"
569 Style
.ColumnLimit
= 40;
570 // Make sure selectors with 0, 1, or more arguments are indented when wrapped.
571 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
572 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;");
573 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
574 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;");
575 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
576 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;");
578 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
579 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
580 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;");
581 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
582 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
583 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;");
585 // Continuation indent width should win over aligning colons if the function
587 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
588 Style
.ColumnLimit
= 40;
589 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
590 " dontAlignNamef:(NSRect)theRect {\n"
593 // Make sure we don't break aligning for short parameter names.
594 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
595 " aShortf:(NSRect)theRect {\n"
598 // Format pairs correctly.
599 Style
.ColumnLimit
= 80;
600 verifyFormat("- (void)drawRectOn:(id)surface\n"
601 " ofSize:(aaaaaaaa)height\n"
603 " atOrigin:(size_t)x\n"
607 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
609 // BraceWrapping AfterFunction is respected for ObjC methods
610 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
611 Style
.BreakBeforeBraces
= FormatStyle::BS_Custom
;
612 Style
.BraceWrapping
.AfterFunction
= true;
613 verifyFormat("@implementation Foo\n"
614 "- (void)foo:(id)bar\n"
620 TEST_F(FormatTestObjC
, FormatObjCMethodExpr
) {
621 verifyFormat("[foo bar:baz];");
622 verifyFormat("[foo bar]->baz;");
623 verifyFormat("return [foo bar:baz];");
624 verifyFormat("return (a)[foo bar:baz];");
625 verifyFormat("f([foo bar:baz]);");
626 verifyFormat("f(2, [foo bar:baz]);");
627 verifyFormat("f(2, a ? b : c);");
628 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
631 verifyFormat("int a = +[foo bar:baz];");
632 verifyFormat("int a = -[foo bar:baz];");
633 verifyFormat("int a = ![foo bar:baz];");
634 verifyFormat("int a = ~[foo bar:baz];");
635 verifyFormat("int a = ++[foo bar:baz];");
636 verifyFormat("int a = --[foo bar:baz];");
637 verifyFormat("int a = sizeof [foo bar:baz];");
638 verifyFormat("int a = alignof [foo bar:baz];");
639 verifyFormat("int a = &[foo bar:baz];");
640 verifyFormat("int a = *[foo bar:baz];");
641 // FIXME: Make casts work, without breaking f()[4].
642 // verifyFormat("int a = (int)[foo bar:baz];");
643 // verifyFormat("return (int)[foo bar:baz];");
644 // verifyFormat("(void)[foo bar:baz];");
645 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
648 verifyFormat("[foo bar:baz], [foo bar:baz];");
649 verifyFormat("[foo bar:baz] = [foo bar:baz];");
650 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
651 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
652 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
653 verifyFormat("[foo bar:baz] += [foo bar:baz];");
654 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
655 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
656 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
657 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] : [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];");
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];");
681 verifyFormat("return in[42];");
682 verifyFormat("for (auto v : in[1]) {\n}");
683 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
684 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
685 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
686 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
687 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
688 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
690 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
691 verifyFormat("[self aaaaa:MACRO(a, b:c:, d:e:)];");
692 verifyFormat("[self aaaaa:MACRO(a, b:c:d:, e:f:g:)];");
693 verifyFormat("int XYMyFoo(int a, int b) NS_SWIFT_NAME(foo(self:scale:));");
694 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
695 verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
697 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
698 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
699 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
700 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
701 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
702 verifyFormat("[button setAction:@selector(zoomOut:)];");
703 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
705 verifyFormat("arr[[self indexForFoo:a]];");
706 verifyFormat("throw [self errorFor:a];");
707 verifyFormat("@throw [self errorFor:a];");
709 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
710 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
711 verifyFormat("4 > 4 ? (id)a : (id)baz;");
713 unsigned PreviousColumnLimit
= Style
.ColumnLimit
;
714 Style
.ColumnLimit
= 50;
717 // ([object a:42] == 0 || [object a:42
719 verifyFormat("bool a = ([object a:42] == 0 ||\n"
720 " [object a:42 b:42] == 0);");
721 Style
.ColumnLimit
= PreviousColumnLimit
;
722 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
723 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
725 // This tests that the formatter doesn't break after "backing" but before ":",
726 // which would be at 80 columns.
729 " if ((self = [super initWithContentRect:contentRect\n"
730 " styleMask:styleMask ?: otherMask\n"
731 " backing:NSBackingStoreBuffered\n"
735 "[foo checkThatBreakingAfterColonWorksOk:\n"
736 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
738 verifyFormat("[myObj short:arg1 // Force line break\n"
739 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
740 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
744 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
745 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
746 " pos.width(), pos.height())\n"
747 " styleMask:NSBorderlessWindowMask\n"
748 " backing:NSBackingStoreBuffered\n"
751 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
752 " with:contentsNativeView];");
755 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
756 " owner:nillllll];");
759 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
760 " forType:kBookmarkButtonDragType];");
762 verifyFormat("[defaultCenter addObserver:self\n"
763 " selector:@selector(willEnterFullscreen)\n"
764 " name:kWillEnterFullscreenNotification\n"
766 verifyFormat("[image_rep drawInRect:drawRect\n"
767 " fromRect:NSZeroRect\n"
768 " operation:NSCompositeCopy\n"
770 " respectFlipped:NO\n"
772 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
773 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
774 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
775 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
776 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
777 " aaaaaaaaaaaaaaaaaaaaaa];");
780 "scoped_nsobject<NSTextField> message(\n"
781 " // The frame will be fixed up when |-setMessageText:| is called.\n"
782 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
783 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
784 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
785 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
787 verifyFormat("[self param:function( //\n"
790 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
791 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
792 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
794 // Variadic parameters.
796 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
798 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
799 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
800 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
802 verifyFormat("[self // break\n"
806 // Formats pair-parameters.
807 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
808 verifyFormat("[I drawRectOn:surface //\n"
810 " atOrigin:cc:dd];");
812 // Inline block as a first argument.
813 verifyFormat("[object justBlock:^{\n"
816 verifyFormat("[object\n"
822 verifyFormat("[object\n"
826 " blockWithLongerName:^{\n"
829 verifyFormat("[object\n"
830 " blockWithLongerName:^{\n"
836 verifyFormat("[object\n"
845 // Space between cast rparen and selector name component.
846 verifyFormat("[((Foo *)foo) bar];");
847 verifyFormat("[((Foo *)foo) bar:1 blech:2];");
849 Style
.ColumnLimit
= 20;
850 verifyFormat("aaaaa = [a aa:aa\n"
852 verifyFormat("aaaaaa = [aa aa:aa\n"
855 // Message receiver taking multiple lines.
857 verifyFormat("[[object block:^{\n"
860 // Arguments just fit into one line.
861 verifyFormat("[[object block:^{\n"
863 "}] aaaaaaa:42 b:42];");
864 // Arguments just over a column limit.
865 verifyFormat("[[object block:^{\n"
869 // Arguments just fit into one line.
870 Style
.ColumnLimit
= 23;
871 verifyFormat("[[obj a:42\n"
874 " d:42] e:42 f:42];");
876 // Arguments do not fit into one line with a receiver.
877 Style
.ColumnLimit
= 20;
878 verifyFormat("[[obj a:42] a:42\n"
880 verifyFormat("[[obj a:42] a:42\n"
883 verifyFormat("[[obj aaaaaa:42\n"
888 // Avoid breaking receiver expression.
889 Style
.ColumnLimit
= 30;
890 verifyFormat("fooooooo =\n"
891 " [[obj fooo] aaa:42\n"
893 verifyFormat("[[[obj foo] bar] aa:42\n"
897 // Avoid breaking between unary operators and ObjC method expressions.
898 Style
.ColumnLimit
= 45;
899 verifyFormat("if (a012345678901234567890123 &&\n"
902 verifyFormat("if (a012345678901234567890123 &&\n"
905 verifyFormat("if (a012345678901234567890123 &&\n"
909 Style
.ColumnLimit
= 70;
912 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
913 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
914 " pos.width(), pos.height())\n"
915 " syeMask:NSBorderlessWindowMask\n"
916 " bking:NSBackingStoreBuffered\n"
920 Style
.ColumnLimit
= 60;
921 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
922 " .aaaaaaaa];"); // FIXME: Indentation seems off.
923 // FIXME: This violates the column limit.
925 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
926 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
927 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
929 Style
= getChromiumStyle(FormatStyle::LK_ObjC
);
930 Style
.ColumnLimit
= 80;
933 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
934 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
935 " pos.width(), pos.height())\n"
936 " styleMask:NSBorderlessWindowMask\n"
937 " backing:NSBackingStoreBuffered\n"
941 // Respect continuation indent and colon alignment (e.g. when object name is
942 // short, and first selector is the longest one)
943 Style
= getLLVMStyle();
944 Style
.Language
= FormatStyle::LK_ObjC
;
945 Style
.ContinuationIndentWidth
= 8;
946 verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
948 " waitUntilDone:false];");
949 verifyFormat("[self performSelector:@selector(loadAccessories)\n"
950 " withObjectOnMainThread:nil\n"
951 " waitUntilDone:false];");
953 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
954 " performSelectorOnMainThread:@selector(loadAccessories)\n"
956 " waitUntilDone:false];");
958 "[self // force wrapping\n"
959 " performSelectorOnMainThread:@selector(loadAccessories)\n"
961 " waitUntilDone:false];");
963 // The appropriate indentation is used after a block statement.
964 Style
.ContinuationIndentWidth
= 4;
966 "void aaaaaaaaaaaaaaaaaaaaa(int c) {\n"
970 " [dddddddddddddddddddddddddddddddddddddddddddddddddddddddd\n"
971 " eeeeeeeeeeeeeeeeeeeeeeeeeeeee:^(fffffffffffffff gggggggg) {\n"
977 TEST_F(FormatTestObjC
, ObjCAt
) {
978 verifyFormat("@autoreleasepool");
979 verifyFormat("@catch");
980 verifyFormat("@class");
981 verifyFormat("@compatibility_alias");
982 verifyFormat("@defs");
983 verifyFormat("@dynamic");
984 verifyFormat("@encode");
985 verifyFormat("@end");
986 verifyFormat("@finally");
987 verifyFormat("@implementation");
988 verifyFormat("@import");
989 verifyFormat("@interface");
990 verifyFormat("@optional");
991 verifyFormat("@package");
992 verifyFormat("@private");
993 verifyFormat("@property");
994 verifyFormat("@protected");
995 verifyFormat("@protocol");
996 verifyFormat("@public");
997 verifyFormat("@required");
998 verifyFormat("@selector");
999 verifyFormat("@synchronized");
1000 verifyFormat("@synthesize");
1001 verifyFormat("@throw");
1002 verifyFormat("@try");
1004 EXPECT_EQ("@interface", format("@ interface"));
1006 // The precise formatting of this doesn't matter, nobody writes code like
1008 verifyFormat("@ /*foo*/ interface");
1011 TEST_F(FormatTestObjC
, ObjCBlockTypesAndVariables
) {
1012 verifyFormat("void DoStuffWithBlockType(int (^)(char));");
1013 verifyFormat("int (^foo)(char, float);");
1014 verifyFormat("int (^foo[10])(char, float);");
1015 verifyFormat("int (^foo[kNumEntries])(char, float);");
1016 verifyFormat("int (^foo[kNumEntries + 10])(char, float);");
1017 verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);");
1019 verifyFormat("int *p = ^int *() { //\n"
1020 " return nullptr;\n"
1023 verifyFormat("int * (^p)(void) = ^int *(void) { //\n"
1024 " return nullptr;\n"
1027 // WebKit forces function braces onto a newline, but blocks should not.
1028 verifyFormat("int* p = ^int*() { //\n"
1029 " return nullptr;\n"
1034 TEST_F(FormatTestObjC
, ObjCSnippets
) {
1035 verifyFormat("@autoreleasepool {\n"
1038 verifyFormat("@class Foo, Bar;");
1039 verifyFormat("@compatibility_alias AliasName ExistingClass;");
1040 verifyFormat("@dynamic textColor;");
1041 verifyFormat("char *buf1 = @encode(int *);");
1042 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
1043 verifyFormat("char *buf1 = @encode(int **);");
1044 verifyFormat("Protocol *proto = @protocol(p1);");
1045 verifyFormat("SEL s = @selector(foo:);");
1046 verifyFormat("@synchronized(self) {\n"
1050 verifyFormat("@import foo.bar;\n"
1053 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1055 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
1056 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1058 verifyFormat("extern UIWindow *MainWindow(void) "
1059 "NS_SWIFT_NAME(getter:MyHelper.mainWindow());");
1061 verifyFormat("extern UIWindow *MainWindow(void) "
1062 "CF_SWIFT_NAME(getter:MyHelper.mainWindow());");
1064 Style
.ColumnLimit
= 50;
1065 verifyFormat("@interface Foo\n"
1066 "- (void)doStuffWithFoo:(id)name\n"
1069 " NS_SWIFT_NAME(doStuff(withFoo:bar:baz:));\n"
1072 Style
= getMozillaStyle();
1073 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
1074 verifyFormat("@property BOOL editable;");
1076 Style
= getWebKitStyle();
1077 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
1078 verifyFormat("@property BOOL editable;");
1080 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
1081 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
1082 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
1085 TEST_F(FormatTestObjC
, ObjCForIn
) {
1086 verifyFormat("- (void)test {\n"
1087 " for (NSString *n in arrayOfStrings) {\n"
1091 verifyFormat("- (void)test {\n"
1092 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
1096 verifyFormat("for (Foo *x in bar) {\n}");
1097 verifyFormat("for (Foo *x in [bar baz]) {\n}");
1098 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
1099 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
1100 verifyFormat("for (Foo *x in [bar baz:^{\n"
1105 TEST_F(FormatTestObjC
, ObjCCxxKeywords
) {
1106 verifyFormat("+ (instancetype)new {\n"
1109 verifyFormat("+ (instancetype)myNew {\n"
1110 " return [self new];\n"
1112 verifyFormat("SEL NewSelector(void) { return @selector(new); }");
1113 verifyFormat("SEL MacroSelector(void) { return MACRO(new); }");
1114 verifyFormat("+ (instancetype)delete {\n"
1117 verifyFormat("+ (instancetype)myDelete {\n"
1118 " return [self delete];\n"
1120 verifyFormat("SEL DeleteSelector(void) { return @selector(delete); }");
1121 verifyFormat("SEL MacroSelector(void) { return MACRO(delete); }");
1122 verifyFormat("MACRO(new:)");
1123 verifyFormat("MACRO(delete:)");
1124 verifyFormat("foo = @{MACRO(new:) : MACRO(delete:)}");
1125 verifyFormat("@implementation Foo\n"
1127 "- (Class)class {\n"
1132 verifyFormat("@implementation Foo\n"
1133 "- (Class)class {\n"
1138 verifyFormat("@implementation Foo\n"
1139 "+ (Class)class {\n"
1144 verifyFormat("@implementation Foo\n"
1145 "- (Class)class:(Class)klass {\n"
1150 verifyFormat("@implementation Foo\n"
1151 "+ (Class)class:(Class)klass {\n"
1157 verifyFormat("@interface Foo\n"
1162 verifyFormat("@interface Foo\n"
1166 verifyFormat("@interface Foo\n"
1170 verifyFormat("@interface Foo\n"
1171 "- (Class)class:(Class)klass;\n"
1174 verifyFormat("@interface Foo\n"
1175 "+ (Class)class:(Class)klass;\n"
1180 TEST_F(FormatTestObjC
, ObjCLiterals
) {
1181 verifyFormat("@\"String\"");
1183 verifyFormat("@+4.8");
1184 verifyFormat("@-4");
1185 verifyFormat("@1LL");
1186 verifyFormat("@.5");
1187 verifyFormat("@'c'");
1188 verifyFormat("@true");
1190 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
1191 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
1192 verifyFormat("NSNumber *favoriteColor = @(Green);");
1193 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
1195 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
1198 TEST_F(FormatTestObjC
, ObjCDictLiterals
) {
1200 verifyFormat("@{}");
1201 verifyFormat("@{@\"one\" : @1}");
1202 verifyFormat("return @{@\"one\" : @1;");
1203 verifyFormat("@{@\"one\" : @1}");
1205 verifyFormat("@{@\"one\" : @{@2 : @1}}");
1207 " @\"one\" : @{@2 : @1},\n"
1210 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
1211 verifyIncompleteFormat("[self setDict:@{}");
1212 verifyIncompleteFormat("[self setDict:@{@1 : @2}");
1213 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
1215 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
1217 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
1219 verifyFormat("NSDictionary *d = @{\n"
1220 " @\"nam\" : NSUserNam(),\n"
1221 " @\"dte\" : [NSDate date],\n"
1222 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
1226 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1231 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
1232 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
1235 // We should try to be robust in case someone forgets the "@".
1236 verifyFormat("NSDictionary *d = {\n"
1237 " @\"nam\" : NSUserNam(),\n"
1238 " @\"dte\" : [NSDate date],\n"
1239 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
1241 verifyFormat("NSMutableDictionary *dictionary =\n"
1242 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
1243 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
1244 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
1245 " cccccccccccccccc : ccccccccccccccc\n"
1248 // Ensure that casts before the key are kept on the same line as the key.
1250 "NSDictionary *d = @{\n"
1251 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1252 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
1254 Style
.ColumnLimit
= 40;
1255 verifyFormat("int Foo() {\n"
1256 " a12345 = @{a12345 : a12345};\n"
1258 verifyFormat("int Foo() {\n"
1259 " a12345 = @{a12345 : @(a12345)};\n"
1261 verifyFormat("int Foo() {\n"
1262 " a12345 = @{(Foo *)a12345 : @(a12345)};\n"
1264 verifyFormat("int Foo() {\n"
1265 " a12345 = @{@(a12345) : a12345};\n"
1267 verifyFormat("int Foo() {\n"
1268 " a12345 = @{@(a12345) : @YES};\n"
1270 Style
.SpacesInContainerLiterals
= false;
1271 verifyFormat("int Foo() {\n"
1272 " b12345 = @{b12345: b12345};\n"
1274 verifyFormat("int Foo() {\n"
1275 " b12345 = @{(Foo *)b12345: @(b12345)};\n"
1277 Style
.SpacesInContainerLiterals
= true;
1279 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
1282 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1287 TEST_F(FormatTestObjC
, ObjCArrayLiterals
) {
1288 verifyIncompleteFormat("@[");
1289 verifyFormat("@[]");
1291 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
1292 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
1293 verifyFormat("NSArray *array = @[ [foo description] ];");
1296 "NSArray *some_variable = @[\n"
1297 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1298 " @\"aaaaaaaaaaaaaaaaa\",\n"
1299 " @\"aaaaaaaaaaaaaaaaa\",\n"
1300 " @\"aaaaaaaaaaaaaaaaa\",\n"
1303 "NSArray *some_variable = @[\n"
1304 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1305 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
1307 verifyFormat("NSArray *some_variable = @[\n"
1308 " @\"aaaaaaaaaaaaaaaaa\",\n"
1309 " @\"aaaaaaaaaaaaaaaaa\",\n"
1310 " @\"aaaaaaaaaaaaaaaaa\",\n"
1311 " @\"aaaaaaaaaaaaaaaaa\",\n"
1313 verifyFormat("NSArray *array = @[\n"
1315 " @\"a\",\n" // Trailing comma -> one per line.
1318 // We should try to be robust in case someone forgets the "@".
1319 verifyFormat("NSArray *some_variable = [\n"
1320 " @\"aaaaaaaaaaaaaaaaa\",\n"
1321 " @\"aaaaaaaaaaaaaaaaa\",\n"
1322 " @\"aaaaaaaaaaaaaaaaa\",\n"
1323 " @\"aaaaaaaaaaaaaaaaa\",\n"
1326 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
1327 " index:(NSUInteger)index\n"
1328 " nonDigitAttributes:\n"
1329 " (NSDictionary *)noDigitAttributes;");
1330 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
1331 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
1333 Style
.ColumnLimit
= 40;
1334 verifyFormat("int Foo() {\n"
1335 " a12345 = @[ a12345, a12345 ];\n"
1337 verifyFormat("int Foo() {\n"
1338 " a123 = @[ (Foo *)a12345, @(a12345) ];\n"
1340 Style
.SpacesInContainerLiterals
= false;
1341 verifyFormat("int Foo() {\n"
1342 " b12345 = @[b12345, b12345];\n"
1344 verifyFormat("int Foo() {\n"
1345 " b12345 = @[(Foo *)b12345, @(b12345)];\n"
1347 Style
.SpacesInContainerLiterals
= true;
1348 Style
.ColumnLimit
= 20;
1349 // We can't break string literals inside NSArray literals
1350 // (that raises -Wobjc-string-concatenation).
1351 verifyFormat("NSArray *foo = @[\n"
1352 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1356 TEST_F(FormatTestObjC
, BreaksCallStatementWhereSemiJustOverTheLimit
) {
1357 Style
.ColumnLimit
= 60;
1358 // If the statement starting with 'a = ...' is put on a single line, the ';'
1360 verifyFormat("int f(int a) {\n"
1361 " a = [self aaaaaaaaaa:bbbbbbbbb\n"
1362 " ccccccccc:dddddddd\n"
1367 TEST_F(FormatTestObjC
, AlwaysBreakBeforeMultilineStrings
) {
1368 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
1369 Style
.ColumnLimit
= 40;
1370 verifyFormat("aaaa = @\"bbbb\"\n"
1372 verifyFormat("aaaa(@\"bbbb\"\n"
1374 verifyFormat("aaaa(qqq, @\"bbbb\"\n"
1376 verifyFormat("[aaaa qqqq:@\"bbbb\"\n"
1378 verifyFormat("aaaa = [aaaa qqqq:@\"bbbb\"\n"
1380 verifyFormat("[aaaa qqqq:@\"bbbb\"\n"
1387 TEST_F(FormatTestObjC
, DisambiguatesCallsFromCppLambdas
) {
1388 verifyFormat("x = ([a foo:bar] && b->c == 'd');");
1389 verifyFormat("x = ([a foo:bar] + b->c == 'd');");
1390 verifyFormat("x = ([a foo:bar] + !b->c == 'd');");
1391 verifyFormat("x = ([a foo:bar] + ~b->c == 'd');");
1392 verifyFormat("x = ([a foo:bar] - b->c == 'd');");
1393 verifyFormat("x = ([a foo:bar] / b->c == 'd');");
1394 verifyFormat("x = ([a foo:bar] % b->c == 'd');");
1395 verifyFormat("x = ([a foo:bar] | b->c == 'd');");
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' : 'e');");
1404 // FIXME: The following are wrongly classified as C++ lambda expressions.
1405 // For example this code:
1406 // x = ([a foo:bar] & b->c == 'd');
1408 // 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');");
1412 // verifyFormat("x = ([a foo:bar] >> b->c == 'd');");
1415 TEST_F(FormatTestObjC
, DisambiguatesCallsFromStructuredBindings
) {
1416 verifyFormat("int f() {\n"
1417 " if (a && [f arg])\n"
1420 verifyFormat("int f() {\n"
1421 " if (a & [f arg])\n"
1424 verifyFormat("int f() {\n"
1425 " for (auto &[elem] : list)\n"
1428 verifyFormat("int f() {\n"
1429 " for (auto &&[elem] : list)\n"
1434 " for (auto /**/ const /**/ volatile /**/ && /**/ [elem] : list)\n"
1439 TEST_F(FormatTestObjC
, BreakLineBeforeNestedBlockParam
) {
1440 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
1441 Style
.ObjCBreakBeforeNestedBlockParam
= false;
1442 Style
.ColumnLimit
= 0;
1444 verifyFormat("[self.test1 t:self callback:^(typeof(self) self, NSNumber *u, "
1449 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1450 "NSNumber *u, NSNumber *v) {\n"
1454 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1455 "NSNumber *u, NSNumber *v) {\n"
1457 "} w:self callback2:^(typeof(self) self, NSNumber *a, NSNumber "
1458 "*b, NSNumber *c) {\n"
1461 verifyFormat("[self.test1 t:self w:self callback:^(typeof(self) self, "
1462 "NSNumber *u, NSNumber *v) {\n"
1466 Style
.ColumnLimit
= 80;
1468 "[self.test_method a:self b:self\n"
1469 " callback:^(typeof(self) self, NSNumber *u, NSNumber *v) {\n"
1473 verifyFormat("[self block:^(void) {\n"
1475 "} completionHandler:^(void) {\n"
1477 " [self block:^(void) {\n"
1479 " } completionHandler:^(void) {\n"
1484 Style
.ColumnLimit
= 0;
1485 verifyFormat("[[SessionService sharedService] "
1486 "loadWindowWithCompletionBlock:^(SessionWindow *window) {\n"
1488 " [self windowDidLoad:window];\n"
1490 " [self errorLoadingWindow];\n"
1493 verifyFormat("[controller test:^{\n"
1495 "} withTimeout:5 completionHandler:^{\n"
1499 "[self setupTextFieldSignals:@[\n"
1500 " self.documentWidthField,\n"
1501 " self.documentHeightField,\n"
1502 "] solver:^(NSTextField *textField) {\n"
1503 " return [self.representedObject solveEquationForTextField:textField];\n"
1507 TEST_F(FormatTestObjC
, IfNotUnlikely
) {
1508 Style
= getGoogleStyle(FormatStyle::LK_ObjC
);
1510 verifyFormat("if (argc < 5) [obj func:arg];");
1511 verifyFormat("if (argc < 5) [[obj1 method1:arg1] method2:arg2];");
1512 verifyFormat("if (argc < 5) [[foo bar] baz:i[0]];");
1513 verifyFormat("if (argc < 5) [[foo bar] baz:i[0]][1];");
1515 verifyFormat("if (argc < 5)\n"
1516 " [obj func:arg];\n"
1518 " [obj func:arg2];");
1520 verifyFormat("if (argc < 5) [[unlikely]]\n"
1521 " [obj func:arg];\n"
1523 " [obj func:arg2];");
1526 TEST_F(FormatTestObjC
, AttributesOnObjCDecl
) {
1527 Style
.AttributeMacros
.push_back("ATTRIBUTE_MACRO");
1529 // Check '__attribute__' macro directly.
1530 verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1533 verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1536 verifyFormat("__attribute__((objc_subclassing_restricted))\n"
1537 "@implementation Foo\n"
1540 // Check AttributeMacro gets treated the same, with or without parentheses.
1541 verifyFormat("ATTRIBUTE_MACRO\n"
1544 verifyFormat("ATTRIBUTE_MACRO(X)\n"
1548 // Indenter also needs to understand multiple attribute macros.
1549 // Try each of the three kinds paired with each of the other kind.
1551 // Column limit, but no reflow.
1552 verifyFormat("ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO\n"
1555 verifyFormat("ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X)\n"
1558 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO\n"
1561 verifyFormat("ATTRIBUTE_MACRO __attribute__((X))\n"
1564 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO(X)\n"
1567 verifyFormat("ATTRIBUTE_MACRO(X) __attribute__((X))\n"
1571 // Column limit that requires reflow.
1572 Style
.ColumnLimit
= 30;
1573 verifyFormat("ATTRIBUTE_MACRO(X)\n"
1577 verifyFormat("ATTRIBUTE_MACRO\n"
1578 "ATTRIBUTE_MACRO(X)\n"
1581 verifyFormat("__attribute__((X))\n"
1585 verifyFormat("ATTRIBUTE_MACRO\n"
1586 "__attribute__((X))\n"
1589 verifyFormat("__attribute__((X))\n"
1590 "ATTRIBUTE_MACRO(X)\n"
1593 verifyFormat("ATTRIBUTE_MACRO(X)\n"
1594 "__attribute__((X))\n"
1599 Style
.ColumnLimit
= 0;
1600 verifyFormat("ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO\n"
1603 verifyFormat("ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X)\n"
1606 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO\n"
1609 verifyFormat("ATTRIBUTE_MACRO __attribute__((X))\n"
1612 verifyFormat("__attribute__((X)) ATTRIBUTE_MACRO(X)\n"
1615 verifyFormat("ATTRIBUTE_MACRO(X) __attribute__((X))\n"
1620 TEST_F(FormatTestObjC
, AttributesOnObjCMethodDecl
) {
1621 Style
.AttributeMacros
.push_back("ATTRIBUTE_MACRO");
1623 // Check '__attribute__' macro directly.
1624 verifyFormat("- (id)init __attribute__((objc_designated_initializer));");
1626 // Check AttributeMacro gets treated the same, with or without parentheses.
1627 verifyFormat("- (id)init ATTRIBUTE_MACRO;");
1628 verifyFormat("- (id)init ATTRIBUTE_MACRO(X);");
1630 // Indenter also needs to understand multiple attribute macros.
1632 // Column limit (default), but no reflow.
1633 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1634 verifyFormat("- (id)init ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1635 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO;");
1636 verifyFormat("- (id)init ATTRIBUTE_MACRO __attribute__((X));");
1637 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO(X);");
1638 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) __attribute__((X));");
1640 // Column limit that requires reflow.
1641 Style
.ColumnLimit
= 30;
1643 // Reflow after method name.
1644 verifyFormat("- (id)initWithReallyLongName\n"
1645 " __attribute__((X))\n"
1646 " ATTRIBUTE_MACRO;");
1647 verifyFormat("- (id)initWithReallyLongName\n"
1648 " ATTRIBUTE_MACRO(X)\n"
1649 " ATTRIBUTE_MACRO;");
1650 verifyFormat("- (id)initWithReallyLongName\n"
1651 " ATTRIBUTE_MACRO\n"
1652 " ATTRIBUTE_MACRO;");
1653 // Reflow after first macro.
1654 // FIXME: these should indent but don't.
1656 verifyFormat("- (id)init ATTRIBUTE_MACRO(X)\n"
1657 " ATTRIBUTE_MACRO;");
1658 verifyFormat("- (id)init ATTRIBUTE_MACRO\n"
1659 " ATTRIBUTE_MACRO(X);");
1660 verifyFormat("- (id)init __attribute__((X))\n"
1661 " ATTRIBUTE_MACRO;");
1662 verifyFormat("- (id)init ATTRIBUTE_MACRO\n"
1663 " __attribute__((X));");
1664 verifyFormat("- (id)init __attribute__((X))\n"
1665 " ATTRIBUTE_MACRO(X);");
1666 verifyFormat("- (id)init ATTRIBUTE_MACRO(X)\n"
1667 " __attribute__((X));");
1671 Style
.ColumnLimit
= 0;
1672 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1673 verifyFormat("- (id)init ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1674 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO;");
1675 verifyFormat("- (id)init ATTRIBUTE_MACRO __attribute__((X));");
1676 verifyFormat("- (id)init __attribute__((X)) ATTRIBUTE_MACRO(X);");
1677 verifyFormat("- (id)init ATTRIBUTE_MACRO(X) __attribute__((X));");
1680 TEST_F(FormatTestObjC
, AttributesOnObjCProperty
) {
1681 Style
.AttributeMacros
.push_back("ATTRIBUTE_MACRO");
1683 // Check '__attribute__' macro directly.
1684 verifyFormat("@property(weak) id delegate "
1685 "__attribute__((objc_designated_initializer));");
1687 // Check AttributeMacro gets treated the same, with or without parentheses.
1688 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO;");
1689 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO(X);");
1691 // Indenter also needs to understand multiple attribute macros.
1693 // Column limit (default), but no reflow.
1695 "@property(weak) id delegate ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1697 "@property(weak) id delegate ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1699 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO;");
1701 "@property(weak) id delegate ATTRIBUTE_MACRO __attribute__((X));");
1703 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO(X);");
1705 "@property(weak) id delegate ATTRIBUTE_MACRO(X) __attribute__((X));");
1707 // Column limit that requires reflow.
1708 Style
.ColumnLimit
= 50;
1710 // Reflow after method name.
1711 verifyFormat("@property(weak) id delegateWithLongName\n"
1712 " __attribute__((X)) ATTRIBUTE_MACRO;");
1713 verifyFormat("@property(weak) id delegateWithLongName\n"
1714 " ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1715 verifyFormat("@property(weak) id delegateWithLongName\n"
1716 " ATTRIBUTE_MACRO ATTRIBUTE_MACRO;");
1717 // Reflow after first macro.
1718 // FIXME: these should indent but don't.
1720 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO(X)\n"
1721 " ATTRIBUTE_MACRO;");
1722 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO\n"
1723 " ATTRIBUTE_MACRO(X);");
1724 verifyFormat("@property(weak) id delegate __attribute__((X))\n"
1725 " ATTRIBUTE_MACRO;");
1726 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO\n"
1727 " __attribute__((X));");
1728 verifyFormat("@property(weak) id delegate __attribute__((X))\n"
1729 " ATTRIBUTE_MACRO(X);");
1730 verifyFormat("@property(weak) id delegate ATTRIBUTE_MACRO(X)\n"
1731 " __attribute__((X));");
1735 Style
.ColumnLimit
= 0;
1737 "@property(weak) id delegate ATTRIBUTE_MACRO(X) ATTRIBUTE_MACRO;");
1739 "@property(weak) id delegate ATTRIBUTE_MACRO ATTRIBUTE_MACRO(X);");
1741 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO;");
1743 "@property(weak) id delegate ATTRIBUTE_MACRO __attribute__((X));");
1745 "@property(weak) id delegate __attribute__((X)) ATTRIBUTE_MACRO(X);");
1747 "@property(weak) id delegate ATTRIBUTE_MACRO(X) __attribute__((X));");
1752 } // end namespace format
1753 } // end namespace clang