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