FormatTestRawStrings.cpp 28 KB

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