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;
23 using clang::tooling::toReplacements;
29 class FormatTestObjC : public ::testing::Test {
32 Style = getLLVMStyle();
33 Style.Language = FormatStyle::LK_ObjC;
36 enum IncompleteCheck {
42 std::string format(llvm::StringRef Code,
43 IncompleteCheck CheckIncomplete = IC_ExpectComplete) {
44 DEBUG(llvm::errs() << "---\n");
45 DEBUG(llvm::errs() << Code << "\n\n");
46 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
47 bool IncompleteFormat = false;
48 tooling::Replacements Replaces =
49 reformat(Style, Code, Ranges, "<stdin>", &IncompleteFormat);
50 if (CheckIncomplete != IC_DoNotCheck) {
51 bool ExpectedIncompleteFormat = CheckIncomplete == IC_ExpectIncomplete;
52 EXPECT_EQ(ExpectedIncompleteFormat, IncompleteFormat) << Code << "\n\n";
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), IC_ExpectIncomplete));
71 TEST_F(FormatTestObjC, DetectsObjCInHeaders) {
72 Style = getStyle("LLVM", "a.h", "none", "@interface\n"
74 EXPECT_EQ(FormatStyle::LK_ObjC, Style.Language);
75 Style = getStyle("LLVM", "a.h", "none", "@interface\n"
77 EXPECT_EQ(FormatStyle::LK_ObjC, Style.Language);
79 // No recognizable ObjC.
80 Style = getStyle("LLVM", "a.h", "none", "void f() {}");
81 EXPECT_EQ(FormatStyle::LK_Cpp, Style.Language);
84 TEST_F(FormatTestObjC, FormatObjCTryCatch) {
85 verifyFormat("@try {\n"
87 "} @catch (NSException e) {\n"
92 verifyFormat("DEBUG({\n"
99 TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) {
100 verifyFormat("@autoreleasepool {\n"
103 "@autoreleasepool {\n"
106 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
107 verifyFormat("@autoreleasepool\n"
117 TEST_F(FormatTestObjC, FormatObjCInterface) {
118 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
131 verifyFormat("@interface /* wait for it */ Foo\n"
133 "// Look, a comment!\n"
134 "- (int)answerWith:(int)i;\n"
137 verifyFormat("@interface Foo\n"
142 verifyFormat("@interface Foo : Bar\n"
146 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
150 verifyFormat("@interface Foo (HackStuff)\n"
154 verifyFormat("@interface Foo ()\n"
158 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
162 verifyFormat("@interface Foo {\n"
168 verifyFormat("@interface Foo : Bar {\n"
174 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
180 verifyFormat("@interface Foo (HackStuff) {\n"
186 verifyFormat("@interface Foo () {\n"
192 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
198 Style = getGoogleStyle(FormatStyle::LK_ObjC);
199 verifyFormat("@interface Foo : NSObject<NSSomeDelegate> {\n"
211 verifyFormat("@interface Foo : Bar<Baz, Quux>\n"
214 verifyFormat("@interface Foo (HackStuff)<MyProtocol>\n"
217 Style.BinPackParameters = false;
218 Style.ColumnLimit = 80;
219 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ()<\n"
220 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
221 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
222 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
223 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
227 TEST_F(FormatTestObjC, FormatObjCImplementation) {
228 verifyFormat("@implementation Foo : NSObject {\n"
241 verifyFormat("@implementation Foo\n"
246 "// Look, a comment!\n"
247 "- (int)answerWith:(int)i {\n"
250 "+ (int)answerWith:(int)i {\n"
255 verifyFormat("@implementation Foo\n"
257 "@implementation Bar\n"
260 EXPECT_EQ("@implementation Foo : Bar\n"
264 format("@implementation Foo : Bar\n"
269 verifyFormat("@implementation Foo {\n"
275 verifyFormat("@implementation Foo : Bar {\n"
281 verifyFormat("@implementation Foo (HackStuff)\n"
284 verifyFormat("@implementation ObjcClass\n"
289 Style = getGoogleStyle(FormatStyle::LK_ObjC);
290 verifyFormat("@implementation Foo : NSObject {\n"
304 TEST_F(FormatTestObjC, FormatObjCProtocol) {
305 verifyFormat("@protocol Foo\n"
306 "@property(weak) id delegate;\n"
307 "- (NSUInteger)numberOfThings;\n"
310 verifyFormat("@protocol MyProtocol <NSObject>\n"
311 "- (NSUInteger)numberOfThings;\n"
314 verifyFormat("@protocol Foo;\n"
317 verifyFormat("@protocol Foo\n"
322 verifyFormat("@protocol myProtocol\n"
323 "- (void)mandatoryWithInt:(int)i;\n"
325 "- (void)optional;\n"
327 "- (void)required;\n"
329 "@property(assign) int madProp;\n"
332 verifyFormat("@property(nonatomic, assign, readonly)\n"
333 " int *looooooooooooooooooooooooooooongNumber;\n"
334 "@property(nonatomic, assign, readonly)\n"
335 " NSString *looooooooooooooooooooooooooooongName;");
337 verifyFormat("@implementation PR18406\n"
341 Style = getGoogleStyle(FormatStyle::LK_ObjC);
342 verifyFormat("@protocol MyProtocol<NSObject>\n"
343 "- (NSUInteger)numberOfThings;\n"
347 TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
348 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
349 " rect:(NSRect)theRect\n"
350 " interval:(float)theInterval {\n"
352 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
353 " longKeyword:(NSRect)theRect\n"
354 " longerKeyword:(float)theInterval\n"
355 " error:(NSError **)theError {\n"
357 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
358 " longKeyword:(NSRect)theRect\n"
359 " evenLongerKeyword:(float)theInterval\n"
360 " error:(NSError **)theError {\n"
362 Style.ColumnLimit = 60;
363 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
364 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
365 " NS_DESIGNATED_INITIALIZER;");
366 verifyFormat("- (void)drawRectOn:(id)surface\n"
367 " ofSize:(size_t)height\n"
370 // Continuation indent width should win over aligning colons if the function
372 Style = getGoogleStyle(FormatStyle::LK_ObjC);
373 Style.ColumnLimit = 40;
374 Style.IndentWrappedFunctionNames = true;
375 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
376 " dontAlignNamef:(NSRect)theRect {\n"
379 // Make sure we don't break aligning for short parameter names.
380 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
381 " aShortf:(NSRect)theRect {\n"
384 // Format pairs correctly.
385 Style.ColumnLimit = 80;
386 verifyFormat("- (void)drawRectOn:(id)surface\n"
387 " ofSize:(aaaaaaaa)height\n"
389 " atOrigin:(size_t)x\n"
393 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
396 TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
397 verifyFormat("[foo bar:baz];");
398 verifyFormat("return [foo bar:baz];");
399 verifyFormat("return (a)[foo bar:baz];");
400 verifyFormat("f([foo bar:baz]);");
401 verifyFormat("f(2, [foo bar:baz]);");
402 verifyFormat("f(2, a ? b : c);");
403 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
406 verifyFormat("int a = +[foo bar:baz];");
407 verifyFormat("int a = -[foo bar:baz];");
408 verifyFormat("int a = ![foo bar:baz];");
409 verifyFormat("int a = ~[foo bar:baz];");
410 verifyFormat("int a = ++[foo bar:baz];");
411 verifyFormat("int a = --[foo bar:baz];");
412 verifyFormat("int a = sizeof [foo bar:baz];");
413 verifyFormat("int a = alignof [foo bar:baz];");
414 verifyFormat("int a = &[foo bar:baz];");
415 verifyFormat("int a = *[foo bar:baz];");
416 // FIXME: Make casts work, without breaking f()[4].
417 // verifyFormat("int a = (int)[foo bar:baz];");
418 // verifyFormat("return (int)[foo bar:baz];");
419 // verifyFormat("(void)[foo bar:baz];");
420 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
423 verifyFormat("[foo bar:baz], [foo bar:baz];");
424 verifyFormat("[foo bar:baz] = [foo bar:baz];");
425 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
426 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
427 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
428 verifyFormat("[foo bar:baz] += [foo bar:baz];");
429 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
430 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
431 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
432 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
433 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
434 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
435 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
436 verifyFormat("[foo bar:baz] || [foo bar:baz];");
437 verifyFormat("[foo bar:baz] && [foo bar:baz];");
438 verifyFormat("[foo bar:baz] | [foo bar:baz];");
439 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
440 verifyFormat("[foo bar:baz] & [foo bar:baz];");
441 verifyFormat("[foo bar:baz] == [foo bar:baz];");
442 verifyFormat("[foo bar:baz] != [foo bar:baz];");
443 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
444 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
445 verifyFormat("[foo bar:baz] > [foo bar:baz];");
446 verifyFormat("[foo bar:baz] < [foo bar:baz];");
447 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
448 verifyFormat("[foo bar:baz] << [foo bar:baz];");
449 verifyFormat("[foo bar:baz] - [foo bar:baz];");
450 verifyFormat("[foo bar:baz] + [foo bar:baz];");
451 verifyFormat("[foo bar:baz] * [foo bar:baz];");
452 verifyFormat("[foo bar:baz] / [foo bar:baz];");
453 verifyFormat("[foo bar:baz] % [foo bar:baz];");
456 verifyFormat("return in[42];");
457 verifyFormat("for (auto v : in[1]) {\n}");
458 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
459 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
460 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
461 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
462 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
463 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
465 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
466 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
467 verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
469 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
470 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
471 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
472 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
473 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
474 verifyFormat("[button setAction:@selector(zoomOut:)];");
475 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
477 verifyFormat("arr[[self indexForFoo:a]];");
478 verifyFormat("throw [self errorFor:a];");
479 verifyFormat("@throw [self errorFor:a];");
481 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
482 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
483 verifyFormat("4 > 4 ? (id)a : (id)baz;");
485 // This tests that the formatter doesn't break after "backing" but before ":",
486 // which would be at 80 columns.
489 " if ((self = [super initWithContentRect:contentRect\n"
490 " styleMask:styleMask ?: otherMask\n"
491 " backing:NSBackingStoreBuffered\n"
495 "[foo checkThatBreakingAfterColonWorksOk:\n"
496 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
498 verifyFormat("[myObj short:arg1 // Force line break\n"
499 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
500 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
504 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
505 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
506 " pos.width(), pos.height())\n"
507 " styleMask:NSBorderlessWindowMask\n"
508 " backing:NSBackingStoreBuffered\n"
511 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
512 " with:contentsNativeView];");
515 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
516 " owner:nillllll];");
519 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
520 " forType:kBookmarkButtonDragType];");
522 verifyFormat("[defaultCenter addObserver:self\n"
523 " selector:@selector(willEnterFullscreen)\n"
524 " name:kWillEnterFullscreenNotification\n"
526 verifyFormat("[image_rep drawInRect:drawRect\n"
527 " fromRect:NSZeroRect\n"
528 " operation:NSCompositeCopy\n"
530 " respectFlipped:NO\n"
532 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
533 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
534 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
535 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
536 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
537 " aaaaaaaaaaaaaaaaaaaaaa];");
540 "scoped_nsobject<NSTextField> message(\n"
541 " // The frame will be fixed up when |-setMessageText:| is called.\n"
542 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
543 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
544 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
545 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
547 verifyFormat("[self param:function( //\n"
550 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
551 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
552 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
554 // Variadic parameters.
556 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
558 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
559 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
560 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
561 verifyFormat("[self // break\n"
564 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
565 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
567 // Formats pair-parameters.
568 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
569 verifyFormat("[I drawRectOn:surface //\n"
571 " atOrigin:cc:dd];");
573 Style.ColumnLimit = 70;
576 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
577 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
578 " pos.width(), pos.height())\n"
579 " syeMask:NSBorderlessWindowMask\n"
580 " bking:NSBackingStoreBuffered\n"
584 Style.ColumnLimit = 60;
585 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
586 " .aaaaaaaa];"); // FIXME: Indentation seems off.
587 // FIXME: This violates the column limit.
589 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
590 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
591 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
593 Style = getChromiumStyle(FormatStyle::LK_ObjC);
594 Style.ColumnLimit = 80;
597 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
598 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
599 " pos.width(), pos.height())\n"
600 " styleMask:NSBorderlessWindowMask\n"
601 " backing:NSBackingStoreBuffered\n"
606 TEST_F(FormatTestObjC, ObjCAt) {
607 verifyFormat("@autoreleasepool");
608 verifyFormat("@catch");
609 verifyFormat("@class");
610 verifyFormat("@compatibility_alias");
611 verifyFormat("@defs");
612 verifyFormat("@dynamic");
613 verifyFormat("@encode");
614 verifyFormat("@end");
615 verifyFormat("@finally");
616 verifyFormat("@implementation");
617 verifyFormat("@import");
618 verifyFormat("@interface");
619 verifyFormat("@optional");
620 verifyFormat("@package");
621 verifyFormat("@private");
622 verifyFormat("@property");
623 verifyFormat("@protected");
624 verifyFormat("@protocol");
625 verifyFormat("@public");
626 verifyFormat("@required");
627 verifyFormat("@selector");
628 verifyFormat("@synchronized");
629 verifyFormat("@synthesize");
630 verifyFormat("@throw");
631 verifyFormat("@try");
633 EXPECT_EQ("@interface", format("@ interface"));
635 // The precise formatting of this doesn't matter, nobody writes code like
637 verifyFormat("@ /*foo*/ interface");
640 TEST_F(FormatTestObjC, ObjCSnippets) {
641 verifyFormat("@autoreleasepool {\n"
644 verifyFormat("@class Foo, Bar;");
645 verifyFormat("@compatibility_alias AliasName ExistingClass;");
646 verifyFormat("@dynamic textColor;");
647 verifyFormat("char *buf1 = @encode(int *);");
648 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
649 verifyFormat("char *buf1 = @encode(int **);");
650 verifyFormat("Protocol *proto = @protocol(p1);");
651 verifyFormat("SEL s = @selector(foo:);");
652 verifyFormat("@synchronized(self) {\n"
656 verifyFormat("@import foo.bar;\n"
659 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
661 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
662 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
664 Style = getMozillaStyle();
665 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
666 verifyFormat("@property BOOL editable;");
668 Style = getWebKitStyle();
669 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
670 verifyFormat("@property BOOL editable;");
672 Style = getGoogleStyle(FormatStyle::LK_ObjC);
673 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
674 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
677 TEST_F(FormatTestObjC, ObjCForIn) {
678 verifyFormat("- (void)test {\n"
679 " for (NSString *n in arrayOfStrings) {\n"
683 verifyFormat("- (void)test {\n"
684 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
690 TEST_F(FormatTestObjC, ObjCLiterals) {
691 verifyFormat("@\"String\"");
693 verifyFormat("@+4.8");
695 verifyFormat("@1LL");
697 verifyFormat("@'c'");
698 verifyFormat("@true");
700 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
701 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
702 verifyFormat("NSNumber *favoriteColor = @(Green);");
703 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
705 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
708 TEST_F(FormatTestObjC, ObjCDictLiterals) {
711 verifyFormat("@{@\"one\" : @1}");
712 verifyFormat("return @{@\"one\" : @1;");
713 verifyFormat("@{@\"one\" : @1}");
715 verifyFormat("@{@\"one\" : @{@2 : @1}}");
717 " @\"one\" : @{@2 : @1},\n"
720 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
721 verifyIncompleteFormat("[self setDict:@{}");
722 verifyIncompleteFormat("[self setDict:@{@1 : @2}");
723 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
725 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
727 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
729 verifyFormat("NSDictionary *d = @{\n"
730 " @\"nam\" : NSUserNam(),\n"
731 " @\"dte\" : [NSDate date],\n"
732 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
736 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
741 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
742 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
745 // We should try to be robust in case someone forgets the "@".
746 verifyFormat("NSDictionary *d = {\n"
747 " @\"nam\" : NSUserNam(),\n"
748 " @\"dte\" : [NSDate date],\n"
749 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
751 verifyFormat("NSMutableDictionary *dictionary =\n"
752 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
753 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
754 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
755 " cccccccccccccccc : ccccccccccccccc\n"
758 // Ensure that casts before the key are kept on the same line as the key.
760 "NSDictionary *d = @{\n"
761 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
762 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
765 Style = getGoogleStyle(FormatStyle::LK_ObjC);
768 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
773 TEST_F(FormatTestObjC, ObjCArrayLiterals) {
774 verifyIncompleteFormat("@[");
777 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
778 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
779 verifyFormat("NSArray *array = @[ [foo description] ];");
782 "NSArray *some_variable = @[\n"
783 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
784 " @\"aaaaaaaaaaaaaaaaa\",\n"
785 " @\"aaaaaaaaaaaaaaaaa\",\n"
786 " @\"aaaaaaaaaaaaaaaaa\",\n"
789 "NSArray *some_variable = @[\n"
790 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
791 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
793 verifyFormat("NSArray *some_variable = @[\n"
794 " @\"aaaaaaaaaaaaaaaaa\",\n"
795 " @\"aaaaaaaaaaaaaaaaa\",\n"
796 " @\"aaaaaaaaaaaaaaaaa\",\n"
797 " @\"aaaaaaaaaaaaaaaaa\",\n"
799 verifyFormat("NSArray *array = @[\n"
801 " @\"a\",\n" // Trailing comma -> one per line.
804 // We should try to be robust in case someone forgets the "@".
805 verifyFormat("NSArray *some_variable = [\n"
806 " @\"aaaaaaaaaaaaaaaaa\",\n"
807 " @\"aaaaaaaaaaaaaaaaa\",\n"
808 " @\"aaaaaaaaaaaaaaaaa\",\n"
809 " @\"aaaaaaaaaaaaaaaaa\",\n"
812 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
813 " index:(NSUInteger)index\n"
814 " nonDigitAttributes:\n"
815 " (NSDictionary *)noDigitAttributes;");
816 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
817 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
821 } // end namespace format
822 } // end namespace clang