blob: ded4607334e7d0849c00740841f47deaac403d9e [file] [log] [blame]
Daniel Jasper03a04fe2016-12-12 12:42:29 +00001//===- unittest/Format/FormatTestObjC.cpp - Formatting unit tests----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "clang/Format/Format.h"
11
12#include "../Tooling/ReplacementTest.h"
13#include "FormatTestUtils.h"
14
15#include "clang/Frontend/TextDiagnosticPrinter.h"
16#include "llvm/Support/Debug.h"
17#include "llvm/Support/MemoryBuffer.h"
18#include "gtest/gtest.h"
19
20#define DEBUG_TYPE "format-test"
21
22using clang::tooling::ReplacementTest;
Daniel Jasper03a04fe2016-12-12 12:42:29 +000023
24namespace clang {
25namespace format {
26namespace {
27
28class FormatTestObjC : public ::testing::Test {
29protected:
30 FormatTestObjC() {
31 Style = getLLVMStyle();
32 Style.Language = FormatStyle::LK_ObjC;
33 }
34
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000035 enum StatusCheck {
36 SC_ExpectComplete,
37 SC_ExpectIncomplete,
38 SC_DoNotCheck
Daniel Jasper03a04fe2016-12-12 12:42:29 +000039 };
40
41 std::string format(llvm::StringRef Code,
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000042 StatusCheck CheckComplete = SC_ExpectComplete) {
Daniel Jasper03a04fe2016-12-12 12:42:29 +000043 DEBUG(llvm::errs() << "---\n");
44 DEBUG(llvm::errs() << Code << "\n\n");
45 std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000046 FormattingAttemptStatus Status;
Daniel Jasper03a04fe2016-12-12 12:42:29 +000047 tooling::Replacements Replaces =
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000048 reformat(Style, Code, Ranges, "<stdin>", &Status);
49 if (CheckComplete != SC_DoNotCheck) {
50 bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
51 EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
52 << Code << "\n\n";
Daniel Jasper03a04fe2016-12-12 12:42:29 +000053 }
54 auto Result = applyAllReplacements(Code, Replaces);
55 EXPECT_TRUE(static_cast<bool>(Result));
56 DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
57 return *Result;
58 }
59
60 void verifyFormat(StringRef Code) {
Mark Zerenc9a918c2018-04-04 21:09:00 +000061 EXPECT_EQ(Code.str(), format(Code)) << "Expected code is not stable";
Daniel Jasper03a04fe2016-12-12 12:42:29 +000062 EXPECT_EQ(Code.str(), format(test::messUp(Code)));
63 }
64
65 void verifyIncompleteFormat(StringRef Code) {
Krasimir Georgievbcda54b2017-04-21 14:35:20 +000066 EXPECT_EQ(Code.str(), format(test::messUp(Code), SC_ExpectIncomplete));
Daniel Jasper03a04fe2016-12-12 12:42:29 +000067 }
68
69 FormatStyle Style;
70};
71
Antonio Maiorano3adfb6a2017-01-17 00:12:27 +000072TEST(FormatTestObjCStyle, DetectsObjCInHeaders) {
73 auto Style = getStyle("LLVM", "a.h", "none", "@interface\n"
74 "- (id)init;");
75 ASSERT_TRUE((bool)Style);
76 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
77
Daniel Jasper03a04fe2016-12-12 12:42:29 +000078 Style = getStyle("LLVM", "a.h", "none", "@interface\n"
79 "+ (id)init;");
Antonio Maiorano3adfb6a2017-01-17 00:12:27 +000080 ASSERT_TRUE((bool)Style);
81 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
Daniel Jasper03a04fe2016-12-12 12:42:29 +000082
Krasimir Georgiev11ef5312017-12-12 13:43:59 +000083 Style = getStyle("LLVM", "a.h", "none", "@interface\n"
84 "@end\n"
85 "//comment");
86 ASSERT_TRUE((bool)Style);
87 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
88
89 Style = getStyle("LLVM", "a.h", "none", "@interface\n"
90 "@end //comment");
91 ASSERT_TRUE((bool)Style);
92 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
93
Daniel Jasper03a04fe2016-12-12 12:42:29 +000094 // No recognizable ObjC.
95 Style = getStyle("LLVM", "a.h", "none", "void f() {}");
Antonio Maiorano3adfb6a2017-01-17 00:12:27 +000096 ASSERT_TRUE((bool)Style);
97 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
Ben Hamiltone2e3e672018-01-17 17:33:08 +000098
99 Style = getStyle("{}", "a.h", "none", "@interface Foo\n@end\n");
100 ASSERT_TRUE((bool)Style);
101 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
102
103 Style = getStyle("{}", "a.h", "none",
104 "const int interface = 1;\nconst int end = 2;\n");
105 ASSERT_TRUE((bool)Style);
106 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
107
108 Style = getStyle("{}", "a.h", "none", "@protocol Foo\n@end\n");
109 ASSERT_TRUE((bool)Style);
110 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
111
112 Style = getStyle("{}", "a.h", "none",
113 "const int protocol = 1;\nconst int end = 2;\n");
114 ASSERT_TRUE((bool)Style);
115 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
116
Ben Hamiltone2e3e672018-01-17 17:33:08 +0000117 Style =
118 getStyle("{}", "a.h", "none", "typedef NS_ENUM(NSInteger, Foo) {};\n");
119 ASSERT_TRUE((bool)Style);
120 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
121
122 Style = getStyle("{}", "a.h", "none", "enum Foo {};");
123 ASSERT_TRUE((bool)Style);
124 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
125
Ben Hamiltone2e3e672018-01-17 17:33:08 +0000126 Style =
127 getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }\n");
128 ASSERT_TRUE((bool)Style);
129 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
130
131 Style =
132 getStyle("{}", "a.h", "none", "inline void Foo() { Log(\"Foo\"); }\n");
133 ASSERT_TRUE((bool)Style);
134 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
135
136 Style =
137 getStyle("{}", "a.h", "none", "inline void Foo() { id = @[1, 2, 3]; }\n");
138 ASSERT_TRUE((bool)Style);
139 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
140
141 Style = getStyle("{}", "a.h", "none",
142 "inline void Foo() { id foo = @{1: 2, 3: 4, 5: 6}; }\n");
143 ASSERT_TRUE((bool)Style);
144 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
145
146 Style = getStyle("{}", "a.h", "none",
147 "inline void Foo() { int foo[] = {1, 2, 3}; }\n");
148 ASSERT_TRUE((bool)Style);
149 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
Jacek Olesiakce4f0af2018-02-15 08:47:56 +0000150
151 // ObjC characteristic types.
152 Style = getStyle("{}", "a.h", "none", "extern NSString *kFoo;\n");
153 ASSERT_TRUE((bool)Style);
154 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
155
156 Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n");
157 ASSERT_TRUE((bool)Style);
158 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
159
160 Style = getStyle("{}", "a.h", "none", "NSObject *Foo();\n");
161 ASSERT_TRUE((bool)Style);
162 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
163
164 Style = getStyle("{}", "a.h", "none", "NSSet *Foo();\n");
165 ASSERT_TRUE((bool)Style);
166 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000167}
168
169TEST_F(FormatTestObjC, FormatObjCTryCatch) {
170 verifyFormat("@try {\n"
171 " f();\n"
172 "} @catch (NSException e) {\n"
173 " @throw;\n"
174 "} @finally {\n"
175 " exit(42);\n"
176 "}");
177 verifyFormat("DEBUG({\n"
178 " @try {\n"
179 " } @finally {\n"
180 " }\n"
181 "});\n");
182}
183
184TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) {
185 verifyFormat("@autoreleasepool {\n"
186 " f();\n"
187 "}\n"
188 "@autoreleasepool {\n"
189 " f();\n"
190 "}\n");
Francois Ferranda2484b22018-02-27 13:48:27 +0000191 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
192 Style.BraceWrapping.AfterControlStatement = true;
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000193 verifyFormat("@autoreleasepool\n"
194 "{\n"
195 " f();\n"
196 "}\n"
197 "@autoreleasepool\n"
198 "{\n"
199 " f();\n"
200 "}\n");
201}
202
Ben Hamilton56d1c012018-02-06 18:01:47 +0000203TEST_F(FormatTestObjC, FormatObjCGenerics) {
204 Style.ColumnLimit = 40;
205 verifyFormat("int aaaaaaaaaaaaaaaa(\n"
206 " NSArray<aaaaaaaaaaaaaaaaaa *>\n"
207 " aaaaaaaaaaaaaaaaa);\n");
208 verifyFormat("int aaaaaaaaaaaaaaaa(\n"
209 " NSArray<aaaaaaaaaaaaaaaaaaa<\n"
210 " aaaaaaaaaaaaaaaa *> *>\n"
211 " aaaaaaaaaaaaaaaaa);\n");
212}
213
Francois Ferrandba91c3d2018-02-27 13:48:21 +0000214TEST_F(FormatTestObjC, FormatObjCSynchronized) {
215 verifyFormat("@synchronized(self) {\n"
216 " f();\n"
217 "}\n"
218 "@synchronized(self) {\n"
219 " f();\n"
220 "}\n");
Francois Ferranda2484b22018-02-27 13:48:27 +0000221 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
222 Style.BraceWrapping.AfterControlStatement = true;
Francois Ferrandba91c3d2018-02-27 13:48:21 +0000223 verifyFormat("@synchronized(self)\n"
224 "{\n"
225 " f();\n"
226 "}\n"
227 "@synchronized(self)\n"
228 "{\n"
229 " f();\n"
230 "}\n");
231}
232
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000233TEST_F(FormatTestObjC, FormatObjCInterface) {
234 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
235 "@public\n"
236 " int field1;\n"
237 "@protected\n"
238 " int field2;\n"
239 "@private\n"
240 " int field3;\n"
241 "@package\n"
242 " int field4;\n"
243 "}\n"
244 "+ (id)init;\n"
245 "@end");
246
247 verifyFormat("@interface /* wait for it */ Foo\n"
248 "+ (id)init;\n"
249 "// Look, a comment!\n"
250 "- (int)answerWith:(int)i;\n"
251 "@end");
252
253 verifyFormat("@interface Foo\n"
254 "@end\n"
255 "@interface Bar\n"
256 "@end");
257
258 verifyFormat("@interface Foo : Bar\n"
Nico Weberc068ff72018-01-23 17:10:25 +0000259 "@property(assign, readwrite) NSInteger bar;\n"
260 "+ (id)init;\n"
261 "@end");
262
263 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @interface Foo : Bar\n"
264 "@property(assign, readwrite) NSInteger bar;\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000265 "+ (id)init;\n"
266 "@end");
267
268 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
269 "+ (id)init;\n"
270 "@end");
271
272 verifyFormat("@interface Foo (HackStuff)\n"
273 "+ (id)init;\n"
274 "@end");
275
276 verifyFormat("@interface Foo ()\n"
277 "+ (id)init;\n"
278 "@end");
279
280 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
281 "+ (id)init;\n"
282 "@end");
283
284 verifyFormat("@interface Foo {\n"
285 " int _i;\n"
286 "}\n"
287 "+ (id)init;\n"
288 "@end");
289
290 verifyFormat("@interface Foo : Bar {\n"
291 " int _i;\n"
292 "}\n"
293 "+ (id)init;\n"
294 "@end");
295
296 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
297 " int _i;\n"
298 "}\n"
299 "+ (id)init;\n"
300 "@end");
301
Ben Hamilton1462e842018-04-05 15:26:25 +0000302 verifyFormat("@interface Foo <Baz : Blech> : Bar <Baz, Quux> {\n"
303 " int _i;\n"
304 "}\n"
305 "+ (id)init;\n"
306 "@end");
307
308 verifyFormat("@interface Foo <Bar : Baz <Blech>> : Xyzzy <Corge> {\n"
309 " int _i;\n"
310 "}\n"
311 "+ (id)init;\n"
312 "@end");
313
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000314 verifyFormat("@interface Foo (HackStuff) {\n"
315 " int _i;\n"
316 "}\n"
317 "+ (id)init;\n"
318 "@end");
319
320 verifyFormat("@interface Foo () {\n"
321 " int _i;\n"
322 "}\n"
323 "+ (id)init;\n"
324 "@end");
325
326 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
327 " int _i;\n"
328 "}\n"
329 "+ (id)init;\n"
330 "@end");
331
Ben Hamilton5dd40182018-01-29 20:01:49 +0000332 Style.ColumnLimit = 40;
333 verifyFormat("@interface ccccccccccccc () <\n"
334 " ccccccccccccc, ccccccccccccc,\n"
335 " ccccccccccccc, ccccccccccccc> {\n"
336 "}");
Ben Hamilton4dc658c2018-02-02 20:15:14 +0000337 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
Ben Hamilton5dd40182018-01-29 20:01:49 +0000338 verifyFormat("@interface ddddddddddddd () <\n"
339 " ddddddddddddd,\n"
340 " ddddddddddddd,\n"
341 " ddddddddddddd,\n"
342 " ddddddddddddd> {\n"
343 "}");
344
Ben Hamilton4dc658c2018-02-02 20:15:14 +0000345 Style.BinPackParameters = false;
346 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
347 verifyFormat("@interface eeeeeeeeeeeee () <\n"
348 " eeeeeeeeeeeee,\n"
349 " eeeeeeeeeeeee,\n"
350 " eeeeeeeeeeeee,\n"
351 " eeeeeeeeeeeee> {\n"
352 "}");
353 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Always;
354 verifyFormat("@interface fffffffffffff () <\n"
355 " fffffffffffff, fffffffffffff,\n"
356 " fffffffffffff, fffffffffffff> {\n"
357 "}");
358
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000359 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000360 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000361 " @public\n"
362 " int field1;\n"
363 " @protected\n"
364 " int field2;\n"
365 " @private\n"
366 " int field3;\n"
367 " @package\n"
368 " int field4;\n"
369 "}\n"
370 "+ (id)init;\n"
371 "@end");
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000372 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000373 "+ (id)init;\n"
374 "@end");
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000375 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000376 "+ (id)init;\n"
377 "@end");
Ben Hamilton3a47fdd2018-02-08 01:49:10 +0000378 Style.ColumnLimit = 40;
379 // BinPackParameters should be true by default.
380 verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n"
381 " int eeeee, int eeeee);\n");
382 // ObjCBinPackProtocolList should be BPS_Never by default.
383 verifyFormat("@interface fffffffffffff () <\n"
384 " fffffffffffff,\n"
385 " fffffffffffff,\n"
386 " fffffffffffff,\n"
387 " fffffffffffff> {\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000388 "}");
389}
390
391TEST_F(FormatTestObjC, FormatObjCImplementation) {
392 verifyFormat("@implementation Foo : NSObject {\n"
393 "@public\n"
394 " int field1;\n"
395 "@protected\n"
396 " int field2;\n"
397 "@private\n"
398 " int field3;\n"
399 "@package\n"
400 " int field4;\n"
401 "}\n"
402 "+ (id)init {\n}\n"
403 "@end");
404
405 verifyFormat("@implementation Foo\n"
406 "+ (id)init {\n"
407 " if (true)\n"
408 " return nil;\n"
409 "}\n"
410 "// Look, a comment!\n"
411 "- (int)answerWith:(int)i {\n"
412 " return i;\n"
413 "}\n"
414 "+ (int)answerWith:(int)i {\n"
415 " return i;\n"
416 "}\n"
417 "@end");
418
419 verifyFormat("@implementation Foo\n"
420 "@end\n"
421 "@implementation Bar\n"
422 "@end");
423
424 EXPECT_EQ("@implementation Foo : Bar\n"
425 "+ (id)init {\n}\n"
426 "- (void)foo {\n}\n"
427 "@end",
428 format("@implementation Foo : Bar\n"
429 "+(id)init{}\n"
430 "-(void)foo{}\n"
431 "@end"));
432
433 verifyFormat("@implementation Foo {\n"
434 " int _i;\n"
435 "}\n"
436 "+ (id)init {\n}\n"
437 "@end");
438
439 verifyFormat("@implementation Foo : Bar {\n"
440 " int _i;\n"
441 "}\n"
442 "+ (id)init {\n}\n"
443 "@end");
444
445 verifyFormat("@implementation Foo (HackStuff)\n"
446 "+ (id)init {\n}\n"
447 "@end");
448 verifyFormat("@implementation ObjcClass\n"
449 "- (void)method;\n"
450 "{}\n"
451 "@end");
452
453 Style = getGoogleStyle(FormatStyle::LK_ObjC);
454 verifyFormat("@implementation Foo : NSObject {\n"
455 " @public\n"
456 " int field1;\n"
457 " @protected\n"
458 " int field2;\n"
459 " @private\n"
460 " int field3;\n"
461 " @package\n"
462 " int field4;\n"
463 "}\n"
464 "+ (id)init {\n}\n"
465 "@end");
466}
467
468TEST_F(FormatTestObjC, FormatObjCProtocol) {
469 verifyFormat("@protocol Foo\n"
470 "@property(weak) id delegate;\n"
471 "- (NSUInteger)numberOfThings;\n"
472 "@end");
473
474 verifyFormat("@protocol MyProtocol <NSObject>\n"
475 "- (NSUInteger)numberOfThings;\n"
476 "@end");
477
478 verifyFormat("@protocol Foo;\n"
479 "@protocol Bar;\n");
480
481 verifyFormat("@protocol Foo\n"
482 "@end\n"
483 "@protocol Bar\n"
484 "@end");
485
Nico Weberc068ff72018-01-23 17:10:25 +0000486 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n"
487 "@property(assign, readwrite) NSInteger bar;\n"
488 "@end");
489
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000490 verifyFormat("@protocol myProtocol\n"
491 "- (void)mandatoryWithInt:(int)i;\n"
492 "@optional\n"
493 "- (void)optional;\n"
494 "@required\n"
495 "- (void)required;\n"
496 "@optional\n"
497 "@property(assign) int madProp;\n"
498 "@end\n");
499
500 verifyFormat("@property(nonatomic, assign, readonly)\n"
501 " int *looooooooooooooooooooooooooooongNumber;\n"
502 "@property(nonatomic, assign, readonly)\n"
503 " NSString *looooooooooooooooooooooooooooongName;");
504
505 verifyFormat("@implementation PR18406\n"
506 "}\n"
507 "@end");
508
509 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000510 verifyFormat("@protocol MyProtocol <NSObject>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000511 "- (NSUInteger)numberOfThings;\n"
512 "@end");
513}
514
515TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
516 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
517 " rect:(NSRect)theRect\n"
518 " interval:(float)theInterval {\n"
519 "}");
520 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
521 " longKeyword:(NSRect)theRect\n"
522 " longerKeyword:(float)theInterval\n"
523 " error:(NSError **)theError {\n"
524 "}");
525 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
526 " longKeyword:(NSRect)theRect\n"
527 " evenLongerKeyword:(float)theInterval\n"
528 " error:(NSError **)theError {\n"
529 "}");
Ben Hamilton5f911342018-03-22 03:23:53 +0000530 verifyFormat("+ (instancetype)new;\n");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000531 Style.ColumnLimit = 60;
532 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
533 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
534 " NS_DESIGNATED_INITIALIZER;");
535 verifyFormat("- (void)drawRectOn:(id)surface\n"
536 " ofSize:(size_t)height\n"
537 " :(size_t)width;");
Ben Hamiltonf90ad9c2018-04-05 15:26:23 +0000538 Style.ColumnLimit = 40;
Ben Hamilton416348e2018-04-12 15:11:48 +0000539 // Make sure selectors with 0, 1, or more arguments are indented when wrapped.
Ben Hamilton4dc1cdc2018-03-30 15:38:45 +0000540 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
541 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n");
542 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
543 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
544 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
545 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
546 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
547 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
548 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
549 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
550 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
551 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
552 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
553
Ben Hamilton416348e2018-04-12 15:11:48 +0000554 // Continuation indent width should win over aligning colons if the function
555 // name is long.
556 Style = getGoogleStyle(FormatStyle::LK_ObjC);
557 Style.ColumnLimit = 40;
558 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
559 " dontAlignNamef:(NSRect)theRect {\n"
560 "}");
561
562 // Make sure we don't break aligning for short parameter names.
563 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
564 " aShortf:(NSRect)theRect {\n"
565 "}");
566
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000567 // Format pairs correctly.
568 Style.ColumnLimit = 80;
569 verifyFormat("- (void)drawRectOn:(id)surface\n"
570 " ofSize:(aaaaaaaa)height\n"
571 " :(size_t)width\n"
572 " atOrigin:(size_t)x\n"
573 " :(size_t)y\n"
574 " aaaaa:(a)yyy\n"
575 " bbb:(d)cccc;");
576 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
577}
578
579TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
580 verifyFormat("[foo bar:baz];");
581 verifyFormat("return [foo bar:baz];");
582 verifyFormat("return (a)[foo bar:baz];");
583 verifyFormat("f([foo bar:baz]);");
584 verifyFormat("f(2, [foo bar:baz]);");
585 verifyFormat("f(2, a ? b : c);");
586 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
587
588 // Unary operators.
589 verifyFormat("int a = +[foo bar:baz];");
590 verifyFormat("int a = -[foo bar:baz];");
591 verifyFormat("int a = ![foo bar:baz];");
592 verifyFormat("int a = ~[foo bar:baz];");
593 verifyFormat("int a = ++[foo bar:baz];");
594 verifyFormat("int a = --[foo bar:baz];");
595 verifyFormat("int a = sizeof [foo bar:baz];");
596 verifyFormat("int a = alignof [foo bar:baz];");
597 verifyFormat("int a = &[foo bar:baz];");
598 verifyFormat("int a = *[foo bar:baz];");
599 // FIXME: Make casts work, without breaking f()[4].
600 // verifyFormat("int a = (int)[foo bar:baz];");
601 // verifyFormat("return (int)[foo bar:baz];");
602 // verifyFormat("(void)[foo bar:baz];");
603 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
604
605 // Binary operators.
606 verifyFormat("[foo bar:baz], [foo bar:baz];");
607 verifyFormat("[foo bar:baz] = [foo bar:baz];");
608 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
609 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
610 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
611 verifyFormat("[foo bar:baz] += [foo bar:baz];");
612 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
613 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
614 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
615 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
616 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
617 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
618 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
619 verifyFormat("[foo bar:baz] || [foo bar:baz];");
620 verifyFormat("[foo bar:baz] && [foo bar:baz];");
621 verifyFormat("[foo bar:baz] | [foo bar:baz];");
622 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
623 verifyFormat("[foo bar:baz] & [foo bar:baz];");
624 verifyFormat("[foo bar:baz] == [foo bar:baz];");
625 verifyFormat("[foo bar:baz] != [foo bar:baz];");
626 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
627 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
628 verifyFormat("[foo bar:baz] > [foo bar:baz];");
629 verifyFormat("[foo bar:baz] < [foo bar:baz];");
630 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
631 verifyFormat("[foo bar:baz] << [foo bar:baz];");
632 verifyFormat("[foo bar:baz] - [foo bar:baz];");
633 verifyFormat("[foo bar:baz] + [foo bar:baz];");
634 verifyFormat("[foo bar:baz] * [foo bar:baz];");
635 verifyFormat("[foo bar:baz] / [foo bar:baz];");
636 verifyFormat("[foo bar:baz] % [foo bar:baz];");
637 // Whew!
638
639 verifyFormat("return in[42];");
640 verifyFormat("for (auto v : in[1]) {\n}");
641 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
642 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
643 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
644 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
645 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
646 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
647 "}");
648 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
Ben Hamiltonad991862018-03-20 14:53:25 +0000649 verifyFormat("[self aaaaa:MACRO(a, b:c:, d:e:)];");
650 verifyFormat("[self aaaaa:MACRO(a, b:c:d:, e:f:g:)];");
651 verifyFormat("int XYMyFoo(int a, int b) NS_SWIFT_NAME(foo(self:scale:));");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000652 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
653 verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
654
655 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
656 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
657 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
658 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
659 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
660 verifyFormat("[button setAction:@selector(zoomOut:)];");
661 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
662
663 verifyFormat("arr[[self indexForFoo:a]];");
664 verifyFormat("throw [self errorFor:a];");
665 verifyFormat("@throw [self errorFor:a];");
666
667 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
668 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
669 verifyFormat("4 > 4 ? (id)a : (id)baz;");
670
671 // This tests that the formatter doesn't break after "backing" but before ":",
672 // which would be at 80 columns.
673 verifyFormat(
674 "void f() {\n"
675 " if ((self = [super initWithContentRect:contentRect\n"
676 " styleMask:styleMask ?: otherMask\n"
677 " backing:NSBackingStoreBuffered\n"
678 " defer:YES]))");
679
680 verifyFormat(
681 "[foo checkThatBreakingAfterColonWorksOk:\n"
682 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
683
684 verifyFormat("[myObj short:arg1 // Force line break\n"
685 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
686 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
687 " error:arg4];");
688 verifyFormat(
689 "void f() {\n"
690 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
691 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
692 " pos.width(), pos.height())\n"
693 " styleMask:NSBorderlessWindowMask\n"
694 " backing:NSBackingStoreBuffered\n"
695 " defer:NO]);\n"
696 "}");
697 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
698 " with:contentsNativeView];");
699
700 verifyFormat(
701 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
702 " owner:nillllll];");
703
704 verifyFormat(
705 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
706 " forType:kBookmarkButtonDragType];");
707
708 verifyFormat("[defaultCenter addObserver:self\n"
709 " selector:@selector(willEnterFullscreen)\n"
710 " name:kWillEnterFullscreenNotification\n"
711 " object:nil];");
712 verifyFormat("[image_rep drawInRect:drawRect\n"
713 " fromRect:NSZeroRect\n"
714 " operation:NSCompositeCopy\n"
715 " fraction:1.0\n"
716 " respectFlipped:NO\n"
717 " hints:nil];");
718 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
719 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
720 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
721 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
722 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
723 " aaaaaaaaaaaaaaaaaaaaaa];");
724
725 verifyFormat(
726 "scoped_nsobject<NSTextField> message(\n"
727 " // The frame will be fixed up when |-setMessageText:| is called.\n"
728 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
729 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
730 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
731 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
732 " aaaa:bbb];");
733 verifyFormat("[self param:function( //\n"
734 " parameter)]");
735 verifyFormat(
736 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
737 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
738 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
739
740 // Variadic parameters.
741 verifyFormat(
742 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
743 verifyFormat(
744 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
745 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
746 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
747 verifyFormat("[self // break\n"
748 " a:a\n"
749 " aaa:aaa];");
750 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
751 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
752
753 // Formats pair-parameters.
754 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
755 verifyFormat("[I drawRectOn:surface //\n"
Francois Ferrand38d80132018-02-09 15:41:56 +0000756 " ofSize:aa:bbb\n"
757 " atOrigin:cc:dd];");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000758
Jacek Olesiakfb7f5c02018-02-07 10:35:08 +0000759 // Inline block as a first argument.
760 verifyFormat("[object justBlock:^{\n"
761 " a = 42;\n"
762 "}];");
763 verifyFormat("[object\n"
764 " justBlock:^{\n"
765 " a = 42;\n"
766 " }\n"
767 " notBlock:42\n"
768 " a:42];");
769 verifyFormat("[object\n"
770 " firstBlock:^{\n"
771 " a = 42;\n"
772 " }\n"
773 " blockWithLongerName:^{\n"
774 " a = 42;\n"
775 " }];");
776 verifyFormat("[object\n"
777 " blockWithLongerName:^{\n"
778 " a = 42;\n"
779 " }\n"
780 " secondBlock:^{\n"
781 " a = 42;\n"
782 " }];");
783 verifyFormat("[object\n"
784 " firstBlock:^{\n"
785 " a = 42;\n"
786 " }\n"
787 " notBlock:42\n"
788 " secondBlock:^{\n"
789 " a = 42;\n"
790 " }];");
791
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000792 Style.ColumnLimit = 70;
793 verifyFormat(
794 "void f() {\n"
795 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
796 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
797 " pos.width(), pos.height())\n"
798 " syeMask:NSBorderlessWindowMask\n"
799 " bking:NSBackingStoreBuffered\n"
800 " der:NO]);\n"
801 "}");
802
803 Style.ColumnLimit = 60;
804 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
805 " .aaaaaaaa];"); // FIXME: Indentation seems off.
806 // FIXME: This violates the column limit.
807 verifyFormat(
808 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
809 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
810 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
811
812 Style = getChromiumStyle(FormatStyle::LK_ObjC);
813 Style.ColumnLimit = 80;
814 verifyFormat(
815 "void f() {\n"
816 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
817 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
818 " pos.width(), pos.height())\n"
819 " styleMask:NSBorderlessWindowMask\n"
820 " backing:NSBackingStoreBuffered\n"
821 " defer:NO]);\n"
822 "}");
Francois Ferrand38d80132018-02-09 15:41:56 +0000823
824 // Respect continuation indent and colon alignment (e.g. when object name is
825 // short, and first selector is the longest one)
826 Style = getLLVMStyle();
827 Style.Language = FormatStyle::LK_ObjC;
828 Style.ContinuationIndentWidth = 8;
829 verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
830 " withObject:nil\n"
831 " waitUntilDone:false];");
832 verifyFormat("[self performSelector:@selector(loadAccessories)\n"
833 " withObjectOnMainThread:nil\n"
834 " waitUntilDone:false];");
835 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
836 " performSelectorOnMainThread:@selector(loadAccessories)\n"
837 " withObject:nil\n"
838 " waitUntilDone:false];");
839 verifyFormat("[self // force wrapping\n"
840 " performSelectorOnMainThread:@selector(loadAccessories)\n"
841 " withObject:nil\n"
842 " waitUntilDone:false];");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000843}
844
845TEST_F(FormatTestObjC, ObjCAt) {
846 verifyFormat("@autoreleasepool");
847 verifyFormat("@catch");
848 verifyFormat("@class");
849 verifyFormat("@compatibility_alias");
850 verifyFormat("@defs");
851 verifyFormat("@dynamic");
852 verifyFormat("@encode");
853 verifyFormat("@end");
854 verifyFormat("@finally");
855 verifyFormat("@implementation");
856 verifyFormat("@import");
857 verifyFormat("@interface");
858 verifyFormat("@optional");
859 verifyFormat("@package");
860 verifyFormat("@private");
861 verifyFormat("@property");
862 verifyFormat("@protected");
863 verifyFormat("@protocol");
864 verifyFormat("@public");
865 verifyFormat("@required");
866 verifyFormat("@selector");
867 verifyFormat("@synchronized");
868 verifyFormat("@synthesize");
869 verifyFormat("@throw");
870 verifyFormat("@try");
871
872 EXPECT_EQ("@interface", format("@ interface"));
873
874 // The precise formatting of this doesn't matter, nobody writes code like
875 // this.
876 verifyFormat("@ /*foo*/ interface");
877}
878
Ben Hamilton788a2222018-03-12 15:42:40 +0000879TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) {
880 verifyFormat("void DoStuffWithBlockType(int (^)(char));");
881 verifyFormat("int (^foo)(char, float);");
882 verifyFormat("int (^foo[10])(char, float);");
883 verifyFormat("int (^foo[kNumEntries])(char, float);");
884 verifyFormat("int (^foo[kNumEntries + 10])(char, float);");
885 verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);");
886}
887
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000888TEST_F(FormatTestObjC, ObjCSnippets) {
889 verifyFormat("@autoreleasepool {\n"
890 " foo();\n"
891 "}");
892 verifyFormat("@class Foo, Bar;");
893 verifyFormat("@compatibility_alias AliasName ExistingClass;");
894 verifyFormat("@dynamic textColor;");
895 verifyFormat("char *buf1 = @encode(int *);");
896 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
897 verifyFormat("char *buf1 = @encode(int **);");
898 verifyFormat("Protocol *proto = @protocol(p1);");
899 verifyFormat("SEL s = @selector(foo:);");
900 verifyFormat("@synchronized(self) {\n"
901 " f();\n"
902 "}");
903
904 verifyFormat("@import foo.bar;\n"
905 "@import baz;");
906
907 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
908
909 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
910 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
911
912 Style = getMozillaStyle();
913 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
914 verifyFormat("@property BOOL editable;");
915
916 Style = getWebKitStyle();
917 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
918 verifyFormat("@property BOOL editable;");
919
920 Style = getGoogleStyle(FormatStyle::LK_ObjC);
921 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
922 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
923}
924
925TEST_F(FormatTestObjC, ObjCForIn) {
926 verifyFormat("- (void)test {\n"
927 " for (NSString *n in arrayOfStrings) {\n"
928 " foo(n);\n"
929 " }\n"
930 "}");
931 verifyFormat("- (void)test {\n"
932 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
933 " foo(n);\n"
934 " }\n"
935 "}");
Ben Hamilton1d6c6ee2018-03-06 17:21:42 +0000936 verifyFormat("for (Foo *x in bar) {\n}");
937 verifyFormat("for (Foo *x in [bar baz]) {\n}");
938 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
939 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
940 verifyFormat("for (Foo *x in [bar baz:^{\n"
941 " [uh oh];\n"
942 " }]) {\n}");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000943}
944
Ben Hamilton5f911342018-03-22 03:23:53 +0000945TEST_F(FormatTestObjC, ObjCNew) {
946 verifyFormat("+ (instancetype)new {\n"
947 " return nil;\n"
948 "}\n");
949 verifyFormat("+ (instancetype)myNew {\n"
950 " return [self new];\n"
951 "}\n");
952 verifyFormat("SEL NewSelector(void) { return @selector(new); }\n");
953 verifyFormat("SEL MacroSelector(void) { return MACRO(new); }\n");
954}
955
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000956TEST_F(FormatTestObjC, ObjCLiterals) {
957 verifyFormat("@\"String\"");
958 verifyFormat("@1");
959 verifyFormat("@+4.8");
960 verifyFormat("@-4");
961 verifyFormat("@1LL");
962 verifyFormat("@.5");
963 verifyFormat("@'c'");
964 verifyFormat("@true");
965
966 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
967 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
968 verifyFormat("NSNumber *favoriteColor = @(Green);");
969 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
970
971 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
972}
973
974TEST_F(FormatTestObjC, ObjCDictLiterals) {
975 verifyFormat("@{");
976 verifyFormat("@{}");
977 verifyFormat("@{@\"one\" : @1}");
978 verifyFormat("return @{@\"one\" : @1;");
979 verifyFormat("@{@\"one\" : @1}");
980
981 verifyFormat("@{@\"one\" : @{@2 : @1}}");
982 verifyFormat("@{\n"
983 " @\"one\" : @{@2 : @1},\n"
984 "}");
985
986 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
987 verifyIncompleteFormat("[self setDict:@{}");
988 verifyIncompleteFormat("[self setDict:@{@1 : @2}");
989 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
990 verifyFormat(
991 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
992 verifyFormat(
993 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
994
995 verifyFormat("NSDictionary *d = @{\n"
996 " @\"nam\" : NSUserNam(),\n"
997 " @\"dte\" : [NSDate date],\n"
998 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
999 "};");
1000 verifyFormat(
1001 "@{\n"
1002 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1003 "regularFont,\n"
1004 "};");
1005 verifyFormat(
1006 "@{\n"
1007 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
1008 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
1009 "};");
1010
1011 // We should try to be robust in case someone forgets the "@".
1012 verifyFormat("NSDictionary *d = {\n"
1013 " @\"nam\" : NSUserNam(),\n"
1014 " @\"dte\" : [NSDate date],\n"
1015 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
1016 "};");
1017 verifyFormat("NSMutableDictionary *dictionary =\n"
1018 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
1019 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
1020 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
1021 " cccccccccccccccc : ccccccccccccccc\n"
1022 " }];");
1023
1024 // Ensure that casts before the key are kept on the same line as the key.
1025 verifyFormat(
1026 "NSDictionary *d = @{\n"
1027 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1028 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
1029 "};");
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001030 Style.ColumnLimit = 40;
1031 verifyFormat("int Foo() {\n"
1032 " a12345 = @{a12345 : a12345};\n"
1033 "}");
1034 verifyFormat("int Foo() {\n"
Ben Hamilton1915d2a2018-04-03 14:07:09 +00001035 " a12345 = @{a12345 : @(a12345)};\n"
1036 "}");
1037 verifyFormat("int Foo() {\n"
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001038 " a12345 = @{(Foo *)a12345 : @(a12345)};\n"
1039 "}");
Ben Hamilton1915d2a2018-04-03 14:07:09 +00001040 verifyFormat("int Foo() {\n"
1041 " a12345 = @{@(a12345) : a12345};\n"
1042 "}");
1043 verifyFormat("int Foo() {\n"
1044 " a12345 = @{@(a12345) : @YES};\n"
1045 "}");
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001046 Style.SpacesInContainerLiterals = false;
1047 verifyFormat("int Foo() {\n"
1048 " b12345 = @{b12345: b12345};\n"
1049 "}");
1050 verifyFormat("int Foo() {\n"
1051 " b12345 = @{(Foo *)b12345: @(b12345)};\n"
1052 "}");
1053 Style.SpacesInContainerLiterals = true;
Daniel Jasper03a04fe2016-12-12 12:42:29 +00001054
1055 Style = getGoogleStyle(FormatStyle::LK_ObjC);
1056 verifyFormat(
1057 "@{\n"
1058 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1059 "regularFont,\n"
1060 "};");
1061}
1062
1063TEST_F(FormatTestObjC, ObjCArrayLiterals) {
1064 verifyIncompleteFormat("@[");
1065 verifyFormat("@[]");
1066 verifyFormat(
1067 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
1068 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
1069 verifyFormat("NSArray *array = @[ [foo description] ];");
1070
1071 verifyFormat(
1072 "NSArray *some_variable = @[\n"
1073 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1074 " @\"aaaaaaaaaaaaaaaaa\",\n"
1075 " @\"aaaaaaaaaaaaaaaaa\",\n"
1076 " @\"aaaaaaaaaaaaaaaaa\",\n"
1077 "];");
1078 verifyFormat(
1079 "NSArray *some_variable = @[\n"
1080 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1081 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
1082 "];");
1083 verifyFormat("NSArray *some_variable = @[\n"
1084 " @\"aaaaaaaaaaaaaaaaa\",\n"
1085 " @\"aaaaaaaaaaaaaaaaa\",\n"
1086 " @\"aaaaaaaaaaaaaaaaa\",\n"
1087 " @\"aaaaaaaaaaaaaaaaa\",\n"
1088 "];");
1089 verifyFormat("NSArray *array = @[\n"
1090 " @\"a\",\n"
1091 " @\"a\",\n" // Trailing comma -> one per line.
1092 "];");
1093
1094 // We should try to be robust in case someone forgets the "@".
1095 verifyFormat("NSArray *some_variable = [\n"
1096 " @\"aaaaaaaaaaaaaaaaa\",\n"
1097 " @\"aaaaaaaaaaaaaaaaa\",\n"
1098 " @\"aaaaaaaaaaaaaaaaa\",\n"
1099 " @\"aaaaaaaaaaaaaaaaa\",\n"
1100 "];");
1101 verifyFormat(
1102 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
1103 " index:(NSUInteger)index\n"
1104 " nonDigitAttributes:\n"
1105 " (NSDictionary *)noDigitAttributes;");
1106 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
1107 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
1108 "]];");
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001109 Style.ColumnLimit = 40;
1110 verifyFormat("int Foo() {\n"
1111 " a12345 = @[ a12345, a12345 ];\n"
1112 "}");
1113 verifyFormat("int Foo() {\n"
1114 " a123 = @[ (Foo *)a12345, @(a12345) ];\n"
1115 "}");
1116 Style.SpacesInContainerLiterals = false;
1117 verifyFormat("int Foo() {\n"
1118 " b12345 = @[b12345, b12345];\n"
1119 "}");
1120 verifyFormat("int Foo() {\n"
1121 " b12345 = @[(Foo *)b12345, @(b12345)];\n"
1122 "}");
1123 Style.SpacesInContainerLiterals = true;
Ben Hamilton09051f22018-02-08 16:07:25 +00001124 Style.ColumnLimit = 20;
1125 // We can't break string literals inside NSArray literals
1126 // (that raises -Wobjc-string-concatenation).
1127 verifyFormat("NSArray *foo = @[\n"
1128 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1129 "];\n");
Daniel Jasper03a04fe2016-12-12 12:42:29 +00001130}
1131} // end namespace
1132} // end namespace format
1133} // end namespace clang