FormatTestObjC.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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<Baz : Blech> : Bar <Baz, Quux> {\n"
  254. " int _i;\n"
  255. "}\n"
  256. "+ (id)init;\n"
  257. "@end");
  258. verifyFormat("@interface Foo<Bar : Baz <Blech>> : Xyzzy <Corge> {\n"
  259. " int _i;\n"
  260. "}\n"
  261. "+ (id)init;\n"
  262. "@end");
  263. verifyFormat("@interface Foo (HackStuff) {\n"
  264. " int _i;\n"
  265. "}\n"
  266. "+ (id)init;\n"
  267. "@end");
  268. verifyFormat("@interface Foo () {\n"
  269. " int _i;\n"
  270. "}\n"
  271. "+ (id)init;\n"
  272. "@end");
  273. verifyFormat("@interface Foo (HackStuff) <MyProtocol> {\n"
  274. " int _i;\n"
  275. "}\n"
  276. "+ (id)init;\n"
  277. "@end");
  278. Style.ColumnLimit = 40;
  279. verifyFormat("@interface ccccccccccccc () <\n"
  280. " ccccccccccccc, ccccccccccccc,\n"
  281. " ccccccccccccc, ccccccccccccc> {\n"
  282. "}");
  283. verifyFormat("@interface ccccccccccccc (ccccccccccc) <\n"
  284. " ccccccccccccc> {\n"
  285. "}");
  286. Style.ObjCBinPackProtocolList = FormatStyle::BPS_Never;
  287. verifyFormat("@interface ddddddddddddd () <\n"
  288. " ddddddddddddd,\n"
  289. " ddddddddddddd,\n"
  290. " ddddddddddddd,\n"
  291. " ddddddddddddd> {\n"
  292. "}");
  293. Style.BinPackParameters = false;
  294. Style.ObjCBinPackProtocolList = FormatStyle::BPS_Auto;
  295. verifyFormat("@interface eeeeeeeeeeeee () <\n"
  296. " eeeeeeeeeeeee,\n"
  297. " eeeeeeeeeeeee,\n"
  298. " eeeeeeeeeeeee,\n"
  299. " eeeeeeeeeeeee> {\n"
  300. "}");
  301. Style.ObjCBinPackProtocolList = FormatStyle::BPS_Always;
  302. verifyFormat("@interface fffffffffffff () <\n"
  303. " fffffffffffff, fffffffffffff,\n"
  304. " fffffffffffff, fffffffffffff> {\n"
  305. "}");
  306. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  307. verifyFormat("@interface Foo : NSObject <NSSomeDelegate> {\n"
  308. " @public\n"
  309. " int field1;\n"
  310. " @protected\n"
  311. " int field2;\n"
  312. " @private\n"
  313. " int field3;\n"
  314. " @package\n"
  315. " int field4;\n"
  316. "}\n"
  317. "+ (id)init;\n"
  318. "@end");
  319. verifyFormat("@interface Foo : Bar <Baz, Quux>\n"
  320. "+ (id)init;\n"
  321. "@end");
  322. verifyFormat("@interface Foo (HackStuff) <MyProtocol>\n"
  323. "+ (id)init;\n"
  324. "@end");
  325. Style.ColumnLimit = 40;
  326. // BinPackParameters should be true by default.
  327. verifyFormat("void eeeeeeee(int eeeee, int eeeee,\n"
  328. " int eeeee, int eeeee);\n");
  329. // ObjCBinPackProtocolList should be BPS_Never by default.
  330. verifyFormat("@interface fffffffffffff () <\n"
  331. " fffffffffffff,\n"
  332. " fffffffffffff,\n"
  333. " fffffffffffff,\n"
  334. " fffffffffffff> {\n"
  335. "}");
  336. }
  337. TEST_F(FormatTestObjC, FormatObjCImplementation) {
  338. verifyFormat("@implementation Foo : NSObject {\n"
  339. "@public\n"
  340. " int field1;\n"
  341. "@protected\n"
  342. " int field2;\n"
  343. "@private\n"
  344. " int field3;\n"
  345. "@package\n"
  346. " int field4;\n"
  347. "}\n"
  348. "+ (id)init {\n}\n"
  349. "@end");
  350. verifyFormat("@implementation Foo\n"
  351. "+ (id)init {\n"
  352. " if (true)\n"
  353. " return nil;\n"
  354. "}\n"
  355. "// Look, a comment!\n"
  356. "- (int)answerWith:(int)i {\n"
  357. " return i;\n"
  358. "}\n"
  359. "+ (int)answerWith:(int)i {\n"
  360. " return i;\n"
  361. "}\n"
  362. "@end");
  363. verifyFormat("@implementation Foo\n"
  364. "@end\n"
  365. "@implementation Bar\n"
  366. "@end");
  367. EXPECT_EQ("@implementation Foo : Bar\n"
  368. "+ (id)init {\n}\n"
  369. "- (void)foo {\n}\n"
  370. "@end",
  371. format("@implementation Foo : Bar\n"
  372. "+(id)init{}\n"
  373. "-(void)foo{}\n"
  374. "@end"));
  375. verifyFormat("@implementation Foo {\n"
  376. " int _i;\n"
  377. "}\n"
  378. "+ (id)init {\n}\n"
  379. "@end");
  380. verifyFormat("@implementation Foo : Bar {\n"
  381. " int _i;\n"
  382. "}\n"
  383. "+ (id)init {\n}\n"
  384. "@end");
  385. verifyFormat("@implementation Foo (HackStuff)\n"
  386. "+ (id)init {\n}\n"
  387. "@end");
  388. verifyFormat("@implementation ObjcClass\n"
  389. "- (void)method;\n"
  390. "{}\n"
  391. "@end");
  392. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  393. verifyFormat("@implementation Foo : NSObject {\n"
  394. " @public\n"
  395. " int field1;\n"
  396. " @protected\n"
  397. " int field2;\n"
  398. " @private\n"
  399. " int field3;\n"
  400. " @package\n"
  401. " int field4;\n"
  402. "}\n"
  403. "+ (id)init {\n}\n"
  404. "@end");
  405. }
  406. TEST_F(FormatTestObjC, FormatObjCProtocol) {
  407. verifyFormat("@protocol Foo\n"
  408. "@property(weak) id delegate;\n"
  409. "- (NSUInteger)numberOfThings;\n"
  410. "@end");
  411. verifyFormat("@protocol MyProtocol <NSObject>\n"
  412. "- (NSUInteger)numberOfThings;\n"
  413. "@end");
  414. verifyFormat("@protocol Foo;\n"
  415. "@protocol Bar;\n");
  416. verifyFormat("@protocol Foo\n"
  417. "@end\n"
  418. "@protocol Bar\n"
  419. "@end");
  420. verifyFormat("FOUNDATION_EXPORT NS_AVAILABLE_IOS(10.0) @protocol Foo\n"
  421. "@property(assign, readwrite) NSInteger bar;\n"
  422. "@end");
  423. verifyFormat("@protocol myProtocol\n"
  424. "- (void)mandatoryWithInt:(int)i;\n"
  425. "@optional\n"
  426. "- (void)optional;\n"
  427. "@required\n"
  428. "- (void)required;\n"
  429. "@optional\n"
  430. "@property(assign) int madProp;\n"
  431. "@end\n");
  432. verifyFormat("@property(nonatomic, assign, readonly)\n"
  433. " int *looooooooooooooooooooooooooooongNumber;\n"
  434. "@property(nonatomic, assign, readonly)\n"
  435. " NSString *looooooooooooooooooooooooooooongName;");
  436. verifyFormat("@implementation PR18406\n"
  437. "}\n"
  438. "@end");
  439. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  440. verifyFormat("@protocol MyProtocol <NSObject>\n"
  441. "- (NSUInteger)numberOfThings;\n"
  442. "@end");
  443. }
  444. TEST_F(FormatTestObjC, FormatObjCMethodDeclarations) {
  445. verifyFormat("- (void)doSomethingWith:(GTMFoo *)theFoo\n"
  446. " rect:(NSRect)theRect\n"
  447. " interval:(float)theInterval {\n"
  448. "}");
  449. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  450. " longKeyword:(NSRect)theRect\n"
  451. " longerKeyword:(float)theInterval\n"
  452. " error:(NSError **)theError {\n"
  453. "}");
  454. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  455. " longKeyword:(NSRect)theRect\n"
  456. " evenLongerKeyword:(float)theInterval\n"
  457. " error:(NSError **)theError {\n"
  458. "}");
  459. verifyFormat("+ (instancetype)new;\n");
  460. Style.ColumnLimit = 60;
  461. verifyFormat("- (instancetype)initXxxxxx:(id<x>)x\n"
  462. " y:(id<yyyyyyyyyyyyyyyyyyyy>)y\n"
  463. " NS_DESIGNATED_INITIALIZER;");
  464. verifyFormat("- (void)drawRectOn:(id)surface\n"
  465. " ofSize:(size_t)height\n"
  466. " :(size_t)width;");
  467. Style.ColumnLimit = 40;
  468. // Make sure selectors with 0, 1, or more arguments are indented when wrapped.
  469. verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  470. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa;\n");
  471. verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  472. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
  473. verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  474. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
  475. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
  476. verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  477. " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
  478. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
  479. verifyFormat("- (aaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n"
  480. " aaaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a\n"
  481. " aaaaaaaaaaaaaaaaaaaaaaaaaaa:(int)a;\n");
  482. // Continuation indent width should win over aligning colons if the function
  483. // name is long.
  484. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  485. Style.ColumnLimit = 40;
  486. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  487. " dontAlignNamef:(NSRect)theRect {\n"
  488. "}");
  489. // Make sure we don't break aligning for short parameter names.
  490. verifyFormat("- (void)shortf:(GTMFoo *)theFoo\n"
  491. " aShortf:(NSRect)theRect {\n"
  492. "}");
  493. // Format pairs correctly.
  494. Style.ColumnLimit = 80;
  495. verifyFormat("- (void)drawRectOn:(id)surface\n"
  496. " ofSize:(aaaaaaaa)height\n"
  497. " :(size_t)width\n"
  498. " atOrigin:(size_t)x\n"
  499. " :(size_t)y\n"
  500. " aaaaa:(a)yyy\n"
  501. " bbb:(d)cccc;");
  502. verifyFormat("- (void)drawRectOn:(id)surface ofSize:(aaa)height:(bbb)width;");
  503. }
  504. TEST_F(FormatTestObjC, FormatObjCMethodExpr) {
  505. verifyFormat("[foo bar:baz];");
  506. verifyFormat("return [foo bar:baz];");
  507. verifyFormat("return (a)[foo bar:baz];");
  508. verifyFormat("f([foo bar:baz]);");
  509. verifyFormat("f(2, [foo bar:baz]);");
  510. verifyFormat("f(2, a ? b : c);");
  511. verifyFormat("[[self initWithInt:4] bar:[baz quux:arrrr]];");
  512. // Unary operators.
  513. verifyFormat("int a = +[foo bar:baz];");
  514. verifyFormat("int a = -[foo bar:baz];");
  515. verifyFormat("int a = ![foo bar:baz];");
  516. verifyFormat("int a = ~[foo bar:baz];");
  517. verifyFormat("int a = ++[foo bar:baz];");
  518. verifyFormat("int a = --[foo bar:baz];");
  519. verifyFormat("int a = sizeof [foo bar:baz];");
  520. verifyFormat("int a = alignof [foo bar:baz];");
  521. verifyFormat("int a = &[foo bar:baz];");
  522. verifyFormat("int a = *[foo bar:baz];");
  523. // FIXME: Make casts work, without breaking f()[4].
  524. // verifyFormat("int a = (int)[foo bar:baz];");
  525. // verifyFormat("return (int)[foo bar:baz];");
  526. // verifyFormat("(void)[foo bar:baz];");
  527. verifyFormat("return (MyType *)[self.tableView cellForRowAtIndexPath:cell];");
  528. // Binary operators.
  529. verifyFormat("[foo bar:baz], [foo bar:baz];");
  530. verifyFormat("[foo bar:baz] = [foo bar:baz];");
  531. verifyFormat("[foo bar:baz] *= [foo bar:baz];");
  532. verifyFormat("[foo bar:baz] /= [foo bar:baz];");
  533. verifyFormat("[foo bar:baz] %= [foo bar:baz];");
  534. verifyFormat("[foo bar:baz] += [foo bar:baz];");
  535. verifyFormat("[foo bar:baz] -= [foo bar:baz];");
  536. verifyFormat("[foo bar:baz] <<= [foo bar:baz];");
  537. verifyFormat("[foo bar:baz] >>= [foo bar:baz];");
  538. verifyFormat("[foo bar:baz] &= [foo bar:baz];");
  539. verifyFormat("[foo bar:baz] ^= [foo bar:baz];");
  540. verifyFormat("[foo bar:baz] |= [foo bar:baz];");
  541. verifyFormat("[foo bar:baz] ? [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. verifyFormat("[foo bar:baz] >> [foo bar:baz];");
  554. verifyFormat("[foo bar:baz] << [foo bar:baz];");
  555. verifyFormat("[foo bar:baz] - [foo bar:baz];");
  556. verifyFormat("[foo bar:baz] + [foo bar:baz];");
  557. verifyFormat("[foo bar:baz] * [foo bar:baz];");
  558. verifyFormat("[foo bar:baz] / [foo bar:baz];");
  559. verifyFormat("[foo bar:baz] % [foo bar:baz];");
  560. // Whew!
  561. verifyFormat("return in[42];");
  562. verifyFormat("for (auto v : in[1]) {\n}");
  563. verifyFormat("for (int i = 0; i < in[a]; ++i) {\n}");
  564. verifyFormat("for (int i = 0; in[a] < i; ++i) {\n}");
  565. verifyFormat("for (int i = 0; i < n; ++i, ++in[a]) {\n}");
  566. verifyFormat("for (int i = 0; i < n; ++i, in[a]++) {\n}");
  567. verifyFormat("for (int i = 0; i < f(in[a]); ++i, in[a]++) {\n}");
  568. verifyFormat("for (id foo in [self getStuffFor:bla]) {\n"
  569. "}");
  570. verifyFormat("[self aaaaa:MACRO(a, b:, c:)];");
  571. verifyFormat("[self aaaaa:MACRO(a, b:c:, d:e:)];");
  572. verifyFormat("[self aaaaa:MACRO(a, b:c:d:, e:f:g:)];");
  573. verifyFormat("int XYMyFoo(int a, int b) NS_SWIFT_NAME(foo(self:scale:));");
  574. verifyFormat("[self aaaaa:(1 + 2) bbbbb:3];");
  575. verifyFormat("[self aaaaa:(Type)a bbbbb:3];");
  576. verifyFormat("[self stuffWithInt:(4 + 2) float:4.5];");
  577. verifyFormat("[self stuffWithInt:a ? b : c float:4.5];");
  578. verifyFormat("[self stuffWithInt:a ? [self foo:bar] : c];");
  579. verifyFormat("[self stuffWithInt:a ? (e ? f : g) : c];");
  580. verifyFormat("[cond ? obj1 : obj2 methodWithParam:param]");
  581. verifyFormat("[button setAction:@selector(zoomOut:)];");
  582. verifyFormat("[color getRed:&r green:&g blue:&b alpha:&a];");
  583. verifyFormat("arr[[self indexForFoo:a]];");
  584. verifyFormat("throw [self errorFor:a];");
  585. verifyFormat("@throw [self errorFor:a];");
  586. verifyFormat("[(id)foo bar:(id)baz quux:(id)snorf];");
  587. verifyFormat("[(id)foo bar:(id) ? baz : quux];");
  588. verifyFormat("4 > 4 ? (id)a : (id)baz;");
  589. // This tests that the formatter doesn't break after "backing" but before ":",
  590. // which would be at 80 columns.
  591. verifyFormat(
  592. "void f() {\n"
  593. " if ((self = [super initWithContentRect:contentRect\n"
  594. " styleMask:styleMask ?: otherMask\n"
  595. " backing:NSBackingStoreBuffered\n"
  596. " defer:YES]))");
  597. verifyFormat(
  598. "[foo checkThatBreakingAfterColonWorksOk:\n"
  599. " [bar ifItDoes:reduceOverallLineLengthLikeInThisCase]];");
  600. verifyFormat("[myObj short:arg1 // Force line break\n"
  601. " longKeyword:arg2 != nil ? arg2 : @\"longKeyword\"\n"
  602. " evenLongerKeyword:arg3 ?: @\"evenLongerKeyword\"\n"
  603. " error:arg4];");
  604. verifyFormat(
  605. "void f() {\n"
  606. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  607. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  608. " pos.width(), pos.height())\n"
  609. " styleMask:NSBorderlessWindowMask\n"
  610. " backing:NSBackingStoreBuffered\n"
  611. " defer:NO]);\n"
  612. "}");
  613. verifyFormat("[contentsContainer replaceSubview:[subviews objectAtIndex:0]\n"
  614. " with:contentsNativeView];");
  615. verifyFormat(
  616. "[pboard addTypes:[NSArray arrayWithObject:kBookmarkButtonDragType]\n"
  617. " owner:nillllll];");
  618. verifyFormat(
  619. "[pboard setData:[NSData dataWithBytes:&button length:sizeof(button)]\n"
  620. " forType:kBookmarkButtonDragType];");
  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. verifyFormat(
  638. "scoped_nsobject<NSTextField> message(\n"
  639. " // The frame will be fixed up when |-setMessageText:| is called.\n"
  640. " [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, 0, 0)]);");
  641. verifyFormat("[self aaaaaa:bbbbbbbbbbbbb\n"
  642. " aaaaaaaaaa:bbbbbbbbbbbbbbbbb\n"
  643. " aaaaa:bbbbbbbbbbb + bbbbbbbbbbbb\n"
  644. " aaaa:bbb];");
  645. verifyFormat("[self param:function( //\n"
  646. " parameter)]");
  647. verifyFormat(
  648. "[self aaaaaaaaaa:aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  649. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa |\n"
  650. " aaaaaaaaaaaaaaa | aaaaaaaaaaaaaaa];");
  651. // Variadic parameters.
  652. verifyFormat(
  653. "NSArray *myStrings = [NSArray stringarray:@\"a\", @\"b\", nil];");
  654. verifyFormat(
  655. "[self aaaaaaaaaaaaa:aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  656. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa,\n"
  657. " aaaaaaaaaaaaaaa, aaaaaaaaaaaaaaa];");
  658. verifyFormat("[self // break\n"
  659. " a:a\n"
  660. " aaa:aaa];");
  661. verifyFormat("bool a = ([aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaa ||\n"
  662. " [aaaaaaaa aaaaa] == aaaaaaaaaaaaaaaaaaaa);");
  663. // Formats pair-parameters.
  664. verifyFormat("[I drawRectOn:surface ofSize:aa:bbb atOrigin:cc:dd];");
  665. verifyFormat("[I drawRectOn:surface //\n"
  666. " ofSize:aa:bbb\n"
  667. " atOrigin:cc:dd];");
  668. // Inline block as a first argument.
  669. verifyFormat("[object justBlock:^{\n"
  670. " a = 42;\n"
  671. "}];");
  672. verifyFormat("[object\n"
  673. " justBlock:^{\n"
  674. " a = 42;\n"
  675. " }\n"
  676. " notBlock:42\n"
  677. " a:42];");
  678. verifyFormat("[object\n"
  679. " firstBlock:^{\n"
  680. " a = 42;\n"
  681. " }\n"
  682. " blockWithLongerName:^{\n"
  683. " a = 42;\n"
  684. " }];");
  685. verifyFormat("[object\n"
  686. " blockWithLongerName:^{\n"
  687. " a = 42;\n"
  688. " }\n"
  689. " secondBlock:^{\n"
  690. " a = 42;\n"
  691. " }];");
  692. verifyFormat("[object\n"
  693. " firstBlock:^{\n"
  694. " a = 42;\n"
  695. " }\n"
  696. " notBlock:42\n"
  697. " secondBlock:^{\n"
  698. " a = 42;\n"
  699. " }];");
  700. Style.ColumnLimit = 70;
  701. verifyFormat(
  702. "void f() {\n"
  703. " popup_wdow_.reset([[RenderWidgetPopupWindow alloc]\n"
  704. " iniithContentRect:NSMakRet(origin_global.x, origin_global.y,\n"
  705. " pos.width(), pos.height())\n"
  706. " syeMask:NSBorderlessWindowMask\n"
  707. " bking:NSBackingStoreBuffered\n"
  708. " der:NO]);\n"
  709. "}");
  710. Style.ColumnLimit = 60;
  711. verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n"
  712. " .aaaaaaaa];"); // FIXME: Indentation seems off.
  713. // FIXME: This violates the column limit.
  714. verifyFormat(
  715. "[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  716. " aaaaaaaaaaaaaaaaa:aaaaaaaa\n"
  717. " aaa:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];");
  718. Style = getChromiumStyle(FormatStyle::LK_ObjC);
  719. Style.ColumnLimit = 80;
  720. verifyFormat(
  721. "void f() {\n"
  722. " popup_window_.reset([[RenderWidgetPopupWindow alloc]\n"
  723. " initWithContentRect:NSMakeRect(origin_global.x, origin_global.y,\n"
  724. " pos.width(), pos.height())\n"
  725. " styleMask:NSBorderlessWindowMask\n"
  726. " backing:NSBackingStoreBuffered\n"
  727. " defer:NO]);\n"
  728. "}");
  729. // Respect continuation indent and colon alignment (e.g. when object name is
  730. // short, and first selector is the longest one)
  731. Style = getLLVMStyle();
  732. Style.Language = FormatStyle::LK_ObjC;
  733. Style.ContinuationIndentWidth = 8;
  734. verifyFormat("[self performSelectorOnMainThread:@selector(loadAccessories)\n"
  735. " withObject:nil\n"
  736. " waitUntilDone:false];");
  737. verifyFormat("[self performSelector:@selector(loadAccessories)\n"
  738. " withObjectOnMainThread:nil\n"
  739. " waitUntilDone:false];");
  740. verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaaaa\n"
  741. " performSelectorOnMainThread:@selector(loadAccessories)\n"
  742. " withObject:nil\n"
  743. " waitUntilDone:false];");
  744. verifyFormat("[self // force wrapping\n"
  745. " performSelectorOnMainThread:@selector(loadAccessories)\n"
  746. " withObject:nil\n"
  747. " waitUntilDone:false];");
  748. }
  749. TEST_F(FormatTestObjC, ObjCAt) {
  750. verifyFormat("@autoreleasepool");
  751. verifyFormat("@catch");
  752. verifyFormat("@class");
  753. verifyFormat("@compatibility_alias");
  754. verifyFormat("@defs");
  755. verifyFormat("@dynamic");
  756. verifyFormat("@encode");
  757. verifyFormat("@end");
  758. verifyFormat("@finally");
  759. verifyFormat("@implementation");
  760. verifyFormat("@import");
  761. verifyFormat("@interface");
  762. verifyFormat("@optional");
  763. verifyFormat("@package");
  764. verifyFormat("@private");
  765. verifyFormat("@property");
  766. verifyFormat("@protected");
  767. verifyFormat("@protocol");
  768. verifyFormat("@public");
  769. verifyFormat("@required");
  770. verifyFormat("@selector");
  771. verifyFormat("@synchronized");
  772. verifyFormat("@synthesize");
  773. verifyFormat("@throw");
  774. verifyFormat("@try");
  775. EXPECT_EQ("@interface", format("@ interface"));
  776. // The precise formatting of this doesn't matter, nobody writes code like
  777. // this.
  778. verifyFormat("@ /*foo*/ interface");
  779. }
  780. TEST_F(FormatTestObjC, ObjCBlockTypesAndVariables) {
  781. verifyFormat("void DoStuffWithBlockType(int (^)(char));");
  782. verifyFormat("int (^foo)(char, float);");
  783. verifyFormat("int (^foo[10])(char, float);");
  784. verifyFormat("int (^foo[kNumEntries])(char, float);");
  785. verifyFormat("int (^foo[kNumEntries + 10])(char, float);");
  786. verifyFormat("int (^foo[(kNumEntries + 10)])(char, float);");
  787. }
  788. TEST_F(FormatTestObjC, ObjCSnippets) {
  789. verifyFormat("@autoreleasepool {\n"
  790. " foo();\n"
  791. "}");
  792. verifyFormat("@class Foo, Bar;");
  793. verifyFormat("@compatibility_alias AliasName ExistingClass;");
  794. verifyFormat("@dynamic textColor;");
  795. verifyFormat("char *buf1 = @encode(int *);");
  796. verifyFormat("char *buf1 = @encode(typeof(4 * 5));");
  797. verifyFormat("char *buf1 = @encode(int **);");
  798. verifyFormat("Protocol *proto = @protocol(p1);");
  799. verifyFormat("SEL s = @selector(foo:);");
  800. verifyFormat("@synchronized(self) {\n"
  801. " f();\n"
  802. "}");
  803. verifyFormat("@import foo.bar;\n"
  804. "@import baz;");
  805. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  806. verifyFormat("@property(assign, nonatomic) CGFloat hoverAlpha;");
  807. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  808. Style = getMozillaStyle();
  809. verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
  810. verifyFormat("@property BOOL editable;");
  811. Style = getWebKitStyle();
  812. verifyFormat("@property (assign, getter=isEditable) BOOL editable;");
  813. verifyFormat("@property BOOL editable;");
  814. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  815. verifyFormat("@synthesize dropArrowPosition = dropArrowPosition_;");
  816. verifyFormat("@property(assign, getter=isEditable) BOOL editable;");
  817. }
  818. TEST_F(FormatTestObjC, ObjCForIn) {
  819. verifyFormat("- (void)test {\n"
  820. " for (NSString *n in arrayOfStrings) {\n"
  821. " foo(n);\n"
  822. " }\n"
  823. "}");
  824. verifyFormat("- (void)test {\n"
  825. " for (NSString *n in (__bridge NSArray *)arrayOfStrings) {\n"
  826. " foo(n);\n"
  827. " }\n"
  828. "}");
  829. verifyFormat("for (Foo *x in bar) {\n}");
  830. verifyFormat("for (Foo *x in [bar baz]) {\n}");
  831. verifyFormat("for (Foo *x in [bar baz:blech]) {\n}");
  832. verifyFormat("for (Foo *x in [bar baz:blech, 1, 2, 3, 0]) {\n}");
  833. verifyFormat("for (Foo *x in [bar baz:^{\n"
  834. " [uh oh];\n"
  835. " }]) {\n}");
  836. }
  837. TEST_F(FormatTestObjC, ObjCCxxKeywords) {
  838. verifyFormat("+ (instancetype)new {\n"
  839. " return nil;\n"
  840. "}\n");
  841. verifyFormat("+ (instancetype)myNew {\n"
  842. " return [self new];\n"
  843. "}\n");
  844. verifyFormat("SEL NewSelector(void) { return @selector(new); }\n");
  845. verifyFormat("SEL MacroSelector(void) { return MACRO(new); }\n");
  846. verifyFormat("+ (instancetype)delete {\n"
  847. " return nil;\n"
  848. "}\n");
  849. verifyFormat("+ (instancetype)myDelete {\n"
  850. " return [self delete];\n"
  851. "}\n");
  852. verifyFormat("SEL DeleteSelector(void) { return @selector(delete); }\n");
  853. verifyFormat("SEL MacroSelector(void) { return MACRO(delete); }\n");
  854. verifyFormat("MACRO(new:)\n");
  855. verifyFormat("MACRO(delete:)\n");
  856. verifyFormat("foo = @{MACRO(new:) : MACRO(delete:)}\n");
  857. }
  858. TEST_F(FormatTestObjC, ObjCLiterals) {
  859. verifyFormat("@\"String\"");
  860. verifyFormat("@1");
  861. verifyFormat("@+4.8");
  862. verifyFormat("@-4");
  863. verifyFormat("@1LL");
  864. verifyFormat("@.5");
  865. verifyFormat("@'c'");
  866. verifyFormat("@true");
  867. verifyFormat("NSNumber *smallestInt = @(-INT_MAX - 1);");
  868. verifyFormat("NSNumber *piOverTwo = @(M_PI / 2);");
  869. verifyFormat("NSNumber *favoriteColor = @(Green);");
  870. verifyFormat("NSString *path = @(getenv(\"PATH\"));");
  871. verifyFormat("[dictionary setObject:@(1) forKey:@\"number\"];");
  872. }
  873. TEST_F(FormatTestObjC, ObjCDictLiterals) {
  874. verifyFormat("@{");
  875. verifyFormat("@{}");
  876. verifyFormat("@{@\"one\" : @1}");
  877. verifyFormat("return @{@\"one\" : @1;");
  878. verifyFormat("@{@\"one\" : @1}");
  879. verifyFormat("@{@\"one\" : @{@2 : @1}}");
  880. verifyFormat("@{\n"
  881. " @\"one\" : @{@2 : @1},\n"
  882. "}");
  883. verifyFormat("@{1 > 2 ? @\"one\" : @\"two\" : 1 > 2 ? @1 : @2}");
  884. verifyIncompleteFormat("[self setDict:@{}");
  885. verifyIncompleteFormat("[self setDict:@{@1 : @2}");
  886. verifyFormat("NSLog(@\"%@\", @{@1 : @2, @2 : @3}[@1]);");
  887. verifyFormat(
  888. "NSDictionary *masses = @{@\"H\" : @1.0078, @\"He\" : @4.0026};");
  889. verifyFormat(
  890. "NSDictionary *settings = @{AVEncoderKey : @(AVAudioQualityMax)};");
  891. verifyFormat("NSDictionary *d = @{\n"
  892. " @\"nam\" : NSUserNam(),\n"
  893. " @\"dte\" : [NSDate date],\n"
  894. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  895. "};");
  896. verifyFormat(
  897. "@{\n"
  898. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  899. "regularFont,\n"
  900. "};");
  901. verifyFormat(
  902. "@{\n"
  903. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee :\n"
  904. " reeeeeeeeeeeeeeeeeeeeeeeegularFont,\n"
  905. "};");
  906. // We should try to be robust in case someone forgets the "@".
  907. verifyFormat("NSDictionary *d = {\n"
  908. " @\"nam\" : NSUserNam(),\n"
  909. " @\"dte\" : [NSDate date],\n"
  910. " @\"processInfo\" : [NSProcessInfo processInfo]\n"
  911. "};");
  912. verifyFormat("NSMutableDictionary *dictionary =\n"
  913. " [NSMutableDictionary dictionaryWithDictionary:@{\n"
  914. " aaaaaaaaaaaaaaaaaaaaa : aaaaaaaaaaaaa,\n"
  915. " bbbbbbbbbbbbbbbbbb : bbbbb,\n"
  916. " cccccccccccccccc : ccccccccccccccc\n"
  917. " }];");
  918. // Ensure that casts before the key are kept on the same line as the key.
  919. verifyFormat(
  920. "NSDictionary *d = @{\n"
  921. " (aaaaaaaa id)aaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaaaaaaaaaaaa,\n"
  922. " (aaaaaaaa id)aaaaaaaaaaaaaa : (aaaaaaaa id)aaaaaaaaaaaaaa,\n"
  923. "};");
  924. Style.ColumnLimit = 40;
  925. verifyFormat("int Foo() {\n"
  926. " a12345 = @{a12345 : a12345};\n"
  927. "}");
  928. verifyFormat("int Foo() {\n"
  929. " a12345 = @{a12345 : @(a12345)};\n"
  930. "}");
  931. verifyFormat("int Foo() {\n"
  932. " a12345 = @{(Foo *)a12345 : @(a12345)};\n"
  933. "}");
  934. verifyFormat("int Foo() {\n"
  935. " a12345 = @{@(a12345) : a12345};\n"
  936. "}");
  937. verifyFormat("int Foo() {\n"
  938. " a12345 = @{@(a12345) : @YES};\n"
  939. "}");
  940. Style.SpacesInContainerLiterals = false;
  941. verifyFormat("int Foo() {\n"
  942. " b12345 = @{b12345: b12345};\n"
  943. "}");
  944. verifyFormat("int Foo() {\n"
  945. " b12345 = @{(Foo *)b12345: @(b12345)};\n"
  946. "}");
  947. Style.SpacesInContainerLiterals = true;
  948. Style = getGoogleStyle(FormatStyle::LK_ObjC);
  949. verifyFormat(
  950. "@{\n"
  951. " NSFontAttributeNameeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee : "
  952. "regularFont,\n"
  953. "};");
  954. }
  955. TEST_F(FormatTestObjC, ObjCArrayLiterals) {
  956. verifyIncompleteFormat("@[");
  957. verifyFormat("@[]");
  958. verifyFormat(
  959. "NSArray *array = @[ @\" Hey \", NSApp, [NSNumber numberWithInt:42] ];");
  960. verifyFormat("return @[ @3, @[], @[ @4, @5 ] ];");
  961. verifyFormat("NSArray *array = @[ [foo description] ];");
  962. verifyFormat(
  963. "NSArray *some_variable = @[\n"
  964. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  965. " @\"aaaaaaaaaaaaaaaaa\",\n"
  966. " @\"aaaaaaaaaaaaaaaaa\",\n"
  967. " @\"aaaaaaaaaaaaaaaaa\",\n"
  968. "];");
  969. verifyFormat(
  970. "NSArray *some_variable = @[\n"
  971. " aaaa == bbbbbbbbbbb ? @\"aaaaaaaaaaaa\" : @\"aaaaaaaaaaaaaa\",\n"
  972. " @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\", @\"aaaaaaaaaaaaaaaa\"\n"
  973. "];");
  974. verifyFormat("NSArray *some_variable = @[\n"
  975. " @\"aaaaaaaaaaaaaaaaa\",\n"
  976. " @\"aaaaaaaaaaaaaaaaa\",\n"
  977. " @\"aaaaaaaaaaaaaaaaa\",\n"
  978. " @\"aaaaaaaaaaaaaaaaa\",\n"
  979. "];");
  980. verifyFormat("NSArray *array = @[\n"
  981. " @\"a\",\n"
  982. " @\"a\",\n" // Trailing comma -> one per line.
  983. "];");
  984. // We should try to be robust in case someone forgets the "@".
  985. verifyFormat("NSArray *some_variable = [\n"
  986. " @\"aaaaaaaaaaaaaaaaa\",\n"
  987. " @\"aaaaaaaaaaaaaaaaa\",\n"
  988. " @\"aaaaaaaaaaaaaaaaa\",\n"
  989. " @\"aaaaaaaaaaaaaaaaa\",\n"
  990. "];");
  991. verifyFormat(
  992. "- (NSAttributedString *)attributedStringForSegment:(NSUInteger)segment\n"
  993. " index:(NSUInteger)index\n"
  994. " nonDigitAttributes:\n"
  995. " (NSDictionary *)noDigitAttributes;");
  996. verifyFormat("[someFunction someLooooooooooooongParameter:@[\n"
  997. " NSBundle.mainBundle.infoDictionary[@\"a\"]\n"
  998. "]];");
  999. Style.ColumnLimit = 40;
  1000. verifyFormat("int Foo() {\n"
  1001. " a12345 = @[ a12345, a12345 ];\n"
  1002. "}");
  1003. verifyFormat("int Foo() {\n"
  1004. " a123 = @[ (Foo *)a12345, @(a12345) ];\n"
  1005. "}");
  1006. Style.SpacesInContainerLiterals = false;
  1007. verifyFormat("int Foo() {\n"
  1008. " b12345 = @[b12345, b12345];\n"
  1009. "}");
  1010. verifyFormat("int Foo() {\n"
  1011. " b12345 = @[(Foo *)b12345, @(b12345)];\n"
  1012. "}");
  1013. Style.SpacesInContainerLiterals = true;
  1014. Style.ColumnLimit = 20;
  1015. // We can't break string literals inside NSArray literals
  1016. // (that raises -Wobjc-string-concatenation).
  1017. verifyFormat("NSArray *foo = @[\n"
  1018. " @\"aaaaaaaaaaaaaaaaaaaaaaaaaa\"\n"
  1019. "];\n");
  1020. }
  1021. } // end namespace
  1022. } // end namespace format
  1023. } // end namespace clang