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