blob: 5ea05f45928993d7b009d4fbd580e824ba683d28 [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
302 verifyFormat("@interface Foo (HackStuff) {\n"
303 " int _i;\n"
304 "}\n"
305 "+ (id)init;\n"
306 "@end");
307
308 verifyFormat("@interface Foo () {\n"
309 " int _i;\n"
310 "}\n"
311 "+ (id)init;\n"
312 "@end");
313
314 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
315 " int _i;\n"
316 "}\n"
317 "+ (id)init;\n"
318 "@end");
319
Ben Hamilton5dd40182018-01-29 20:01:49 +0000320 Style.ColumnLimit = 40;
321 verifyFormat("@interface ccccccccccccc () <\n"
322 " ccccccccccccc, ccccccccccccc,\n"
323 " ccccccccccccc, ccccccccccccc> {\n"
324 "}");
Ben Hamilton4dc658c2018-02-02 20:15:14 +0000325 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
Ben Hamilton5dd40182018-01-29 20:01:49 +0000326 verifyFormat("@interface ddddddddddddd () <\n"
327 " ddddddddddddd,\n"
328 " ddddddddddddd,\n"
329 " ddddddddddddd,\n"
330 " ddddddddddddd> {\n"
331 "}");
332
Ben Hamilton4dc658c2018-02-02 20:15:14 +0000333 Style.BinPackParameters = false;
334 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
335 verifyFormat("@interface eeeeeeeeeeeee () <\n"
336 " eeeeeeeeeeeee,\n"
337 " eeeeeeeeeeeee,\n"
338 " eeeeeeeeeeeee,\n"
339 " eeeeeeeeeeeee> {\n"
340 "}");
341 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Always;
342 verifyFormat("@interface fffffffffffff () <\n"
343 " fffffffffffff, fffffffffffff,\n"
344 " fffffffffffff, fffffffffffff> {\n"
345 "}");
346
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000347 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000348 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000349 " @public\n"
350 " int field1;\n"
351 " @protected\n"
352 " int field2;\n"
353 " @private\n"
354 " int field3;\n"
355 " @package\n"
356 " int field4;\n"
357 "}\n"
358 "+ (id)init;\n"
359 "@end");
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000360 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000361 "+ (id)init;\n"
362 "@end");
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000363 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000364 "+ (id)init;\n"
365 "@end");
Ben Hamilton3a47fdd2018-02-08 01:49:10 +0000366 Style.ColumnLimit = 40;
367 // BinPackParameters should be true by default.
368 verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n"
369 " int eeeee, int eeeee);\n");
370 // ObjCBinPackProtocolList should be BPS_Never by default.
371 verifyFormat("@interface fffffffffffff () <\n"
372 " fffffffffffff,\n"
373 " fffffffffffff,\n"
374 " fffffffffffff,\n"
375 " fffffffffffff> {\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000376 "}");
377}
378
379TEST_F(FormatTestObjC, FormatObjCImplementation) {
380 verifyFormat("@implementation Foo : NSObject {\n"
381 "@public\n"
382 " int field1;\n"
383 "@protected\n"
384 " int field2;\n"
385 "@private\n"
386 " int field3;\n"
387 "@package\n"
388 " int field4;\n"
389 "}\n"
390 "+ (id)init {\n}\n"
391 "@end");
392
393 verifyFormat("@implementation Foo\n"
394 "+ (id)init {\n"
395 " if (true)\n"
396 " return nil;\n"
397 "}\n"
398 "// Look, a comment!\n"
399 "- (int)answerWith:(int)i {\n"
400 " return i;\n"
401 "}\n"
402 "+ (int)answerWith:(int)i {\n"
403 " return i;\n"
404 "}\n"
405 "@end");
406
407 verifyFormat("@implementation Foo\n"
408 "@end\n"
409 "@implementation Bar\n"
410 "@end");
411
412 EXPECT_EQ("@implementation Foo : Bar\n"
413 "+ (id)init {\n}\n"
414 "- (void)foo {\n}\n"
415 "@end",
416 format("@implementation Foo : Bar\n"
417 "+(id)init{}\n"
418 "-(void)foo{}\n"
419 "@end"));
420
421 verifyFormat("@implementation Foo {\n"
422 " int _i;\n"
423 "}\n"
424 "+ (id)init {\n}\n"
425 "@end");
426
427 verifyFormat("@implementation Foo : Bar {\n"
428 " int _i;\n"
429 "}\n"
430 "+ (id)init {\n}\n"
431 "@end");
432
433 verifyFormat("@implementation Foo (HackStuff)\n"
434 "+ (id)init {\n}\n"
435 "@end");
436 verifyFormat("@implementation ObjcClass\n"
437 "- (void)method;\n"
438 "{}\n"
439 "@end");
440
441 Style = getGoogleStyle(FormatStyle::LK_ObjC);
442 verifyFormat("@implementation Foo : NSObject {\n"
443 " @public\n"
444 " int field1;\n"
445 " @protected\n"
446 " int field2;\n"
447 " @private\n"
448 " int field3;\n"
449 " @package\n"
450 " int field4;\n"
451 "}\n"
452 "+ (id)init {\n}\n"
453 "@end");
454}
455
456TEST_F(FormatTestObjC, FormatObjCProtocol) {
457 verifyFormat("@protocol Foo\n"
458 "@property(weak) id delegate;\n"
459 "- (NSUInteger)numberOfThings;\n"
460 "@end");
461
462 verifyFormat("@protocol MyProtocol <NSObject>\n"
463 "- (NSUInteger)numberOfThings;\n"
464 "@end");
465
466 verifyFormat("@protocol Foo;\n"
467 "@protocol Bar;\n");
468
469 verifyFormat("@protocol Foo\n"
470 "@end\n"
471 "@protocol Bar\n"
472 "@end");
473
Nico Weberc068ff72018-01-23 17:10:25 +0000474 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n"
475 "@property(assign, readwrite) NSInteger bar;\n"
476 "@end");
477
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000478 verifyFormat("@protocol myProtocol\n"
479 "- (void)mandatoryWithInt:(int)i;\n"
480 "@optional\n"
481 "- (void)optional;\n"
482 "@required\n"
483 "- (void)required;\n"
484 "@optional\n"
485 "@property(assign) int madProp;\n"
486 "@end\n");
487
488 verifyFormat("@property(nonatomic, assign, readonly)\n"
489 " int *looooooooooooooooooooooooooooongNumber;\n"
490 "@property(nonatomic, assign, readonly)\n"
491 " NSString *looooooooooooooooooooooooooooongName;");
492
493 verifyFormat("@implementation PR18406\n"
494 "}\n"
495 "@end");
496
497 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000498 verifyFormat("@protocol MyProtocol <NSObject>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000499 "- (NSUInteger)numberOfThings;\n"
500 "@end");
501}
502
503TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
504 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
505 " rect:(NSRect)theRect\n"
506 " interval:(float)theInterval {\n"
507 "}");
508 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
509 " longKeyword:(NSRect)theRect\n"
510 " longerKeyword:(float)theInterval\n"
511 " error:(NSError **)theError {\n"
512 "}");
513 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
514 " longKeyword:(NSRect)theRect\n"
515 " evenLongerKeyword:(float)theInterval\n"
516 " error:(NSError **)theError {\n"
517 "}");
Ben Hamilton5f911342018-03-22 03:23:53 +0000518 verifyFormat("+ (instancetype)new;\n");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000519 Style.ColumnLimit = 60;
520 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
521 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
522 " NS_DESIGNATED_INITIALIZER;");
523 verifyFormat("- (void)drawRectOn:(id)surface\n"
524 " ofSize:(size_t)height\n"
525 " :(size_t)width;");
Ben Hamiltonf90ad9c2018-04-05 15:26:23 +0000526 Style.ColumnLimit = 40;
527 // Make sure selectors with 0, 1, or more arguments are not indented
528 // when IndentWrappedFunctionNames is false.
529 Style.IndentWrappedFunctionNames = false;
530 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
531 "aaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n");
532 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
533 "aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
534 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
535 "aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
536 "aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
537 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
538 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
539 "aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
540 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
541 "aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
542 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000543
544 // Continuation indent width should win over aligning colons if the function
545 // name is long.
Ben Hamilton5b0c3ad2017-12-14 21:44:11 +0000546 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000547 Style.ColumnLimit = 40;
548 Style.IndentWrappedFunctionNames = true;
549 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
550 " dontAlignNamef:(NSRect)theRect {\n"
551 "}");
552
553 // Make sure we don't break aligning for short parameter names.
554 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
555 " aShortf:(NSRect)theRect {\n"
556 "}");
Ben Hamilton5b0c3ad2017-12-14 21:44:11 +0000557
Ben Hamilton4dc1cdc2018-03-30 15:38:45 +0000558 // Make sure selectors with 0, 1, or more arguments are indented
559 // when IndentWrappedFunctionNames is true.
560 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
561 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n");
562 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
563 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
564 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
565 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
566 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
567 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
568 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
569 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
570 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
571 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
572 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
573
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000574 // Format pairs correctly.
575 Style.ColumnLimit = 80;
576 verifyFormat("- (void)drawRectOn:(id)surface\n"
577 " ofSize:(aaaaaaaa)height\n"
578 " :(size_t)width\n"
579 " atOrigin:(size_t)x\n"
580 " :(size_t)y\n"
581 " aaaaa:(a)yyy\n"
582 " bbb:(d)cccc;");
583 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
584}
585
586TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
587 verifyFormat("[foo bar:baz];");
588 verifyFormat("return [foo bar:baz];");
589 verifyFormat("return (a)[foo bar:baz];");
590 verifyFormat("f([foo bar:baz]);");
591 verifyFormat("f(2, [foo bar:baz]);");
592 verifyFormat("f(2, a ? b : c);");
593 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
594
595 // Unary operators.
596 verifyFormat("int a = +[foo bar:baz];");
597 verifyFormat("int a = -[foo bar:baz];");
598 verifyFormat("int a = ![foo bar:baz];");
599 verifyFormat("int a = ~[foo bar:baz];");
600 verifyFormat("int a = ++[foo bar:baz];");
601 verifyFormat("int a = --[foo bar:baz];");
602 verifyFormat("int a = sizeof [foo bar:baz];");
603 verifyFormat("int a = alignof [foo bar:baz];");
604 verifyFormat("int a = &[foo bar:baz];");
605 verifyFormat("int a = *[foo bar:baz];");
606 // FIXME: Make casts work, without breaking f()[4].
607 // verifyFormat("int a = (int)[foo bar:baz];");
608 // verifyFormat("return (int)[foo bar:baz];");
609 // verifyFormat("(void)[foo bar:baz];");
610 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
611
612 // Binary operators.
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];");
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] : [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 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
638 verifyFormat("[foo bar:baz] << [foo bar:baz];");
639 verifyFormat("[foo bar:baz] - [foo bar:baz];");
640 verifyFormat("[foo bar:baz] + [foo bar:baz];");
641 verifyFormat("[foo bar:baz] * [foo bar:baz];");
642 verifyFormat("[foo bar:baz] / [foo bar:baz];");
643 verifyFormat("[foo bar:baz] % [foo bar:baz];");
644 // Whew!
645
646 verifyFormat("return in[42];");
647 verifyFormat("for (auto v : in[1]) {\n}");
648 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
649 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
650 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
651 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
652 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
653 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
654 "}");
655 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
Ben Hamiltonad991862018-03-20 14:53:25 +0000656 verifyFormat("[self aaaaa:MACRO(a, b:c:, d:e:)];");
657 verifyFormat("[self aaaaa:MACRO(a, b:c:d:, e:f:g:)];");
658 verifyFormat("int XYMyFoo(int a, int b) NS_SWIFT_NAME(foo(self:scale:));");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000659 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
660 verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
661
662 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
663 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
664 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
665 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
666 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
667 verifyFormat("[button setAction:@selector(zoomOut:)];");
668 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
669
670 verifyFormat("arr[[self indexForFoo:a]];");
671 verifyFormat("throw [self errorFor:a];");
672 verifyFormat("@throw [self errorFor:a];");
673
674 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
675 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
676 verifyFormat("4 > 4 ? (id)a : (id)baz;");
677
678 // This tests that the formatter doesn't break after "backing" but before ":",
679 // which would be at 80 columns.
680 verifyFormat(
681 "void f() {\n"
682 " if ((self = [super initWithContentRect:contentRect\n"
683 " styleMask:styleMask ?: otherMask\n"
684 " backing:NSBackingStoreBuffered\n"
685 " defer:YES]))");
686
687 verifyFormat(
688 "[foo checkThatBreakingAfterColonWorksOk:\n"
689 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
690
691 verifyFormat("[myObj short:arg1 // Force line break\n"
692 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
693 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
694 " error:arg4];");
695 verifyFormat(
696 "void f() {\n"
697 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
698 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
699 " pos.width(), pos.height())\n"
700 " styleMask:NSBorderlessWindowMask\n"
701 " backing:NSBackingStoreBuffered\n"
702 " defer:NO]);\n"
703 "}");
704 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
705 " with:contentsNativeView];");
706
707 verifyFormat(
708 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
709 " owner:nillllll];");
710
711 verifyFormat(
712 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
713 " forType:kBookmarkButtonDragType];");
714
715 verifyFormat("[defaultCenter addObserver:self\n"
716 " selector:@selector(willEnterFullscreen)\n"
717 " name:kWillEnterFullscreenNotification\n"
718 " object:nil];");
719 verifyFormat("[image_rep drawInRect:drawRect\n"
720 " fromRect:NSZeroRect\n"
721 " operation:NSCompositeCopy\n"
722 " fraction:1.0\n"
723 " respectFlipped:NO\n"
724 " hints:nil];");
725 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
726 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
727 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
728 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
729 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
730 " aaaaaaaaaaaaaaaaaaaaaa];");
731
732 verifyFormat(
733 "scoped_nsobject<NSTextField> message(\n"
734 " // The frame will be fixed up when |-setMessageText:| is called.\n"
735 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
736 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
737 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
738 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
739 " aaaa:bbb];");
740 verifyFormat("[self param:function( //\n"
741 " parameter)]");
742 verifyFormat(
743 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
744 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
745 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
746
747 // Variadic parameters.
748 verifyFormat(
749 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
750 verifyFormat(
751 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
752 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
753 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
754 verifyFormat("[self // break\n"
755 " a:a\n"
756 " aaa:aaa];");
757 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
758 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
759
760 // Formats pair-parameters.
761 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
762 verifyFormat("[I drawRectOn:surface //\n"
Francois Ferrand38d80132018-02-09 15:41:56 +0000763 " ofSize:aa:bbb\n"
764 " atOrigin:cc:dd];");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000765
Jacek Olesiakfb7f5c02018-02-07 10:35:08 +0000766 // Inline block as a first argument.
767 verifyFormat("[object justBlock:^{\n"
768 " a = 42;\n"
769 "}];");
770 verifyFormat("[object\n"
771 " justBlock:^{\n"
772 " a = 42;\n"
773 " }\n"
774 " notBlock:42\n"
775 " a:42];");
776 verifyFormat("[object\n"
777 " firstBlock:^{\n"
778 " a = 42;\n"
779 " }\n"
780 " blockWithLongerName:^{\n"
781 " a = 42;\n"
782 " }];");
783 verifyFormat("[object\n"
784 " blockWithLongerName:^{\n"
785 " a = 42;\n"
786 " }\n"
787 " secondBlock:^{\n"
788 " a = 42;\n"
789 " }];");
790 verifyFormat("[object\n"
791 " firstBlock:^{\n"
792 " a = 42;\n"
793 " }\n"
794 " notBlock:42\n"
795 " secondBlock:^{\n"
796 " a = 42;\n"
797 " }];");
798
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000799 Style.ColumnLimit = 70;
800 verifyFormat(
801 "void f() {\n"
802 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
803 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
804 " pos.width(), pos.height())\n"
805 " syeMask:NSBorderlessWindowMask\n"
806 " bking:NSBackingStoreBuffered\n"
807 " der:NO]);\n"
808 "}");
809
810 Style.ColumnLimit = 60;
811 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
812 " .aaaaaaaa];"); // FIXME: Indentation seems off.
813 // FIXME: This violates the column limit.
814 verifyFormat(
815 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
816 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
817 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
818
819 Style = getChromiumStyle(FormatStyle::LK_ObjC);
820 Style.ColumnLimit = 80;
821 verifyFormat(
822 "void f() {\n"
823 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
824 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
825 " pos.width(), pos.height())\n"
826 " styleMask:NSBorderlessWindowMask\n"
827 " backing:NSBackingStoreBuffered\n"
828 " defer:NO]);\n"
829 "}");
Francois Ferrand38d80132018-02-09 15:41:56 +0000830
831 // Respect continuation indent and colon alignment (e.g. when object name is
832 // short, and first selector is the longest one)
833 Style = getLLVMStyle();
834 Style.Language = FormatStyle::LK_ObjC;
835 Style.ContinuationIndentWidth = 8;
836 verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
837 " withObject:nil\n"
838 " waitUntilDone:false];");
839 verifyFormat("[self performSelector:@selector(loadAccessories)\n"
840 " withObjectOnMainThread:nil\n"
841 " waitUntilDone:false];");
842 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
843 " performSelectorOnMainThread:@selector(loadAccessories)\n"
844 " withObject:nil\n"
845 " waitUntilDone:false];");
846 verifyFormat("[self // force wrapping\n"
847 " performSelectorOnMainThread:@selector(loadAccessories)\n"
848 " withObject:nil\n"
849 " waitUntilDone:false];");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000850}
851
852TEST_F(FormatTestObjC, ObjCAt) {
853 verifyFormat("@autoreleasepool");
854 verifyFormat("@catch");
855 verifyFormat("@class");
856 verifyFormat("@compatibility_alias");
857 verifyFormat("@defs");
858 verifyFormat("@dynamic");
859 verifyFormat("@encode");
860 verifyFormat("@end");
861 verifyFormat("@finally");
862 verifyFormat("@implementation");
863 verifyFormat("@import");
864 verifyFormat("@interface");
865 verifyFormat("@optional");
866 verifyFormat("@package");
867 verifyFormat("@private");
868 verifyFormat("@property");
869 verifyFormat("@protected");
870 verifyFormat("@protocol");
871 verifyFormat("@public");
872 verifyFormat("@required");
873 verifyFormat("@selector");
874 verifyFormat("@synchronized");
875 verifyFormat("@synthesize");
876 verifyFormat("@throw");
877 verifyFormat("@try");
878
879 EXPECT_EQ("@interface", format("@ interface"));
880
881 // The precise formatting of this doesn't matter, nobody writes code like
882 // this.
883 verifyFormat("@ /*foo*/ interface");
884}
885
Ben Hamilton788a2222018-03-12 15:42:40 +0000886TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) {
887 verifyFormat("void DoStuffWithBlockType(int (^)(char));");
888 verifyFormat("int (^foo)(char, float);");
889 verifyFormat("int (^foo[10])(char, float);");
890 verifyFormat("int (^foo[kNumEntries])(char, float);");
891 verifyFormat("int (^foo[kNumEntries + 10])(char, float);");
892 verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);");
893}
894
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000895TEST_F(FormatTestObjC, ObjCSnippets) {
896 verifyFormat("@autoreleasepool {\n"
897 " foo();\n"
898 "}");
899 verifyFormat("@class Foo, Bar;");
900 verifyFormat("@compatibility_alias AliasName ExistingClass;");
901 verifyFormat("@dynamic textColor;");
902 verifyFormat("char *buf1 = @encode(int *);");
903 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
904 verifyFormat("char *buf1 = @encode(int **);");
905 verifyFormat("Protocol *proto = @protocol(p1);");
906 verifyFormat("SEL s = @selector(foo:);");
907 verifyFormat("@synchronized(self) {\n"
908 " f();\n"
909 "}");
910
911 verifyFormat("@import foo.bar;\n"
912 "@import baz;");
913
914 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
915
916 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
917 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
918
919 Style = getMozillaStyle();
920 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
921 verifyFormat("@property BOOL editable;");
922
923 Style = getWebKitStyle();
924 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
925 verifyFormat("@property BOOL editable;");
926
927 Style = getGoogleStyle(FormatStyle::LK_ObjC);
928 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
929 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
930}
931
932TEST_F(FormatTestObjC, ObjCForIn) {
933 verifyFormat("- (void)test {\n"
934 " for (NSString *n in arrayOfStrings) {\n"
935 " foo(n);\n"
936 " }\n"
937 "}");
938 verifyFormat("- (void)test {\n"
939 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
940 " foo(n);\n"
941 " }\n"
942 "}");
Ben Hamilton1d6c6ee2018-03-06 17:21:42 +0000943 verifyFormat("for (Foo *x in bar) {\n}");
944 verifyFormat("for (Foo *x in [bar baz]) {\n}");
945 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
946 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
947 verifyFormat("for (Foo *x in [bar baz:^{\n"
948 " [uh oh];\n"
949 " }]) {\n}");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000950}
951
Ben Hamilton5f911342018-03-22 03:23:53 +0000952TEST_F(FormatTestObjC, ObjCNew) {
953 verifyFormat("+ (instancetype)new {\n"
954 " return nil;\n"
955 "}\n");
956 verifyFormat("+ (instancetype)myNew {\n"
957 " return [self new];\n"
958 "}\n");
959 verifyFormat("SEL NewSelector(void) { return @selector(new); }\n");
960 verifyFormat("SEL MacroSelector(void) { return MACRO(new); }\n");
961}
962
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000963TEST_F(FormatTestObjC, ObjCLiterals) {
964 verifyFormat("@\"String\"");
965 verifyFormat("@1");
966 verifyFormat("@+4.8");
967 verifyFormat("@-4");
968 verifyFormat("@1LL");
969 verifyFormat("@.5");
970 verifyFormat("@'c'");
971 verifyFormat("@true");
972
973 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
974 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
975 verifyFormat("NSNumber *favoriteColor = @(Green);");
976 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
977
978 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
979}
980
981TEST_F(FormatTestObjC, ObjCDictLiterals) {
982 verifyFormat("@{");
983 verifyFormat("@{}");
984 verifyFormat("@{@\"one\" : @1}");
985 verifyFormat("return @{@\"one\" : @1;");
986 verifyFormat("@{@\"one\" : @1}");
987
988 verifyFormat("@{@\"one\" : @{@2 : @1}}");
989 verifyFormat("@{\n"
990 " @\"one\" : @{@2 : @1},\n"
991 "}");
992
993 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
994 verifyIncompleteFormat("[self setDict:@{}");
995 verifyIncompleteFormat("[self setDict:@{@1 : @2}");
996 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
997 verifyFormat(
998 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
999 verifyFormat(
1000 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
1001
1002 verifyFormat("NSDictionary *d = @{\n"
1003 " @\"nam\" : NSUserNam(),\n"
1004 " @\"dte\" : [NSDate date],\n"
1005 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
1006 "};");
1007 verifyFormat(
1008 "@{\n"
1009 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1010 "regularFont,\n"
1011 "};");
1012 verifyFormat(
1013 "@{\n"
1014 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
1015 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
1016 "};");
1017
1018 // We should try to be robust in case someone forgets the "@".
1019 verifyFormat("NSDictionary *d = {\n"
1020 " @\"nam\" : NSUserNam(),\n"
1021 " @\"dte\" : [NSDate date],\n"
1022 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
1023 "};");
1024 verifyFormat("NSMutableDictionary *dictionary =\n"
1025 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
1026 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
1027 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
1028 " cccccccccccccccc : ccccccccccccccc\n"
1029 " }];");
1030
1031 // Ensure that casts before the key are kept on the same line as the key.
1032 verifyFormat(
1033 "NSDictionary *d = @{\n"
1034 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1035 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
1036 "};");
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001037 Style.ColumnLimit = 40;
1038 verifyFormat("int Foo() {\n"
1039 " a12345 = @{a12345 : a12345};\n"
1040 "}");
1041 verifyFormat("int Foo() {\n"
Ben Hamilton1915d2a2018-04-03 14:07:09 +00001042 " a12345 = @{a12345 : @(a12345)};\n"
1043 "}");
1044 verifyFormat("int Foo() {\n"
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001045 " a12345 = @{(Foo *)a12345 : @(a12345)};\n"
1046 "}");
Ben Hamilton1915d2a2018-04-03 14:07:09 +00001047 verifyFormat("int Foo() {\n"
1048 " a12345 = @{@(a12345) : a12345};\n"
1049 "}");
1050 verifyFormat("int Foo() {\n"
1051 " a12345 = @{@(a12345) : @YES};\n"
1052 "}");
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001053 Style.SpacesInContainerLiterals = false;
1054 verifyFormat("int Foo() {\n"
1055 " b12345 = @{b12345: b12345};\n"
1056 "}");
1057 verifyFormat("int Foo() {\n"
1058 " b12345 = @{(Foo *)b12345: @(b12345)};\n"
1059 "}");
1060 Style.SpacesInContainerLiterals = true;
Daniel Jasper03a04fe2016-12-12 12:42:29 +00001061
1062 Style = getGoogleStyle(FormatStyle::LK_ObjC);
1063 verifyFormat(
1064 "@{\n"
1065 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1066 "regularFont,\n"
1067 "};");
1068}
1069
1070TEST_F(FormatTestObjC, ObjCArrayLiterals) {
1071 verifyIncompleteFormat("@[");
1072 verifyFormat("@[]");
1073 verifyFormat(
1074 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
1075 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
1076 verifyFormat("NSArray *array = @[ [foo description] ];");
1077
1078 verifyFormat(
1079 "NSArray *some_variable = @[\n"
1080 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1081 " @\"aaaaaaaaaaaaaaaaa\",\n"
1082 " @\"aaaaaaaaaaaaaaaaa\",\n"
1083 " @\"aaaaaaaaaaaaaaaaa\",\n"
1084 "];");
1085 verifyFormat(
1086 "NSArray *some_variable = @[\n"
1087 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1088 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
1089 "];");
1090 verifyFormat("NSArray *some_variable = @[\n"
1091 " @\"aaaaaaaaaaaaaaaaa\",\n"
1092 " @\"aaaaaaaaaaaaaaaaa\",\n"
1093 " @\"aaaaaaaaaaaaaaaaa\",\n"
1094 " @\"aaaaaaaaaaaaaaaaa\",\n"
1095 "];");
1096 verifyFormat("NSArray *array = @[\n"
1097 " @\"a\",\n"
1098 " @\"a\",\n" // Trailing comma -> one per line.
1099 "];");
1100
1101 // We should try to be robust in case someone forgets the "@".
1102 verifyFormat("NSArray *some_variable = [\n"
1103 " @\"aaaaaaaaaaaaaaaaa\",\n"
1104 " @\"aaaaaaaaaaaaaaaaa\",\n"
1105 " @\"aaaaaaaaaaaaaaaaa\",\n"
1106 " @\"aaaaaaaaaaaaaaaaa\",\n"
1107 "];");
1108 verifyFormat(
1109 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
1110 " index:(NSUInteger)index\n"
1111 " nonDigitAttributes:\n"
1112 " (NSDictionary *)noDigitAttributes;");
1113 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
1114 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
1115 "]];");
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001116 Style.ColumnLimit = 40;
1117 verifyFormat("int Foo() {\n"
1118 " a12345 = @[ a12345, a12345 ];\n"
1119 "}");
1120 verifyFormat("int Foo() {\n"
1121 " a123 = @[ (Foo *)a12345, @(a12345) ];\n"
1122 "}");
1123 Style.SpacesInContainerLiterals = false;
1124 verifyFormat("int Foo() {\n"
1125 " b12345 = @[b12345, b12345];\n"
1126 "}");
1127 verifyFormat("int Foo() {\n"
1128 " b12345 = @[(Foo *)b12345, @(b12345)];\n"
1129 "}");
1130 Style.SpacesInContainerLiterals = true;
Ben Hamilton09051f22018-02-08 16:07:25 +00001131 Style.ColumnLimit = 20;
1132 // We can't break string literals inside NSArray literals
1133 // (that raises -Wobjc-string-concatenation).
1134 verifyFormat("NSArray *foo = @[\n"
1135 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1136 "];\n");
Daniel Jasper03a04fe2016-12-12 12:42:29 +00001137}
1138} // end namespace
1139} // end namespace format
1140} // end namespace clang