FormatTestRawStrings.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. //===- unittest/Format/FormatTestRawStrings.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. using clang::tooling::toReplacements;
  19. namespace clang {
  20. namespace format {
  21. namespace {
  22. class FormatTestRawStrings : public ::testing::Test {
  23. protected:
  24. enum StatusCheck { SC_ExpectComplete, SC_ExpectIncomplete, SC_DoNotCheck };
  25. std::string format(llvm::StringRef Code,
  26. const FormatStyle &Style = getLLVMStyle(),
  27. StatusCheck CheckComplete = SC_ExpectComplete) {
  28. LLVM_DEBUG(llvm::errs() << "---\n");
  29. LLVM_DEBUG(llvm::errs() << Code << "\n\n");
  30. std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
  31. FormattingAttemptStatus Status;
  32. tooling::Replacements Replaces =
  33. reformat(Style, Code, Ranges, "<stdin>", &Status);
  34. if (CheckComplete != SC_DoNotCheck) {
  35. bool ExpectedCompleteFormat = CheckComplete == SC_ExpectComplete;
  36. EXPECT_EQ(ExpectedCompleteFormat, Status.FormatComplete)
  37. << Code << "\n\n";
  38. }
  39. ReplacementCount = Replaces.size();
  40. auto Result = applyAllReplacements(Code, Replaces);
  41. EXPECT_TRUE(static_cast<bool>(Result));
  42. LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n");
  43. return *Result;
  44. }
  45. FormatStyle getStyleWithColumns(FormatStyle Style, unsigned ColumnLimit) {
  46. Style.ColumnLimit = ColumnLimit;
  47. return Style;
  48. }
  49. FormatStyle getLLVMStyleWithColumns(unsigned ColumnLimit) {
  50. return getStyleWithColumns(getLLVMStyle(), ColumnLimit);
  51. }
  52. int ReplacementCount;
  53. FormatStyle getRawStringPbStyleWithColumns(unsigned ColumnLimit) {
  54. FormatStyle Style = getLLVMStyle();
  55. Style.ColumnLimit = ColumnLimit;
  56. Style.RawStringFormats = {
  57. {
  58. /*Language=*/FormatStyle::LK_TextProto,
  59. /*Delimiters=*/{"pb"},
  60. /*EnclosingFunctions=*/{},
  61. /*CanonicalDelimiter=*/"",
  62. /*BasedOnStyle=*/"google",
  63. },
  64. };
  65. return Style;
  66. }
  67. FormatStyle getRawStringLLVMCppStyleBasedOn(std::string BasedOnStyle) {
  68. FormatStyle Style = getLLVMStyle();
  69. Style.RawStringFormats = {
  70. {
  71. /*Language=*/FormatStyle::LK_Cpp,
  72. /*Delimiters=*/{"cpp"},
  73. /*EnclosingFunctions=*/{},
  74. /*CanonicalDelimiter=*/"",
  75. BasedOnStyle,
  76. },
  77. };
  78. return Style;
  79. }
  80. FormatStyle getRawStringGoogleCppStyleBasedOn(std::string BasedOnStyle) {
  81. FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
  82. Style.RawStringFormats = {
  83. {
  84. /*Language=*/FormatStyle::LK_Cpp,
  85. /*Delimiters=*/{"cpp"},
  86. /*EnclosingFunctions=*/{},
  87. /*CanonicalDelimiter=*/"",
  88. BasedOnStyle,
  89. },
  90. };
  91. return Style;
  92. }
  93. // Gcc 4.8 doesn't support raw string literals in macros, which breaks some
  94. // build bots. We use this function instead.
  95. void expect_eq(const std::string Expected, const std::string Actual) {
  96. EXPECT_EQ(Expected, Actual);
  97. }
  98. };
  99. TEST_F(FormatTestRawStrings, ReformatsAccordingToBaseStyle) {
  100. // llvm style puts '*' on the right.
  101. // google style puts '*' on the left.
  102. // Use the llvm style if the raw string style has no BasedOnStyle.
  103. expect_eq(R"test(int *i = R"cpp(int *p = nullptr;)cpp")test",
  104. format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test",
  105. getRawStringLLVMCppStyleBasedOn("")));
  106. // Use the google style if the raw string style has BasedOnStyle=google.
  107. expect_eq(R"test(int *i = R"cpp(int* p = nullptr;)cpp")test",
  108. format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test",
  109. getRawStringLLVMCppStyleBasedOn("google")));
  110. // Use the llvm style if the raw string style has no BasedOnStyle=llvm.
  111. expect_eq(R"test(int* i = R"cpp(int *p = nullptr;)cpp")test",
  112. format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test",
  113. getRawStringGoogleCppStyleBasedOn("llvm")));
  114. }
  115. TEST_F(FormatTestRawStrings, UsesConfigurationOverBaseStyle) {
  116. // llvm style puts '*' on the right.
  117. // google style puts '*' on the left.
  118. // Uses the configured google style inside raw strings even if BasedOnStyle in
  119. // the raw string format is llvm.
  120. FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
  121. EXPECT_EQ(0, parseConfiguration("---\n"
  122. "Language: Cpp\n"
  123. "BasedOnStyle: Google", &Style).value());
  124. Style.RawStringFormats = {{
  125. FormatStyle::LK_Cpp,
  126. {"cpp"},
  127. {},
  128. /*CanonicalDelimiter=*/"",
  129. /*BasedOnStyle=*/"llvm",
  130. }};
  131. expect_eq(R"test(int* i = R"cpp(int* j = 0;)cpp";)test",
  132. format(R"test(int * i = R"cpp(int * j = 0;)cpp";)test", Style));
  133. }
  134. TEST_F(FormatTestRawStrings, MatchesDelimitersCaseSensitively) {
  135. // Don't touch the 'PB' raw string, format the 'pb' raw string.
  136. expect_eq(R"test(
  137. s = R"PB(item:1)PB";
  138. t = R"pb(item: 1)pb";)test",
  139. format(R"test(
  140. s = R"PB(item:1)PB";
  141. t = R"pb(item:1)pb";)test",
  142. getRawStringPbStyleWithColumns(40)));
  143. }
  144. TEST_F(FormatTestRawStrings, RespectsClangFormatOff) {
  145. expect_eq(R"test(
  146. // clang-format off
  147. s = R"pb(item: 1)pb";
  148. // clang-format on
  149. t = R"pb(item: 1)pb";)test",
  150. format(R"test(
  151. // clang-format off
  152. s = R"pb(item: 1)pb";
  153. // clang-format on
  154. t = R"pb(item: 1)pb";)test",
  155. getRawStringPbStyleWithColumns(40)));
  156. }
  157. TEST_F(FormatTestRawStrings, ReformatsShortRawStringsOnSingleLine) {
  158. expect_eq(
  159. R"test(P p = TP(R"pb()pb");)test",
  160. format(
  161. R"test(P p = TP(R"pb( )pb");)test",
  162. getRawStringPbStyleWithColumns(40)));
  163. expect_eq(
  164. R"test(P p = TP(R"pb(item_1: 1)pb");)test",
  165. format(
  166. R"test(P p = TP(R"pb(item_1:1)pb");)test",
  167. getRawStringPbStyleWithColumns(40)));
  168. expect_eq(
  169. R"test(P p = TP(R"pb(item_1: 1)pb");)test",
  170. format(
  171. R"test(P p = TP(R"pb( item_1 : 1 )pb");)test",
  172. getRawStringPbStyleWithColumns(40)));
  173. expect_eq(
  174. R"test(P p = TP(R"pb(item_1: 1 item_2: 2)pb");)test",
  175. format(
  176. R"test(P p = TP(R"pb(item_1:1 item_2:2)pb");)test",
  177. getRawStringPbStyleWithColumns(40)));
  178. // Merge two short lines into one.
  179. expect_eq(R"test(
  180. std::string s = R"pb(
  181. item_1: 1 item_2: 2
  182. )pb";
  183. )test",
  184. format(R"test(
  185. std::string s = R"pb(
  186. item_1:1
  187. item_2:2
  188. )pb";
  189. )test",
  190. getRawStringPbStyleWithColumns(40)));
  191. }
  192. TEST_F(FormatTestRawStrings, BreaksShortRawStringsWhenNeeded) {
  193. // The raw string contains multiple submessage entries, so break for
  194. // readability.
  195. expect_eq(R"test(
  196. P p = TP(R"pb(item_1 < 1 >
  197. item_2: { 2 })pb");)test",
  198. format(
  199. R"test(
  200. P p = TP(R"pb(item_1<1> item_2:{2})pb");)test",
  201. getRawStringPbStyleWithColumns(40)));
  202. }
  203. TEST_F(FormatTestRawStrings, BreaksRawStringsExceedingColumnLimit) {
  204. expect_eq(R"test(
  205. P p = TPPPPPPPPPPPPPPP(
  206. R"pb(item_1: 1, item_2: 2)pb");)test",
  207. format(R"test(
  208. P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2)pb");)test",
  209. getRawStringPbStyleWithColumns(40)));
  210. expect_eq(R"test(
  211. P p =
  212. TPPPPPPPPPPPPPPP(
  213. R"pb(item_1: 1,
  214. item_2: 2,
  215. item_3: 3)pb");)test",
  216. format(R"test(
  217. P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2, item_3: 3)pb");)test",
  218. getRawStringPbStyleWithColumns(40)));
  219. expect_eq(R"test(
  220. P p = TP(R"pb(item_1 < 1 >
  221. item_2: < 2 >
  222. item_3 {})pb");)test",
  223. format(R"test(
  224. P p = TP(R"pb(item_1<1> item_2:<2> item_3{ })pb");)test",
  225. getRawStringPbStyleWithColumns(40)));
  226. expect_eq(
  227. R"test(
  228. P p = TP(R"pb(item_1: 1,
  229. item_2: 2,
  230. item_3: 3,
  231. item_4: 4)pb");)test",
  232. format(
  233. R"test(
  234. P p = TP(R"pb(item_1: 1, item_2: 2, item_3: 3, item_4: 4)pb");)test",
  235. getRawStringPbStyleWithColumns(40)));
  236. expect_eq(R"test(
  237. P p = TPPPPPPPPPPPPPPP(
  238. R"pb(item_1 < 1 >,
  239. item_2: { 2 },
  240. item_3: < 3 >,
  241. item_4: { 4 })pb");)test",
  242. format(R"test(
  243. P p = TPPPPPPPPPPPPPPP(R"pb(item_1<1>, item_2: {2}, item_3: <3>, item_4:{4})pb");)test",
  244. getRawStringPbStyleWithColumns(40)));
  245. // Breaks before a short raw string exceeding the column limit.
  246. expect_eq(R"test(
  247. FFFFFFFFFFFFFFFFFFFFFFFFFFF(
  248. R"pb(key: 1)pb");
  249. P p = TPPPPPPPPPPPPPPPPPPPP(
  250. R"pb(key: 2)pb");
  251. auto TPPPPPPPPPPPPPPPPPPPP =
  252. R"pb(key: 3)pb";
  253. P p = TPPPPPPPPPPPPPPPPPPPP(
  254. R"pb(i: 1, j: 2)pb");
  255. int f(string s) {
  256. FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(
  257. R"pb(key: 1)pb");
  258. P p = TPPPPPPPPPPPPPPPPPPPP(
  259. R"pb(key: 2)pb");
  260. auto TPPPPPPPPPPPPPPPPPPPP =
  261. R"pb(key: 3)pb";
  262. if (s.empty())
  263. P p = TPPPPPPPPPPPPPPPPPPPP(
  264. R"pb(i: 1, j: 2)pb");
  265. }
  266. )test",
  267. format(R"test(
  268. FFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
  269. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
  270. auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
  271. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
  272. int f(string s) {
  273. FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
  274. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
  275. auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
  276. if (s.empty())
  277. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
  278. }
  279. )test",
  280. getRawStringPbStyleWithColumns(40)));
  281. }
  282. TEST_F(FormatTestRawStrings, FormatsRawStringArguments) {
  283. expect_eq(R"test(
  284. P p = TP(R"pb(key { 1 })pb", param_2);)test",
  285. format(R"test(
  286. P p = TP(R"pb(key{1})pb",param_2);)test",
  287. getRawStringPbStyleWithColumns(40)));
  288. expect_eq(R"test(
  289. PPPPPPPPPPPPP(R"pb(keykeyk)pb",
  290. param_2);)test",
  291. format(R"test(
  292. PPPPPPPPPPPPP(R"pb(keykeyk)pb", param_2);)test",
  293. getRawStringPbStyleWithColumns(40)));
  294. expect_eq(R"test(
  295. P p = TP(
  296. R"pb(item: { i: 1, s: 's' }
  297. item: { i: 2, s: 't' })pb");)test",
  298. format(R"test(
  299. P p = TP(R"pb(item: {i: 1, s: 's'} item: {i: 2, s: 't'})pb");)test",
  300. getRawStringPbStyleWithColumns(40)));
  301. expect_eq(R"test(
  302. FFFFFFFFFFFFFFFFFFF(
  303. R"pb(key: "value")pb",
  304. R"pb(key2: "value")pb");)test",
  305. format(R"test(
  306. FFFFFFFFFFFFFFFFFFF(R"pb(key: "value")pb", R"pb(key2: "value")pb");)test",
  307. getRawStringPbStyleWithColumns(40)));
  308. // Formats the first out of two arguments.
  309. expect_eq(R"test(
  310. FFFFFFFF(R"pb(key: 1)pb", argument2);
  311. struct S {
  312. const s =
  313. f(R"pb(key: 1)pb", argument2);
  314. void f() {
  315. if (gol)
  316. return g(R"pb(key: 1)pb",
  317. 132789237);
  318. return g(R"pb(key: 1)pb", "172893");
  319. }
  320. };)test",
  321. format(R"test(
  322. FFFFFFFF(R"pb(key:1)pb", argument2);
  323. struct S {
  324. const s = f(R"pb(key:1)pb", argument2);
  325. void f() {
  326. if (gol)
  327. return g(R"pb(key:1)pb", 132789237);
  328. return g(R"pb(key:1)pb", "172893");
  329. }
  330. };)test",
  331. getRawStringPbStyleWithColumns(40)));
  332. // Formats the second out of two arguments.
  333. expect_eq(R"test(
  334. FFFFFFFF(argument1, R"pb(key: 2)pb");
  335. struct S {
  336. const s =
  337. f(argument1, R"pb(key: 2)pb");
  338. void f() {
  339. if (gol)
  340. return g(12784137,
  341. R"pb(key: 2)pb");
  342. return g(17283122, R"pb(key: 2)pb");
  343. }
  344. };)test",
  345. format(R"test(
  346. FFFFFFFF(argument1, R"pb(key:2)pb");
  347. struct S {
  348. const s = f(argument1, R"pb(key:2)pb");
  349. void f() {
  350. if (gol)
  351. return g(12784137, R"pb(key:2)pb");
  352. return g(17283122, R"pb(key:2)pb");
  353. }
  354. };)test",
  355. getRawStringPbStyleWithColumns(40)));
  356. // Formats two short raw string arguments.
  357. expect_eq(R"test(
  358. FFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
  359. format(R"test(
  360. FFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
  361. getRawStringPbStyleWithColumns(40)));
  362. // TODO(krasimir): The original source code fits on one line, so the
  363. // non-optimizing formatter is chosen. But after the formatting in protos is
  364. // made, the code doesn't fit on one line anymore and further formatting
  365. // splits it.
  366. //
  367. // Should we disable raw string formatting for the non-optimizing formatter?
  368. expect_eq(R"test(
  369. FFFFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
  370. format(R"test(
  371. FFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
  372. getRawStringPbStyleWithColumns(40)));
  373. // Formats two short raw string arguments, puts second on newline.
  374. expect_eq(R"test(
  375. FFFFFFFF(R"pb(key: 1)pb",
  376. R"pb(key: 2)pb");)test",
  377. format(R"test(
  378. FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
  379. getRawStringPbStyleWithColumns(40)));
  380. // Formats both arguments.
  381. expect_eq(R"test(
  382. FFFFFFFF(R"pb(key: 1)pb",
  383. R"pb(key: 2)pb");
  384. struct S {
  385. const s = f(R"pb(key: 1)pb",
  386. R"pb(key: 2)pb");
  387. void f() {
  388. if (gol)
  389. return g(R"pb(key: 1)pb",
  390. R"pb(key: 2)pb");
  391. return g(R"pb(k1)pb", R"pb(k2)pb");
  392. }
  393. };)test",
  394. format(R"test(
  395. FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");
  396. struct S {
  397. const s = f(R"pb(key:1)pb", R"pb(key:2)pb");
  398. void f() {
  399. if (gol)
  400. return g(R"pb(key:1)pb", R"pb(key:2)pb");
  401. return g(R"pb( k1 )pb", R"pb( k2 )pb");
  402. }
  403. };)test",
  404. getRawStringPbStyleWithColumns(40)));
  405. }
  406. TEST_F(FormatTestRawStrings, RawStringStartingWithNewlines) {
  407. expect_eq(R"test(
  408. std::string s = R"pb(
  409. item_1: 1
  410. )pb";
  411. )test",
  412. format(R"test(
  413. std::string s = R"pb(
  414. item_1:1
  415. )pb";
  416. )test",
  417. getRawStringPbStyleWithColumns(40)));
  418. expect_eq(R"test(
  419. std::string s = R"pb(
  420. item_1: 1
  421. )pb";
  422. )test",
  423. format(R"test(
  424. std::string s = R"pb(
  425. item_1:1
  426. )pb";
  427. )test",
  428. getRawStringPbStyleWithColumns(40)));
  429. expect_eq(R"test(
  430. std::string s = R"pb(
  431. item_1: 1
  432. )pb";
  433. )test",
  434. format(R"test(
  435. std::string s = R"pb(
  436. item_1:1
  437. )pb";
  438. )test",
  439. getRawStringPbStyleWithColumns(40)));
  440. expect_eq(R"test(
  441. std::string s = R"pb(
  442. item_1: 1,
  443. item_2: 2
  444. )pb";
  445. )test",
  446. format(R"test(
  447. std::string s = R"pb(
  448. item_1:1, item_2:2
  449. )pb";
  450. )test",
  451. getRawStringPbStyleWithColumns(40)));
  452. expect_eq(R"test(
  453. std::string s = R"pb(
  454. book {
  455. title: "Alice's Adventures"
  456. author: "Lewis Caroll"
  457. }
  458. book {
  459. title: "Peter Pan"
  460. author: "J. M. Barrie"
  461. }
  462. )pb";
  463. )test",
  464. format(R"test(
  465. std::string s = R"pb(
  466. book { title: "Alice's Adventures" author: "Lewis Caroll" }
  467. book { title: "Peter Pan" author: "J. M. Barrie" }
  468. )pb";
  469. )test",
  470. getRawStringPbStyleWithColumns(40)));
  471. }
  472. TEST_F(FormatTestRawStrings, BreaksBeforeRawStrings) {
  473. expect_eq(R"test(
  474. ASSERT_TRUE(
  475. ParseFromString(R"pb(item_1: 1)pb"),
  476. ptr);)test",
  477. format(R"test(
  478. ASSERT_TRUE(ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
  479. getRawStringPbStyleWithColumns(40)));
  480. expect_eq(R"test(
  481. ASSERT_TRUE(toolong::ParseFromString(
  482. R"pb(item_1: 1)pb"),
  483. ptr);)test",
  484. format(R"test(
  485. ASSERT_TRUE(toolong::ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
  486. getRawStringPbStyleWithColumns(40)));
  487. expect_eq(R"test(
  488. ASSERT_TRUE(ParseFromString(
  489. R"pb(item_1: 1,
  490. item_2: 2)pb"),
  491. ptr);)test",
  492. format(R"test(
  493. ASSERT_TRUE(ParseFromString(R"pb(item_1: 1, item_2: 2)pb"), ptr);)test",
  494. getRawStringPbStyleWithColumns(40)));
  495. expect_eq(R"test(
  496. ASSERT_TRUE(
  497. ParseFromString(
  498. R"pb(item_1: 1 item_2: 2)pb"),
  499. ptr);)test",
  500. format(R"test(
  501. ASSERT_TRUE(ParseFromString(R"pb(item_1: 1 item_2: 2)pb"), ptr);)test",
  502. getRawStringPbStyleWithColumns(40)));
  503. }
  504. TEST_F(FormatTestRawStrings, RawStringsInOperands) {
  505. // Formats the raw string first operand of a binary operator expression.
  506. expect_eq(R"test(auto S = R"pb(item_1: 1)pb" + rest;)test",
  507. format(R"test(auto S = R"pb(item_1:1)pb" + rest;)test",
  508. getRawStringPbStyleWithColumns(40)));
  509. expect_eq(R"test(
  510. auto S = R"pb(item_1: 1, item_2: 2)pb" +
  511. rest;)test",
  512. format(R"test(
  513. auto S = R"pb(item_1:1,item_2:2)pb"+rest;)test",
  514. getRawStringPbStyleWithColumns(40)));
  515. expect_eq(R"test(
  516. auto S =
  517. R"pb(item_1: 1 item_2: 2)pb" + rest;)test",
  518. format(R"test(
  519. auto S = R"pb(item_1:1 item_2:2)pb"+rest;)test",
  520. getRawStringPbStyleWithColumns(40)));
  521. expect_eq(R"test(
  522. auto S = R"pb(item_1: 1,
  523. item_2: 2,
  524. item_3: 3)pb" + rest;)test",
  525. format(R"test(
  526. auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
  527. getRawStringPbStyleWithColumns(40)));
  528. expect_eq(R"test(
  529. auto S = R"pb(item_1: 1,
  530. item_2: 2,
  531. item_3: 3)pb" +
  532. longlongrest;)test",
  533. format(R"test(
  534. auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test",
  535. getRawStringPbStyleWithColumns(40)));
  536. // Formats the raw string second operand of a binary operator expression.
  537. expect_eq(R"test(auto S = first + R"pb(item_1: 1)pb";)test",
  538. format(R"test(auto S = first + R"pb(item_1:1)pb";)test",
  539. getRawStringPbStyleWithColumns(40)));
  540. expect_eq(R"test(
  541. auto S = first + R"pb(item_1: 1,
  542. item_2: 2)pb";)test",
  543. format(R"test(
  544. auto S = first+R"pb(item_1:1,item_2:2)pb";)test",
  545. getRawStringPbStyleWithColumns(40)));
  546. expect_eq(R"test(
  547. auto S = first + R"pb(item_1: 1
  548. item_2: 2)pb";)test",
  549. format(R"test(
  550. auto S = first+R"pb(item_1:1 item_2:2)pb";)test",
  551. getRawStringPbStyleWithColumns(40)));
  552. expect_eq(R"test(
  553. auto S = R"pb(item_1: 1,
  554. item_2: 2,
  555. item_3: 3)pb" + rest;)test",
  556. format(R"test(
  557. auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
  558. getRawStringPbStyleWithColumns(40)));
  559. expect_eq(R"test(
  560. auto S = R"pb(item_1: 1,
  561. item_2: 2,
  562. item_3: 3)pb" +
  563. longlongrest;)test",
  564. format(R"test(
  565. auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test",
  566. getRawStringPbStyleWithColumns(40)));
  567. // Formats the raw string operands in expressions.
  568. expect_eq(R"test(
  569. auto S = R"pb(item_1: 1)pb" +
  570. R"pb(item_2: 2)pb";
  571. )test",
  572. format(R"test(
  573. auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb";
  574. )test",
  575. getRawStringPbStyleWithColumns(40)));
  576. expect_eq(R"test(
  577. auto S = R"pb(item_1: 1)pb" +
  578. R"pb(item_2: 2)pb" +
  579. R"pb(item_3: 3)pb";
  580. )test",
  581. format(R"test(
  582. auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"+R"pb(item_3:3)pb";
  583. )test",
  584. getRawStringPbStyleWithColumns(40)));
  585. expect_eq(R"test(
  586. auto S = (count < 3)
  587. ? R"pb(item_1: 1)pb"
  588. : R"pb(item_2: 2)pb";
  589. )test",
  590. format(R"test(
  591. auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2)pb";
  592. )test",
  593. getRawStringPbStyleWithColumns(40)));
  594. expect_eq(R"test(
  595. auto S =
  596. (count < 3)
  597. ? R"pb(item_1: 1, item_2: 2)pb"
  598. : R"pb(item_3: 3)pb";
  599. )test",
  600. format(R"test(
  601. auto S=(count<3)?R"pb(item_1:1,item_2:2)pb":R"pb(item_3:3)pb";
  602. )test",
  603. getRawStringPbStyleWithColumns(40)));
  604. expect_eq(R"test(
  605. auto S =
  606. (count < 3)
  607. ? R"pb(item_1: 1)pb"
  608. : R"pb(item_2: 2, item_3: 3)pb";
  609. )test",
  610. format(R"test(
  611. auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2,item_3:3)pb";
  612. )test",
  613. getRawStringPbStyleWithColumns(40)));
  614. }
  615. TEST_F(FormatTestRawStrings, PrefixAndSuffixAlignment) {
  616. // Keep the suffix at the end of line if not on newline.
  617. expect_eq(R"test(
  618. int s() {
  619. auto S = PTP(
  620. R"pb(
  621. item_1: 1,
  622. item_2: 2)pb");
  623. })test",
  624. format(R"test(
  625. int s() {
  626. auto S = PTP(
  627. R"pb(
  628. item_1: 1,
  629. item_2: 2)pb");
  630. })test",
  631. getRawStringPbStyleWithColumns(20)));
  632. // Align the suffix with the surrounding indent if the prefix is not on
  633. // a line of its own.
  634. expect_eq(R"test(
  635. int s() {
  636. auto S = PTP(R"pb(
  637. item_1: 1,
  638. item_2: 2
  639. )pb");
  640. })test",
  641. format(R"test(
  642. int s() {
  643. auto S = PTP(R"pb(
  644. item_1: 1,
  645. item_2: 2
  646. )pb");
  647. })test",
  648. getRawStringPbStyleWithColumns(20)));
  649. // Align the prefix with the suffix if both the prefix and suffix are on a
  650. // line of their own.
  651. expect_eq(R"test(
  652. int s() {
  653. auto S = PTP(
  654. R"pb(
  655. item_1: 1,
  656. item_2: 2,
  657. )pb");
  658. })test",
  659. format(R"test(
  660. int s() {
  661. auto S = PTP(
  662. R"pb(
  663. item_1: 1,
  664. item_2: 2,
  665. )pb");
  666. })test",
  667. getRawStringPbStyleWithColumns(20)));
  668. }
  669. TEST_F(FormatTestRawStrings, EstimatesPenalty) {
  670. // The penalty for characters exceeding the column limit in the raw string
  671. // forces 'hh' to be put on a newline.
  672. expect_eq(R"test(
  673. ff(gggggg,
  674. hh(R"pb(key {
  675. i1: k1
  676. i2: k2
  677. })pb"));
  678. )test",
  679. format(R"test(
  680. ff(gggggg, hh(R"pb(key {
  681. i1: k1
  682. i2: k2
  683. })pb"));
  684. )test",
  685. getRawStringPbStyleWithColumns(20)));
  686. }
  687. TEST_F(FormatTestRawStrings, DontFormatNonRawStrings) {
  688. expect_eq(R"test(a = R"pb(key:value)";)test",
  689. format(R"test(a = R"pb(key:value)";)test",
  690. getRawStringPbStyleWithColumns(20)));
  691. }
  692. TEST_F(FormatTestRawStrings, FormatsRawStringsWithEnclosingFunctionName) {
  693. FormatStyle Style = getRawStringPbStyleWithColumns(40);
  694. Style.RawStringFormats[0].EnclosingFunctions.push_back(
  695. "PARSE_TEXT_PROTO");
  696. Style.RawStringFormats[0].EnclosingFunctions.push_back("ParseTextProto");
  697. expect_eq(R"test(a = PARSE_TEXT_PROTO(R"(key: value)");)test",
  698. format(R"test(a = PARSE_TEXT_PROTO(R"(key:value)");)test", Style));
  699. expect_eq(R"test(
  700. a = PARSE_TEXT_PROTO /**/ (
  701. /**/ R"(key: value)");)test",
  702. format(R"test(
  703. a = PARSE_TEXT_PROTO/**/(/**/R"(key:value)");)test",
  704. Style));
  705. expect_eq(R"test(
  706. a = ParseTextProto<ProtoType>(
  707. R"(key: value)");)test",
  708. format(R"test(
  709. a = ParseTextProto<ProtoType>(R"(key:value)");)test",
  710. Style));
  711. }
  712. TEST_F(FormatTestRawStrings, UpdatesToCanonicalDelimiters) {
  713. FormatStyle Style = getRawStringPbStyleWithColumns(25);
  714. Style.RawStringFormats[0].CanonicalDelimiter = "proto";
  715. expect_eq(R"test(a = R"proto(key: value)proto";)test",
  716. format(R"test(a = R"pb(key:value)pb";)test", Style));
  717. // Don't update to canonical delimiter if it occurs as a raw string suffix in
  718. // the raw string content.
  719. expect_eq(R"test(a = R"pb(key: ")proto")pb";)test",
  720. format(R"test(a = R"pb(key:")proto")pb";)test", Style));
  721. }
  722. TEST_F(FormatTestRawStrings, PenalizesPrefixExcessChars) {
  723. FormatStyle Style = getRawStringPbStyleWithColumns(60);
  724. // The '(' in R"pb is at column 60, no break.
  725. expect_eq(R"test(
  726. xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
  727. Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
  728. )pb"));
  729. )test",
  730. format(R"test(
  731. xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
  732. Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
  733. )pb"));
  734. )test", Style));
  735. // The '(' in R"pb is at column 61, break.
  736. expect_eq(R"test(
  737. xxxxxxxaaaaax wwwwwww =
  738. _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
  739. Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
  740. )pb"));
  741. )test",
  742. format(R"test(
  743. xxxxxxxaaaaax wwwwwww = _Verxrrrrrrrrr(PARSE_TEXT_PROTO(R"pb(
  744. Category: aaaaaaaaaaaaaaaaaaaaaaaaaa
  745. )pb"));
  746. )test", Style));
  747. }
  748. TEST_F(FormatTestRawStrings, KeepsRBraceFolloedByMoreLBracesOnSameLine) {
  749. FormatStyle Style = getRawStringPbStyleWithColumns(80);
  750. expect_eq(
  751. R"test(
  752. int f() {
  753. if (1) {
  754. TTTTTTTTTTTTTTTTTTTTT s = PARSE_TEXT_PROTO(R"pb(
  755. ttttttttt {
  756. ppppppppppppp {
  757. [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_1: "123_1" }
  758. [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_2: "123_2" }
  759. }
  760. }
  761. )pb");
  762. }
  763. }
  764. )test",
  765. format(
  766. R"test(
  767. int f() {
  768. if (1) {
  769. TTTTTTTTTTTTTTTTTTTTT s = PARSE_TEXT_PROTO(R"pb(
  770. ttttttttt {
  771. ppppppppppppp {
  772. [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_1: "123_1" }
  773. [cccccccccc.pppppppppppppp.TTTTTTTTTTTTTTTTTTTT] { field_2: "123_2" }}}
  774. )pb");
  775. }
  776. }
  777. )test",
  778. Style));
  779. }
  780. } // end namespace
  781. } // end namespace format
  782. } // end namespace clang