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