FormatTestRawStrings.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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 = {{/*Delimiter=*/"pb",
  57. /*Kind=*/FormatStyle::LK_TextProto,
  58. /*BasedOnStyle=*/"google"}};
  59. return Style;
  60. }
  61. FormatStyle getRawStringLLVMCppStyleBasedOn(std::string BasedOnStyle) {
  62. FormatStyle Style = getLLVMStyle();
  63. Style.RawStringFormats = {{/*Delimiter=*/"cpp",
  64. /*Kind=*/FormatStyle::LK_Cpp, BasedOnStyle}};
  65. return Style;
  66. }
  67. FormatStyle getRawStringGoogleCppStyleBasedOn(std::string BasedOnStyle) {
  68. FormatStyle Style = getGoogleStyle(FormatStyle::LK_Cpp);
  69. Style.RawStringFormats = {{/*Delimiter=*/"cpp",
  70. /*Kind=*/FormatStyle::LK_Cpp, BasedOnStyle}};
  71. return Style;
  72. }
  73. // Gcc 4.8 doesn't support raw string literals in macros, which breaks some
  74. // build bots. We use this function instead.
  75. void expect_eq(const std::string Expected, const std::string Actual) {
  76. EXPECT_EQ(Expected, Actual);
  77. }
  78. };
  79. TEST_F(FormatTestRawStrings, ReformatsAccordingToBaseStyle) {
  80. // llvm style puts '*' on the right.
  81. // google style puts '*' on the left.
  82. // Use the llvm style if the raw string style has no BasedOnStyle.
  83. expect_eq(R"test(int *i = R"cpp(int *p = nullptr;)cpp")test",
  84. format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test",
  85. getRawStringLLVMCppStyleBasedOn("")));
  86. // Use the google style if the raw string style has BasedOnStyle=google.
  87. expect_eq(R"test(int *i = R"cpp(int* p = nullptr;)cpp")test",
  88. format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test",
  89. getRawStringLLVMCppStyleBasedOn("google")));
  90. // Use the llvm style if the raw string style has no BasedOnStyle=llvm.
  91. expect_eq(R"test(int* i = R"cpp(int *p = nullptr;)cpp")test",
  92. format(R"test(int * i = R"cpp(int * p = nullptr;)cpp")test",
  93. getRawStringGoogleCppStyleBasedOn("llvm")));
  94. }
  95. TEST_F(FormatTestRawStrings, MatchesDelimitersCaseSensitively) {
  96. // Don't touch the 'PB' raw string, format the 'pb' raw string.
  97. expect_eq(R"test(
  98. s = R"PB(item:1)PB";
  99. t = R"pb(item: 1)pb";)test",
  100. format(R"test(
  101. s = R"PB(item:1)PB";
  102. t = R"pb(item:1)pb";)test",
  103. getRawStringPbStyleWithColumns(40)));
  104. FormatStyle MixedStyle = getLLVMStyle();
  105. MixedStyle.RawStringFormats = {
  106. {/*Delimiter=*/"cpp", /*Kind=*/FormatStyle::LK_Cpp,
  107. /*BasedOnStyle=*/"llvm"},
  108. {/*Delimiter=*/"CPP", /*Kind=*/FormatStyle::LK_Cpp,
  109. /*BasedOnStyle=*/"google"}};
  110. // Format the 'cpp' raw string with '*' on the right.
  111. // Format the 'CPP' raw string with '*' on the left.
  112. // Do not format the 'Cpp' raw string.
  113. // Do not format non-raw strings.
  114. expect_eq(R"test(
  115. a = R"cpp(int *i = 0;)cpp";
  116. b = R"CPP(int* j = 0;)CPP";
  117. c = R"Cpp(int * k = 0;)Cpp";
  118. d = R"cpp(int * k = 0;)Cpp";)test",
  119. format(R"test(
  120. a = R"cpp(int * i = 0;)cpp";
  121. b = R"CPP(int * j = 0;)CPP";
  122. c = R"Cpp(int * k = 0;)Cpp";
  123. d = R"cpp(int * k = 0;)Cpp";)test",
  124. MixedStyle));
  125. }
  126. TEST_F(FormatTestRawStrings, ReformatsShortRawStringsOnSingleLine) {
  127. expect_eq(
  128. R"test(P p = TP(R"pb()pb");)test",
  129. format(
  130. R"test(P p = TP(R"pb( )pb");)test",
  131. getRawStringPbStyleWithColumns(40)));
  132. expect_eq(
  133. R"test(P p = TP(R"pb(item_1: 1)pb");)test",
  134. format(
  135. R"test(P p = TP(R"pb(item_1:1)pb");)test",
  136. getRawStringPbStyleWithColumns(40)));
  137. expect_eq(
  138. R"test(P p = TP(R"pb(item_1: 1)pb");)test",
  139. format(
  140. R"test(P p = TP(R"pb( item_1 : 1 )pb");)test",
  141. getRawStringPbStyleWithColumns(40)));
  142. expect_eq(
  143. R"test(P p = TP(R"pb(item_1: 1 item_2: 2)pb");)test",
  144. format(
  145. R"test(P p = TP(R"pb(item_1:1 item_2:2)pb");)test",
  146. getRawStringPbStyleWithColumns(40)));
  147. expect_eq(
  148. R"test(P p = TP(R"pb(item_1 <1> item_2: {2})pb");)test",
  149. format(
  150. R"test(P p = TP(R"pb(item_1<1> item_2:{2})pb");)test",
  151. getRawStringPbStyleWithColumns(40)));
  152. // Merge two short lines into one.
  153. expect_eq(R"test(
  154. std::string s = R"pb(
  155. item_1: 1 item_2: 2
  156. )pb";
  157. )test",
  158. format(R"test(
  159. std::string s = R"pb(
  160. item_1:1
  161. item_2:2
  162. )pb";
  163. )test",
  164. getRawStringPbStyleWithColumns(40)));
  165. }
  166. TEST_F(FormatTestRawStrings, BreaksRawStringsExceedingColumnLimit) {
  167. expect_eq(R"test(
  168. P p = TPPPPPPPPPPPPPPP(
  169. R"pb(item_1: 1, item_2: 2)pb");)test",
  170. format(R"test(
  171. P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2)pb");)test",
  172. getRawStringPbStyleWithColumns(40)));
  173. expect_eq(R"test(
  174. P p =
  175. TPPPPPPPPPPPPPPP(
  176. R"pb(item_1: 1,
  177. item_2: 2,
  178. item_3: 3)pb");)test",
  179. format(R"test(
  180. P p = TPPPPPPPPPPPPPPP(R"pb(item_1: 1, item_2: 2, item_3: 3)pb");)test",
  181. getRawStringPbStyleWithColumns(40)));
  182. expect_eq(R"test(
  183. P p = TP(R"pb(item_1 <1>
  184. item_2: <2>
  185. item_3 {})pb");)test",
  186. format(R"test(
  187. P p = TP(R"pb(item_1<1> item_2:<2> item_3{ })pb");)test",
  188. getRawStringPbStyleWithColumns(40)));
  189. expect_eq(
  190. R"test(
  191. P p = TP(R"pb(item_1: 1,
  192. item_2: 2,
  193. item_3: 3,
  194. item_4: 4)pb");)test",
  195. format(
  196. R"test(
  197. P p = TP(R"pb(item_1: 1, item_2: 2, item_3: 3, item_4: 4)pb");)test",
  198. getRawStringPbStyleWithColumns(40)));
  199. expect_eq(R"test(
  200. P p = TPPPPPPPPPPPPPPP(
  201. R"pb(item_1 <1>,
  202. item_2: {2},
  203. item_3: <3>,
  204. item_4: {4})pb");)test",
  205. format(R"test(
  206. P p = TPPPPPPPPPPPPPPP(R"pb(item_1<1>, item_2: {2}, item_3: <3>, item_4:{4})pb");)test",
  207. getRawStringPbStyleWithColumns(40)));
  208. // Breaks before a short raw string exceeding the column limit.
  209. expect_eq(R"test(
  210. FFFFFFFFFFFFFFFFFFFFFFFFFFF(
  211. R"pb(key: 1)pb");
  212. P p = TPPPPPPPPPPPPPPPPPPPP(
  213. R"pb(key: 2)pb");
  214. auto TPPPPPPPPPPPPPPPPPPPP =
  215. R"pb(key: 3)pb";
  216. P p = TPPPPPPPPPPPPPPPPPPPP(
  217. R"pb(i: 1, j: 2)pb");
  218. int f(string s) {
  219. FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(
  220. R"pb(key: 1)pb");
  221. P p = TPPPPPPPPPPPPPPPPPPPP(
  222. R"pb(key: 2)pb");
  223. auto TPPPPPPPPPPPPPPPPPPPP =
  224. R"pb(key: 3)pb";
  225. if (s.empty())
  226. P p = TPPPPPPPPPPPPPPPPPPPP(
  227. R"pb(i: 1, j: 2)pb");
  228. }
  229. )test",
  230. format(R"test(
  231. FFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
  232. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
  233. auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
  234. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
  235. int f(string s) {
  236. FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF(R"pb(key:1)pb");
  237. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(key:2)pb");
  238. auto TPPPPPPPPPPPPPPPPPPPP = R"pb(key:3)pb";
  239. if (s.empty())
  240. P p = TPPPPPPPPPPPPPPPPPPPP(R"pb(i: 1, j:2)pb");
  241. }
  242. )test",
  243. getRawStringPbStyleWithColumns(40)));
  244. }
  245. TEST_F(FormatTestRawStrings, FormatsRawStringArguments) {
  246. expect_eq(R"test(
  247. P p = TP(R"pb(key {1})pb", param_2);)test",
  248. format(R"test(
  249. P p = TP(R"pb(key{1})pb",param_2);)test",
  250. getRawStringPbStyleWithColumns(40)));
  251. expect_eq(R"test(
  252. PPPPPPPPPPPPP(R"pb(keykeyk)pb",
  253. param_2);)test",
  254. format(R"test(
  255. PPPPPPPPPPPPP(R"pb(keykeyk)pb", param_2);)test",
  256. getRawStringPbStyleWithColumns(40)));
  257. expect_eq(R"test(
  258. P p =
  259. TP(R"pb(item: {i: 1, s: 's'}
  260. item: {i: 2, s: 't'})pb");)test",
  261. format(R"test(
  262. P p = TP(R"pb(item: {i: 1, s: 's'} item: {i: 2, s: 't'})pb");)test",
  263. getRawStringPbStyleWithColumns(40)));
  264. expect_eq(R"test(
  265. FFFFFFFFFFFFFFFFFFF(
  266. R"pb(key: "value")pb",
  267. R"pb(key2: "value")pb");)test",
  268. format(R"test(
  269. FFFFFFFFFFFFFFFFFFF(R"pb(key: "value")pb", R"pb(key2: "value")pb");)test",
  270. getRawStringPbStyleWithColumns(40)));
  271. // Formats the first out of two arguments.
  272. expect_eq(R"test(
  273. FFFFFFFF(R"pb(key: 1)pb", argument2);
  274. struct S {
  275. const s =
  276. f(R"pb(key: 1)pb", argument2);
  277. void f() {
  278. if (gol)
  279. return g(R"pb(key: 1)pb",
  280. 132789237);
  281. return g(R"pb(key: 1)pb", "172893");
  282. }
  283. };)test",
  284. format(R"test(
  285. FFFFFFFF(R"pb(key:1)pb", argument2);
  286. struct S {
  287. const s = f(R"pb(key:1)pb", argument2);
  288. void f() {
  289. if (gol)
  290. return g(R"pb(key:1)pb", 132789237);
  291. return g(R"pb(key:1)pb", "172893");
  292. }
  293. };)test",
  294. getRawStringPbStyleWithColumns(40)));
  295. // Formats the second out of two arguments.
  296. expect_eq(R"test(
  297. FFFFFFFF(argument1, R"pb(key: 2)pb");
  298. struct S {
  299. const s =
  300. f(argument1, R"pb(key: 2)pb");
  301. void f() {
  302. if (gol)
  303. return g(12784137,
  304. R"pb(key: 2)pb");
  305. return g(17283122, R"pb(key: 2)pb");
  306. }
  307. };)test",
  308. format(R"test(
  309. FFFFFFFF(argument1, R"pb(key:2)pb");
  310. struct S {
  311. const s = f(argument1, R"pb(key:2)pb");
  312. void f() {
  313. if (gol)
  314. return g(12784137, R"pb(key:2)pb");
  315. return g(17283122, R"pb(key:2)pb");
  316. }
  317. };)test",
  318. getRawStringPbStyleWithColumns(40)));
  319. // Formats two short raw string arguments.
  320. expect_eq(R"test(
  321. FFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
  322. format(R"test(
  323. FFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
  324. getRawStringPbStyleWithColumns(40)));
  325. // TODO(krasimir): The original source code fits on one line, so the
  326. // non-optimizing formatter is chosen. But after the formatting in protos is
  327. // made, the code doesn't fit on one line anymore and further formatting
  328. // splits it.
  329. //
  330. // Should we disable raw string formatting for the non-optimizing formatter?
  331. expect_eq(R"test(
  332. FFFFFFF(R"pb(key: 1)pb", R"pb(key: 2)pb");)test",
  333. format(R"test(
  334. FFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
  335. getRawStringPbStyleWithColumns(40)));
  336. // Formats two short raw string arguments, puts second on newline.
  337. expect_eq(R"test(
  338. FFFFFFFF(R"pb(key: 1)pb",
  339. R"pb(key: 2)pb");)test",
  340. format(R"test(
  341. FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");)test",
  342. getRawStringPbStyleWithColumns(40)));
  343. // Formats both arguments.
  344. expect_eq(R"test(
  345. FFFFFFFF(R"pb(key: 1)pb",
  346. R"pb(key: 2)pb");
  347. struct S {
  348. const s = f(R"pb(key: 1)pb",
  349. R"pb(key: 2)pb");
  350. void f() {
  351. if (gol)
  352. return g(R"pb(key: 1)pb",
  353. R"pb(key: 2)pb");
  354. return g(R"pb(k1)pb", R"pb(k2)pb");
  355. }
  356. };)test",
  357. format(R"test(
  358. FFFFFFFF(R"pb(key:1)pb", R"pb(key:2)pb");
  359. struct S {
  360. const s = f(R"pb(key:1)pb", R"pb(key:2)pb");
  361. void f() {
  362. if (gol)
  363. return g(R"pb(key:1)pb", R"pb(key:2)pb");
  364. return g(R"pb( k1 )pb", R"pb( k2 )pb");
  365. }
  366. };)test",
  367. getRawStringPbStyleWithColumns(40)));
  368. }
  369. TEST_F(FormatTestRawStrings, RawStringStartingWithNewlines) {
  370. expect_eq(R"test(
  371. std::string s = R"pb(
  372. item_1: 1
  373. )pb";
  374. )test",
  375. format(R"test(
  376. std::string s = R"pb(
  377. item_1:1
  378. )pb";
  379. )test",
  380. getRawStringPbStyleWithColumns(40)));
  381. expect_eq(R"test(
  382. std::string s = R"pb(
  383. item_1: 1
  384. )pb";
  385. )test",
  386. format(R"test(
  387. std::string s = R"pb(
  388. item_1:1
  389. )pb";
  390. )test",
  391. getRawStringPbStyleWithColumns(40)));
  392. expect_eq(R"test(
  393. std::string s = R"pb(
  394. item_1: 1
  395. )pb";
  396. )test",
  397. format(R"test(
  398. std::string s = R"pb(
  399. item_1:1
  400. )pb";
  401. )test",
  402. getRawStringPbStyleWithColumns(40)));
  403. expect_eq(R"test(
  404. std::string s = R"pb(
  405. item_1: 1,
  406. item_2: 2
  407. )pb";
  408. )test",
  409. format(R"test(
  410. std::string s = R"pb(
  411. item_1:1, item_2:2
  412. )pb";
  413. )test",
  414. getRawStringPbStyleWithColumns(40)));
  415. expect_eq(R"test(
  416. std::string s = R"pb(
  417. book {
  418. title: "Alice's Adventures"
  419. author: "Lewis Caroll"
  420. }
  421. book {
  422. title: "Peter Pan"
  423. author: "J. M. Barrie"
  424. }
  425. )pb";
  426. )test",
  427. format(R"test(
  428. std::string s = R"pb(
  429. book { title: "Alice's Adventures" author: "Lewis Caroll" }
  430. book { title: "Peter Pan" author: "J. M. Barrie" }
  431. )pb";
  432. )test",
  433. getRawStringPbStyleWithColumns(40)));
  434. }
  435. TEST_F(FormatTestRawStrings, BreaksBeforeRawStrings) {
  436. expect_eq(R"test(
  437. ASSERT_TRUE(
  438. ParseFromString(R"pb(item_1: 1)pb"),
  439. ptr);)test",
  440. format(R"test(
  441. ASSERT_TRUE(ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
  442. getRawStringPbStyleWithColumns(40)));
  443. expect_eq(R"test(
  444. ASSERT_TRUE(toolong::ParseFromString(
  445. R"pb(item_1: 1)pb"),
  446. ptr);)test",
  447. format(R"test(
  448. ASSERT_TRUE(toolong::ParseFromString(R"pb(item_1: 1)pb"), ptr);)test",
  449. getRawStringPbStyleWithColumns(40)));
  450. expect_eq(R"test(
  451. ASSERT_TRUE(ParseFromString(
  452. R"pb(item_1: 1,
  453. item_2: 2)pb"),
  454. ptr);)test",
  455. format(R"test(
  456. ASSERT_TRUE(ParseFromString(R"pb(item_1: 1, item_2: 2)pb"), ptr);)test",
  457. getRawStringPbStyleWithColumns(40)));
  458. expect_eq(R"test(
  459. ASSERT_TRUE(
  460. ParseFromString(
  461. R"pb(item_1: 1 item_2: 2)pb"),
  462. ptr);)test",
  463. format(R"test(
  464. ASSERT_TRUE(ParseFromString(R"pb(item_1: 1 item_2: 2)pb"), ptr);)test",
  465. getRawStringPbStyleWithColumns(40)));
  466. }
  467. TEST_F(FormatTestRawStrings, RawStringsInOperands) {
  468. // Formats the raw string first operand of a binary operator expression.
  469. expect_eq(R"test(auto S = R"pb(item_1: 1)pb" + rest;)test",
  470. format(R"test(auto S = R"pb(item_1:1)pb" + rest;)test",
  471. getRawStringPbStyleWithColumns(40)));
  472. expect_eq(R"test(
  473. auto S = R"pb(item_1: 1, item_2: 2)pb" +
  474. rest;)test",
  475. format(R"test(
  476. auto S = R"pb(item_1:1,item_2:2)pb"+rest;)test",
  477. getRawStringPbStyleWithColumns(40)));
  478. expect_eq(R"test(
  479. auto S =
  480. R"pb(item_1: 1 item_2: 2)pb" + rest;)test",
  481. format(R"test(
  482. auto S = R"pb(item_1:1 item_2:2)pb"+rest;)test",
  483. getRawStringPbStyleWithColumns(40)));
  484. expect_eq(R"test(
  485. auto S = R"pb(item_1: 1,
  486. item_2: 2,
  487. item_3: 3)pb" + rest;)test",
  488. format(R"test(
  489. auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+rest;)test",
  490. getRawStringPbStyleWithColumns(40)));
  491. expect_eq(R"test(
  492. auto S = R"pb(item_1: 1,
  493. item_2: 2,
  494. item_3: 3)pb" +
  495. longlongrest;)test",
  496. format(R"test(
  497. auto S = R"pb(item_1:1,item_2:2,item_3:3)pb"+longlongrest;)test",
  498. getRawStringPbStyleWithColumns(40)));
  499. // Formats the raw string second operand of a binary operator expression.
  500. expect_eq(R"test(auto S = first + R"pb(item_1: 1)pb";)test",
  501. format(R"test(auto S = first + R"pb(item_1:1)pb";)test",
  502. getRawStringPbStyleWithColumns(40)));
  503. expect_eq(R"test(
  504. auto S = first + R"pb(item_1: 1,
  505. item_2: 2)pb";)test",
  506. format(R"test(
  507. auto S = first+R"pb(item_1:1,item_2:2)pb";)test",
  508. getRawStringPbStyleWithColumns(40)));
  509. expect_eq(R"test(
  510. auto S = first + R"pb(item_1: 1
  511. item_2: 2)pb";)test",
  512. format(R"test(
  513. auto S = first+R"pb(item_1:1 item_2:2)pb";)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 operands in expressions.
  531. expect_eq(R"test(
  532. auto S = R"pb(item_1: 1)pb" +
  533. R"pb(item_2: 2)pb";
  534. )test",
  535. format(R"test(
  536. auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb";
  537. )test",
  538. getRawStringPbStyleWithColumns(40)));
  539. expect_eq(R"test(
  540. auto S = R"pb(item_1: 1)pb" +
  541. R"pb(item_2: 2)pb" +
  542. R"pb(item_3: 3)pb";
  543. )test",
  544. format(R"test(
  545. auto S=R"pb(item_1:1)pb"+R"pb(item_2:2)pb"+R"pb(item_3:3)pb";
  546. )test",
  547. getRawStringPbStyleWithColumns(40)));
  548. expect_eq(R"test(
  549. auto S = (count < 3)
  550. ? R"pb(item_1: 1)pb"
  551. : R"pb(item_2: 2)pb";
  552. )test",
  553. format(R"test(
  554. auto S=(count<3)?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 =
  559. (count < 3)
  560. ? R"pb(item_1: 1, item_2: 2)pb"
  561. : R"pb(item_3: 3)pb";
  562. )test",
  563. format(R"test(
  564. auto S=(count<3)?R"pb(item_1:1,item_2:2)pb":R"pb(item_3:3)pb";
  565. )test",
  566. getRawStringPbStyleWithColumns(40)));
  567. expect_eq(R"test(
  568. auto S =
  569. (count < 3)
  570. ? R"pb(item_1: 1)pb"
  571. : R"pb(item_2: 2, item_3: 3)pb";
  572. )test",
  573. format(R"test(
  574. auto S=(count<3)?R"pb(item_1:1)pb":R"pb(item_2:2,item_3:3)pb";
  575. )test",
  576. getRawStringPbStyleWithColumns(40)));
  577. }
  578. TEST_F(FormatTestRawStrings, PrefixAndSuffixAlignment) {
  579. // Keep the suffix at the end of line if not on newline.
  580. expect_eq(R"test(
  581. int s() {
  582. auto S = PTP(
  583. R"pb(
  584. item_1: 1,
  585. item_2: 2)pb");
  586. })test",
  587. format(R"test(
  588. int s() {
  589. auto S = PTP(
  590. R"pb(
  591. item_1: 1,
  592. item_2: 2)pb");
  593. })test",
  594. getRawStringPbStyleWithColumns(20)));
  595. // Align the suffix with the surrounding FirstIndent if the prefix is not on
  596. // a line of its own.
  597. expect_eq(R"test(
  598. int s() {
  599. auto S = PTP(
  600. R"pb(
  601. item_1: 1,
  602. item_2: 2
  603. )pb");
  604. })test",
  605. format(R"test(
  606. int s() {
  607. auto S = PTP(R"pb(
  608. item_1: 1,
  609. item_2: 2
  610. )pb");
  611. })test",
  612. getRawStringPbStyleWithColumns(20)));
  613. // Align the prefix with the suffix if both the prefix and suffix are on a
  614. // line of their 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(
  626. R"pb(
  627. item_1: 1,
  628. item_2: 2,
  629. )pb");
  630. })test",
  631. getRawStringPbStyleWithColumns(20)));
  632. }
  633. TEST_F(FormatTestRawStrings, EstimatesPenalty) {
  634. // The penalty for characters exceeding the column limit in the raw string
  635. // forces 'hh' to be put on a newline.
  636. expect_eq(R"test(
  637. ff(gggggg,
  638. hh(R"pb(key {
  639. i1: k1
  640. i2: k2
  641. })pb"));
  642. )test",
  643. format(R"test(
  644. ff(gggggg, hh(R"pb(key {
  645. i1: k1
  646. i2: k2
  647. })pb"));
  648. )test",
  649. getRawStringPbStyleWithColumns(20)));
  650. }
  651. TEST_F(FormatTestRawStrings, DontFormatNonRawStrings) {
  652. expect_eq(R"test(a = R"pb(key:value)";)test",
  653. format(R"test(a = R"pb(key:value)";)test",
  654. getRawStringPbStyleWithColumns(20)));
  655. }
  656. } // end namespace
  657. } // end namespace format
  658. } // end namespace clang