FormatTestObjC.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  1. //===- 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. #include "clang/Format/Format.h"
  10. #include "../Tooling/ReplacementTest.h"
  11. #include "FormatTestUtils.h"
  12. #include "clang/Frontend/TextDiagnosticPrinter.h"
  13. #include "llvm/Support/Debug.h"
  14. #include "llvm/Support/MemoryBuffer.h"
  15. #include "gtest/gtest.h"
  16. #define DEBUG_TYPE "format-test"
  17. using clang::tooling::ReplacementTest;
  18. namespace clang {
  19. namespace format {
  20. namespace {
  21. class FormatTestObjC : public ::testing::Test {
  22. protected:
  23. FormatTestObjC() {
  24. Style = getLLVMStyle();
  25. Style.Language = FormatStyle::LK_ObjC;
  26. }
  27. enum StatusCheck {
  28. SC_ExpectComplete,
  29. SC_ExpectIncomplete,
  30. SC_DoNotCheck
  31. };
  32. std::string format(llvm::StringRef Code,
  33. StatusCheck CheckComplete = SC_ExpectComplete) {
  34. DEBUG(llvm::errs() << "---\n");
  35. DEBUG(llvm::errs() << Code << "\n\n");
  36. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  37. FormattingAttemptStatus Status;
  38. tooling::Replacements Replaces =
  39. reformat(Style, Code, Ranges, "<stdin>", &Status);
  40. if (CheckComplete != SC_DoNotCheck) {
  41. bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
  42. EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
  43. << Code << "\n\n";
  44. }
  45. auto Result = applyAllReplacements(Code, Replaces);
  46. EXPECT_TRUE(static_cast<bool>(Result));
  47. DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
  48. return *Result;
  49. }
  50. void verifyFormat(StringRef Code) {
  51. EXPECT_EQ(Code.str(), format(Code)) << "Expected code is not stable";
  52. EXPECT_EQ(Code.str(), format(test::messUp(Code)));
  53. }
  54. void verifyIncompleteFormat(StringRef Code) {
  55. EXPECT_EQ(Code.str(), format(test::messUp(Code), SC_ExpectIncomplete));
  56. }
  57. FormatStyle Style;
  58. };
  59. TEST(FormatTestObjCStyle, DetectsObjCInHeaders) {
  60. auto Style = getStyle("LLVM", "a.h", "none", "@interface\n"
  61. "- (id)init;");
  62. ASSERT_TRUE((bool)Style);
  63. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  64. Style = getStyle("LLVM", "a.h", "none", "@interface\n"
  65. "+ (id)init;");
  66. ASSERT_TRUE((bool)Style);
  67. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  68. Style = getStyle("LLVM", "a.h", "none", "@interface\n"
  69. "@end\n"
  70. "//comment");
  71. ASSERT_TRUE((bool)Style);
  72. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  73. Style = getStyle("LLVM", "a.h", "none", "@interface\n"
  74. "@end //comment");
  75. ASSERT_TRUE((bool)Style);
  76. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  77. // No recognizable ObjC.
  78. Style = getStyle("LLVM", "a.h", "none", "void f() {}");
  79. ASSERT_TRUE((bool)Style);
  80. EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
  81. Style = getStyle("{}", "a.h", "none", "@interface Foo\n@end\n");
  82. ASSERT_TRUE((bool)Style);
  83. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  84. Style = getStyle("{}", "a.h", "none",
  85. "const int interface = 1;\nconst int end = 2;\n");
  86. ASSERT_TRUE((bool)Style);
  87. EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
  88. Style = getStyle("{}", "a.h", "none", "@protocol Foo\n@end\n");
  89. ASSERT_TRUE((bool)Style);
  90. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  91. Style = getStyle("{}", "a.h", "none",
  92. "const int protocol = 1;\nconst int end = 2;\n");
  93. ASSERT_TRUE((bool)Style);
  94. EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
  95. Style =
  96. getStyle("{}", "a.h", "none", "typedef NS_ENUM(NSInteger, Foo) {};\n");
  97. ASSERT_TRUE((bool)Style);
  98. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  99. Style = getStyle("{}", "a.h", "none", "enum Foo {};");
  100. ASSERT_TRUE((bool)Style);
  101. EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
  102. Style =
  103. getStyle("{}", "a.h", "none", "inline void Foo() { Log(@\"Foo\"); }\n");
  104. ASSERT_TRUE((bool)Style);
  105. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  106. Style =
  107. getStyle("{}", "a.h", "none", "inline void Foo() { Log(\"Foo\"); }\n");
  108. ASSERT_TRUE((bool)Style);
  109. EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
  110. Style =
  111. getStyle("{}", "a.h", "none", "inline void Foo() { id = @[1, 2, 3]; }\n");
  112. ASSERT_TRUE((bool)Style);
  113. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  114. Style = getStyle("{}", "a.h", "none",
  115. "inline void Foo() { id foo = @{1: 2, 3: 4, 5: 6}; }\n");
  116. ASSERT_TRUE((bool)Style);
  117. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  118. Style = getStyle("{}", "a.h", "none",
  119. "inline void Foo() { int foo[] = {1, 2, 3}; }\n");
  120. ASSERT_TRUE((bool)Style);
  121. EXPECT_EQ(FormatStyle::LK_Cpp, Style->Language);
  122. // ObjC characteristic types.
  123. Style = getStyle("{}", "a.h", "none", "extern NSString *kFoo;\n");
  124. ASSERT_TRUE((bool)Style);
  125. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  126. Style = getStyle("{}", "a.h", "none", "extern NSInteger Foo();\n");
  127. ASSERT_TRUE((bool)Style);
  128. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  129. Style = getStyle("{}", "a.h", "none", "NSObject *Foo();\n");
  130. ASSERT_TRUE((bool)Style);
  131. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  132. Style = getStyle("{}", "a.h", "none", "NSSet *Foo();\n");
  133. ASSERT_TRUE((bool)Style);
  134. EXPECT_EQ(FormatStyle::LK_ObjC, Style->Language);
  135. }
  136. TEST_F(FormatTestObjC, FormatObjCTryCatch) {
  137. verifyFormat("@try {\n"
  138. " f();\n"
  139. "} @catch (NSException e) {\n"
  140. " @throw;\n"
  141. "} @finally {\n"
  142. " exit(42);\n"
  143. "}");
  144. verifyFormat("DEBUG({\n"
  145. " @try {\n"
  146. " } @finally {\n"
  147. " }\n"
  148. "});\n");
  149. }
  150. TEST_F(FormatTestObjC, FormatObjCAutoreleasepool) {
  151. verifyFormat("@autoreleasepool {\n"
  152. " f();\n"
  153. "}\n"
  154. "@autoreleasepool {\n"
  155. " f();\n"
  156. "}\n");
  157. Style.BreakBeforeBraces = FormatStyle::BS_Custom;
  158. Style.BraceWrapping.AfterControlStatement = true;
  159. verifyFormat("@autoreleasepool\n"
  160. "{\n"
  161. " f();\n"
  162. "}\n"
  163. "@autoreleasepool\n"
  164. "{\n"
  165. " f();\n"
  166. "}\n");
  167. }
  168. TEST_F(FormatTestObjC, FormatObjCGenerics) {
  169. Style.ColumnLimit = 40;
  170. verifyFormat("int aaaaaaaaaaaaaaaa(\n"
  171. " NSArray<aaaaaaaaaaaaaaaaaa *>\n"
  172. " aaaaaaaaaaaaaaaaa);\n");
  173. verifyFormat("int aaaaaaaaaaaaaaaa(\n"
  174. " NSArray<aaaaaaaaaaaaaaaaaaa<\n"
  175. " aaaaaaaaaaaaaaaa *> *>\n"
  176. " aaaaaaaaaaaaaaaaa);\n");
  177. }
  178. TEST_F(FormatTestObjC, FormatObjCSynchronized) {
  179. verifyFormat("@synchronized(self) {\n"
  180. " f();\n"
  181. "}\n"
  182. "@synchronized(self) {\n"
  183. " f();\n"
  184. "}\n");
  185. Style.BreakBeforeBraces = FormatStyle::BS_Custom;
  186. Style.BraceWrapping.AfterControlStatement = true;
  187. verifyFormat("@synchronized(self)\n"
  188. "{\n"
  189. " f();\n"
  190. "}\n"
  191. "@synchronized(self)\n"
  192. "{\n"
  193. " f();\n"
  194. "}\n");
  195. }
  196. TEST_F(FormatTestObjC, FormatObjCInterface) {
  197. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  198. "@public\n"
  199. " int field1;\n"
  200. "@protected\n"
  201. " int field2;\n"
  202. "@private\n"
  203. " int field3;\n"
  204. "@package\n"
  205. " int field4;\n"
  206. "}\n"
  207. "+ (id)init;\n"
  208. "@end");
  209. verifyFormat("@interface /* wait for it */ Foo\n"
  210. "+ (id)init;\n"
  211. "// Look, a comment!\n"
  212. "- (int)answerWith:(int)i;\n"
  213. "@end");
  214. verifyFormat("@interface Foo\n"
  215. "@end\n"
  216. "@interface Bar\n"
  217. "@end");
  218. verifyFormat("@interface Foo : Bar\n"
  219. "@property(assign, readwrite) NSInteger bar;\n"
  220. "+ (id)init;\n"
  221. "@end");
  222. verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @interface Foo : Bar\n"
  223. "@property(assign, readwrite) NSInteger bar;\n"
  224. "+ (id)init;\n"
  225. "@end");
  226. verifyFormat("@interface Foo : /**/ Bar /**/ <Baz, /**/ Quux>\n"
  227. "+ (id)init;\n"
  228. "@end");
  229. verifyFormat("@interface Foo (HackStuff)\n"
  230. "+ (id)init;\n"
  231. "@end");
  232. verifyFormat("@interface Foo ()\n"
  233. "+ (id)init;\n"
  234. "@end");
  235. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  236. "+ (id)init;\n"
  237. "@end");
  238. verifyFormat("@interface Foo {\n"
  239. " int _i;\n"
  240. "}\n"
  241. "+ (id)init;\n"
  242. "@end");
  243. verifyFormat("@interface Foo : Bar {\n"
  244. " int _i;\n"
  245. "}\n"
  246. "+ (id)init;\n"
  247. "@end");
  248. verifyFormat("@interface Foo : Bar <Baz, Quux> {\n"
  249. " int _i;\n"
  250. "}\n"
  251. "+ (id)init;\n"
  252. "@end");
  253. verifyFormat("@interface Foo (HackStuff) {\n"
  254. " int _i;\n"
  255. "}\n"
  256. "+ (id)init;\n"
  257. "@end");
  258. verifyFormat("@interface Foo () {\n"
  259. " int _i;\n"
  260. "}\n"
  261. "+ (id)init;\n"
  262. "@end");
  263. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  264. " int _i;\n"
  265. "}\n"
  266. "+ (id)init;\n"
  267. "@end");
  268. Style.ColumnLimit = 40;
  269. verifyFormat("@interface ccccccccccccc () <\n"
  270. " ccccccccccccc, ccccccccccccc,\n"
  271. " ccccccccccccc, ccccccccccccc> {\n"
  272. "}");
  273. Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
  274. verifyFormat("@interface ddddddddddddd () <\n"
  275. " ddddddddddddd,\n"
  276. " ddddddddddddd,\n"
  277. " ddddddddddddd,\n"
  278. " ddddddddddddd> {\n"
  279. "}");
  280. Style.BinPackParameters = false;
  281. Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
  282. verifyFormat("@interface eeeeeeeeeeeee () <\n"
  283. " eeeeeeeeeeeee,\n"
  284. " eeeeeeeeeeeee,\n"
  285. " eeeeeeeeeeeee,\n"
  286. " eeeeeeeeeeeee> {\n"
  287. "}");
  288. Style.ObjCBinPackProtocolList = FormatStyle::BPS_Always;
  289. verifyFormat("@interface fffffffffffff () <\n"
  290. " fffffffffffff, fffffffffffff,\n"
  291. " fffffffffffff, fffffffffffff> {\n"
  292. "}");
  293. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  294. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  295. " @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");
  306. verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
  307. "+ (id)init;\n"
  308. "@end");
  309. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  310. "+ (id)init;\n"
  311. "@end");
  312. Style.ColumnLimit = 40;
  313. // BinPackParameters should be true by default.
  314. verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n"
  315. " int eeeee, int eeeee);\n");
  316. // ObjCBinPackProtocolList should be BPS_Never by default.
  317. verifyFormat("@interface fffffffffffff () <\n"
  318. " fffffffffffff,\n"
  319. " fffffffffffff,\n"
  320. " fffffffffffff,\n"
  321. " fffffffffffff> {\n"
  322. "}");
  323. }
  324. TEST_F(FormatTestObjC, FormatObjCImplementation) {
  325. verifyFormat("@implementation Foo : NSObject {\n"
  326. "@public\n"
  327. " int field1;\n"
  328. "@protected\n"
  329. " int field2;\n"
  330. "@private\n"
  331. " int field3;\n"
  332. "@package\n"
  333. " int field4;\n"
  334. "}\n"
  335. "+ (id)init {\n}\n"
  336. "@end");
  337. verifyFormat("@implementation Foo\n"
  338. "+ (id)init {\n"
  339. " if (true)\n"
  340. " return nil;\n"
  341. "}\n"
  342. "// Look, a comment!\n"
  343. "- (int)answerWith:(int)i {\n"
  344. " return i;\n"
  345. "}\n"
  346. "+ (int)answerWith:(int)i {\n"
  347. " return i;\n"
  348. "}\n"
  349. "@end");
  350. verifyFormat("@implementation Foo\n"
  351. "@end\n"
  352. "@implementation Bar\n"
  353. "@end");
  354. EXPECT_EQ("@implementation Foo : Bar\n"
  355. "+ (id)init {\n}\n"
  356. "- (void)foo {\n}\n"
  357. "@end",
  358. format("@implementation Foo : Bar\n"
  359. "+(id)init{}\n"
  360. "-(void)foo{}\n"
  361. "@end"));
  362. verifyFormat("@implementation Foo {\n"
  363. " int _i;\n"
  364. "}\n"
  365. "+ (id)init {\n}\n"
  366. "@end");
  367. verifyFormat("@implementation Foo : Bar {\n"
  368. " int _i;\n"
  369. "}\n"
  370. "+ (id)init {\n}\n"
  371. "@end");
  372. verifyFormat("@implementation Foo (HackStuff)\n"
  373. "+ (id)init {\n}\n"
  374. "@end");
  375. verifyFormat("@implementation ObjcClass\n"
  376. "- (void)method;\n"
  377. "{}\n"
  378. "@end");
  379. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  380. verifyFormat("@implementation Foo : NSObject {\n"
  381. " @public\n"
  382. " int field1;\n"
  383. " @protected\n"
  384. " int field2;\n"
  385. " @private\n"
  386. " int field3;\n"
  387. " @package\n"
  388. " int field4;\n"
  389. "}\n"
  390. "+ (id)init {\n}\n"
  391. "@end");
  392. }
  393. TEST_F(FormatTestObjC, FormatObjCProtocol) {
  394. verifyFormat("@protocol Foo\n"
  395. "@property(weak) id delegate;\n"
  396. "- (NSUInteger)numberOfThings;\n"
  397. "@end");
  398. verifyFormat("@protocol MyProtocol <NSObject>\n"
  399. "- (NSUInteger)numberOfThings;\n"
  400. "@end");
  401. verifyFormat("@protocol Foo;\n"
  402. "@protocol Bar;\n");
  403. verifyFormat("@protocol Foo\n"
  404. "@end\n"
  405. "@protocol Bar\n"
  406. "@end");
  407. verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n"
  408. "@property(assign, readwrite) NSInteger bar;\n"
  409. "@end");
  410. verifyFormat("@protocol myProtocol\n"
  411. "- (void)mandatoryWithInt:(int)i;\n"
  412. "@optional\n"
  413. "- (void)optional;\n"
  414. "@required\n"
  415. "- (void)required;\n"
  416. "@optional\n"
  417. "@property(assign) int madProp;\n"
  418. "@end\n");
  419. verifyFormat("@property(nonatomic, assign, readonly)\n"
  420. " int *looooooooooooooooooooooooooooongNumber;\n"
  421. "@property(nonatomic, assign, readonly)\n"
  422. " NSString *looooooooooooooooooooooooooooongName;");
  423. verifyFormat("@implementation PR18406\n"
  424. "}\n"
  425. "@end");
  426. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  427. verifyFormat("@protocol MyProtocol <NSObject>\n"
  428. "- (NSUInteger)numberOfThings;\n"
  429. "@end");
  430. }
  431. TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
  432. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  433. " rect:(NSRect)theRect\n"
  434. " interval:(float)theInterval {\n"
  435. "}");
  436. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  437. " longKeyword:(NSRect)theRect\n"
  438. " longerKeyword:(float)theInterval\n"
  439. " error:(NSError **)theError {\n"
  440. "}");
  441. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  442. " longKeyword:(NSRect)theRect\n"
  443. " evenLongerKeyword:(float)theInterval\n"
  444. " error:(NSError **)theError {\n"
  445. "}");
  446. verifyFormat("+ (instancetype)new;\n");
  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. // Continuation indent width should win over aligning colons if the function
  455. // name is long.
  456. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  457. Style.ColumnLimit = 40;
  458. Style.IndentWrappedFunctionNames = true;
  459. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  460. " dontAlignNamef:(NSRect)theRect {\n"
  461. "}");
  462. // Make sure we don't break aligning for short parameter names.
  463. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  464. " aShortf:(NSRect)theRect {\n"
  465. "}");
  466. // Make sure selectors with 0, 1, or more arguments are indented
  467. // when IndentWrappedFunctionNames is true.
  468. verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  469. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n");
  470. verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  471. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
  472. verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  473. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
  474. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
  475. verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  476. " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
  477. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
  478. verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  479. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
  480. " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
  481. // Format pairs correctly.
  482. Style.ColumnLimit = 80;
  483. verifyFormat("- (void)drawRectOn:(id)surface\n"
  484. " ofSize:(aaaaaaaa)height\n"
  485. " :(size_t)width\n"
  486. " atOrigin:(size_t)x\n"
  487. " :(size_t)y\n"
  488. " aaaaa:(a)yyy\n"
  489. " bbb:(d)cccc;");
  490. verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
  491. }
  492. TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
  493. verifyFormat("[foo bar:baz];");
  494. verifyFormat("return [foo bar:baz];");
  495. verifyFormat("return (a)[foo bar:baz];");
  496. verifyFormat("f([foo bar:baz]);");
  497. verifyFormat("f(2, [foo bar:baz]);");
  498. verifyFormat("f(2, a ? b : c);");
  499. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  500. // Unary operators.
  501. verifyFormat("int a = +[foo bar:baz];");
  502. verifyFormat("int a = -[foo bar:baz];");
  503. verifyFormat("int a = ![foo bar:baz];");
  504. verifyFormat("int a = ~[foo bar:baz];");
  505. verifyFormat("int a = ++[foo bar:baz];");
  506. verifyFormat("int a = --[foo bar:baz];");
  507. verifyFormat("int a = sizeof [foo bar:baz];");
  508. verifyFormat("int a = alignof [foo bar:baz];");
  509. verifyFormat("int a = &[foo bar:baz];");
  510. verifyFormat("int a = *[foo bar:baz];");
  511. // FIXME: Make casts work, without breaking f()[4].
  512. // verifyFormat("int a = (int)[foo bar:baz];");
  513. // verifyFormat("return (int)[foo bar:baz];");
  514. // verifyFormat("(void)[foo bar:baz];");
  515. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  516. // Binary operators.
  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];");
  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] : [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. 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. // Whew!
  549. verifyFormat("return in[42];");
  550. verifyFormat("for (auto v : in[1]) {\n}");
  551. verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
  552. verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
  553. verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
  554. verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
  555. verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
  556. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  557. "}");
  558. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  559. verifyFormat("[self aaaaa:MACRO(a, b:c:, d:e:)];");
  560. verifyFormat("[self aaaaa:MACRO(a, b:c:d:, e:f:g:)];");
  561. verifyFormat("int XYMyFoo(int a, int b) NS_SWIFT_NAME(foo(self:scale:));");
  562. verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
  563. verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
  564. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  565. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  566. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  567. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  568. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  569. verifyFormat("[button setAction:@selector(zoomOut:)];");
  570. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  571. verifyFormat("arr[[self indexForFoo:a]];");
  572. verifyFormat("throw [self errorFor:a];");
  573. verifyFormat("@throw [self errorFor:a];");
  574. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  575. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  576. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  577. // This tests that the formatter doesn't break after "backing" but before ":",
  578. // which would be at 80 columns.
  579. verifyFormat(
  580. "void f() {\n"
  581. " if ((self = [super initWithContentRect:contentRect\n"
  582. " styleMask:styleMask ?: otherMask\n"
  583. " backing:NSBackingStoreBuffered\n"
  584. " defer:YES]))");
  585. verifyFormat(
  586. "[foo checkThatBreakingAfterColonWorksOk:\n"
  587. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  588. verifyFormat("[myObj short:arg1 // Force line break\n"
  589. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  590. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  591. " error:arg4];");
  592. verifyFormat(
  593. "void f() {\n"
  594. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  595. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  596. " pos.width(), pos.height())\n"
  597. " styleMask:NSBorderlessWindowMask\n"
  598. " backing:NSBackingStoreBuffered\n"
  599. " defer:NO]);\n"
  600. "}");
  601. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  602. " with:contentsNativeView];");
  603. verifyFormat(
  604. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  605. " owner:nillllll];");
  606. verifyFormat(
  607. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  608. " forType:kBookmarkButtonDragType];");
  609. verifyFormat("[defaultCenter addObserver:self\n"
  610. " selector:@selector(willEnterFullscreen)\n"
  611. " name:kWillEnterFullscreenNotification\n"
  612. " object:nil];");
  613. verifyFormat("[image_rep drawInRect:drawRect\n"
  614. " fromRect:NSZeroRect\n"
  615. " operation:NSCompositeCopy\n"
  616. " fraction:1.0\n"
  617. " respectFlipped:NO\n"
  618. " hints:nil];");
  619. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n"
  620. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  621. verifyFormat("[aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaa)\n"
  622. " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  623. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n"
  624. " aaaaaaaaaaaaaaaaaaaaaa];");
  625. verifyFormat(
  626. "scoped_nsobject<NSTextField> message(\n"
  627. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  628. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  629. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  630. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  631. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  632. " aaaa:bbb];");
  633. verifyFormat("[self param:function( //\n"
  634. " parameter)]");
  635. verifyFormat(
  636. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  637. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  638. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  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. // Formats pair-parameters.
  652. verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
  653. verifyFormat("[I drawRectOn:surface //\n"
  654. " ofSize:aa:bbb\n"
  655. " atOrigin:cc:dd];");
  656. // Inline block as a first argument.
  657. verifyFormat("[object justBlock:^{\n"
  658. " a = 42;\n"
  659. "}];");
  660. verifyFormat("[object\n"
  661. " justBlock:^{\n"
  662. " a = 42;\n"
  663. " }\n"
  664. " notBlock:42\n"
  665. " a:42];");
  666. verifyFormat("[object\n"
  667. " firstBlock:^{\n"
  668. " a = 42;\n"
  669. " }\n"
  670. " blockWithLongerName:^{\n"
  671. " a = 42;\n"
  672. " }];");
  673. verifyFormat("[object\n"
  674. " blockWithLongerName:^{\n"
  675. " a = 42;\n"
  676. " }\n"
  677. " secondBlock:^{\n"
  678. " a = 42;\n"
  679. " }];");
  680. verifyFormat("[object\n"
  681. " firstBlock:^{\n"
  682. " a = 42;\n"
  683. " }\n"
  684. " notBlock:42\n"
  685. " secondBlock:^{\n"
  686. " a = 42;\n"
  687. " }];");
  688. Style.ColumnLimit = 70;
  689. verifyFormat(
  690. "void f() {\n"
  691. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  692. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  693. " pos.width(), pos.height())\n"
  694. " syeMask:NSBorderlessWindowMask\n"
  695. " bking:NSBackingStoreBuffered\n"
  696. " der:NO]);\n"
  697. "}");
  698. Style.ColumnLimit = 60;
  699. verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
  700. " .aaaaaaaa];"); // FIXME: Indentation seems off.
  701. // FIXME: This violates the column limit.
  702. verifyFormat(
  703. "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  704. " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
  705. " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  706. Style = getChromiumStyle(FormatStyle::LK_ObjC);
  707. Style.ColumnLimit = 80;
  708. verifyFormat(
  709. "void f() {\n"
  710. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  711. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  712. " pos.width(), pos.height())\n"
  713. " styleMask:NSBorderlessWindowMask\n"
  714. " backing:NSBackingStoreBuffered\n"
  715. " defer:NO]);\n"
  716. "}");
  717. // Respect continuation indent and colon alignment (e.g. when object name is
  718. // short, and first selector is the longest one)
  719. Style = getLLVMStyle();
  720. Style.Language = FormatStyle::LK_ObjC;
  721. Style.ContinuationIndentWidth = 8;
  722. verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
  723. " withObject:nil\n"
  724. " waitUntilDone:false];");
  725. verifyFormat("[self performSelector:@selector(loadAccessories)\n"
  726. " withObjectOnMainThread:nil\n"
  727. " waitUntilDone:false];");
  728. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  729. " performSelectorOnMainThread:@selector(loadAccessories)\n"
  730. " withObject:nil\n"
  731. " waitUntilDone:false];");
  732. verifyFormat("[self // force wrapping\n"
  733. " performSelectorOnMainThread:@selector(loadAccessories)\n"
  734. " withObject:nil\n"
  735. " waitUntilDone:false];");
  736. }
  737. TEST_F(FormatTestObjC, ObjCAt) {
  738. verifyFormat("@autoreleasepool");
  739. verifyFormat("@catch");
  740. verifyFormat("@class");
  741. verifyFormat("@compatibility_alias");
  742. verifyFormat("@defs");
  743. verifyFormat("@dynamic");
  744. verifyFormat("@encode");
  745. verifyFormat("@end");
  746. verifyFormat("@finally");
  747. verifyFormat("@implementation");
  748. verifyFormat("@import");
  749. verifyFormat("@interface");
  750. verifyFormat("@optional");
  751. verifyFormat("@package");
  752. verifyFormat("@private");
  753. verifyFormat("@property");
  754. verifyFormat("@protected");
  755. verifyFormat("@protocol");
  756. verifyFormat("@public");
  757. verifyFormat("@required");
  758. verifyFormat("@selector");
  759. verifyFormat("@synchronized");
  760. verifyFormat("@synthesize");
  761. verifyFormat("@throw");
  762. verifyFormat("@try");
  763. EXPECT_EQ("@interface", format("@ interface"));
  764. // The precise formatting of this doesn't matter, nobody writes code like
  765. // this.
  766. verifyFormat("@ /*foo*/ interface");
  767. }
  768. TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) {
  769. verifyFormat("void DoStuffWithBlockType(int (^)(char));");
  770. verifyFormat("int (^foo)(char, float);");
  771. verifyFormat("int (^foo[10])(char, float);");
  772. verifyFormat("int (^foo[kNumEntries])(char, float);");
  773. verifyFormat("int (^foo[kNumEntries + 10])(char, float);");
  774. verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);");
  775. }
  776. TEST_F(FormatTestObjC, ObjCSnippets) {
  777. verifyFormat("@autoreleasepool {\n"
  778. " foo();\n"
  779. "}");
  780. verifyFormat("@class Foo, Bar;");
  781. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  782. verifyFormat("@dynamic textColor;");
  783. verifyFormat("char *buf1 = @encode(int *);");
  784. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  785. verifyFormat("char *buf1 = @encode(int **);");
  786. verifyFormat("Protocol *proto = @protocol(p1);");
  787. verifyFormat("SEL s = @selector(foo:);");
  788. verifyFormat("@synchronized(self) {\n"
  789. " f();\n"
  790. "}");
  791. verifyFormat("@import foo.bar;\n"
  792. "@import baz;");
  793. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  794. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  795. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  796. Style = getMozillaStyle();
  797. verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
  798. verifyFormat("@property BOOL editable;");
  799. Style = getWebKitStyle();
  800. verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
  801. verifyFormat("@property BOOL editable;");
  802. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  803. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  804. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  805. }
  806. TEST_F(FormatTestObjC, ObjCForIn) {
  807. verifyFormat("- (void)test {\n"
  808. " for (NSString *n in arrayOfStrings) {\n"
  809. " foo(n);\n"
  810. " }\n"
  811. "}");
  812. verifyFormat("- (void)test {\n"
  813. " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
  814. " foo(n);\n"
  815. " }\n"
  816. "}");
  817. verifyFormat("for (Foo *x in bar) {\n}");
  818. verifyFormat("for (Foo *x in [bar baz]) {\n}");
  819. verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
  820. verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
  821. verifyFormat("for (Foo *x in [bar baz:^{\n"
  822. " [uh oh];\n"
  823. " }]) {\n}");
  824. }
  825. TEST_F(FormatTestObjC, ObjCNew) {
  826. verifyFormat("+ (instancetype)new {\n"
  827. " return nil;\n"
  828. "}\n");
  829. verifyFormat("+ (instancetype)myNew {\n"
  830. " return [self new];\n"
  831. "}\n");
  832. verifyFormat("SEL NewSelector(void) { return @selector(new); }\n");
  833. verifyFormat("SEL MacroSelector(void) { return MACRO(new); }\n");
  834. }
  835. TEST_F(FormatTestObjC, ObjCLiterals) {
  836. verifyFormat("@\"String\"");
  837. verifyFormat("@1");
  838. verifyFormat("@+4.8");
  839. verifyFormat("@-4");
  840. verifyFormat("@1LL");
  841. verifyFormat("@.5");
  842. verifyFormat("@'c'");
  843. verifyFormat("@true");
  844. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  845. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  846. verifyFormat("NSNumber *favoriteColor = @(Green);");
  847. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  848. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  849. }
  850. TEST_F(FormatTestObjC, ObjCDictLiterals) {
  851. verifyFormat("@{");
  852. verifyFormat("@{}");
  853. verifyFormat("@{@\"one\" : @1}");
  854. verifyFormat("return @{@\"one\" : @1;");
  855. verifyFormat("@{@\"one\" : @1}");
  856. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  857. verifyFormat("@{\n"
  858. " @\"one\" : @{@2 : @1},\n"
  859. "}");
  860. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  861. verifyIncompleteFormat("[self setDict:@{}");
  862. verifyIncompleteFormat("[self setDict:@{@1 : @2}");
  863. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  864. verifyFormat(
  865. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  866. verifyFormat(
  867. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  868. verifyFormat("NSDictionary *d = @{\n"
  869. " @\"nam\" : NSUserNam(),\n"
  870. " @\"dte\" : [NSDate date],\n"
  871. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  872. "};");
  873. verifyFormat(
  874. "@{\n"
  875. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  876. "regularFont,\n"
  877. "};");
  878. verifyFormat(
  879. "@{\n"
  880. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  881. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  882. "};");
  883. // We should try to be robust in case someone forgets the "@".
  884. verifyFormat("NSDictionary *d = {\n"
  885. " @\"nam\" : NSUserNam(),\n"
  886. " @\"dte\" : [NSDate date],\n"
  887. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  888. "};");
  889. verifyFormat("NSMutableDictionary *dictionary =\n"
  890. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  891. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  892. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  893. " cccccccccccccccc : ccccccccccccccc\n"
  894. " }];");
  895. // Ensure that casts before the key are kept on the same line as the key.
  896. verifyFormat(
  897. "NSDictionary *d = @{\n"
  898. " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  899. " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
  900. "};");
  901. Style.ColumnLimit = 40;
  902. verifyFormat("int Foo() {\n"
  903. " a12345 = @{a12345 : a12345};\n"
  904. "}");
  905. verifyFormat("int Foo() {\n"
  906. " a12345 = @{a12345 : @(a12345)};\n"
  907. "}");
  908. verifyFormat("int Foo() {\n"
  909. " a12345 = @{(Foo *)a12345 : @(a12345)};\n"
  910. "}");
  911. verifyFormat("int Foo() {\n"
  912. " a12345 = @{@(a12345) : a12345};\n"
  913. "}");
  914. verifyFormat("int Foo() {\n"
  915. " a12345 = @{@(a12345) : @YES};\n"
  916. "}");
  917. Style.SpacesInContainerLiterals = false;
  918. verifyFormat("int Foo() {\n"
  919. " b12345 = @{b12345: b12345};\n"
  920. "}");
  921. verifyFormat("int Foo() {\n"
  922. " b12345 = @{(Foo *)b12345: @(b12345)};\n"
  923. "}");
  924. Style.SpacesInContainerLiterals = true;
  925. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  926. verifyFormat(
  927. "@{\n"
  928. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  929. "regularFont,\n"
  930. "};");
  931. }
  932. TEST_F(FormatTestObjC, ObjCArrayLiterals) {
  933. verifyIncompleteFormat("@[");
  934. verifyFormat("@[]");
  935. verifyFormat(
  936. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  937. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  938. verifyFormat("NSArray *array = @[ [foo description] ];");
  939. verifyFormat(
  940. "NSArray *some_variable = @[\n"
  941. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  942. " @\"aaaaaaaaaaaaaaaaa\",\n"
  943. " @\"aaaaaaaaaaaaaaaaa\",\n"
  944. " @\"aaaaaaaaaaaaaaaaa\",\n"
  945. "];");
  946. verifyFormat(
  947. "NSArray *some_variable = @[\n"
  948. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  949. " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
  950. "];");
  951. verifyFormat("NSArray *some_variable = @[\n"
  952. " @\"aaaaaaaaaaaaaaaaa\",\n"
  953. " @\"aaaaaaaaaaaaaaaaa\",\n"
  954. " @\"aaaaaaaaaaaaaaaaa\",\n"
  955. " @\"aaaaaaaaaaaaaaaaa\",\n"
  956. "];");
  957. verifyFormat("NSArray *array = @[\n"
  958. " @\"a\",\n"
  959. " @\"a\",\n" // Trailing comma -> one per line.
  960. "];");
  961. // We should try to be robust in case someone forgets the "@".
  962. verifyFormat("NSArray *some_variable = [\n"
  963. " @\"aaaaaaaaaaaaaaaaa\",\n"
  964. " @\"aaaaaaaaaaaaaaaaa\",\n"
  965. " @\"aaaaaaaaaaaaaaaaa\",\n"
  966. " @\"aaaaaaaaaaaaaaaaa\",\n"
  967. "];");
  968. verifyFormat(
  969. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  970. " index:(NSUInteger)index\n"
  971. " nonDigitAttributes:\n"
  972. " (NSDictionary *)noDigitAttributes;");
  973. verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
  974. " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
  975. "]];");
  976. Style.ColumnLimit = 40;
  977. verifyFormat("int Foo() {\n"
  978. " a12345 = @[ a12345, a12345 ];\n"
  979. "}");
  980. verifyFormat("int Foo() {\n"
  981. " a123 = @[ (Foo *)a12345, @(a12345) ];\n"
  982. "}");
  983. Style.SpacesInContainerLiterals = false;
  984. verifyFormat("int Foo() {\n"
  985. " b12345 = @[b12345, b12345];\n"
  986. "}");
  987. verifyFormat("int Foo() {\n"
  988. " b12345 = @[(Foo *)b12345, @(b12345)];\n"
  989. "}");
  990. Style.SpacesInContainerLiterals = true;
  991. Style.ColumnLimit = 20;
  992. // We can't break string literals inside NSArray literals
  993. // (that raises -Wobjc-string-concatenation).
  994. verifyFormat("NSArray *foo = @[\n"
  995. " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  996. "];\n");
  997. }
  998. } // end namespace
  999. } // end namespace format
  1000. } // end namespace clang