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