blob: 35c4d886386fcde3d4c0d1879ef9fd7582a730cc [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
Ben Hamiltone2e3e672018-01-17 17:33:08 +0000116 Style =
117 getStyle("{}", "a.h", "none", "typedef NS_ENUM(NSInteger, Foo) {};\n");
118 ASSERT_TRUE((bool)Style);
119 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
120
121 Style = getStyle("{}", "a.h", "none", "enum Foo {};");
122 ASSERT_TRUE((bool)Style);
123 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
124
Ben Hamiltone2e3e672018-01-17 17:33:08 +0000125 Style =
126 getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }\n");
127 ASSERT_TRUE((bool)Style);
128 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
129
130 Style =
131 getStyle("{}", "a.h", "none", "inline void Foo() { Log(\"Foo\"); }\n");
132 ASSERT_TRUE((bool)Style);
133 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
134
135 Style =
136 getStyle("{}", "a.h", "none", "inline void Foo() { id = @[1, 2, 3]; }\n");
137 ASSERT_TRUE((bool)Style);
138 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
139
140 Style = getStyle("{}", "a.h", "none",
141 "inline void Foo() { id foo = @{1: 2, 3: 4, 5: 6}; }\n");
142 ASSERT_TRUE((bool)Style);
143 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
144
145 Style = getStyle("{}", "a.h", "none",
146 "inline void Foo() { int foo[] = {1, 2, 3}; }\n");
147 ASSERT_TRUE((bool)Style);
148 EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
Jacek Olesiakce4f0af2018-02-15 08:47:56 +0000149
150 // ObjC characteristic types.
151 Style = getStyle("{}", "a.h", "none", "extern NSString *kFoo;\n");
152 ASSERT_TRUE((bool)Style);
153 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
154
155 Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n");
156 ASSERT_TRUE((bool)Style);
157 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
158
159 Style = getStyle("{}", "a.h", "none", "NSObject *Foo();\n");
160 ASSERT_TRUE((bool)Style);
161 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
162
163 Style = getStyle("{}", "a.h", "none", "NSSet *Foo();\n");
164 ASSERT_TRUE((bool)Style);
165 EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000166}
167
168TEST_F(FormatTestObjC, FormatObjCTryCatch) {
169 verifyFormat("@try {\n"
170 " f();\n"
171 "} @catch (NSException e) {\n"
172 " @throw;\n"
173 "} @finally {\n"
174 " exit(42);\n"
175 "}");
176 verifyFormat("DEBUG({\n"
177 " @try {\n"
178 " } @finally {\n"
179 " }\n"
180 "});\n");
181}
182
183TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) {
184 verifyFormat("@autoreleasepool {\n"
185 " f();\n"
186 "}\n"
187 "@autoreleasepool {\n"
188 " f();\n"
189 "}\n");
Francois Ferranda2484b22018-02-27 13:48:27 +0000190 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
191 Style.BraceWrapping.AfterControlStatement = true;
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000192 verifyFormat("@autoreleasepool\n"
193 "{\n"
194 " f();\n"
195 "}\n"
196 "@autoreleasepool\n"
197 "{\n"
198 " f();\n"
199 "}\n");
200}
201
Ben Hamilton56d1c012018-02-06 18:01:47 +0000202TEST_F(FormatTestObjC, FormatObjCGenerics) {
203 Style.ColumnLimit = 40;
204 verifyFormat("int aaaaaaaaaaaaaaaa(\n"
205 " NSArray<aaaaaaaaaaaaaaaaaa *>\n"
206 " aaaaaaaaaaaaaaaaa);\n");
207 verifyFormat("int aaaaaaaaaaaaaaaa(\n"
208 " NSArray<aaaaaaaaaaaaaaaaaaa<\n"
209 " aaaaaaaaaaaaaaaa *> *>\n"
210 " aaaaaaaaaaaaaaaaa);\n");
211}
212
Francois Ferrandba91c3d2018-02-27 13:48:21 +0000213TEST_F(FormatTestObjC, FormatObjCSynchronized) {
214 verifyFormat("@synchronized(self) {\n"
215 " f();\n"
216 "}\n"
217 "@synchronized(self) {\n"
218 " f();\n"
219 "}\n");
Francois Ferranda2484b22018-02-27 13:48:27 +0000220 Style.BreakBeforeBraces = FormatStyle::BS_Custom;
221 Style.BraceWrapping.AfterControlStatement = true;
Francois Ferrandba91c3d2018-02-27 13:48:21 +0000222 verifyFormat("@synchronized(self)\n"
223 "{\n"
224 " f();\n"
225 "}\n"
226 "@synchronized(self)\n"
227 "{\n"
228 " f();\n"
229 "}\n");
230}
231
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000232TEST_F(FormatTestObjC, FormatObjCInterface) {
233 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
234 "@public\n"
235 " int field1;\n"
236 "@protected\n"
237 " int field2;\n"
238 "@private\n"
239 " int field3;\n"
240 "@package\n"
241 " int field4;\n"
242 "}\n"
243 "+ (id)init;\n"
244 "@end");
245
246 verifyFormat("@interface /* wait for it */ Foo\n"
247 "+ (id)init;\n"
248 "// Look, a comment!\n"
249 "- (int)answerWith:(int)i;\n"
250 "@end");
251
252 verifyFormat("@interface Foo\n"
253 "@end\n"
254 "@interface Bar\n"
255 "@end");
256
257 verifyFormat("@interface Foo : Bar\n"
Nico Weberc068ff72018-01-23 17:10:25 +0000258 "@property(assign, readwrite) NSInteger bar;\n"
259 "+ (id)init;\n"
260 "@end");
261
262 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @interface Foo : Bar\n"
263 "@property(assign, readwrite) NSInteger bar;\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000264 "+ (id)init;\n"
265 "@end");
266
267 verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
268 "+ (id)init;\n"
269 "@end");
270
271 verifyFormat("@interface Foo (HackStuff)\n"
272 "+ (id)init;\n"
273 "@end");
274
275 verifyFormat("@interface Foo ()\n"
276 "+ (id)init;\n"
277 "@end");
278
279 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
280 "+ (id)init;\n"
281 "@end");
282
283 verifyFormat("@interface Foo {\n"
284 " int _i;\n"
285 "}\n"
286 "+ (id)init;\n"
287 "@end");
288
289 verifyFormat("@interface Foo : Bar {\n"
290 " int _i;\n"
291 "}\n"
292 "+ (id)init;\n"
293 "@end");
294
295 verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
296 " int _i;\n"
297 "}\n"
298 "+ (id)init;\n"
299 "@end");
300
301 verifyFormat("@interface Foo (HackStuff) {\n"
302 " int _i;\n"
303 "}\n"
304 "+ (id)init;\n"
305 "@end");
306
307 verifyFormat("@interface Foo () {\n"
308 " int _i;\n"
309 "}\n"
310 "+ (id)init;\n"
311 "@end");
312
313 verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
314 " int _i;\n"
315 "}\n"
316 "+ (id)init;\n"
317 "@end");
318
Ben Hamilton5dd40182018-01-29 20:01:49 +0000319 Style.ColumnLimit = 40;
320 verifyFormat("@interface ccccccccccccc () <\n"
321 " ccccccccccccc, ccccccccccccc,\n"
322 " ccccccccccccc, ccccccccccccc> {\n"
323 "}");
Ben Hamilton4dc658c2018-02-02 20:15:14 +0000324 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
Ben Hamilton5dd40182018-01-29 20:01:49 +0000325 verifyFormat("@interface ddddddddddddd () <\n"
326 " ddddddddddddd,\n"
327 " ddddddddddddd,\n"
328 " ddddddddddddd,\n"
329 " ddddddddddddd> {\n"
330 "}");
331
Ben Hamilton4dc658c2018-02-02 20:15:14 +0000332 Style.BinPackParameters = false;
333 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
334 verifyFormat("@interface eeeeeeeeeeeee () <\n"
335 " eeeeeeeeeeeee,\n"
336 " eeeeeeeeeeeee,\n"
337 " eeeeeeeeeeeee,\n"
338 " eeeeeeeeeeeee> {\n"
339 "}");
340 Style.ObjCBinPackProtocolList = FormatStyle::BPS_Always;
341 verifyFormat("@interface fffffffffffff () <\n"
342 " fffffffffffff, fffffffffffff,\n"
343 " fffffffffffff, fffffffffffff> {\n"
344 "}");
345
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000346 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000347 verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000348 " @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"
358 "@end");
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000359 verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000360 "+ (id)init;\n"
361 "@end");
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000362 verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000363 "+ (id)init;\n"
364 "@end");
Ben Hamilton3a47fdd2018-02-08 01:49:10 +0000365 Style.ColumnLimit = 40;
366 // BinPackParameters should be true by default.
367 verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n"
368 " int eeeee, int eeeee);\n");
369 // ObjCBinPackProtocolList should be BPS_Never by default.
370 verifyFormat("@interface fffffffffffff () <\n"
371 " fffffffffffff,\n"
372 " fffffffffffff,\n"
373 " fffffffffffff,\n"
374 " fffffffffffff> {\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000375 "}");
376}
377
378TEST_F(FormatTestObjC, FormatObjCImplementation) {
379 verifyFormat("@implementation Foo : NSObject {\n"
380 "@public\n"
381 " int field1;\n"
382 "@protected\n"
383 " int field2;\n"
384 "@private\n"
385 " int field3;\n"
386 "@package\n"
387 " int field4;\n"
388 "}\n"
389 "+ (id)init {\n}\n"
390 "@end");
391
392 verifyFormat("@implementation Foo\n"
393 "+ (id)init {\n"
394 " if (true)\n"
395 " return nil;\n"
396 "}\n"
397 "// Look, a comment!\n"
398 "- (int)answerWith:(int)i {\n"
399 " return i;\n"
400 "}\n"
401 "+ (int)answerWith:(int)i {\n"
402 " return i;\n"
403 "}\n"
404 "@end");
405
406 verifyFormat("@implementation Foo\n"
407 "@end\n"
408 "@implementation Bar\n"
409 "@end");
410
411 EXPECT_EQ("@implementation Foo : Bar\n"
412 "+ (id)init {\n}\n"
413 "- (void)foo {\n}\n"
414 "@end",
415 format("@implementation Foo : Bar\n"
416 "+(id)init{}\n"
417 "-(void)foo{}\n"
418 "@end"));
419
420 verifyFormat("@implementation Foo {\n"
421 " int _i;\n"
422 "}\n"
423 "+ (id)init {\n}\n"
424 "@end");
425
426 verifyFormat("@implementation Foo : Bar {\n"
427 " int _i;\n"
428 "}\n"
429 "+ (id)init {\n}\n"
430 "@end");
431
432 verifyFormat("@implementation Foo (HackStuff)\n"
433 "+ (id)init {\n}\n"
434 "@end");
435 verifyFormat("@implementation ObjcClass\n"
436 "- (void)method;\n"
437 "{}\n"
438 "@end");
439
440 Style = getGoogleStyle(FormatStyle::LK_ObjC);
441 verifyFormat("@implementation Foo : NSObject {\n"
442 " @public\n"
443 " int field1;\n"
444 " @protected\n"
445 " int field2;\n"
446 " @private\n"
447 " int field3;\n"
448 " @package\n"
449 " int field4;\n"
450 "}\n"
451 "+ (id)init {\n}\n"
452 "@end");
453}
454
455TEST_F(FormatTestObjC, FormatObjCProtocol) {
456 verifyFormat("@protocol Foo\n"
457 "@property(weak) id delegate;\n"
458 "- (NSUInteger)numberOfThings;\n"
459 "@end");
460
461 verifyFormat("@protocol MyProtocol <NSObject>\n"
462 "- (NSUInteger)numberOfThings;\n"
463 "@end");
464
465 verifyFormat("@protocol Foo;\n"
466 "@protocol Bar;\n");
467
468 verifyFormat("@protocol Foo\n"
469 "@end\n"
470 "@protocol Bar\n"
471 "@end");
472
Nico Weberc068ff72018-01-23 17:10:25 +0000473 verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n"
474 "@property(assign, readwrite) NSInteger bar;\n"
475 "@end");
476
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000477 verifyFormat("@protocol myProtocol\n"
478 "- (void)mandatoryWithInt:(int)i;\n"
479 "@optional\n"
480 "- (void)optional;\n"
481 "@required\n"
482 "- (void)required;\n"
483 "@optional\n"
484 "@property(assign) int madProp;\n"
485 "@end\n");
486
487 verifyFormat("@property(nonatomic, assign, readonly)\n"
488 " int *looooooooooooooooooooooooooooongNumber;\n"
489 "@property(nonatomic, assign, readonly)\n"
490 " NSString *looooooooooooooooooooooooooooongName;");
491
492 verifyFormat("@implementation PR18406\n"
493 "}\n"
494 "@end");
495
496 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Ben Hamiltonf84f1182018-01-18 18:37:16 +0000497 verifyFormat("@protocol MyProtocol <NSObject>\n"
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000498 "- (NSUInteger)numberOfThings;\n"
499 "@end");
500}
501
502TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
503 verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
504 " rect:(NSRect)theRect\n"
505 " interval:(float)theInterval {\n"
506 "}");
507 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
508 " longKeyword:(NSRect)theRect\n"
509 " longerKeyword:(float)theInterval\n"
510 " error:(NSError **)theError {\n"
511 "}");
512 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
513 " longKeyword:(NSRect)theRect\n"
514 " evenLongerKeyword:(float)theInterval\n"
515 " error:(NSError **)theError {\n"
516 "}");
517 Style.ColumnLimit = 60;
518 verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
519 " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
520 " NS_DESIGNATED_INITIALIZER;");
521 verifyFormat("- (void)drawRectOn:(id)surface\n"
522 " ofSize:(size_t)height\n"
523 " :(size_t)width;");
524
525 // Continuation indent width should win over aligning colons if the function
526 // name is long.
Ben Hamilton5b0c3ad2017-12-14 21:44:11 +0000527 Style = getGoogleStyle(FormatStyle::LK_ObjC);
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000528 Style.ColumnLimit = 40;
529 Style.IndentWrappedFunctionNames = true;
530 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
531 " dontAlignNamef:(NSRect)theRect {\n"
532 "}");
533
534 // Make sure we don't break aligning for short parameter names.
535 verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
536 " aShortf:(NSRect)theRect {\n"
537 "}");
Ben Hamilton5b0c3ad2017-12-14 21:44:11 +0000538
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000539 // Format pairs correctly.
540 Style.ColumnLimit = 80;
541 verifyFormat("- (void)drawRectOn:(id)surface\n"
542 " ofSize:(aaaaaaaa)height\n"
543 " :(size_t)width\n"
544 " atOrigin:(size_t)x\n"
545 " :(size_t)y\n"
546 " aaaaa:(a)yyy\n"
547 " bbb:(d)cccc;");
548 verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
549}
550
551TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
552 verifyFormat("[foo bar:baz];");
553 verifyFormat("return [foo bar:baz];");
554 verifyFormat("return (a)[foo bar:baz];");
555 verifyFormat("f([foo bar:baz]);");
556 verifyFormat("f(2, [foo bar:baz]);");
557 verifyFormat("f(2, a ? b : c);");
558 verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
559
560 // Unary operators.
561 verifyFormat("int a = +[foo bar:baz];");
562 verifyFormat("int a = -[foo bar:baz];");
563 verifyFormat("int a = ![foo bar:baz];");
564 verifyFormat("int a = ~[foo bar:baz];");
565 verifyFormat("int a = ++[foo bar:baz];");
566 verifyFormat("int a = --[foo bar:baz];");
567 verifyFormat("int a = sizeof [foo bar:baz];");
568 verifyFormat("int a = alignof [foo bar:baz];");
569 verifyFormat("int a = &[foo bar:baz];");
570 verifyFormat("int a = *[foo bar:baz];");
571 // FIXME: Make casts work, without breaking f()[4].
572 // verifyFormat("int a = (int)[foo bar:baz];");
573 // verifyFormat("return (int)[foo bar:baz];");
574 // verifyFormat("(void)[foo bar:baz];");
575 verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
576
577 // Binary operators.
578 verifyFormat("[foo bar:baz], [foo bar:baz];");
579 verifyFormat("[foo bar:baz] = [foo bar:baz];");
580 verifyFormat("[foo bar:baz] *= [foo bar:baz];");
581 verifyFormat("[foo bar:baz] /= [foo bar:baz];");
582 verifyFormat("[foo bar:baz] %= [foo bar:baz];");
583 verifyFormat("[foo bar:baz] += [foo bar:baz];");
584 verifyFormat("[foo bar:baz] -= [foo bar:baz];");
585 verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
586 verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
587 verifyFormat("[foo bar:baz] &= [foo bar:baz];");
588 verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
589 verifyFormat("[foo bar:baz] |= [foo bar:baz];");
590 verifyFormat("[foo bar:baz] ? [foo bar:baz] : [foo bar:baz];");
591 verifyFormat("[foo bar:baz] || [foo bar:baz];");
592 verifyFormat("[foo bar:baz] && [foo bar:baz];");
593 verifyFormat("[foo bar:baz] | [foo bar:baz];");
594 verifyFormat("[foo bar:baz] ^ [foo bar:baz];");
595 verifyFormat("[foo bar:baz] & [foo bar:baz];");
596 verifyFormat("[foo bar:baz] == [foo bar:baz];");
597 verifyFormat("[foo bar:baz] != [foo bar:baz];");
598 verifyFormat("[foo bar:baz] >= [foo bar:baz];");
599 verifyFormat("[foo bar:baz] <= [foo bar:baz];");
600 verifyFormat("[foo bar:baz] > [foo bar:baz];");
601 verifyFormat("[foo bar:baz] < [foo bar:baz];");
602 verifyFormat("[foo bar:baz] >> [foo bar:baz];");
603 verifyFormat("[foo bar:baz] << [foo bar:baz];");
604 verifyFormat("[foo bar:baz] - [foo bar:baz];");
605 verifyFormat("[foo bar:baz] + [foo bar:baz];");
606 verifyFormat("[foo bar:baz] * [foo bar:baz];");
607 verifyFormat("[foo bar:baz] / [foo bar:baz];");
608 verifyFormat("[foo bar:baz] % [foo bar:baz];");
609 // Whew!
610
611 verifyFormat("return in[42];");
612 verifyFormat("for (auto v : in[1]) {\n}");
613 verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
614 verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
615 verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
616 verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
617 verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
618 verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
619 "}");
620 verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
Ben Hamiltonad991862018-03-20 14:53:25 +0000621 verifyFormat("[self aaaaa:MACRO(a, b:c:, d:e:)];");
622 verifyFormat("[self aaaaa:MACRO(a, b:c:d:, e:f:g:)];");
623 verifyFormat("int XYMyFoo(int a, int b) NS_SWIFT_NAME(foo(self:scale:));");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000624 verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
625 verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
626
627 verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
628 verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
629 verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
630 verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
631 verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
632 verifyFormat("[button setAction:@selector(zoomOut:)];");
633 verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
634
635 verifyFormat("arr[[self indexForFoo:a]];");
636 verifyFormat("throw [self errorFor:a];");
637 verifyFormat("@throw [self errorFor:a];");
638
639 verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
640 verifyFormat("[(id)foo bar:(id) ? baz : quux];");
641 verifyFormat("4 > 4 ? (id)a : (id)baz;");
642
643 // This tests that the formatter doesn't break after "backing" but before ":",
644 // which would be at 80 columns.
645 verifyFormat(
646 "void f() {\n"
647 " if ((self = [super initWithContentRect:contentRect\n"
648 " styleMask:styleMask ?: otherMask\n"
649 " backing:NSBackingStoreBuffered\n"
650 " defer:YES]))");
651
652 verifyFormat(
653 "[foo checkThatBreakingAfterColonWorksOk:\n"
654 " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
655
656 verifyFormat("[myObj short:arg1 // Force line break\n"
657 " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
658 " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
659 " error:arg4];");
660 verifyFormat(
661 "void f() {\n"
662 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
663 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
664 " pos.width(), pos.height())\n"
665 " styleMask:NSBorderlessWindowMask\n"
666 " backing:NSBackingStoreBuffered\n"
667 " defer:NO]);\n"
668 "}");
669 verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
670 " with:contentsNativeView];");
671
672 verifyFormat(
673 "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
674 " owner:nillllll];");
675
676 verifyFormat(
677 "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
678 " forType:kBookmarkButtonDragType];");
679
680 verifyFormat("[defaultCenter addObserver:self\n"
681 " selector:@selector(willEnterFullscreen)\n"
682 " name:kWillEnterFullscreenNotification\n"
683 " object:nil];");
684 verifyFormat("[image_rep drawInRect:drawRect\n"
685 " fromRect:NSZeroRect\n"
686 " operation:NSCompositeCopy\n"
687 " fraction:1.0\n"
688 " respectFlipped:NO\n"
689 " hints:nil];");
690 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
691 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
692 verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
693 " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
694 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
695 " aaaaaaaaaaaaaaaaaaaaaa];");
696
697 verifyFormat(
698 "scoped_nsobject<NSTextField> message(\n"
699 " // The frame will be fixed up when |-setMessageText:| is called.\n"
700 " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
701 verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
702 " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
703 " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
704 " aaaa:bbb];");
705 verifyFormat("[self param:function( //\n"
706 " parameter)]");
707 verifyFormat(
708 "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
709 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
710 " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
711
712 // Variadic parameters.
713 verifyFormat(
714 "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
715 verifyFormat(
716 "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
717 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
718 " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
719 verifyFormat("[self // break\n"
720 " a:a\n"
721 " aaa:aaa];");
722 verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
723 " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
724
725 // Formats pair-parameters.
726 verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
727 verifyFormat("[I drawRectOn:surface //\n"
Francois Ferrand38d80132018-02-09 15:41:56 +0000728 " ofSize:aa:bbb\n"
729 " atOrigin:cc:dd];");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000730
Jacek Olesiakfb7f5c02018-02-07 10:35:08 +0000731 // Inline block as a first argument.
732 verifyFormat("[object justBlock:^{\n"
733 " a = 42;\n"
734 "}];");
735 verifyFormat("[object\n"
736 " justBlock:^{\n"
737 " a = 42;\n"
738 " }\n"
739 " notBlock:42\n"
740 " a:42];");
741 verifyFormat("[object\n"
742 " firstBlock:^{\n"
743 " a = 42;\n"
744 " }\n"
745 " blockWithLongerName:^{\n"
746 " a = 42;\n"
747 " }];");
748 verifyFormat("[object\n"
749 " blockWithLongerName:^{\n"
750 " a = 42;\n"
751 " }\n"
752 " secondBlock:^{\n"
753 " a = 42;\n"
754 " }];");
755 verifyFormat("[object\n"
756 " firstBlock:^{\n"
757 " a = 42;\n"
758 " }\n"
759 " notBlock:42\n"
760 " secondBlock:^{\n"
761 " a = 42;\n"
762 " }];");
763
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000764 Style.ColumnLimit = 70;
765 verifyFormat(
766 "void f() {\n"
767 " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
768 " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
769 " pos.width(), pos.height())\n"
770 " syeMask:NSBorderlessWindowMask\n"
771 " bking:NSBackingStoreBuffered\n"
772 " der:NO]);\n"
773 "}");
774
775 Style.ColumnLimit = 60;
776 verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
777 " .aaaaaaaa];"); // FIXME: Indentation seems off.
778 // FIXME: This violates the column limit.
779 verifyFormat(
780 "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
781 " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
782 " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
783
784 Style = getChromiumStyle(FormatStyle::LK_ObjC);
785 Style.ColumnLimit = 80;
786 verifyFormat(
787 "void f() {\n"
788 " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
789 " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
790 " pos.width(), pos.height())\n"
791 " styleMask:NSBorderlessWindowMask\n"
792 " backing:NSBackingStoreBuffered\n"
793 " defer:NO]);\n"
794 "}");
Francois Ferrand38d80132018-02-09 15:41:56 +0000795
796 // Respect continuation indent and colon alignment (e.g. when object name is
797 // short, and first selector is the longest one)
798 Style = getLLVMStyle();
799 Style.Language = FormatStyle::LK_ObjC;
800 Style.ContinuationIndentWidth = 8;
801 verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
802 " withObject:nil\n"
803 " waitUntilDone:false];");
804 verifyFormat("[self performSelector:@selector(loadAccessories)\n"
805 " withObjectOnMainThread:nil\n"
806 " waitUntilDone:false];");
807 verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
808 " performSelectorOnMainThread:@selector(loadAccessories)\n"
809 " withObject:nil\n"
810 " waitUntilDone:false];");
811 verifyFormat("[self // force wrapping\n"
812 " performSelectorOnMainThread:@selector(loadAccessories)\n"
813 " withObject:nil\n"
814 " waitUntilDone:false];");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000815}
816
817TEST_F(FormatTestObjC, ObjCAt) {
818 verifyFormat("@autoreleasepool");
819 verifyFormat("@catch");
820 verifyFormat("@class");
821 verifyFormat("@compatibility_alias");
822 verifyFormat("@defs");
823 verifyFormat("@dynamic");
824 verifyFormat("@encode");
825 verifyFormat("@end");
826 verifyFormat("@finally");
827 verifyFormat("@implementation");
828 verifyFormat("@import");
829 verifyFormat("@interface");
830 verifyFormat("@optional");
831 verifyFormat("@package");
832 verifyFormat("@private");
833 verifyFormat("@property");
834 verifyFormat("@protected");
835 verifyFormat("@protocol");
836 verifyFormat("@public");
837 verifyFormat("@required");
838 verifyFormat("@selector");
839 verifyFormat("@synchronized");
840 verifyFormat("@synthesize");
841 verifyFormat("@throw");
842 verifyFormat("@try");
843
844 EXPECT_EQ("@interface", format("@ interface"));
845
846 // The precise formatting of this doesn't matter, nobody writes code like
847 // this.
848 verifyFormat("@ /*foo*/ interface");
849}
850
Ben Hamilton788a2222018-03-12 15:42:40 +0000851TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) {
852 verifyFormat("void DoStuffWithBlockType(int (^)(char));");
853 verifyFormat("int (^foo)(char, float);");
854 verifyFormat("int (^foo[10])(char, float);");
855 verifyFormat("int (^foo[kNumEntries])(char, float);");
856 verifyFormat("int (^foo[kNumEntries + 10])(char, float);");
857 verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);");
858}
859
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000860TEST_F(FormatTestObjC, ObjCSnippets) {
861 verifyFormat("@autoreleasepool {\n"
862 " foo();\n"
863 "}");
864 verifyFormat("@class Foo, Bar;");
865 verifyFormat("@compatibility_alias AliasName ExistingClass;");
866 verifyFormat("@dynamic textColor;");
867 verifyFormat("char *buf1 = @encode(int *);");
868 verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
869 verifyFormat("char *buf1 = @encode(int **);");
870 verifyFormat("Protocol *proto = @protocol(p1);");
871 verifyFormat("SEL s = @selector(foo:);");
872 verifyFormat("@synchronized(self) {\n"
873 " f();\n"
874 "}");
875
876 verifyFormat("@import foo.bar;\n"
877 "@import baz;");
878
879 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
880
881 verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
882 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
883
884 Style = getMozillaStyle();
885 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
886 verifyFormat("@property BOOL editable;");
887
888 Style = getWebKitStyle();
889 verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
890 verifyFormat("@property BOOL editable;");
891
892 Style = getGoogleStyle(FormatStyle::LK_ObjC);
893 verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
894 verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
895}
896
897TEST_F(FormatTestObjC, ObjCForIn) {
898 verifyFormat("- (void)test {\n"
899 " for (NSString *n in arrayOfStrings) {\n"
900 " foo(n);\n"
901 " }\n"
902 "}");
903 verifyFormat("- (void)test {\n"
904 " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
905 " foo(n);\n"
906 " }\n"
907 "}");
Ben Hamilton1d6c6ee2018-03-06 17:21:42 +0000908 verifyFormat("for (Foo *x in bar) {\n}");
909 verifyFormat("for (Foo *x in [bar baz]) {\n}");
910 verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
911 verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
912 verifyFormat("for (Foo *x in [bar baz:^{\n"
913 " [uh oh];\n"
914 " }]) {\n}");
Daniel Jasper03a04fe2016-12-12 12:42:29 +0000915}
916
917TEST_F(FormatTestObjC, ObjCLiterals) {
918 verifyFormat("@\"String\"");
919 verifyFormat("@1");
920 verifyFormat("@+4.8");
921 verifyFormat("@-4");
922 verifyFormat("@1LL");
923 verifyFormat("@.5");
924 verifyFormat("@'c'");
925 verifyFormat("@true");
926
927 verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
928 verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
929 verifyFormat("NSNumber *favoriteColor = @(Green);");
930 verifyFormat("NSString *path = @(getenv(\"PATH\"));");
931
932 verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
933}
934
935TEST_F(FormatTestObjC, ObjCDictLiterals) {
936 verifyFormat("@{");
937 verifyFormat("@{}");
938 verifyFormat("@{@\"one\" : @1}");
939 verifyFormat("return @{@\"one\" : @1;");
940 verifyFormat("@{@\"one\" : @1}");
941
942 verifyFormat("@{@\"one\" : @{@2 : @1}}");
943 verifyFormat("@{\n"
944 " @\"one\" : @{@2 : @1},\n"
945 "}");
946
947 verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
948 verifyIncompleteFormat("[self setDict:@{}");
949 verifyIncompleteFormat("[self setDict:@{@1 : @2}");
950 verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
951 verifyFormat(
952 "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
953 verifyFormat(
954 "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
955
956 verifyFormat("NSDictionary *d = @{\n"
957 " @\"nam\" : NSUserNam(),\n"
958 " @\"dte\" : [NSDate date],\n"
959 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
960 "};");
961 verifyFormat(
962 "@{\n"
963 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
964 "regularFont,\n"
965 "};");
966 verifyFormat(
967 "@{\n"
968 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
969 " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
970 "};");
971
972 // We should try to be robust in case someone forgets the "@".
973 verifyFormat("NSDictionary *d = {\n"
974 " @\"nam\" : NSUserNam(),\n"
975 " @\"dte\" : [NSDate date],\n"
976 " @\"processInfo\" : [NSProcessInfo processInfo]\n"
977 "};");
978 verifyFormat("NSMutableDictionary *dictionary =\n"
979 " [NSMutableDictionary dictionaryWithDictionary:@{\n"
980 " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
981 " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
982 " cccccccccccccccc : ccccccccccccccc\n"
983 " }];");
984
985 // Ensure that casts before the key are kept on the same line as the key.
986 verifyFormat(
987 "NSDictionary *d = @{\n"
988 " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
989 " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
990 "};");
991
992 Style = getGoogleStyle(FormatStyle::LK_ObjC);
993 verifyFormat(
994 "@{\n"
995 " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
996 "regularFont,\n"
997 "};");
998}
999
1000TEST_F(FormatTestObjC, ObjCArrayLiterals) {
1001 verifyIncompleteFormat("@[");
1002 verifyFormat("@[]");
1003 verifyFormat(
1004 "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
1005 verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
1006 verifyFormat("NSArray *array = @[ [foo description] ];");
1007
1008 verifyFormat(
1009 "NSArray *some_variable = @[\n"
1010 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1011 " @\"aaaaaaaaaaaaaaaaa\",\n"
1012 " @\"aaaaaaaaaaaaaaaaa\",\n"
1013 " @\"aaaaaaaaaaaaaaaaa\",\n"
1014 "];");
1015 verifyFormat(
1016 "NSArray *some_variable = @[\n"
1017 " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
1018 " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
1019 "];");
1020 verifyFormat("NSArray *some_variable = @[\n"
1021 " @\"aaaaaaaaaaaaaaaaa\",\n"
1022 " @\"aaaaaaaaaaaaaaaaa\",\n"
1023 " @\"aaaaaaaaaaaaaaaaa\",\n"
1024 " @\"aaaaaaaaaaaaaaaaa\",\n"
1025 "];");
1026 verifyFormat("NSArray *array = @[\n"
1027 " @\"a\",\n"
1028 " @\"a\",\n" // Trailing comma -> one per line.
1029 "];");
1030
1031 // We should try to be robust in case someone forgets the "@".
1032 verifyFormat("NSArray *some_variable = [\n"
1033 " @\"aaaaaaaaaaaaaaaaa\",\n"
1034 " @\"aaaaaaaaaaaaaaaaa\",\n"
1035 " @\"aaaaaaaaaaaaaaaaa\",\n"
1036 " @\"aaaaaaaaaaaaaaaaa\",\n"
1037 "];");
1038 verifyFormat(
1039 "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
1040 " index:(NSUInteger)index\n"
1041 " nonDigitAttributes:\n"
1042 " (NSDictionary *)noDigitAttributes;");
1043 verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
1044 " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
1045 "]];");
Ben Hamilton09051f22018-02-08 16:07:25 +00001046 Style.ColumnLimit = 20;
1047 // We can't break string literals inside NSArray literals
1048 // (that raises -Wobjc-string-concatenation).
1049 verifyFormat("NSArray *foo = @[\n"
1050 " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
1051 "];\n");
Daniel Jasper03a04fe2016-12-12 12:42:29 +00001052}
1053} // end namespace
1054} // end namespace format
1055} // end namespace clang