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, MixIncludeAndImport) {
44   EXPECT_EQ("#include \"a.h\"\n"
45             "#import \"b.h\"\n"
46             "#include \"c.h\"\n",
47             sort("#include \"a.h\"\n"
48                  "#include \"c.h\"\n"
49                  "#import \"b.h\"\n"));
50 }
51 
52 TEST_F(SortIncludesTest, FixTrailingComments) {
53   EXPECT_EQ("#include \"a.h\"  // comment\n"
54             "#include \"bb.h\" // comment\n"
55             "#include \"ccc.h\"\n",
56             sort("#include \"a.h\" // comment\n"
57                  "#include \"ccc.h\"\n"
58                  "#include \"bb.h\" // comment\n"));
59 }
60 
61 TEST_F(SortIncludesTest, LeadingWhitespace) {
62   EXPECT_EQ("#include \"a.h\"\n"
63             "#include \"b.h\"\n"
64             "#include \"c.h\"\n",
65             sort(" #include \"a.h\"\n"
66                  "  #include \"c.h\"\n"
67                  "   #include \"b.h\"\n"));
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, GreaterInComment) {
77   EXPECT_EQ("#include \"a.h\"\n"
78             "#include \"b.h\" // >\n"
79             "#include \"c.h\"\n",
80             sort("#include \"a.h\"\n"
81                  "#include \"c.h\"\n"
82                  "#include \"b.h\" // >\n"));
83 }
84 
85 TEST_F(SortIncludesTest, SortsLocallyInEachBlock) {
86   EXPECT_EQ("#include \"a.h\"\n"
87             "#include \"c.h\"\n"
88             "\n"
89             "#include \"b.h\"\n",
90             sort("#include \"a.h\"\n"
91                  "#include \"c.h\"\n"
92                  "\n"
93                  "#include \"b.h\"\n"));
94 }
95 
96 TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
97   EXPECT_EQ("#include \"a.h\"\n"
98             "#include \"c.h\"\n"
99             "#include <b.h>\n"
100             "#include <d.h>\n",
101             sort("#include <d.h>\n"
102                  "#include <b.h>\n"
103                  "#include \"c.h\"\n"
104                  "#include \"a.h\"\n"));
105 
106   Style = getGoogleStyle(FormatStyle::LK_Cpp);
107   EXPECT_EQ("#include <b.h>\n"
108             "#include <d.h>\n"
109             "#include \"a.h\"\n"
110             "#include \"c.h\"\n",
111             sort("#include <d.h>\n"
112                  "#include <b.h>\n"
113                  "#include \"c.h\"\n"
114                  "#include \"a.h\"\n"));
115 }
116 
117 TEST_F(SortIncludesTest, HandlesMultilineIncludes) {
118   EXPECT_EQ("#include \"a.h\"\n"
119             "#include \"b.h\"\n"
120             "#include \"c.h\"\n",
121             sort("#include \"a.h\"\n"
122                  "#include \\\n"
123                  "\"c.h\"\n"
124                  "#include \"b.h\"\n"));
125 }
126 
127 TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
128   EXPECT_EQ("#include \"llvm/a.h\"\n"
129             "#include \"b.h\"\n"
130             "#include \"c.h\"\n",
131             sort("#include \"llvm/a.h\"\n"
132                  "#include \"c.h\"\n"
133                  "#include \"b.h\"\n"));
134   EXPECT_EQ("#include \"llvm/a.h\"\n"
135             "#include \"b.h\"\n"
136             "#include \"c.h\"\n",
137             sort("#include \"llvm/a.h\"\n"
138                  "#include \"c.h\"\n"
139                  "#include \"b.h\"\n",
140                  "input.mm"));
141 
142   // Don't do this in headers.
143   EXPECT_EQ("#include \"b.h\"\n"
144             "#include \"c.h\"\n"
145             "#include \"llvm/a.h\"\n",
146             sort("#include \"llvm/a.h\"\n"
147                  "#include \"c.h\"\n"
148                  "#include \"b.h\"\n",
149                  "some_header.h"));
150 }
151 
152 } // end namespace
153 } // end namespace format
154 } // end namespace clang
155