blob: fe86cfe8ce9540953d2bc287f5b502e758bff127 [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;
539 // Make sure selectors with 0, 1, or more arguments are not indented
540 // when IndentWrappedFunctionNames is false.
541 Style.IndentWrappedFunctionNames = false;
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");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000555
556 // Continuation indent width should win over aligning colons if the function
557 // name is long.
Ben Hamilton5b0c3ad2017-12-14 21:44:11 +0000558 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000559 Style.ColumnLimit = 40;
560 Style.IndentWrappedFunctionNames = true;
561 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
562 " dontAlignNamef:(NSRect)theRect {\n"
563 "}");
564
565 // Make sure we don't break aligning for short parameter names.
566 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
567 " aShortf:(NSRect)theRect {\n"
568 "}");
Ben Hamilton5b0c3ad2017-12-14 21:44:11 +0000569
Ben Hamilton4dc1cdc2018-03-30 15:38:45 +0000570 // Make sure selectors with 0, 1, or more arguments are indented
571 // when IndentWrappedFunctionNames is true.
572 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
573 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n");
574 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
575 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
576 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
577 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
578 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
579 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
580 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
581 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
582 verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
583 " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
584 " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
585
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000586 // Format pairs correctly.
587 Style.ColumnLimit = 80;
588 verifyFormat("- (void)drawRectOn:(id)surface\n"
589 " ofSize:(aaaaaaaa)height\n"
590 " :(size_t)width\n"
591 " atOrigin:(size_t)x\n"
592 " :(size_t)y\n"
593 " aaaaa:(a)yyy\n"
594 " bbb:(d)cccc;");
595 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
596}
597
598TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
599 verifyFormat("[foo bar:baz];");
600 verifyFormat("return [foo bar:baz];");
601 verifyFormat("return (a)[foo bar:baz];");
602 verifyFormat("f([foo bar:baz]);");
603 verifyFormat("f(2, [foo bar:baz]);");
604 verifyFormat("f(2, a ? b : c);");
605 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
606
607 // Unary operators.
608 verifyFormat("int a = +[foo bar:baz];");
609 verifyFormat("int a = -[foo bar:baz];");
610 verifyFormat("int a = ![foo bar:baz];");
611 verifyFormat("int a = ~[foo bar:baz];");
612 verifyFormat("int a = ++[foo bar:baz];");
613 verifyFormat("int a = --[foo bar:baz];");
614 verifyFormat("int a = sizeof [foo bar:baz];");
615 verifyFormat("int a = alignof [foo bar:baz];");
616 verifyFormat("int a = &[foo bar:baz];");
617 verifyFormat("int a = *[foo bar:baz];");
618 // FIXME: Make casts work, without breaking f()[4].
619 // verifyFormat("int a = (int)[foo bar:baz];");
620 // verifyFormat("return (int)[foo bar:baz];");
621 // verifyFormat("(void)[foo bar:baz];");
622 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
623
624 // Binary operators.
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 verifyFormat("[foo bar:baz] ? [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 verifyFormat("[foo bar:baz] != [foo bar:baz];");
645 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
646 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
647 verifyFormat("[foo bar:baz] > [foo bar:baz];");
648 verifyFormat("[foo bar:baz] < [foo bar:baz];");
649 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
650 verifyFormat("[foo bar:baz] << [foo bar:baz];");
651 verifyFormat("[foo bar:baz] - [foo bar:baz];");
652 verifyFormat("[foo bar:baz] + [foo bar:baz];");
653 verifyFormat("[foo bar:baz] * [foo bar:baz];");
654 verifyFormat("[foo bar:baz] / [foo bar:baz];");
655 verifyFormat("[foo bar:baz] % [foo bar:baz];");
656 // Whew!
657
658 verifyFormat("return in[42];");
659 verifyFormat("for (auto v : in[1]) {\n}");
660 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
661 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
662 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
663 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
664 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
665 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
666 "}");
667 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
Ben Hamiltonad991862018-03-20 14:53:25 +0000668 verifyFormat("[self aaaaa:MACRO(a, b:c:, d:e:)];");
669 verifyFormat("[self aaaaa:MACRO(a, b:c:d:, e:f:g:)];");
670 verifyFormat("int XYMyFoo(int a, int b) NS_SWIFT_NAME(foo(self:scale:));");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000671 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
672 verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
673
674 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
675 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
676 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
677 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
678 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
679 verifyFormat("[button setAction:@selector(zoomOut:)];");
680 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
681
682 verifyFormat("arr[[self indexForFoo:a]];");
683 verifyFormat("throw [self errorFor:a];");
684 verifyFormat("@throw [self errorFor:a];");
685
686 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
687 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
688 verifyFormat("4 > 4 ? (id)a : (id)baz;");
689
690 // This tests that the formatter doesn't break after "backing" but before ":",
691 // which would be at 80 columns.
692 verifyFormat(
693 "void f() {\n"
694 " if ((self = [super initWithContentRect:contentRect\n"
695 " styleMask:styleMask ?: otherMask\n"
696 " backing:NSBackingStoreBuffered\n"
697 " defer:YES]))");
698
699 verifyFormat(
700 "[foo checkThatBreakingAfterColonWorksOk:\n"
701 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
702
703 verifyFormat("[myObj short:arg1 // Force line break\n"
704 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
705 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
706 " error:arg4];");
707 verifyFormat(
708 "void f() {\n"
709 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
710 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
711 " pos.width(), pos.height())\n"
712 " styleMask:NSBorderlessWindowMask\n"
713 " backing:NSBackingStoreBuffered\n"
714 " defer:NO]);\n"
715 "}");
716 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
717 " with:contentsNativeView];");
718
719 verifyFormat(
720 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
721 " owner:nillllll];");
722
723 verifyFormat(
724 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
725 " forType:kBookmarkButtonDragType];");
726
727 verifyFormat("[defaultCenter addObserver:self\n"
728 " selector:@selector(willEnterFullscreen)\n"
729 " name:kWillEnterFullscreenNotification\n"
730 " object:nil];");
731 verifyFormat("[image_rep drawInRect:drawRect\n"
732 " fromRect:NSZeroRect\n"
733 " operation:NSCompositeCopy\n"
734 " fraction:1.0\n"
735 " respectFlipped:NO\n"
736 " hints:nil];");
737 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
738 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
739 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
740 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
741 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
742 " aaaaaaaaaaaaaaaaaaaaaa];");
743
744 verifyFormat(
745 "scoped_nsobject<NSTextField> message(\n"
746 " // The frame will be fixed up when |-setMessageText:| is called.\n"
747 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
748 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
749 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
750 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
751 " aaaa:bbb];");
752 verifyFormat("[self param:function( //\n"
753 " parameter)]");
754 verifyFormat(
755 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
756 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
757 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
758
759 // Variadic parameters.
760 verifyFormat(
761 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
762 verifyFormat(
763 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
764 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
765 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
766 verifyFormat("[self // break\n"
767 " a:a\n"
768 " aaa:aaa];");
769 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
770 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
771
772 // Formats pair-parameters.
773 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
774 verifyFormat("[I drawRectOn:surface //\n"
Francois Ferrand38d80132018-02-09 15:41:56 +0000775 " ofSize:aa:bbb\n"
776 " atOrigin:cc:dd];");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000777
Jacek Olesiakfb7f5c02018-02-07 10:35:08 +0000778 // Inline block as a first argument.
779 verifyFormat("[object justBlock:^{\n"
780 " a = 42;\n"
781 "}];");
782 verifyFormat("[object\n"
783 " justBlock:^{\n"
784 " a = 42;\n"
785 " }\n"
786 " notBlock:42\n"
787 " a:42];");
788 verifyFormat("[object\n"
789 " firstBlock:^{\n"
790 " a = 42;\n"
791 " }\n"
792 " blockWithLongerName:^{\n"
793 " a = 42;\n"
794 " }];");
795 verifyFormat("[object\n"
796 " blockWithLongerName:^{\n"
797 " a = 42;\n"
798 " }\n"
799 " secondBlock:^{\n"
800 " a = 42;\n"
801 " }];");
802 verifyFormat("[object\n"
803 " firstBlock:^{\n"
804 " a = 42;\n"
805 " }\n"
806 " notBlock:42\n"
807 " secondBlock:^{\n"
808 " a = 42;\n"
809 " }];");
810
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000811 Style.ColumnLimit = 70;
812 verifyFormat(
813 "void f() {\n"
814 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
815 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
816 " pos.width(), pos.height())\n"
817 " syeMask:NSBorderlessWindowMask\n"
818 " bking:NSBackingStoreBuffered\n"
819 " der:NO]);\n"
820 "}");
821
822 Style.ColumnLimit = 60;
823 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
824 " .aaaaaaaa];"); // FIXME: Indentation seems off.
825 // FIXME: This violates the column limit.
826 verifyFormat(
827 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
828 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
829 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
830
831 Style = getChromiumStyle(FormatStyle::LK_ObjC);
832 Style.ColumnLimit = 80;
833 verifyFormat(
834 "void f() {\n"
835 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
836 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
837 " pos.width(), pos.height())\n"
838 " styleMask:NSBorderlessWindowMask\n"
839 " backing:NSBackingStoreBuffered\n"
840 " defer:NO]);\n"
841 "}");
Francois Ferrand38d80132018-02-09 15:41:56 +0000842
843 // Respect continuation indent and colon alignment (e.g. when object name is
844 // short, and first selector is the longest one)
845 Style = getLLVMStyle();
846 Style.Language = FormatStyle::LK_ObjC;
847 Style.ContinuationIndentWidth = 8;
848 verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
849 " withObject:nil\n"
850 " waitUntilDone:false];");
851 verifyFormat("[self performSelector:@selector(loadAccessories)\n"
852 " withObjectOnMainThread:nil\n"
853 " waitUntilDone:false];");
854 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
855 " performSelectorOnMainThread:@selector(loadAccessories)\n"
856 " withObject:nil\n"
857 " waitUntilDone:false];");
858 verifyFormat("[self // force wrapping\n"
859 " performSelectorOnMainThread:@selector(loadAccessories)\n"
860 " withObject:nil\n"
861 " waitUntilDone:false];");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000862}
863
864TEST_F(FormatTestObjC, ObjCAt) {
865 verifyFormat("@autoreleasepool");
866 verifyFormat("@catch");
867 verifyFormat("@class");
868 verifyFormat("@compatibility_alias");
869 verifyFormat("@defs");
870 verifyFormat("@dynamic");
871 verifyFormat("@encode");
872 verifyFormat("@end");
873 verifyFormat("@finally");
874 verifyFormat("@implementation");
875 verifyFormat("@import");
876 verifyFormat("@interface");
877 verifyFormat("@optional");
878 verifyFormat("@package");
879 verifyFormat("@private");
880 verifyFormat("@property");
881 verifyFormat("@protected");
882 verifyFormat("@protocol");
883 verifyFormat("@public");
884 verifyFormat("@required");
885 verifyFormat("@selector");
886 verifyFormat("@synchronized");
887 verifyFormat("@synthesize");
888 verifyFormat("@throw");
889 verifyFormat("@try");
890
891 EXPECT_EQ("@interface", format("@ interface"));
892
893 // The precise formatting of this doesn't matter, nobody writes code like
894 // this.
895 verifyFormat("@ /*foo*/ interface");
896}
897
Ben Hamilton788a2222018-03-12 15:42:40 +0000898TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) {
899 verifyFormat("void DoStuffWithBlockType(int (^)(char));");
900 verifyFormat("int (^foo)(char, float);");
901 verifyFormat("int (^foo[10])(char, float);");
902 verifyFormat("int (^foo[kNumEntries])(char, float);");
903 verifyFormat("int (^foo[kNumEntries + 10])(char, float);");
904 verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);");
905}
906
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000907TEST_F(FormatTestObjC, ObjCSnippets) {
908 verifyFormat("@autoreleasepool {\n"
909 " foo();\n"
910 "}");
911 verifyFormat("@class Foo, Bar;");
912 verifyFormat("@compatibility_alias AliasName ExistingClass;");
913 verifyFormat("@dynamic textColor;");
914 verifyFormat("char *buf1 = @encode(int *);");
915 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
916 verifyFormat("char *buf1 = @encode(int **);");
917 verifyFormat("Protocol *proto = @protocol(p1);");
918 verifyFormat("SEL s = @selector(foo:);");
919 verifyFormat("@synchronized(self) {\n"
920 " f();\n"
921 "}");
922
923 verifyFormat("@import foo.bar;\n"
924 "@import baz;");
925
926 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
927
928 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
929 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
930
931 Style = getMozillaStyle();
932 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
933 verifyFormat("@property BOOL editable;");
934
935 Style = getWebKitStyle();
936 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
937 verifyFormat("@property BOOL editable;");
938
939 Style = getGoogleStyle(FormatStyle::LK_ObjC);
940 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
941 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
942}
943
944TEST_F(FormatTestObjC, ObjCForIn) {
945 verifyFormat("- (void)test {\n"
946 " for (NSString *n in arrayOfStrings) {\n"
947 " foo(n);\n"
948 " }\n"
949 "}");
950 verifyFormat("- (void)test {\n"
951 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
952 " foo(n);\n"
953 " }\n"
954 "}");
Ben Hamilton1d6c6ee2018-03-06 17:21:42 +0000955 verifyFormat("for (Foo *x in bar) {\n}");
956 verifyFormat("for (Foo *x in [bar baz]) {\n}");
957 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
958 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
959 verifyFormat("for (Foo *x in [bar baz:^{\n"
960 " [uh oh];\n"
961 " }]) {\n}");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000962}
963
Ben Hamilton5f911342018-03-22 03:23:53 +0000964TEST_F(FormatTestObjC, ObjCNew) {
965 verifyFormat("+ (instancetype)new {\n"
966 " return nil;\n"
967 "}\n");
968 verifyFormat("+ (instancetype)myNew {\n"
969 " return [self new];\n"
970 "}\n");
971 verifyFormat("SEL NewSelector(void) { return @selector(new); }\n");
972 verifyFormat("SEL MacroSelector(void) { return MACRO(new); }\n");
973}
974
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000975TEST_F(FormatTestObjC, ObjCLiterals) {
976 verifyFormat("@\"String\"");
977 verifyFormat("@1");
978 verifyFormat("@+4.8");
979 verifyFormat("@-4");
980 verifyFormat("@1LL");
981 verifyFormat("@.5");
982 verifyFormat("@'c'");
983 verifyFormat("@true");
984
985 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
986 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
987 verifyFormat("NSNumber *favoriteColor = @(Green);");
988 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
989
990 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
991}
992
993TEST_F(FormatTestObjC, ObjCDictLiterals) {
994 verifyFormat("@{");
995 verifyFormat("@{}");
996 verifyFormat("@{@\"one\" : @1}");
997 verifyFormat("return @{@\"one\" : @1;");
998 verifyFormat("@{@\"one\" : @1}");
999
1000 verifyFormat("@{@\"one\" : @{@2 : @1}}");
1001 verifyFormat("@{\n"
1002 " @\"one\" : @{@2 : @1},\n"
1003 "}");
1004
1005 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
1006 verifyIncompleteFormat("[self setDict:@{}");
1007 verifyIncompleteFormat("[self setDict:@{@1 : @2}");
1008 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
1009 verifyFormat(
1010 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
1011 verifyFormat(
1012 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
1013
1014 verifyFormat("NSDictionary *d = @{\n"
1015 " @\"nam\" : NSUserNam(),\n"
1016 " @\"dte\" : [NSDate date],\n"
1017 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
1018 "};");
1019 verifyFormat(
1020 "@{\n"
1021 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1022 "regularFont,\n"
1023 "};");
1024 verifyFormat(
1025 "@{\n"
1026 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
1027 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
1028 "};");
1029
1030 // We should try to be robust in case someone forgets the "@".
1031 verifyFormat("NSDictionary *d = {\n"
1032 " @\"nam\" : NSUserNam(),\n"
1033 " @\"dte\" : [NSDate date],\n"
1034 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
1035 "};");
1036 verifyFormat("NSMutableDictionary *dictionary =\n"
1037 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
1038 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
1039 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
1040 " cccccccccccccccc : ccccccccccccccc\n"
1041 " }];");
1042
1043 // Ensure that casts before the key are kept on the same line as the key.
1044 verifyFormat(
1045 "NSDictionary *d = @{\n"
1046 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
1047 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
1048 "};");
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001049 Style.ColumnLimit = 40;
1050 verifyFormat("int Foo() {\n"
1051 " a12345 = @{a12345 : a12345};\n"
1052 "}");
1053 verifyFormat("int Foo() {\n"
Ben Hamilton1915d2a2018-04-03 14:07:09 +00001054 " a12345 = @{a12345 : @(a12345)};\n"
1055 "}");
1056 verifyFormat("int Foo() {\n"
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001057 " a12345 = @{(Foo *)a12345 : @(a12345)};\n"
1058 "}");
Ben Hamilton1915d2a2018-04-03 14:07:09 +00001059 verifyFormat("int Foo() {\n"
1060 " a12345 = @{@(a12345) : a12345};\n"
1061 "}");
1062 verifyFormat("int Foo() {\n"
1063 " a12345 = @{@(a12345) : @YES};\n"
1064 "}");
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001065 Style.SpacesInContainerLiterals = false;
1066 verifyFormat("int Foo() {\n"
1067 " b12345 = @{b12345: b12345};\n"
1068 "}");
1069 verifyFormat("int Foo() {\n"
1070 " b12345 = @{(Foo *)b12345: @(b12345)};\n"
1071 "}");
1072 Style.SpacesInContainerLiterals = true;
Daniel Jasper03a04fe2016-12-12 12:42:29 +00001073
1074 Style = getGoogleStyle(FormatStyle::LK_ObjC);
1075 verifyFormat(
1076 "@{\n"
1077 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
1078 "regularFont,\n"
1079 "};");
1080}
1081
1082TEST_F(FormatTestObjC, ObjCArrayLiterals) {
1083 verifyIncompleteFormat("@[");
1084 verifyFormat("@[]");
1085 verifyFormat(
1086 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
1087 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
1088 verifyFormat("NSArray *array = @[ [foo description] ];");
1089
1090 verifyFormat(
1091 "NSArray *some_variable = @[\n"
1092 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1093 " @\"aaaaaaaaaaaaaaaaa\",\n"
1094 " @\"aaaaaaaaaaaaaaaaa\",\n"
1095 " @\"aaaaaaaaaaaaaaaaa\",\n"
1096 "];");
1097 verifyFormat(
1098 "NSArray *some_variable = @[\n"
1099 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1100 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
1101 "];");
1102 verifyFormat("NSArray *some_variable = @[\n"
1103 " @\"aaaaaaaaaaaaaaaaa\",\n"
1104 " @\"aaaaaaaaaaaaaaaaa\",\n"
1105 " @\"aaaaaaaaaaaaaaaaa\",\n"
1106 " @\"aaaaaaaaaaaaaaaaa\",\n"
1107 "];");
1108 verifyFormat("NSArray *array = @[\n"
1109 " @\"a\",\n"
1110 " @\"a\",\n" // Trailing comma -> one per line.
1111 "];");
1112
1113 // We should try to be robust in case someone forgets the "@".
1114 verifyFormat("NSArray *some_variable = [\n"
1115 " @\"aaaaaaaaaaaaaaaaa\",\n"
1116 " @\"aaaaaaaaaaaaaaaaa\",\n"
1117 " @\"aaaaaaaaaaaaaaaaa\",\n"
1118 " @\"aaaaaaaaaaaaaaaaa\",\n"
1119 "];");
1120 verifyFormat(
1121 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
1122 " index:(NSUInteger)index\n"
1123 " nonDigitAttributes:\n"
1124 " (NSDictionary *)noDigitAttributes;");
1125 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
1126 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
1127 "]];");
Ben Hamiltond54e7aa2018-03-27 15:01:17 +00001128 Style.ColumnLimit = 40;
1129 verifyFormat("int Foo() {\n"
1130 " a12345 = @[ a12345, a12345 ];\n"
1131 "}");
1132 verifyFormat("int Foo() {\n"
1133 " a123 = @[ (Foo *)a12345, @(a12345) ];\n"
1134 "}");
1135 Style.SpacesInContainerLiterals = false;
1136 verifyFormat("int Foo() {\n"
1137 " b12345 = @[b12345, b12345];\n"
1138 "}");
1139 verifyFormat("int Foo() {\n"
1140 " b12345 = @[(Foo *)b12345, @(b12345)];\n"
1141 "}");
1142 Style.SpacesInContainerLiterals = true;
Ben Hamilton09051f22018-02-08 16:07:25 +00001143 Style.ColumnLimit = 20;
1144 // We can't break string literals inside NSArray literals
1145 // (that raises -Wobjc-string-concatenation).
1146 verifyFormat("NSArray *foo = @[\n"
1147 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1148 "];\n");
Daniel Jasper03a04fe2016-12-12 12:42:29 +00001149}
1150} // end namespace
1151} // end namespace format
1152} // end namespace clang