1 //===- unittest/Format/FormatTestObjC.cpp - Formatting unit tests----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Format/Format.h"
12 #include "../Tooling/ReplacementTest.h"
13 #include "FormatTestUtils.h"
15 #include "clang/Frontend/TextDiagnosticPrinter.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/MemoryBuffer.h"
18 #include "gtest/gtest.h"
20 #define DEBUG_TYPE "format-test"
22 using clang::tooling::ReplacementTest;
28 class FormatTestObjC : public ::testing::Test {
31 Style = getLLVMStyle();
32 Style.Language = FormatStyle::LK_ObjC;
41 std::string format(llvm::StringRef Code,
42 StatusCheck CheckComplete = SC_ExpectComplete) {
43 DEBUG(llvm::errs() << "---\n");
44 DEBUG(llvm::errs() << Code << "\n\n");
45 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
46 FormattingAttemptStatus Status;
47 tooling::Replacements Replaces =
48 reformat(Style, Code, Ranges, "<stdin>", &Status);
49 if (CheckComplete != SC_DoNotCheck) {
50 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
51 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
54 auto Result = applyAllReplacements(Code, Replaces);
55 EXPECT_TRUE(static_cast<bool>(Result));
56 DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
60 void verifyFormat(StringRef Code) {
61 EXPECT_EQ(Code.str(), format(test::messUp(Code)));
64 void verifyIncompleteFormat(StringRef Code) {
65 EXPECT_EQ(Code.str(), format(test::messUp(Code), SC_ExpectIncomplete));
71 TEST(FormatTestObjCStyle, DetectsObjCInHeaders) {
72 auto Style = getStyle("LLVM", "a.h", "none", "@interface\n"
74 ASSERT_TRUE((bool)Style);
75 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
77 Style = getStyle("LLVM", "a.h", "none", "@interface\n"
79 ASSERT_TRUE((bool)Style);
80 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
82 Style = getStyle("LLVM", "a.h", "none", "@interface\n"
85 ASSERT_TRUE((bool)Style);
86 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
88 Style = getStyle("LLVM", "a.h", "none", "@interface\n"
90 ASSERT_TRUE((bool)Style);
91 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
93 // No recognizable ObjC.
94 Style = getStyle("LLVM", "a.h", "none", "void f() {}");
95 ASSERT_TRUE((bool)Style);
96 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
98 Style = getStyle("{}", "a.h", "none", "@interface Foo\n@end\n");
99 ASSERT_TRUE((bool)Style);
100 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
102 Style = getStyle("{}", "a.h", "none",
103 "const int interface = 1;\nconst int end = 2;\n");
104 ASSERT_TRUE((bool)Style);
105 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
107 Style = getStyle("{}", "a.h", "none", "@protocol Foo\n@end\n");
108 ASSERT_TRUE((bool)Style);
109 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
111 Style = getStyle("{}", "a.h", "none",
112 "const int protocol = 1;\nconst int end = 2;\n");
113 ASSERT_TRUE((bool)Style);
114 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
117 getStyle("{}", "a.h", "none", "typedef NS_ENUM(NSInteger, Foo) {};\n");
118 ASSERT_TRUE((bool)Style);
119 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
121 Style = getStyle("{}", "a.h", "none", "enum Foo {};");
122 ASSERT_TRUE((bool)Style);
123 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
126 getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }\n");
127 ASSERT_TRUE((bool)Style);
128 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
131 getStyle("{}", "a.h", "none", "inline void Foo() { Log(\"Foo\"); }\n");
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]; }\n");
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}; }\n");
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}; }\n");
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;\n");
152 ASSERT_TRUE((bool)Style);
153 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
155 Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n");
156 ASSERT_TRUE((bool)Style);
157 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
159 Style = getStyle("{}", "a.h", "none", "NSObject *Foo();\n");
160 ASSERT_TRUE((bool)Style);
161 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
163 Style = getStyle("{}", "a.h", "none", "NSSet *Foo();\n");
164 ASSERT_TRUE((bool)Style);
165 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
168 TEST_F(FormatTestObjC, FormatObjCTryCatch) {
169 verifyFormat("@try {\n"
171 "} @catch (NSException e) {\n"
176 verifyFormat("DEBUG({\n"
183 TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) {
184 verifyFormat("@autoreleasepool {\n"
187 "@autoreleasepool {\n"
190 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
191 Style.BraceWrapping.AfterControlStatement = true;
192 verifyFormat("@autoreleasepool\n"
202 TEST_F(FormatTestObjC, FormatObjCGenerics) {
203 Style.ColumnLimit = 40;
204 verifyFormat("int aaaaaaaaaaaaaaaa(\n"
205 " NSArray<aaaaaaaaaaaaaaaaaa *>\n"
206 " aaaaaaaaaaaaaaaaa);\n");
207 verifyFormat("int aaaaaaaaaaaaaaaa(\n"
208 " NSArray<aaaaaaaaaaaaaaaaaaa<\n"
209 " aaaaaaaaaaaaaaaa *> *>\n"
210 " aaaaaaaaaaaaaaaaa);\n");
213 TEST_F(FormatTestObjC, FormatObjCSynchronized) {
214 verifyFormat("@synchronized(self) {\n"
217 "@synchronized(self) {\n"
220 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
221 Style.BraceWrapping.AfterControlStatement = true;
222 verifyFormat("@synchronized(self)\n"
226 "@synchronized(self)\n"
232 TEST_F(FormatTestObjC, FormatObjCInterface) {
233 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
246 verifyFormat("@interface /* wait for it */ Foo\n"
248 "// Look, a comment!\n"
249 "- (int)answerWith:(int)i;\n"
252 verifyFormat("@interface Foo\n"
257 verifyFormat("@interface Foo : Bar\n"
258 "@property(assign, readwrite) NSInteger bar;\n"
262 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @interface Foo : Bar\n"
263 "@property(assign, readwrite) NSInteger bar;\n"
267 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
271 verifyFormat("@interface Foo (HackStuff)\n"
275 verifyFormat("@interface Foo ()\n"
279 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
283 verifyFormat("@interface Foo {\n"
289 verifyFormat("@interface Foo : Bar {\n"
295 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
301 verifyFormat("@interface Foo (HackStuff) {\n"
307 verifyFormat("@interface Foo () {\n"
313 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
319 Style.ColumnLimit = 40;
320 verifyFormat("@interface ccccccccccccc () <\n"
321 " ccccccccccccc, ccccccccccccc,\n"
322 " ccccccccccccc, ccccccccccccc> {\n"
324 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
325 verifyFormat("@interface ddddddddddddd () <\n"
329 " ddddddddddddd> {\n"
332 Style.BinPackParameters = false;
333 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
334 verifyFormat("@interface eeeeeeeeeeeee () <\n"
338 " eeeeeeeeeeeee> {\n"
340 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Always;
341 verifyFormat("@interface fffffffffffff () <\n"
342 " fffffffffffff, fffffffffffff,\n"
343 " fffffffffffff, fffffffffffff> {\n"
346 Style = getGoogleStyle(FormatStyle::LK_ObjC);
347 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
359 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
362 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
365 Style.ColumnLimit = 40;
366 // BinPackParameters should be true by default.
367 verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n"
368 " int eeeee, int eeeee);\n");
369 // ObjCBinPackProtocolList should be BPS_Never by default.
370 verifyFormat("@interface fffffffffffff () <\n"
374 " fffffffffffff> {\n"
378 TEST_F(FormatTestObjC, FormatObjCImplementation) {
379 verifyFormat("@implementation Foo : NSObject {\n"
392 verifyFormat("@implementation Foo\n"
397 "// Look, a comment!\n"
398 "- (int)answerWith:(int)i {\n"
401 "+ (int)answerWith:(int)i {\n"
406 verifyFormat("@implementation Foo\n"
408 "@implementation Bar\n"
411 EXPECT_EQ("@implementation Foo : Bar\n"
415 format("@implementation Foo : Bar\n"
420 verifyFormat("@implementation Foo {\n"
426 verifyFormat("@implementation Foo : Bar {\n"
432 verifyFormat("@implementation Foo (HackStuff)\n"
435 verifyFormat("@implementation ObjcClass\n"
440 Style = getGoogleStyle(FormatStyle::LK_ObjC);
441 verifyFormat("@implementation Foo : NSObject {\n"
455 TEST_F(FormatTestObjC, FormatObjCProtocol) {
456 verifyFormat("@protocol Foo\n"
457 "@property(weak) id delegate;\n"
458 "- (NSUInteger)numberOfThings;\n"
461 verifyFormat("@protocol MyProtocol <NSObject>\n"
462 "- (NSUInteger)numberOfThings;\n"
465 verifyFormat("@protocol Foo;\n"
468 verifyFormat("@protocol Foo\n"
473 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n"
474 "@property(assign, readwrite) NSInteger bar;\n"
477 verifyFormat("@protocol myProtocol\n"
478 "- (void)mandatoryWithInt:(int)i;\n"
480 "- (void)optional;\n"
482 "- (void)required;\n"
484 "@property(assign) int madProp;\n"
487 verifyFormat("@property(nonatomic, assign, readonly)\n"
488 " int *looooooooooooooooooooooooooooongNumber;\n"
489 "@property(nonatomic, assign, readonly)\n"
490 " NSString *looooooooooooooooooooooooooooongName;");
492 verifyFormat("@implementation PR18406\n"
496 Style = getGoogleStyle(FormatStyle::LK_ObjC);
497 verifyFormat("@protocol MyProtocol <NSObject>\n"
498 "- (NSUInteger)numberOfThings;\n"
502 TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
503 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
504 " rect:(NSRect)theRect\n"
505 " interval:(float)theInterval {\n"
507 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
508 " longKeyword:(NSRect)theRect\n"
509 " longerKeyword:(float)theInterval\n"
510 " error:(NSError **)theError {\n"
512 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
513 " longKeyword:(NSRect)theRect\n"
514 " evenLongerKeyword:(float)theInterval\n"
515 " error:(NSError **)theError {\n"
517 Style.ColumnLimit = 60;
518 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
519 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
520 " NS_DESIGNATED_INITIALIZER;");
521 verifyFormat("- (void)drawRectOn:(id)surface\n"
522 " ofSize:(size_t)height\n"
525 // Continuation indent width should win over aligning colons if the function
527 Style = getGoogleStyle(FormatStyle::LK_ObjC);
528 Style.ColumnLimit = 40;
529 Style.IndentWrappedFunctionNames = true;
530 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
531 " dontAlignNamef:(NSRect)theRect {\n"
534 // Make sure we don't break aligning for short parameter names.
535 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
536 " aShortf:(NSRect)theRect {\n"
539 // Format pairs correctly.
540 Style.ColumnLimit = 80;
541 verifyFormat("- (void)drawRectOn:(id)surface\n"
542 " ofSize:(aaaaaaaa)height\n"
544 " atOrigin:(size_t)x\n"
548 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
551 TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
552 verifyFormat("[foo bar:baz];");
553 verifyFormat("return [foo bar:baz];");
554 verifyFormat("return (a)[foo bar:baz];");
555 verifyFormat("f([foo bar:baz]);");
556 verifyFormat("f(2, [foo bar:baz]);");
557 verifyFormat("f(2, a ? b : c);");
558 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
561 verifyFormat("int a = +[foo bar:baz];");
562 verifyFormat("int a = -[foo bar:baz];");
563 verifyFormat("int a = ![foo bar:baz];");
564 verifyFormat("int a = ~[foo bar:baz];");
565 verifyFormat("int a = ++[foo bar:baz];");
566 verifyFormat("int a = --[foo bar:baz];");
567 verifyFormat("int a = sizeof [foo bar:baz];");
568 verifyFormat("int a = alignof [foo bar:baz];");
569 verifyFormat("int a = &[foo bar:baz];");
570 verifyFormat("int a = *[foo bar:baz];");
571 // FIXME: Make casts work, without breaking f()[4].
572 // verifyFormat("int a = (int)[foo bar:baz];");
573 // verifyFormat("return (int)[foo bar:baz];");
574 // verifyFormat("(void)[foo bar:baz];");
575 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
578 verifyFormat("[foo bar:baz], [foo bar:baz];");
579 verifyFormat("[foo bar:baz] = [foo bar:baz];");
580 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
581 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
582 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
583 verifyFormat("[foo bar:baz] += [foo bar:baz];");
584 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
585 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
586 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
587 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
588 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
589 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
590 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
591 verifyFormat("[foo bar:baz] || [foo bar:baz];");
592 verifyFormat("[foo bar:baz] && [foo bar:baz];");
593 verifyFormat("[foo bar:baz] | [foo bar:baz];");
594 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
595 verifyFormat("[foo bar:baz] & [foo bar:baz];");
596 verifyFormat("[foo bar:baz] == [foo bar:baz];");
597 verifyFormat("[foo bar:baz] != [foo bar:baz];");
598 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
599 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
600 verifyFormat("[foo bar:baz] > [foo bar:baz];");
601 verifyFormat("[foo bar:baz] < [foo bar:baz];");
602 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
603 verifyFormat("[foo bar:baz] << [foo bar:baz];");
604 verifyFormat("[foo bar:baz] - [foo bar:baz];");
605 verifyFormat("[foo bar:baz] + [foo bar:baz];");
606 verifyFormat("[foo bar:baz] * [foo bar:baz];");
607 verifyFormat("[foo bar:baz] / [foo bar:baz];");
608 verifyFormat("[foo bar:baz] % [foo bar:baz];");
611 verifyFormat("return in[42];");
612 verifyFormat("for (auto v : in[1]) {\n}");
613 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
614 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
615 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
616 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
617 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
618 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
620 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
621 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
622 verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
624 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
625 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
626 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
627 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
628 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
629 verifyFormat("[button setAction:@selector(zoomOut:)];");
630 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
632 verifyFormat("arr[[self indexForFoo:a]];");
633 verifyFormat("throw [self errorFor:a];");
634 verifyFormat("@throw [self errorFor:a];");
636 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
637 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
638 verifyFormat("4 > 4 ? (id)a : (id)baz;");
640 // This tests that the formatter doesn't break after "backing" but before ":",
641 // which would be at 80 columns.
644 " if ((self = [super initWithContentRect:contentRect\n"
645 " styleMask:styleMask ?: otherMask\n"
646 " backing:NSBackingStoreBuffered\n"
650 "[foo checkThatBreakingAfterColonWorksOk:\n"
651 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
653 verifyFormat("[myObj short:arg1 // Force line break\n"
654 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
655 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
659 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
660 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
661 " pos.width(), pos.height())\n"
662 " styleMask:NSBorderlessWindowMask\n"
663 " backing:NSBackingStoreBuffered\n"
666 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
667 " with:contentsNativeView];");
670 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
671 " owner:nillllll];");
674 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
675 " forType:kBookmarkButtonDragType];");
677 verifyFormat("[defaultCenter addObserver:self\n"
678 " selector:@selector(willEnterFullscreen)\n"
679 " name:kWillEnterFullscreenNotification\n"
681 verifyFormat("[image_rep drawInRect:drawRect\n"
682 " fromRect:NSZeroRect\n"
683 " operation:NSCompositeCopy\n"
685 " respectFlipped:NO\n"
687 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
688 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
689 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
690 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
691 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
692 " aaaaaaaaaaaaaaaaaaaaaa];");
695 "scoped_nsobject<NSTextField> message(\n"
696 " // The frame will be fixed up when |-setMessageText:| is called.\n"
697 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
698 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
699 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
700 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
702 verifyFormat("[self param:function( //\n"
705 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
706 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
707 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
709 // Variadic parameters.
711 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
713 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
714 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
715 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
716 verifyFormat("[self // break\n"
719 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
720 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
722 // Formats pair-parameters.
723 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
724 verifyFormat("[I drawRectOn:surface //\n"
726 " atOrigin:cc:dd];");
728 // Inline block as a first argument.
729 verifyFormat("[object justBlock:^{\n"
732 verifyFormat("[object\n"
738 verifyFormat("[object\n"
742 " blockWithLongerName:^{\n"
745 verifyFormat("[object\n"
746 " blockWithLongerName:^{\n"
752 verifyFormat("[object\n"
761 Style.ColumnLimit = 70;
764 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
765 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
766 " pos.width(), pos.height())\n"
767 " syeMask:NSBorderlessWindowMask\n"
768 " bking:NSBackingStoreBuffered\n"
772 Style.ColumnLimit = 60;
773 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
774 " .aaaaaaaa];"); // FIXME: Indentation seems off.
775 // FIXME: This violates the column limit.
777 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
778 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
779 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
781 Style = getChromiumStyle(FormatStyle::LK_ObjC);
782 Style.ColumnLimit = 80;
785 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
786 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
787 " pos.width(), pos.height())\n"
788 " styleMask:NSBorderlessWindowMask\n"
789 " backing:NSBackingStoreBuffered\n"
793 // Respect continuation indent and colon alignment (e.g. when object name is
794 // short, and first selector is the longest one)
795 Style = getLLVMStyle();
796 Style.Language = FormatStyle::LK_ObjC;
797 Style.ContinuationIndentWidth = 8;
798 verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
800 " waitUntilDone:false];");
801 verifyFormat("[self performSelector:@selector(loadAccessories)\n"
802 " withObjectOnMainThread:nil\n"
803 " waitUntilDone:false];");
804 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
805 " performSelectorOnMainThread:@selector(loadAccessories)\n"
807 " waitUntilDone:false];");
808 verifyFormat("[self // force wrapping\n"
809 " performSelectorOnMainThread:@selector(loadAccessories)\n"
811 " waitUntilDone:false];");
814 TEST_F(FormatTestObjC, ObjCAt) {
815 verifyFormat("@autoreleasepool");
816 verifyFormat("@catch");
817 verifyFormat("@class");
818 verifyFormat("@compatibility_alias");
819 verifyFormat("@defs");
820 verifyFormat("@dynamic");
821 verifyFormat("@encode");
822 verifyFormat("@end");
823 verifyFormat("@finally");
824 verifyFormat("@implementation");
825 verifyFormat("@import");
826 verifyFormat("@interface");
827 verifyFormat("@optional");
828 verifyFormat("@package");
829 verifyFormat("@private");
830 verifyFormat("@property");
831 verifyFormat("@protected");
832 verifyFormat("@protocol");
833 verifyFormat("@public");
834 verifyFormat("@required");
835 verifyFormat("@selector");
836 verifyFormat("@synchronized");
837 verifyFormat("@synthesize");
838 verifyFormat("@throw");
839 verifyFormat("@try");
841 EXPECT_EQ("@interface", format("@ interface"));
843 // The precise formatting of this doesn't matter, nobody writes code like
845 verifyFormat("@ /*foo*/ interface");
848 TEST_F(FormatTestObjC, ObjCSnippets) {
849 verifyFormat("@autoreleasepool {\n"
852 verifyFormat("@class Foo, Bar;");
853 verifyFormat("@compatibility_alias AliasName ExistingClass;");
854 verifyFormat("@dynamic textColor;");
855 verifyFormat("char *buf1 = @encode(int *);");
856 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
857 verifyFormat("char *buf1 = @encode(int **);");
858 verifyFormat("Protocol *proto = @protocol(p1);");
859 verifyFormat("SEL s = @selector(foo:);");
860 verifyFormat("@synchronized(self) {\n"
864 verifyFormat("@import foo.bar;\n"
867 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
869 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
870 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
872 Style = getMozillaStyle();
873 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
874 verifyFormat("@property BOOL editable;");
876 Style = getWebKitStyle();
877 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
878 verifyFormat("@property BOOL editable;");
880 Style = getGoogleStyle(FormatStyle::LK_ObjC);
881 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
882 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
885 TEST_F(FormatTestObjC, ObjCForIn) {
886 verifyFormat("- (void)test {\n"
887 " for (NSString *n in arrayOfStrings) {\n"
891 verifyFormat("- (void)test {\n"
892 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
896 verifyFormat("for (Foo *x in bar) {\n}");
897 verifyFormat("for (Foo *x in [bar baz]) {\n}");
898 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
899 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
900 verifyFormat("for (Foo *x in [bar baz:^{\n"
905 TEST_F(FormatTestObjC, ObjCLiterals) {
906 verifyFormat("@\"String\"");
908 verifyFormat("@+4.8");
910 verifyFormat("@1LL");
912 verifyFormat("@'c'");
913 verifyFormat("@true");
915 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
916 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
917 verifyFormat("NSNumber *favoriteColor = @(Green);");
918 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
920 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
923 TEST_F(FormatTestObjC, ObjCDictLiterals) {
926 verifyFormat("@{@\"one\" : @1}");
927 verifyFormat("return @{@\"one\" : @1;");
928 verifyFormat("@{@\"one\" : @1}");
930 verifyFormat("@{@\"one\" : @{@2 : @1}}");
932 " @\"one\" : @{@2 : @1},\n"
935 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
936 verifyIncompleteFormat("[self setDict:@{}");
937 verifyIncompleteFormat("[self setDict:@{@1 : @2}");
938 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
940 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
942 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
944 verifyFormat("NSDictionary *d = @{\n"
945 " @\"nam\" : NSUserNam(),\n"
946 " @\"dte\" : [NSDate date],\n"
947 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
951 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
956 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
957 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
960 // We should try to be robust in case someone forgets the "@".
961 verifyFormat("NSDictionary *d = {\n"
962 " @\"nam\" : NSUserNam(),\n"
963 " @\"dte\" : [NSDate date],\n"
964 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
966 verifyFormat("NSMutableDictionary *dictionary =\n"
967 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
968 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
969 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
970 " cccccccccccccccc : ccccccccccccccc\n"
973 // Ensure that casts before the key are kept on the same line as the key.
975 "NSDictionary *d = @{\n"
976 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
977 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
980 Style = getGoogleStyle(FormatStyle::LK_ObjC);
983 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
988 TEST_F(FormatTestObjC, ObjCArrayLiterals) {
989 verifyIncompleteFormat("@[");
992 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
993 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
994 verifyFormat("NSArray *array = @[ [foo description] ];");
997 "NSArray *some_variable = @[\n"
998 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
999 " @\"aaaaaaaaaaaaaaaaa\",\n"
1000 " @\"aaaaaaaaaaaaaaaaa\",\n"
1001 " @\"aaaaaaaaaaaaaaaaa\",\n"
1004 "NSArray *some_variable = @[\n"
1005 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1006 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
1008 verifyFormat("NSArray *some_variable = @[\n"
1009 " @\"aaaaaaaaaaaaaaaaa\",\n"
1010 " @\"aaaaaaaaaaaaaaaaa\",\n"
1011 " @\"aaaaaaaaaaaaaaaaa\",\n"
1012 " @\"aaaaaaaaaaaaaaaaa\",\n"
1014 verifyFormat("NSArray *array = @[\n"
1016 " @\"a\",\n" // Trailing comma -> one per line.
1019 // We should try to be robust in case someone forgets the "@".
1020 verifyFormat("NSArray *some_variable = [\n"
1021 " @\"aaaaaaaaaaaaaaaaa\",\n"
1022 " @\"aaaaaaaaaaaaaaaaa\",\n"
1023 " @\"aaaaaaaaaaaaaaaaa\",\n"
1024 " @\"aaaaaaaaaaaaaaaaa\",\n"
1027 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
1028 " index:(NSUInteger)index\n"
1029 " nonDigitAttributes:\n"
1030 " (NSDictionary *)noDigitAttributes;");
1031 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
1032 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
1034 Style.ColumnLimit = 20;
1035 // We can't break string literals inside NSArray literals
1036 // (that raises -Wobjc-string-concatenation).
1037 verifyFormat("NSArray *foo = @[\n"
1038 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1042 } // end namespace format
1043 } // end namespace clang