brintos

brintos / llvm-project-archived public Read only

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