1 //===- unittest/Format/SortIncludesTest.cpp - Include sort 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 
10 #include "FormatTestUtils.h"
11 #include "clang/Format/Format.h"
12 #include "llvm/Support/Debug.h"
13 #include "gtest/gtest.h"
14 
15 #define DEBUG_TYPE "format-test"
16 
17 namespace clang {
18 namespace format {
19 namespace {
20 
21 class SortIncludesTest : public ::testing::Test {
22 protected:
23   std::string sort(llvm::StringRef Code, StringRef FileName = "input.cpp") {
24     std::vector<tooling::Range> Ranges(1, tooling::Range(0, Code.size()));
25     std::string Sorted =
26         applyAllReplacements(Code, sortIncludes(Style, Code, Ranges, FileName));
27     return applyAllReplacements(Sorted,
28                                 reformat(Style, Sorted, Ranges, FileName));
29   }
30 
31   FormatStyle Style = getLLVMStyle();
32 };
33 
34 TEST_F(SortIncludesTest, BasicSorting) {
35   EXPECT_EQ("#include \"a.h\"\n"
36             "#include \"b.h\"\n"
37             "#include \"c.h\"\n",
38             sort("#include \"a.h\"\n"
39                  "#include \"c.h\"\n"
40                  "#include \"b.h\"\n"));
41 }
42 
43 TEST_F(SortIncludesTest, FixTrailingComments) {
44   EXPECT_EQ("#include \"a.h\"  // comment\n"
45             "#include \"bb.h\" // comment\n"
46             "#include \"ccc.h\"\n",
47             sort("#include \"a.h\" // comment\n"
48                  "#include \"ccc.h\"\n"
49                  "#include \"bb.h\" // comment\n"));
50 }
51 
52 TEST_F(SortIncludesTest, LeadingWhitespace) {
53   EXPECT_EQ("#include \"a.h\"\n"
54             "#include \"b.h\"\n"
55             "#include \"c.h\"\n",
56             sort(" #include \"a.h\"\n"
57                  "  #include \"c.h\"\n"
58                  "   #include \"b.h\"\n"));
59   EXPECT_EQ("#include \"a.h\"\n"
60             "#include \"b.h\"\n"
61             "#include \"c.h\"\n",
62             sort("# include \"a.h\"\n"
63                  "#  include \"c.h\"\n"
64                  "#   include \"b.h\"\n"));
65 }
66 
67 TEST_F(SortIncludesTest, GreaterInComment) {
68   EXPECT_EQ("#include \"a.h\"\n"
69             "#include \"b.h\" // >\n"
70             "#include \"c.h\"\n",
71             sort("#include \"a.h\"\n"
72                  "#include \"c.h\"\n"
73                  "#include \"b.h\" // >\n"));
74 }
75 
76 TEST_F(SortIncludesTest, SortsLocallyInEachBlock) {
77   EXPECT_EQ("#include \"a.h\"\n"
78             "#include \"c.h\"\n"
79             "\n"
80             "#include \"b.h\"\n",
81             sort("#include \"a.h\"\n"
82                  "#include \"c.h\"\n"
83                  "\n"
84                  "#include \"b.h\"\n"));
85 }
86 
87 TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
88   EXPECT_EQ("#include \"a.h\"\n"
89             "#include \"c.h\"\n"
90             "#include <b.h>\n"
91             "#include <d.h>\n",
92             sort("#include <d.h>\n"
93                  "#include <b.h>\n"
94                  "#include \"c.h\"\n"
95                  "#include \"a.h\"\n"));
96 
97   Style = getGoogleStyle(FormatStyle::LK_Cpp);
98   EXPECT_EQ("#include <b.h>\n"
99             "#include <d.h>\n"
100             "#include \"a.h\"\n"
101             "#include \"c.h\"\n",
102             sort("#include <d.h>\n"
103                  "#include <b.h>\n"
104                  "#include \"c.h\"\n"
105                  "#include \"a.h\"\n"));
106 }
107 
108 TEST_F(SortIncludesTest, HandlesMultilineIncludes) {
109   EXPECT_EQ("#include \"a.h\"\n"
110             "#include \"b.h\"\n"
111             "#include \"c.h\"\n",
112             sort("#include \"a.h\"\n"
113                  "#include \\\n"
114                  "\"c.h\"\n"
115                  "#include \"b.h\"\n"));
116 }
117 
118 TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
119   EXPECT_EQ("#include \"llvm/a.h\"\n"
120             "#include \"b.h\"\n"
121             "#include \"c.h\"\n",
122             sort("#include \"llvm/a.h\"\n"
123                  "#include \"c.h\"\n"
124                  "#include \"b.h\"\n"));
125 
126   // Don't do this in headers.
127   EXPECT_EQ("#include \"b.h\"\n"
128             "#include \"c.h\"\n"
129             "#include \"llvm/a.h\"\n",
130             sort("#include \"llvm/a.h\"\n"
131                  "#include \"c.h\"\n"
132                  "#include \"b.h\"\n",
133                  "some_header.h"));
134 }
135 
136 } // end namespace
137 } // end namespace format
138 } // end namespace clang
139