FormatTestRawStrings.cpp 22 KB

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