FormatTestRawStrings.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. DEBUG(llvm::errs() << "---\n");
  29. 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. 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. expect_eq(
  179. R"test(P p = TP(R"pb(item_1 < 1 > item_2: { 2 })pb");)test",
  180. format(
  181. R"test(P p = TP(R"pb(item_1<1> item_2:{2})pb");)test",
  182. getRawStringPbStyleWithColumns(40)));
  183. // Merge two short lines into one.
  184. expect_eq(R"test(
  185. std::string s = R"pb(
  186. item_1: 1 item_2: 2
  187. )pb";
  188. )test",
  189. format(R"test(
  190. std::string s = R"pb(
  191. item_1:1
  192. item_2:2
  193. )pb";
  194. )test",
  195. getRawStringPbStyleWithColumns(40)));
  196. }
  197. TEST_F(FormatTestRawStrings, BreaksRawStringsExceedingColumnLimit) {
  198. expect_eq(R"test(
  199. P p = TPPPPPPPPPPPPPPP(
  200. R"pb(item_1: 1, item_2: 2)pb");)test",
  201. format(R"test(
  202. P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2)pb");)test",
  203. getRawStringPbStyleWithColumns(40)));
  204. expect_eq(R"test(
  205. P p =
  206. TPPPPPPPPPPPPPPP(
  207. R"pb(item_1: 1,
  208. item_2: 2,
  209. item_3: 3)pb");)test",
  210. format(R"test(
  211. P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2, item_3: 3)pb");)test",
  212. getRawStringPbStyleWithColumns(40)));
  213. expect_eq(R"test(
  214. P p = TP(R"pb(item_1 < 1 >
  215. item_2: < 2 >
  216. item_3 {})pb");)test",
  217. format(R"test(
  218. P p = TP(R"pb(item_1<1> item_2:<2> item_3{ })pb");)test",
  219. getRawStringPbStyleWithColumns(40)));
  220. expect_eq(
  221. R"test(
  222. P p = TP(R"pb(item_1: 1,
  223. item_2: 2,
  224. item_3: 3,
  225. item_4: 4)pb");)test",
  226. format(
  227. R"test(
  228. P p = TP(R"pb(item_1: 1, item_2: 2, item_3: 3, item_4: 4)pb");)test",
  229. getRawStringPbStyleWithColumns(40)));
  230. expect_eq(R"test(
  231. P p = TPPPPPPPPPPPPPPP(
  232. R"pb(item_1 < 1 >,
  233. item_2: { 2 },
  234. item_3: < 3 >,
  235. item_4: { 4 })pb");)test",
  236. format(R"test(
  237. P p = TPPPPPPPPPPPPPPP(R"pb(item_1<1>, item_2: {2}, item_3: <3>, item_4:{4})pb");)test",
  238. getRawStringPbStyleWithColumns(40)));
  239. // Breaks before a short raw string exceeding the column limit.
  240. expect_eq(R"test(
  241. FFFFFFFFFFFFFFFFFFFFFFFFFFF(
  242. R"pb(key: 1)pb");
  243. P p = TPPPPPPPPPPPPPPPPPPPP(
  244. R"pb(key: 2)pb");
  245. auto TPPPPPPPPPPPPPPPPPPPP =
  246. R"pb(key: 3)pb";
  247. P p = TPPPPPPPPPPPPPPPPPPPP(
  248. R"pb(i: 1, j: 2)pb");
  249. int f(string s) {
  250. FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(
  251. R"pb(key: 1)pb");
  252. P p = TPPPPPPPPPPPPPPPPPPPP(
  253. R"pb(key: 2)pb");
  254. auto TPPPPPPPPPPPPPPPPPPPP =
  255. R"pb(key: 3)pb";
  256. if (s.empty())
  257. P p = TPPPPPPPPPPPPPPPPPPPP(
  258. R"pb(i: 1, j: 2)pb");
  259. }
  260. )test",
  261. format(R"test(
  262. FFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
  263. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
  264. auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
  265. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
  266. int f(string s) {
  267. FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
  268. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
  269. auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
  270. if (s.empty())
  271. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
  272. }
  273. )test",
  274. getRawStringPbStyleWithColumns(40)));
  275. }
  276. TEST_F(FormatTestRawStrings, FormatsRawStringArguments) {
  277. expect_eq(R"test(
  278. P p = TP(R"pb(key { 1 })pb", param_2);)test",
  279. format(R"test(
  280. P p = TP(R"pb(key{1})pb",param_2);)test",
  281. getRawStringPbStyleWithColumns(40)));
  282. expect_eq(R"test(
  283. PPPPPPPPPPPPP(R"pb(keykeyk)pb",
  284. param_2);)test",
  285. format(R"test(
  286. PPPPPPPPPPPPP(R"pb(keykeyk)pb", param_2);)test",
  287. getRawStringPbStyleWithColumns(40)));
  288. expect_eq(R"test(
  289. P p = TP(
  290. R"pb(item: { i: 1, s: 's' }
  291. item: { i: 2, s: 't' })pb");)test",
  292. format(R"test(
  293. P p = TP(R"pb(item: {i: 1, s: 's'} item: {i: 2, s: 't'})pb");)test",
  294. getRawStringPbStyleWithColumns(40)));
  295. expect_eq(R"test(
  296. FFFFFFFFFFFFFFFFFFF(
  297. R"pb(key: "value")pb",
  298. R"pb(key2: "value")pb");)test",
  299. format(R"test(
  300. FFFFFFFFFFFFFFFFFFF(R"pb(key: "value")pb", R"pb(key2: "value")pb");)test",
  301. getRawStringPbStyleWithColumns(40)));
  302. // Formats the first out of two arguments.
  303. expect_eq(R"test(
  304. FFFFFFFF(R"pb(key: 1)pb", argument2);
  305. struct S {
  306. const s =
  307. f(R"pb(key: 1)pb", argument2);
  308. void f() {
  309. if (gol)
  310. return g(R"pb(key: 1)pb",
  311. 132789237);
  312. return g(R"pb(key: 1)pb", "172893");
  313. }
  314. };)test",
  315. format(R"test(
  316. FFFFFFFF(R"pb(key:1)pb", argument2);
  317. struct S {
  318. const s = f(R"pb(key:1)pb", argument2);
  319. void f() {
  320. if (gol)
  321. return g(R"pb(key:1)pb", 132789237);
  322. return g(R"pb(key:1)pb", "172893");
  323. }
  324. };)test",
  325. getRawStringPbStyleWithColumns(40)));
  326. // Formats the second out of two arguments.
  327. expect_eq(R"test(
  328. FFFFFFFF(argument1, R"pb(key: 2)pb");
  329. struct S {
  330. const s =
  331. f(argument1, R"pb(key: 2)pb");
  332. void f() {
  333. if (gol)
  334. return g(12784137,
  335. R"pb(key: 2)pb");
  336. return g(17283122, R"pb(key: 2)pb");
  337. }
  338. };)test",
  339. format(R"test(
  340. FFFFFFFF(argument1, R"pb(key:2)pb");
  341. struct S {
  342. const s = f(argument1, R"pb(key:2)pb");
  343. void f() {
  344. if (gol)
  345. return g(12784137, R"pb(key:2)pb");
  346. return g(17283122, R"pb(key:2)pb");
  347. }
  348. };)test",
  349. getRawStringPbStyleWithColumns(40)));
  350. // Formats two short raw string arguments.
  351. expect_eq(R"test(
  352. FFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
  353. format(R"test(
  354. FFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
  355. getRawStringPbStyleWithColumns(40)));
  356. // TODO(krasimir): The original source code fits on one line, so the
  357. // non-optimizing formatter is chosen. But after the formatting in protos is
  358. // made, the code doesn't fit on one line anymore and further formatting
  359. // splits it.
  360. //
  361. // Should we disable raw string formatting for the non-optimizing formatter?
  362. expect_eq(R"test(
  363. FFFFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
  364. format(R"test(
  365. FFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
  366. getRawStringPbStyleWithColumns(40)));
  367. // Formats two short raw string arguments, puts second on newline.
  368. expect_eq(R"test(
  369. FFFFFFFF(R"pb(key: 1)pb",
  370. R"pb(key: 2)pb");)test",
  371. format(R"test(
  372. FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
  373. getRawStringPbStyleWithColumns(40)));
  374. // Formats both arguments.
  375. expect_eq(R"test(
  376. FFFFFFFF(R"pb(key: 1)pb",
  377. R"pb(key: 2)pb");
  378. struct S {
  379. const s = f(R"pb(key: 1)pb",
  380. R"pb(key: 2)pb");
  381. void f() {
  382. if (gol)
  383. return g(R"pb(key: 1)pb",
  384. R"pb(key: 2)pb");
  385. return g(R"pb(k1)pb", R"pb(k2)pb");
  386. }
  387. };)test",
  388. format(R"test(
  389. FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");
  390. struct S {
  391. const s = f(R"pb(key:1)pb", R"pb(key:2)pb");
  392. void f() {
  393. if (gol)
  394. return g(R"pb(key:1)pb", R"pb(key:2)pb");
  395. return g(R"pb( k1 )pb", R"pb( k2 )pb");
  396. }
  397. };)test",
  398. getRawStringPbStyleWithColumns(40)));
  399. }
  400. TEST_F(FormatTestRawStrings, RawStringStartingWithNewlines) {
  401. expect_eq(R"test(
  402. std::string s = R"pb(
  403. item_1: 1
  404. )pb";
  405. )test",
  406. format(R"test(
  407. std::string s = R"pb(
  408. item_1:1
  409. )pb";
  410. )test",
  411. getRawStringPbStyleWithColumns(40)));
  412. expect_eq(R"test(
  413. std::string s = R"pb(
  414. item_1: 1
  415. )pb";
  416. )test",
  417. format(R"test(
  418. std::string s = R"pb(
  419. item_1:1
  420. )pb";
  421. )test",
  422. getRawStringPbStyleWithColumns(40)));
  423. expect_eq(R"test(
  424. std::string s = R"pb(
  425. item_1: 1
  426. )pb";
  427. )test",
  428. format(R"test(
  429. std::string s = R"pb(
  430. item_1:1
  431. )pb";
  432. )test",
  433. getRawStringPbStyleWithColumns(40)));
  434. expect_eq(R"test(
  435. std::string s = R"pb(
  436. item_1: 1,
  437. item_2: 2
  438. )pb";
  439. )test",
  440. format(R"test(
  441. std::string s = R"pb(
  442. item_1:1, item_2:2
  443. )pb";
  444. )test",
  445. getRawStringPbStyleWithColumns(40)));
  446. expect_eq(R"test(
  447. std::string s = R"pb(
  448. book {
  449. title: "Alice's Adventures"
  450. author: "Lewis Caroll"
  451. }
  452. book {
  453. title: "Peter Pan"
  454. author: "J. M. Barrie"
  455. }
  456. )pb";
  457. )test",
  458. format(R"test(
  459. std::string s = R"pb(
  460. book { title: "Alice's Adventures" author: "Lewis Caroll" }
  461. book { title: "Peter Pan" author: "J. M. Barrie" }
  462. )pb";
  463. )test",
  464. getRawStringPbStyleWithColumns(40)));
  465. }
  466. TEST_F(FormatTestRawStrings, BreaksBeforeRawStrings) {
  467. expect_eq(R"test(
  468. ASSERT_TRUE(
  469. ParseFromString(R"pb(item_1: 1)pb"),
  470. ptr);)test",
  471. format(R"test(
  472. ASSERT_TRUE(ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
  473. getRawStringPbStyleWithColumns(40)));
  474. expect_eq(R"test(
  475. ASSERT_TRUE(toolong::ParseFromString(
  476. R"pb(item_1: 1)pb"),
  477. ptr);)test",
  478. format(R"test(
  479. ASSERT_TRUE(toolong::ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
  480. getRawStringPbStyleWithColumns(40)));
  481. expect_eq(R"test(
  482. ASSERT_TRUE(ParseFromString(
  483. R"pb(item_1: 1,
  484. item_2: 2)pb"),
  485. ptr);)test",
  486. format(R"test(
  487. ASSERT_TRUE(ParseFromString(R"pb(item_1: 1, item_2: 2)pb"), ptr);)test",
  488. getRawStringPbStyleWithColumns(40)));
  489. expect_eq(R"test(
  490. ASSERT_TRUE(
  491. ParseFromString(
  492. R"pb(item_1: 1 item_2: 2)pb"),
  493. ptr);)test",
  494. format(R"test(
  495. ASSERT_TRUE(ParseFromString(R"pb(item_1: 1 item_2: 2)pb"), ptr);)test",
  496. getRawStringPbStyleWithColumns(40)));
  497. }
  498. TEST_F(FormatTestRawStrings, RawStringsInOperands) {
  499. // Formats the raw string first operand of a binary operator expression.
  500. expect_eq(R"test(auto S = R"pb(item_1: 1)pb" + rest;)test",
  501. format(R"test(auto S = R"pb(item_1:1)pb" + rest;)test",
  502. getRawStringPbStyleWithColumns(40)));
  503. expect_eq(R"test(
  504. auto S = R"pb(item_1: 1, item_2: 2)pb" +
  505. rest;)test",
  506. format(R"test(
  507. auto S = R"pb(item_1:1,item_2:2)pb"+rest;)test",
  508. getRawStringPbStyleWithColumns(40)));
  509. expect_eq(R"test(
  510. auto S =
  511. R"pb(item_1: 1 item_2: 2)pb" + 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 = R"pb(item_1: 1,
  517. item_2: 2,
  518. item_3: 3)pb" + rest;)test",
  519. format(R"test(
  520. auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
  521. getRawStringPbStyleWithColumns(40)));
  522. expect_eq(R"test(
  523. auto S = R"pb(item_1: 1,
  524. item_2: 2,
  525. item_3: 3)pb" +
  526. longlongrest;)test",
  527. format(R"test(
  528. auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test",
  529. getRawStringPbStyleWithColumns(40)));
  530. // Formats the raw string second operand of a binary operator expression.
  531. expect_eq(R"test(auto S = first + R"pb(item_1: 1)pb";)test",
  532. format(R"test(auto S = first + R"pb(item_1:1)pb";)test",
  533. getRawStringPbStyleWithColumns(40)));
  534. expect_eq(R"test(
  535. auto S = first + R"pb(item_1: 1,
  536. item_2: 2)pb";)test",
  537. format(R"test(
  538. auto S = first+R"pb(item_1:1,item_2:2)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 = R"pb(item_1: 1,
  548. item_2: 2,
  549. item_3: 3)pb" + rest;)test",
  550. format(R"test(
  551. auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
  552. getRawStringPbStyleWithColumns(40)));
  553. expect_eq(R"test(
  554. auto S = R"pb(item_1: 1,
  555. item_2: 2,
  556. item_3: 3)pb" +
  557. longlongrest;)test",
  558. format(R"test(
  559. auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test",
  560. getRawStringPbStyleWithColumns(40)));
  561. // Formats the raw string operands in expressions.
  562. expect_eq(R"test(
  563. auto S = R"pb(item_1: 1)pb" +
  564. R"pb(item_2: 2)pb";
  565. )test",
  566. format(R"test(
  567. auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb";
  568. )test",
  569. getRawStringPbStyleWithColumns(40)));
  570. expect_eq(R"test(
  571. auto S = R"pb(item_1: 1)pb" +
  572. R"pb(item_2: 2)pb" +
  573. R"pb(item_3: 3)pb";
  574. )test",
  575. format(R"test(
  576. auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"+R"pb(item_3:3)pb";
  577. )test",
  578. getRawStringPbStyleWithColumns(40)));
  579. expect_eq(R"test(
  580. auto S = (count < 3)
  581. ? R"pb(item_1: 1)pb"
  582. : R"pb(item_2: 2)pb";
  583. )test",
  584. format(R"test(
  585. auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2)pb";
  586. )test",
  587. getRawStringPbStyleWithColumns(40)));
  588. expect_eq(R"test(
  589. auto S =
  590. (count < 3)
  591. ? R"pb(item_1: 1, item_2: 2)pb"
  592. : R"pb(item_3: 3)pb";
  593. )test",
  594. format(R"test(
  595. auto S=(count<3)?R"pb(item_1:1,item_2:2)pb":R"pb(item_3:3)pb";
  596. )test",
  597. getRawStringPbStyleWithColumns(40)));
  598. expect_eq(R"test(
  599. auto S =
  600. (count < 3)
  601. ? R"pb(item_1: 1)pb"
  602. : R"pb(item_2: 2, item_3: 3)pb";
  603. )test",
  604. format(R"test(
  605. auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2,item_3:3)pb";
  606. )test",
  607. getRawStringPbStyleWithColumns(40)));
  608. }
  609. TEST_F(FormatTestRawStrings, PrefixAndSuffixAlignment) {
  610. // Keep the suffix at the end of line if not on newline.
  611. expect_eq(R"test(
  612. int s() {
  613. auto S = PTP(
  614. R"pb(
  615. item_1: 1,
  616. item_2: 2)pb");
  617. })test",
  618. format(R"test(
  619. int s() {
  620. auto S = PTP(
  621. R"pb(
  622. item_1: 1,
  623. item_2: 2)pb");
  624. })test",
  625. getRawStringPbStyleWithColumns(20)));
  626. // Align the suffix with the surrounding indent if the prefix is not on
  627. // a line of its own.
  628. expect_eq(R"test(
  629. int s() {
  630. auto S = PTP(R"pb(
  631. item_1: 1,
  632. item_2: 2
  633. )pb");
  634. })test",
  635. format(R"test(
  636. int s() {
  637. auto S = PTP(R"pb(
  638. item_1: 1,
  639. item_2: 2
  640. )pb");
  641. })test",
  642. getRawStringPbStyleWithColumns(20)));
  643. // Align the prefix with the suffix if both the prefix and suffix are on a
  644. // line of their own.
  645. expect_eq(R"test(
  646. int s() {
  647. auto S = PTP(
  648. R"pb(
  649. item_1: 1,
  650. item_2: 2,
  651. )pb");
  652. })test",
  653. format(R"test(
  654. int s() {
  655. auto S = PTP(
  656. R"pb(
  657. item_1: 1,
  658. item_2: 2,
  659. )pb");
  660. })test",
  661. getRawStringPbStyleWithColumns(20)));
  662. }
  663. TEST_F(FormatTestRawStrings, EstimatesPenalty) {
  664. // The penalty for characters exceeding the column limit in the raw string
  665. // forces 'hh' to be put on a newline.
  666. expect_eq(R"test(
  667. ff(gggggg,
  668. hh(R"pb(key {
  669. i1: k1
  670. i2: k2
  671. })pb"));
  672. )test",
  673. format(R"test(
  674. ff(gggggg, hh(R"pb(key {
  675. i1: k1
  676. i2: k2
  677. })pb"));
  678. )test",
  679. getRawStringPbStyleWithColumns(20)));
  680. }
  681. TEST_F(FormatTestRawStrings, DontFormatNonRawStrings) {
  682. expect_eq(R"test(a = R"pb(key:value)";)test",
  683. format(R"test(a = R"pb(key:value)";)test",
  684. getRawStringPbStyleWithColumns(20)));
  685. }
  686. TEST_F(FormatTestRawStrings, FormatsRawStringsWithEnclosingFunctionName) {
  687. FormatStyle Style = getRawStringPbStyleWithColumns(40);
  688. Style.RawStringFormats[0].EnclosingFunctions.push_back(
  689. "PARSE_TEXT_PROTO");
  690. Style.RawStringFormats[0].EnclosingFunctions.push_back("ParseTextProto");
  691. expect_eq(R"test(a = PARSE_TEXT_PROTO(R"(key: value)");)test",
  692. format(R"test(a = PARSE_TEXT_PROTO(R"(key:value)");)test", Style));
  693. expect_eq(R"test(
  694. a = PARSE_TEXT_PROTO /**/ (
  695. /**/ R"(key: value)");)test",
  696. format(R"test(
  697. a = PARSE_TEXT_PROTO/**/(/**/R"(key:value)");)test",
  698. Style));
  699. expect_eq(R"test(
  700. a = ParseTextProto<ProtoType>(
  701. R"(key: value)");)test",
  702. format(R"test(
  703. a = ParseTextProto<ProtoType>(R"(key:value)");)test",
  704. Style));
  705. }
  706. TEST_F(FormatTestRawStrings, UpdatesToCanonicalDelimiters) {
  707. FormatStyle Style = getRawStringPbStyleWithColumns(25);
  708. Style.RawStringFormats[0].CanonicalDelimiter = "proto";
  709. expect_eq(R"test(a = R"proto(key: value)proto";)test",
  710. format(R"test(a = R"pb(key:value)pb";)test", Style));
  711. // Don't update to canonical delimiter if it occurs as a raw string suffix in
  712. // the raw string content.
  713. expect_eq(R"test(a = R"pb(key: ")proto")pb";)test",
  714. format(R"test(a = R"pb(key:")proto")pb";)test", Style));
  715. }
  716. } // end namespace
  717. } // end namespace format
  718. } // end namespace clang