blob: d9805859f66352deafc20a64242c87f4aaee067a [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
116 Style = getStyle("{}", "a.h", "none", "extern NSString *kFoo;\n");
117 ASSERT_TRUE((bool)Style);
118 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
119
120 Style =
121 getStyle("{}", "a.h", "none", "typedef NS_ENUM(NSInteger, Foo) {};\n");
122 ASSERT_TRUE((bool)Style);
123 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
124
125 Style = getStyle("{}", "a.h", "none", "enum Foo {};");
126 ASSERT_TRUE((bool)Style);
127 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
128
129 Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n");
130 ASSERT_TRUE((bool)Style);
131 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
132
133 Style =
134 getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }\n");
135 ASSERT_TRUE((bool)Style);
136 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
137
138 Style =
139 getStyle("{}", "a.h", "none", "inline void Foo() { Log(\"Foo\"); }\n");
140 ASSERT_TRUE((bool)Style);
141 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
142
143 Style =
144 getStyle("{}", "a.h", "none", "inline void Foo() { id = @[1, 2, 3]; }\n");
145 ASSERT_TRUE((bool)Style);
146 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
147
148 Style = getStyle("{}", "a.h", "none",
149 "inline void Foo() { id foo = @{1: 2, 3: 4, 5: 6}; }\n");
150 ASSERT_TRUE((bool)Style);
151 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
152
153 Style = getStyle("{}", "a.h", "none",
154 "inline void Foo() { int foo[] = {1, 2, 3}; }\n");
155 ASSERT_TRUE((bool)Style);
156 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000157}
158
159TEST_F(FormatTestObjC, FormatObjCTryCatch) {
160 verifyFormat("@try {\n"
161 " f();\n"
162 "} @catch (NSException e) {\n"
163 " @throw;\n"
164 "} @finally {\n"
165 " exit(42);\n"
166 "}");
167 verifyFormat("DEBUG({\n"
168 " @try {\n"
169 " } @finally {\n"
170 " }\n"
171 "});\n");
172}
173
174TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) {
175 verifyFormat("@autoreleasepool {\n"
176 " f();\n"
177 "}\n"
178 "@autoreleasepool {\n"
179 " f();\n"
180 "}\n");
181 Style.BreakBeforeBraces = FormatStyle::BS_Allman;
182 verifyFormat("@autoreleasepool\n"
183 "{\n"
184 " f();\n"
185 "}\n"
186 "@autoreleasepool\n"
187 "{\n"
188 " f();\n"
189 "}\n");
190}
191
192TEST_F(FormatTestObjC, FormatObjCInterface) {
193 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
194 "@public\n"
195 " int field1;\n"
196 "@protected\n"
197 " int field2;\n"
198 "@private\n"
199 " int field3;\n"
200 "@package\n"
201 " int field4;\n"
202 "}\n"
203 "+ (id)init;\n"
204 "@end");
205
206 verifyFormat("@interface /* wait for it */ Foo\n"
207 "+ (id)init;\n"
208 "// Look, a comment!\n"
209 "- (int)answerWith:(int)i;\n"
210 "@end");
211
212 verifyFormat("@interface Foo\n"
213 "@end\n"
214 "@interface Bar\n"
215 "@end");
216
217 verifyFormat("@interface Foo : Bar\n"
Nico Weberc068ff72018-01-23 17:10:25 +0000218 "@property(assign, readwrite) NSInteger bar;\n"
219 "+ (id)init;\n"
220 "@end");
221
222 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @interface Foo : Bar\n"
223 "@property(assign, readwrite) NSInteger bar;\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000224 "+ (id)init;\n"
225 "@end");
226
227 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
228 "+ (id)init;\n"
229 "@end");
230
231 verifyFormat("@interface Foo (HackStuff)\n"
232 "+ (id)init;\n"
233 "@end");
234
235 verifyFormat("@interface Foo ()\n"
236 "+ (id)init;\n"
237 "@end");
238
239 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
240 "+ (id)init;\n"
241 "@end");
242
243 verifyFormat("@interface Foo {\n"
244 " int _i;\n"
245 "}\n"
246 "+ (id)init;\n"
247 "@end");
248
249 verifyFormat("@interface Foo : Bar {\n"
250 " int _i;\n"
251 "}\n"
252 "+ (id)init;\n"
253 "@end");
254
255 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
256 " int _i;\n"
257 "}\n"
258 "+ (id)init;\n"
259 "@end");
260
261 verifyFormat("@interface Foo (HackStuff) {\n"
262 " int _i;\n"
263 "}\n"
264 "+ (id)init;\n"
265 "@end");
266
267 verifyFormat("@interface Foo () {\n"
268 " int _i;\n"
269 "}\n"
270 "+ (id)init;\n"
271 "@end");
272
273 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
274 " int _i;\n"
275 "}\n"
276 "+ (id)init;\n"
277 "@end");
278
Ben Hamilton5dd40182018-01-29 20:01:49 +0000279 Style.ColumnLimit = 40;
280 verifyFormat("@interface ccccccccccccc () <\n"
281 " ccccccccccccc, ccccccccccccc,\n"
282 " ccccccccccccc, ccccccccccccc> {\n"
283 "}");
Ben Hamilton4dc658c2018-02-02 20:15:14 +0000284 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
Ben Hamilton5dd40182018-01-29 20:01:49 +0000285 verifyFormat("@interface ddddddddddddd () <\n"
286 " ddddddddddddd,\n"
287 " ddddddddddddd,\n"
288 " ddddddddddddd,\n"
289 " ddddddddddddd> {\n"
290 "}");
291
Ben Hamilton4dc658c2018-02-02 20:15:14 +0000292 Style.BinPackParameters = false;
293 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
294 verifyFormat("@interface eeeeeeeeeeeee () <\n"
295 " eeeeeeeeeeeee,\n"
296 " eeeeeeeeeeeee,\n"
297 " eeeeeeeeeeeee,\n"
298 " eeeeeeeeeeeee> {\n"
299 "}");
300 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Always;
301 verifyFormat("@interface fffffffffffff () <\n"
302 " fffffffffffff, fffffffffffff,\n"
303 " fffffffffffff, fffffffffffff> {\n"
304 "}");
305
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000306 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000307 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000308 " @public\n"
309 " int field1;\n"
310 " @protected\n"
311 " int field2;\n"
312 " @private\n"
313 " int field3;\n"
314 " @package\n"
315 " int field4;\n"
316 "}\n"
317 "+ (id)init;\n"
318 "@end");
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000319 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000320 "+ (id)init;\n"
321 "@end");
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000322 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000323 "+ (id)init;\n"
324 "@end");
325 Style.BinPackParameters = false;
326 Style.ColumnLimit = 80;
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000327 verifyFormat("@interface aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa () <\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000328 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
329 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
330 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n"
331 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa> {\n"
332 "}");
333}
334
335TEST_F(FormatTestObjC, FormatObjCImplementation) {
336 verifyFormat("@implementation Foo : NSObject {\n"
337 "@public\n"
338 " int field1;\n"
339 "@protected\n"
340 " int field2;\n"
341 "@private\n"
342 " int field3;\n"
343 "@package\n"
344 " int field4;\n"
345 "}\n"
346 "+ (id)init {\n}\n"
347 "@end");
348
349 verifyFormat("@implementation Foo\n"
350 "+ (id)init {\n"
351 " if (true)\n"
352 " return nil;\n"
353 "}\n"
354 "// Look, a comment!\n"
355 "- (int)answerWith:(int)i {\n"
356 " return i;\n"
357 "}\n"
358 "+ (int)answerWith:(int)i {\n"
359 " return i;\n"
360 "}\n"
361 "@end");
362
363 verifyFormat("@implementation Foo\n"
364 "@end\n"
365 "@implementation Bar\n"
366 "@end");
367
368 EXPECT_EQ("@implementation Foo : Bar\n"
369 "+ (id)init {\n}\n"
370 "- (void)foo {\n}\n"
371 "@end",
372 format("@implementation Foo : Bar\n"
373 "+(id)init{}\n"
374 "-(void)foo{}\n"
375 "@end"));
376
377 verifyFormat("@implementation Foo {\n"
378 " int _i;\n"
379 "}\n"
380 "+ (id)init {\n}\n"
381 "@end");
382
383 verifyFormat("@implementation Foo : Bar {\n"
384 " int _i;\n"
385 "}\n"
386 "+ (id)init {\n}\n"
387 "@end");
388
389 verifyFormat("@implementation Foo (HackStuff)\n"
390 "+ (id)init {\n}\n"
391 "@end");
392 verifyFormat("@implementation ObjcClass\n"
393 "- (void)method;\n"
394 "{}\n"
395 "@end");
396
397 Style = getGoogleStyle(FormatStyle::LK_ObjC);
398 verifyFormat("@implementation Foo : NSObject {\n"
399 " @public\n"
400 " int field1;\n"
401 " @protected\n"
402 " int field2;\n"
403 " @private\n"
404 " int field3;\n"
405 " @package\n"
406 " int field4;\n"
407 "}\n"
408 "+ (id)init {\n}\n"
409 "@end");
410}
411
412TEST_F(FormatTestObjC, FormatObjCProtocol) {
413 verifyFormat("@protocol Foo\n"
414 "@property(weak) id delegate;\n"
415 "- (NSUInteger)numberOfThings;\n"
416 "@end");
417
418 verifyFormat("@protocol MyProtocol <NSObject>\n"
419 "- (NSUInteger)numberOfThings;\n"
420 "@end");
421
422 verifyFormat("@protocol Foo;\n"
423 "@protocol Bar;\n");
424
425 verifyFormat("@protocol Foo\n"
426 "@end\n"
427 "@protocol Bar\n"
428 "@end");
429
Nico Weberc068ff72018-01-23 17:10:25 +0000430 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n"
431 "@property(assign, readwrite) NSInteger bar;\n"
432 "@end");
433
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000434 verifyFormat("@protocol myProtocol\n"
435 "- (void)mandatoryWithInt:(int)i;\n"
436 "@optional\n"
437 "- (void)optional;\n"
438 "@required\n"
439 "- (void)required;\n"
440 "@optional\n"
441 "@property(assign) int madProp;\n"
442 "@end\n");
443
444 verifyFormat("@property(nonatomic, assign, readonly)\n"
445 " int *looooooooooooooooooooooooooooongNumber;\n"
446 "@property(nonatomic, assign, readonly)\n"
447 " NSString *looooooooooooooooooooooooooooongName;");
448
449 verifyFormat("@implementation PR18406\n"
450 "}\n"
451 "@end");
452
453 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000454 verifyFormat("@protocol MyProtocol <NSObject>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000455 "- (NSUInteger)numberOfThings;\n"
456 "@end");
457}
458
459TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
460 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
461 " rect:(NSRect)theRect\n"
462 " interval:(float)theInterval {\n"
463 "}");
464 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
465 " longKeyword:(NSRect)theRect\n"
466 " longerKeyword:(float)theInterval\n"
467 " error:(NSError **)theError {\n"
468 "}");
469 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
470 " longKeyword:(NSRect)theRect\n"
471 " evenLongerKeyword:(float)theInterval\n"
472 " error:(NSError **)theError {\n"
473 "}");
474 Style.ColumnLimit = 60;
475 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
476 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
477 " NS_DESIGNATED_INITIALIZER;");
478 verifyFormat("- (void)drawRectOn:(id)surface\n"
479 " ofSize:(size_t)height\n"
480 " :(size_t)width;");
481
482 // Continuation indent width should win over aligning colons if the function
483 // name is long.
Ben Hamilton5b0c3ad2017-12-14 21:44:11 +0000484 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000485 Style.ColumnLimit = 40;
486 Style.IndentWrappedFunctionNames = true;
487 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
488 " dontAlignNamef:(NSRect)theRect {\n"
489 "}");
490
491 // Make sure we don't break aligning for short parameter names.
492 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
493 " aShortf:(NSRect)theRect {\n"
494 "}");
Ben Hamilton5b0c3ad2017-12-14 21:44:11 +0000495
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000496 // Format pairs correctly.
497 Style.ColumnLimit = 80;
498 verifyFormat("- (void)drawRectOn:(id)surface\n"
499 " ofSize:(aaaaaaaa)height\n"
500 " :(size_t)width\n"
501 " atOrigin:(size_t)x\n"
502 " :(size_t)y\n"
503 " aaaaa:(a)yyy\n"
504 " bbb:(d)cccc;");
505 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
506}
507
508TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
509 verifyFormat("[foo bar:baz];");
510 verifyFormat("return [foo bar:baz];");
511 verifyFormat("return (a)[foo bar:baz];");
512 verifyFormat("f([foo bar:baz]);");
513 verifyFormat("f(2, [foo bar:baz]);");
514 verifyFormat("f(2, a ? b : c);");
515 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
516
517 // Unary operators.
518 verifyFormat("int a = +[foo bar:baz];");
519 verifyFormat("int a = -[foo bar:baz];");
520 verifyFormat("int a = ![foo bar:baz];");
521 verifyFormat("int a = ~[foo bar:baz];");
522 verifyFormat("int a = ++[foo bar:baz];");
523 verifyFormat("int a = --[foo bar:baz];");
524 verifyFormat("int a = sizeof [foo bar:baz];");
525 verifyFormat("int a = alignof [foo bar:baz];");
526 verifyFormat("int a = &[foo bar:baz];");
527 verifyFormat("int a = *[foo bar:baz];");
528 // FIXME: Make casts work, without breaking f()[4].
529 // verifyFormat("int a = (int)[foo bar:baz];");
530 // verifyFormat("return (int)[foo bar:baz];");
531 // verifyFormat("(void)[foo bar:baz];");
532 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
533
534 // Binary operators.
535 verifyFormat("[foo bar:baz], [foo bar:baz];");
536 verifyFormat("[foo bar:baz] = [foo bar:baz];");
537 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
538 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
539 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
540 verifyFormat("[foo bar:baz] += [foo bar:baz];");
541 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
542 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
543 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
544 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
545 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
546 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
547 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
548 verifyFormat("[foo bar:baz] || [foo bar:baz];");
549 verifyFormat("[foo bar:baz] && [foo bar:baz];");
550 verifyFormat("[foo bar:baz] | [foo bar:baz];");
551 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
552 verifyFormat("[foo bar:baz] & [foo bar:baz];");
553 verifyFormat("[foo bar:baz] == [foo bar:baz];");
554 verifyFormat("[foo bar:baz] != [foo bar:baz];");
555 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
556 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
557 verifyFormat("[foo bar:baz] > [foo bar:baz];");
558 verifyFormat("[foo bar:baz] < [foo bar:baz];");
559 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
560 verifyFormat("[foo bar:baz] << [foo bar:baz];");
561 verifyFormat("[foo bar:baz] - [foo bar:baz];");
562 verifyFormat("[foo bar:baz] + [foo bar:baz];");
563 verifyFormat("[foo bar:baz] * [foo bar:baz];");
564 verifyFormat("[foo bar:baz] / [foo bar:baz];");
565 verifyFormat("[foo bar:baz] % [foo bar:baz];");
566 // Whew!
567
568 verifyFormat("return in[42];");
569 verifyFormat("for (auto v : in[1]) {\n}");
570 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
571 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
572 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
573 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
574 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
575 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
576 "}");
577 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
578 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
579 verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
580
581 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
582 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
583 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
584 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
585 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
586 verifyFormat("[button setAction:@selector(zoomOut:)];");
587 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
588
589 verifyFormat("arr[[self indexForFoo:a]];");
590 verifyFormat("throw [self errorFor:a];");
591 verifyFormat("@throw [self errorFor:a];");
592
593 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
594 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
595 verifyFormat("4 > 4 ? (id)a : (id)baz;");
596
597 // This tests that the formatter doesn't break after "backing" but before ":",
598 // which would be at 80 columns.
599 verifyFormat(
600 "void f() {\n"
601 " if ((self = [super initWithContentRect:contentRect\n"
602 " styleMask:styleMask ?: otherMask\n"
603 " backing:NSBackingStoreBuffered\n"
604 " defer:YES]))");
605
606 verifyFormat(
607 "[foo checkThatBreakingAfterColonWorksOk:\n"
608 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
609
610 verifyFormat("[myObj short:arg1 // Force line break\n"
611 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
612 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
613 " error:arg4];");
614 verifyFormat(
615 "void f() {\n"
616 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
617 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
618 " pos.width(), pos.height())\n"
619 " styleMask:NSBorderlessWindowMask\n"
620 " backing:NSBackingStoreBuffered\n"
621 " defer:NO]);\n"
622 "}");
623 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
624 " with:contentsNativeView];");
625
626 verifyFormat(
627 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
628 " owner:nillllll];");
629
630 verifyFormat(
631 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
632 " forType:kBookmarkButtonDragType];");
633
634 verifyFormat("[defaultCenter addObserver:self\n"
635 " selector:@selector(willEnterFullscreen)\n"
636 " name:kWillEnterFullscreenNotification\n"
637 " object:nil];");
638 verifyFormat("[image_rep drawInRect:drawRect\n"
639 " fromRect:NSZeroRect\n"
640 " operation:NSCompositeCopy\n"
641 " fraction:1.0\n"
642 " respectFlipped:NO\n"
643 " hints:nil];");
644 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
645 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
646 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
647 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
648 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
649 " aaaaaaaaaaaaaaaaaaaaaa];");
650
651 verifyFormat(
652 "scoped_nsobject<NSTextField> message(\n"
653 " // The frame will be fixed up when |-setMessageText:| is called.\n"
654 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
655 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
656 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
657 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
658 " aaaa:bbb];");
659 verifyFormat("[self param:function( //\n"
660 " parameter)]");
661 verifyFormat(
662 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
663 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
664 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
665
666 // Variadic parameters.
667 verifyFormat(
668 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
669 verifyFormat(
670 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
671 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
672 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
673 verifyFormat("[self // break\n"
674 " a:a\n"
675 " aaa:aaa];");
676 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
677 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
678
679 // Formats pair-parameters.
680 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
681 verifyFormat("[I drawRectOn:surface //\n"
682 " ofSize:aa:bbb\n"
683 " atOrigin:cc:dd];");
684
685 Style.ColumnLimit = 70;
686 verifyFormat(
687 "void f() {\n"
688 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
689 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
690 " pos.width(), pos.height())\n"
691 " syeMask:NSBorderlessWindowMask\n"
692 " bking:NSBackingStoreBuffered\n"
693 " der:NO]);\n"
694 "}");
695
696 Style.ColumnLimit = 60;
697 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
698 " .aaaaaaaa];"); // FIXME: Indentation seems off.
699 // FIXME: This violates the column limit.
700 verifyFormat(
701 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
702 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
703 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
704
705 Style = getChromiumStyle(FormatStyle::LK_ObjC);
706 Style.ColumnLimit = 80;
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}
717
718TEST_F(FormatTestObjC, ObjCAt) {
719 verifyFormat("@autoreleasepool");
720 verifyFormat("@catch");
721 verifyFormat("@class");
722 verifyFormat("@compatibility_alias");
723 verifyFormat("@defs");
724 verifyFormat("@dynamic");
725 verifyFormat("@encode");
726 verifyFormat("@end");
727 verifyFormat("@finally");
728 verifyFormat("@implementation");
729 verifyFormat("@import");
730 verifyFormat("@interface");
731 verifyFormat("@optional");
732 verifyFormat("@package");
733 verifyFormat("@private");
734 verifyFormat("@property");
735 verifyFormat("@protected");
736 verifyFormat("@protocol");
737 verifyFormat("@public");
738 verifyFormat("@required");
739 verifyFormat("@selector");
740 verifyFormat("@synchronized");
741 verifyFormat("@synthesize");
742 verifyFormat("@throw");
743 verifyFormat("@try");
744
745 EXPECT_EQ("@interface", format("@ interface"));
746
747 // The precise formatting of this doesn't matter, nobody writes code like
748 // this.
749 verifyFormat("@ /*foo*/ interface");
750}
751
752TEST_F(FormatTestObjC, ObjCSnippets) {
753 verifyFormat("@autoreleasepool {\n"
754 " foo();\n"
755 "}");
756 verifyFormat("@class Foo, Bar;");
757 verifyFormat("@compatibility_alias AliasName ExistingClass;");
758 verifyFormat("@dynamic textColor;");
759 verifyFormat("char *buf1 = @encode(int *);");
760 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
761 verifyFormat("char *buf1 = @encode(int **);");
762 verifyFormat("Protocol *proto = @protocol(p1);");
763 verifyFormat("SEL s = @selector(foo:);");
764 verifyFormat("@synchronized(self) {\n"
765 " f();\n"
766 "}");
767
768 verifyFormat("@import foo.bar;\n"
769 "@import baz;");
770
771 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
772
773 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
774 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
775
776 Style = getMozillaStyle();
777 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
778 verifyFormat("@property BOOL editable;");
779
780 Style = getWebKitStyle();
781 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
782 verifyFormat("@property BOOL editable;");
783
784 Style = getGoogleStyle(FormatStyle::LK_ObjC);
785 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
786 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
787}
788
789TEST_F(FormatTestObjC, ObjCForIn) {
790 verifyFormat("- (void)test {\n"
791 " for (NSString *n in arrayOfStrings) {\n"
792 " foo(n);\n"
793 " }\n"
794 "}");
795 verifyFormat("- (void)test {\n"
796 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
797 " foo(n);\n"
798 " }\n"
799 "}");
800}
801
802TEST_F(FormatTestObjC, ObjCLiterals) {
803 verifyFormat("@\"String\"");
804 verifyFormat("@1");
805 verifyFormat("@+4.8");
806 verifyFormat("@-4");
807 verifyFormat("@1LL");
808 verifyFormat("@.5");
809 verifyFormat("@'c'");
810 verifyFormat("@true");
811
812 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
813 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
814 verifyFormat("NSNumber *favoriteColor = @(Green);");
815 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
816
817 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
818}
819
820TEST_F(FormatTestObjC, ObjCDictLiterals) {
821 verifyFormat("@{");
822 verifyFormat("@{}");
823 verifyFormat("@{@\"one\" : @1}");
824 verifyFormat("return @{@\"one\" : @1;");
825 verifyFormat("@{@\"one\" : @1}");
826
827 verifyFormat("@{@\"one\" : @{@2 : @1}}");
828 verifyFormat("@{\n"
829 " @\"one\" : @{@2 : @1},\n"
830 "}");
831
832 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
833 verifyIncompleteFormat("[self setDict:@{}");
834 verifyIncompleteFormat("[self setDict:@{@1 : @2}");
835 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
836 verifyFormat(
837 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
838 verifyFormat(
839 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
840
841 verifyFormat("NSDictionary *d = @{\n"
842 " @\"nam\" : NSUserNam(),\n"
843 " @\"dte\" : [NSDate date],\n"
844 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
845 "};");
846 verifyFormat(
847 "@{\n"
848 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
849 "regularFont,\n"
850 "};");
851 verifyFormat(
852 "@{\n"
853 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
854 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
855 "};");
856
857 // We should try to be robust in case someone forgets the "@".
858 verifyFormat("NSDictionary *d = {\n"
859 " @\"nam\" : NSUserNam(),\n"
860 " @\"dte\" : [NSDate date],\n"
861 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
862 "};");
863 verifyFormat("NSMutableDictionary *dictionary =\n"
864 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
865 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
866 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
867 " cccccccccccccccc : ccccccccccccccc\n"
868 " }];");
869
870 // Ensure that casts before the key are kept on the same line as the key.
871 verifyFormat(
872 "NSDictionary *d = @{\n"
873 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
874 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
875 "};");
876
877 Style = getGoogleStyle(FormatStyle::LK_ObjC);
878 verifyFormat(
879 "@{\n"
880 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
881 "regularFont,\n"
882 "};");
883}
884
885TEST_F(FormatTestObjC, ObjCArrayLiterals) {
886 verifyIncompleteFormat("@[");
887 verifyFormat("@[]");
888 verifyFormat(
889 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
890 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
891 verifyFormat("NSArray *array = @[ [foo description] ];");
892
893 verifyFormat(
894 "NSArray *some_variable = @[\n"
895 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
896 " @\"aaaaaaaaaaaaaaaaa\",\n"
897 " @\"aaaaaaaaaaaaaaaaa\",\n"
898 " @\"aaaaaaaaaaaaaaaaa\",\n"
899 "];");
900 verifyFormat(
901 "NSArray *some_variable = @[\n"
902 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
903 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
904 "];");
905 verifyFormat("NSArray *some_variable = @[\n"
906 " @\"aaaaaaaaaaaaaaaaa\",\n"
907 " @\"aaaaaaaaaaaaaaaaa\",\n"
908 " @\"aaaaaaaaaaaaaaaaa\",\n"
909 " @\"aaaaaaaaaaaaaaaaa\",\n"
910 "];");
911 verifyFormat("NSArray *array = @[\n"
912 " @\"a\",\n"
913 " @\"a\",\n" // Trailing comma -> one per line.
914 "];");
915
916 // We should try to be robust in case someone forgets the "@".
917 verifyFormat("NSArray *some_variable = [\n"
918 " @\"aaaaaaaaaaaaaaaaa\",\n"
919 " @\"aaaaaaaaaaaaaaaaa\",\n"
920 " @\"aaaaaaaaaaaaaaaaa\",\n"
921 " @\"aaaaaaaaaaaaaaaaa\",\n"
922 "];");
923 verifyFormat(
924 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
925 " index:(NSUInteger)index\n"
926 " nonDigitAttributes:\n"
927 " (NSDictionary *)noDigitAttributes;");
928 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
929 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
930 "]];");
931}
932} // end namespace
933} // end namespace format
934} // end namespace clang