1 //===- unittest/Format/FormatReplacementTest.cpp - Formatting unit test ---===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "../Tooling/ReplacementTest.h"
10 #include "clang/Format/Format.h"
16 using format::FormatStyle
;
17 using format::getLLVMStyle
;
19 TEST_F(ReplacementTest
, FormatCodeAfterReplacements
) {
20 // Column limit is 20.
21 std::string Code
= "Type *a =\n"
23 "g(iiiii, 0, jjjjj,\n"
24 " 0, kkkkk, 0, mm);\n"
26 std::string Expected
= "auto a = new Type();\n"
32 FileID ID
= Context
.createInMemoryFile("format.cpp", Code
);
33 tooling::Replacements Replaces
= toReplacements(
34 {tooling::Replacement(Context
.Sources
, Context
.getLocation(ID
, 1, 1), 6,
36 tooling::Replacement(Context
.Sources
, Context
.getLocation(ID
, 3, 10), 1,
38 tooling::Replacement(Context
.Sources
, Context
.getLocation(ID
, 4, 3), 1,
40 tooling::Replacement(Context
.Sources
, Context
.getLocation(ID
, 4, 13), 1,
43 FormatStyle Style
= getLLVMStyle();
44 Style
.ColumnLimit
= 20; // Set column limit to 20 to increase readibility.
45 auto FormattedReplaces
= formatReplacements(Code
, Replaces
, Style
);
46 EXPECT_TRUE(static_cast<bool>(FormattedReplaces
))
47 << llvm::toString(FormattedReplaces
.takeError()) << "\n";
48 auto Result
= applyAllReplacements(Code
, *FormattedReplaces
);
49 EXPECT_TRUE(static_cast<bool>(Result
));
50 EXPECT_EQ(Expected
, *Result
);
53 TEST_F(ReplacementTest
, SortIncludesAfterReplacement
) {
54 std::string Code
= "#include \"a.h\"\n"
60 std::string Expected
= "#include \"a.h\"\n"
67 FileID ID
= Context
.createInMemoryFile("fix.cpp", Code
);
68 tooling::Replacements Replaces
= toReplacements(
69 {tooling::Replacement(Context
.Sources
, Context
.getLocation(ID
, 1, 1), 0,
70 "#include \"b.h\"\n")});
72 FormatStyle Style
= getLLVMStyle();
73 Style
.SortIncludes
= FormatStyle::SI_CaseSensitive
;
74 auto FormattedReplaces
= formatReplacements(Code
, Replaces
, Style
);
75 EXPECT_TRUE(static_cast<bool>(FormattedReplaces
))
76 << llvm::toString(FormattedReplaces
.takeError()) << "\n";
77 auto Result
= applyAllReplacements(Code
, *FormattedReplaces
);
78 EXPECT_TRUE(static_cast<bool>(Result
));
79 EXPECT_EQ(Expected
, *Result
);
83 } // namespace tooling