1 | //===- unittest/Format/FormatReplacementTest.cpp - Formatting unit test ---===// |
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-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include "../Tooling/ReplacementTest.h" |
10 | #include "clang/Format/Format.h" |
11 | |
12 | namespace clang { |
13 | namespace tooling { |
14 | namespace { |
15 | |
16 | using format::FormatStyle; |
17 | using format::getLLVMStyle; |
18 | |
19 | TEST_F(ReplacementTest, FormatCodeAfterReplacements) { |
20 | // Column limit is 20. |
21 | std::string Code = "Type *a =\n" |
22 | " new Type();\n" |
23 | "g(iiiii, 0, jjjjj,\n" |
24 | " 0, kkkkk, 0, mm);\n" |
25 | "int bad = format ;" ; |
26 | std::string Expected = "auto a = new Type();\n" |
27 | "g(iiiii, nullptr,\n" |
28 | " jjjjj, nullptr,\n" |
29 | " kkkkk, nullptr,\n" |
30 | " mm);\n" |
31 | "int bad = format ;" ; |
32 | FileID ID = Context.createInMemoryFile(Name: "format.cpp" , Content: Code); |
33 | tooling::Replacements Replaces = toReplacements( |
34 | Replaces: {tooling::Replacement(Context.Sources, Context.getLocation(ID, Line: 1, Column: 1), 6, |
35 | "auto " ), |
36 | tooling::Replacement(Context.Sources, Context.getLocation(ID, Line: 3, Column: 10), 1, |
37 | "nullptr" ), |
38 | tooling::Replacement(Context.Sources, Context.getLocation(ID, Line: 4, Column: 3), 1, |
39 | "nullptr" ), |
40 | tooling::Replacement(Context.Sources, Context.getLocation(ID, Line: 4, Column: 13), 1, |
41 | "nullptr" )}); |
42 | |
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(E: FormattedReplaces.takeError()) << "\n" ; |
48 | auto Result = applyAllReplacements(Code, Replaces: *FormattedReplaces); |
49 | EXPECT_TRUE(static_cast<bool>(Result)); |
50 | EXPECT_EQ(Expected, *Result); |
51 | } |
52 | |
53 | TEST_F(ReplacementTest, SortIncludesAfterReplacement) { |
54 | std::string Code = "#include \"a.h\"\n" |
55 | "#include \"c.h\"\n" |
56 | "\n" |
57 | "int main() {\n" |
58 | " return 0;\n" |
59 | "}" ; |
60 | std::string Expected = "#include \"a.h\"\n" |
61 | "#include \"b.h\"\n" |
62 | "#include \"c.h\"\n" |
63 | "\n" |
64 | "int main() {\n" |
65 | " return 0;\n" |
66 | "}" ; |
67 | FileID ID = Context.createInMemoryFile(Name: "fix.cpp" , Content: Code); |
68 | tooling::Replacements Replaces = toReplacements( |
69 | Replaces: {tooling::Replacement(Context.Sources, Context.getLocation(ID, Line: 1, Column: 1), 0, |
70 | "#include \"b.h\"\n" )}); |
71 | |
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(E: FormattedReplaces.takeError()) << "\n" ; |
77 | auto Result = applyAllReplacements(Code, Replaces: *FormattedReplaces); |
78 | EXPECT_TRUE(static_cast<bool>(Result)); |
79 | EXPECT_EQ(Expected, *Result); |
80 | } |
81 | |
82 | } // namespace |
83 | } // namespace tooling |
84 | } // namespace clang |
85 | |