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