1d89ae9d3SDaniel Jasper //===- unittest/Format/SortIncludesTest.cpp - Include sort unit tests -----===//
2d89ae9d3SDaniel Jasper //
3d89ae9d3SDaniel Jasper //                     The LLVM Compiler Infrastructure
4d89ae9d3SDaniel Jasper //
5d89ae9d3SDaniel Jasper // This file is distributed under the University of Illinois Open Source
6d89ae9d3SDaniel Jasper // License. See LICENSE.TXT for details.
7d89ae9d3SDaniel Jasper //
8d89ae9d3SDaniel Jasper //===----------------------------------------------------------------------===//
9d89ae9d3SDaniel Jasper 
10d89ae9d3SDaniel Jasper #include "FormatTestUtils.h"
11d89ae9d3SDaniel Jasper #include "clang/Format/Format.h"
12d89ae9d3SDaniel Jasper #include "llvm/Support/Debug.h"
13d89ae9d3SDaniel Jasper #include "gtest/gtest.h"
14d89ae9d3SDaniel Jasper 
15d89ae9d3SDaniel Jasper #define DEBUG_TYPE "format-test"
16d89ae9d3SDaniel Jasper 
17d89ae9d3SDaniel Jasper namespace clang {
18d89ae9d3SDaniel Jasper namespace format {
19d89ae9d3SDaniel Jasper namespace {
20d89ae9d3SDaniel Jasper 
21d89ae9d3SDaniel Jasper class SortIncludesTest : public ::testing::Test {
22d89ae9d3SDaniel Jasper protected:
2394a96fceSDaniel Jasper   std::vector<tooling::Range> GetCodeRange(StringRef Code) {
2494a96fceSDaniel Jasper     return std::vector<tooling::Range>(1, tooling::Range(0, Code.size()));
2594a96fceSDaniel Jasper   }
2694a96fceSDaniel Jasper 
27*d6a00780SDaniel Jasper   std::string sort(StringRef Code, std::vector<tooling::Range> Ranges,
28*d6a00780SDaniel Jasper                    StringRef FileName = "input.cc") {
29a992afe8SEric Liu     auto Replaces = sortIncludes(Style, Code, Ranges, FileName);
30a992afe8SEric Liu     Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
31a992afe8SEric Liu     auto Sorted = applyAllReplacements(Code, Replaces);
324f8d9943SEric Liu     EXPECT_TRUE(static_cast<bool>(Sorted));
334f8d9943SEric Liu     auto Result = applyAllReplacements(
344f8d9943SEric Liu         *Sorted, reformat(Style, *Sorted, Ranges, FileName));
354f8d9943SEric Liu     EXPECT_TRUE(static_cast<bool>(Result));
364f8d9943SEric Liu     return *Result;
37d89ae9d3SDaniel Jasper   }
3885c472dcSDaniel Jasper 
39*d6a00780SDaniel Jasper   std::string sort(StringRef Code, StringRef FileName = "input.cpp") {
40*d6a00780SDaniel Jasper     return sort(Code, GetCodeRange(Code), FileName);
41*d6a00780SDaniel Jasper   }
42*d6a00780SDaniel Jasper 
43b68aabf7SDaniel Jasper   unsigned newCursor(llvm::StringRef Code, unsigned Cursor) {
4494a96fceSDaniel Jasper     sortIncludes(Style, Code, GetCodeRange(Code), "input.cpp", &Cursor);
45b68aabf7SDaniel Jasper     return Cursor;
46b68aabf7SDaniel Jasper   }
47b68aabf7SDaniel Jasper 
4885c472dcSDaniel Jasper   FormatStyle Style = getLLVMStyle();
49b68aabf7SDaniel Jasper 
50d89ae9d3SDaniel Jasper };
51d89ae9d3SDaniel Jasper 
52d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, BasicSorting) {
53d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
54d89ae9d3SDaniel Jasper             "#include \"b.h\"\n"
55d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
56d89ae9d3SDaniel Jasper             sort("#include \"a.h\"\n"
57d89ae9d3SDaniel Jasper                  "#include \"c.h\"\n"
58d89ae9d3SDaniel Jasper                  "#include \"b.h\"\n"));
59*d6a00780SDaniel Jasper 
60*d6a00780SDaniel Jasper   EXPECT_EQ("// comment\n"
61*d6a00780SDaniel Jasper             "#include <a>\n"
62*d6a00780SDaniel Jasper             "#include <b>\n",
63*d6a00780SDaniel Jasper             sort("// comment\n"
64*d6a00780SDaniel Jasper                  "#include <b>\n"
65*d6a00780SDaniel Jasper                  "#include <a>\n",
66*d6a00780SDaniel Jasper                  {tooling::Range(25, 1)}));
67d89ae9d3SDaniel Jasper }
68d89ae9d3SDaniel Jasper 
6994a96fceSDaniel Jasper TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) {
7094a96fceSDaniel Jasper   // Identical #includes have led to a failure with an unstable sort.
7194a96fceSDaniel Jasper   std::string Code = "#include <a>\n"
7294a96fceSDaniel Jasper                      "#include <b>\n"
73a992afe8SEric Liu                      "#include <c>\n"
74a992afe8SEric Liu                      "#include <d>\n"
75a992afe8SEric Liu                      "#include <e>\n"
76a992afe8SEric Liu                      "#include <f>\n";
7794a96fceSDaniel Jasper   EXPECT_TRUE(sortIncludes(Style, Code, GetCodeRange(Code), "a.cc").empty());
7894a96fceSDaniel Jasper }
7994a96fceSDaniel Jasper 
809b8c7c72SDaniel Jasper TEST_F(SortIncludesTest, SupportClangFormatOff) {
819b8c7c72SDaniel Jasper   EXPECT_EQ("#include <a>\n"
829b8c7c72SDaniel Jasper             "#include <b>\n"
839b8c7c72SDaniel Jasper             "#include <c>\n"
849b8c7c72SDaniel Jasper             "// clang-format off\n"
859b8c7c72SDaniel Jasper             "#include <b>\n"
869b8c7c72SDaniel Jasper             "#include <a>\n"
879b8c7c72SDaniel Jasper             "#include <c>\n"
889b8c7c72SDaniel Jasper             "// clang-format on\n",
899b8c7c72SDaniel Jasper             sort("#include <b>\n"
909b8c7c72SDaniel Jasper                  "#include <a>\n"
919b8c7c72SDaniel Jasper                  "#include <c>\n"
929b8c7c72SDaniel Jasper                  "// clang-format off\n"
939b8c7c72SDaniel Jasper                  "#include <b>\n"
949b8c7c72SDaniel Jasper                  "#include <a>\n"
959b8c7c72SDaniel Jasper                  "#include <c>\n"
969b8c7c72SDaniel Jasper                  "// clang-format on\n"));
979b8c7c72SDaniel Jasper }
989b8c7c72SDaniel Jasper 
99da446770SDaniel Jasper TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
100da446770SDaniel Jasper   Style.SortIncludes = false;
101da446770SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
102da446770SDaniel Jasper             "#include \"c.h\"\n"
103da446770SDaniel Jasper             "#include \"b.h\"\n",
104da446770SDaniel Jasper             sort("#include \"a.h\"\n"
105da446770SDaniel Jasper                  "#include \"c.h\"\n"
106da446770SDaniel Jasper                  "#include \"b.h\"\n"));
107da446770SDaniel Jasper }
108da446770SDaniel Jasper 
109ff06370eSNico Weber TEST_F(SortIncludesTest, MixIncludeAndImport) {
110ff06370eSNico Weber   EXPECT_EQ("#include \"a.h\"\n"
111ff06370eSNico Weber             "#import \"b.h\"\n"
112ff06370eSNico Weber             "#include \"c.h\"\n",
113ff06370eSNico Weber             sort("#include \"a.h\"\n"
114ff06370eSNico Weber                  "#include \"c.h\"\n"
115ff06370eSNico Weber                  "#import \"b.h\"\n"));
116ff06370eSNico Weber }
117ff06370eSNico Weber 
118d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, FixTrailingComments) {
119d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"  // comment\n"
120d89ae9d3SDaniel Jasper             "#include \"bb.h\" // comment\n"
121d89ae9d3SDaniel Jasper             "#include \"ccc.h\"\n",
122d89ae9d3SDaniel Jasper             sort("#include \"a.h\" // comment\n"
123d89ae9d3SDaniel Jasper                  "#include \"ccc.h\"\n"
124d89ae9d3SDaniel Jasper                  "#include \"bb.h\" // comment\n"));
125d89ae9d3SDaniel Jasper }
126d89ae9d3SDaniel Jasper 
127d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, LeadingWhitespace) {
128d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
129d89ae9d3SDaniel Jasper             "#include \"b.h\"\n"
130d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
131d89ae9d3SDaniel Jasper             sort(" #include \"a.h\"\n"
132d89ae9d3SDaniel Jasper                  "  #include \"c.h\"\n"
133d89ae9d3SDaniel Jasper                  "   #include \"b.h\"\n"));
134d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
135d89ae9d3SDaniel Jasper             "#include \"b.h\"\n"
136d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
137d89ae9d3SDaniel Jasper             sort("# include \"a.h\"\n"
138d89ae9d3SDaniel Jasper                  "#  include \"c.h\"\n"
139d89ae9d3SDaniel Jasper                  "#   include \"b.h\"\n"));
140d89ae9d3SDaniel Jasper }
141d89ae9d3SDaniel Jasper 
142d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, GreaterInComment) {
143d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
144d89ae9d3SDaniel Jasper             "#include \"b.h\" // >\n"
145d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
146d89ae9d3SDaniel Jasper             sort("#include \"a.h\"\n"
147d89ae9d3SDaniel Jasper                  "#include \"c.h\"\n"
148d89ae9d3SDaniel Jasper                  "#include \"b.h\" // >\n"));
149d89ae9d3SDaniel Jasper }
150d89ae9d3SDaniel Jasper 
151d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, SortsLocallyInEachBlock) {
152d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
153d89ae9d3SDaniel Jasper             "#include \"c.h\"\n"
154d89ae9d3SDaniel Jasper             "\n"
155d89ae9d3SDaniel Jasper             "#include \"b.h\"\n",
15685c472dcSDaniel Jasper             sort("#include \"a.h\"\n"
15785c472dcSDaniel Jasper                  "#include \"c.h\"\n"
158d89ae9d3SDaniel Jasper                  "\n"
159d89ae9d3SDaniel Jasper                  "#include \"b.h\"\n"));
160d89ae9d3SDaniel Jasper }
161d89ae9d3SDaniel Jasper 
162d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
16385c472dcSDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
16485c472dcSDaniel Jasper             "#include \"c.h\"\n"
16585c472dcSDaniel Jasper             "#include <b.h>\n"
16685c472dcSDaniel Jasper             "#include <d.h>\n",
16785c472dcSDaniel Jasper             sort("#include <d.h>\n"
16885c472dcSDaniel Jasper                  "#include <b.h>\n"
16985c472dcSDaniel Jasper                  "#include \"c.h\"\n"
17085c472dcSDaniel Jasper                  "#include \"a.h\"\n"));
17185c472dcSDaniel Jasper 
17285c472dcSDaniel Jasper   Style = getGoogleStyle(FormatStyle::LK_Cpp);
173d89ae9d3SDaniel Jasper   EXPECT_EQ("#include <b.h>\n"
174d89ae9d3SDaniel Jasper             "#include <d.h>\n"
175d89ae9d3SDaniel Jasper             "#include \"a.h\"\n"
176d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
177d89ae9d3SDaniel Jasper             sort("#include <d.h>\n"
178d89ae9d3SDaniel Jasper                  "#include <b.h>\n"
179d89ae9d3SDaniel Jasper                  "#include \"c.h\"\n"
180d89ae9d3SDaniel Jasper                  "#include \"a.h\"\n"));
181d89ae9d3SDaniel Jasper }
182d89ae9d3SDaniel Jasper 
183d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, HandlesMultilineIncludes) {
184d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
185d89ae9d3SDaniel Jasper             "#include \"b.h\"\n"
186d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
187d89ae9d3SDaniel Jasper             sort("#include \"a.h\"\n"
188d89ae9d3SDaniel Jasper                  "#include \\\n"
189d89ae9d3SDaniel Jasper                  "\"c.h\"\n"
190d89ae9d3SDaniel Jasper                  "#include \"b.h\"\n"));
191d89ae9d3SDaniel Jasper }
192d89ae9d3SDaniel Jasper 
19385c472dcSDaniel Jasper TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
1949c8ff355SDaniel Jasper   Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
19585c472dcSDaniel Jasper   EXPECT_EQ("#include \"llvm/a.h\"\n"
19685c472dcSDaniel Jasper             "#include \"b.h\"\n"
19785c472dcSDaniel Jasper             "#include \"c.h\"\n",
19885c472dcSDaniel Jasper             sort("#include \"llvm/a.h\"\n"
19985c472dcSDaniel Jasper                  "#include \"c.h\"\n"
2000bfdeb4bSDaniel Jasper                  "#include \"b.h\"\n",
2010bfdeb4bSDaniel Jasper                  "a.cc"));
202afa62faeSNico Weber   EXPECT_EQ("#include \"llvm/a.h\"\n"
203afa62faeSNico Weber             "#include \"b.h\"\n"
204afa62faeSNico Weber             "#include \"c.h\"\n",
205afa62faeSNico Weber             sort("#include \"llvm/a.h\"\n"
206afa62faeSNico Weber                  "#include \"c.h\"\n"
207afa62faeSNico Weber                  "#include \"b.h\"\n",
2089c8ff355SDaniel Jasper                  "a_test.cc"));
2090bfdeb4bSDaniel Jasper   EXPECT_EQ("#include \"llvm/input.h\"\n"
2100bfdeb4bSDaniel Jasper             "#include \"b.h\"\n"
2110bfdeb4bSDaniel Jasper             "#include \"c.h\"\n",
2120bfdeb4bSDaniel Jasper             sort("#include \"llvm/input.h\"\n"
2130bfdeb4bSDaniel Jasper                  "#include \"c.h\"\n"
2140bfdeb4bSDaniel Jasper                  "#include \"b.h\"\n",
215afa62faeSNico Weber                  "input.mm"));
21685c472dcSDaniel Jasper 
2179c8ff355SDaniel Jasper   // Don't allow prefixes.
2189c8ff355SDaniel Jasper   EXPECT_EQ("#include \"b.h\"\n"
2199c8ff355SDaniel Jasper             "#include \"c.h\"\n"
2209c8ff355SDaniel Jasper             "#include \"llvm/not_a.h\"\n",
2219c8ff355SDaniel Jasper             sort("#include \"llvm/not_a.h\"\n"
2229c8ff355SDaniel Jasper                  "#include \"c.h\"\n"
2239c8ff355SDaniel Jasper                  "#include \"b.h\"\n",
2249c8ff355SDaniel Jasper                  "a.cc"));
2259c8ff355SDaniel Jasper 
2269c8ff355SDaniel Jasper   // Don't do this for _main and other suffixes.
2279c8ff355SDaniel Jasper   EXPECT_EQ("#include \"b.h\"\n"
2289c8ff355SDaniel Jasper             "#include \"c.h\"\n"
2299c8ff355SDaniel Jasper             "#include \"llvm/a.h\"\n",
2309c8ff355SDaniel Jasper             sort("#include \"llvm/a.h\"\n"
2319c8ff355SDaniel Jasper                  "#include \"c.h\"\n"
2329c8ff355SDaniel Jasper                  "#include \"b.h\"\n",
2339c8ff355SDaniel Jasper                  "a_main.cc"));
2349c8ff355SDaniel Jasper 
23585c472dcSDaniel Jasper   // Don't do this in headers.
23685c472dcSDaniel Jasper   EXPECT_EQ("#include \"b.h\"\n"
23785c472dcSDaniel Jasper             "#include \"c.h\"\n"
23885c472dcSDaniel Jasper             "#include \"llvm/a.h\"\n",
23985c472dcSDaniel Jasper             sort("#include \"llvm/a.h\"\n"
24085c472dcSDaniel Jasper                  "#include \"c.h\"\n"
24185c472dcSDaniel Jasper                  "#include \"b.h\"\n",
2420bfdeb4bSDaniel Jasper                  "a.h"));
24332d75fa2SDaniel Jasper 
24432d75fa2SDaniel Jasper   // Only do this in the first #include block.
24532d75fa2SDaniel Jasper   EXPECT_EQ("#include <a>\n"
24632d75fa2SDaniel Jasper             "\n"
24732d75fa2SDaniel Jasper             "#include \"b.h\"\n"
24832d75fa2SDaniel Jasper             "#include \"c.h\"\n"
24932d75fa2SDaniel Jasper             "#include \"llvm/a.h\"\n",
25032d75fa2SDaniel Jasper             sort("#include <a>\n"
25132d75fa2SDaniel Jasper                  "\n"
25232d75fa2SDaniel Jasper                  "#include \"llvm/a.h\"\n"
25332d75fa2SDaniel Jasper                  "#include \"c.h\"\n"
25432d75fa2SDaniel Jasper                  "#include \"b.h\"\n",
25532d75fa2SDaniel Jasper                  "a.cc"));
256a252f5d5SDaniel Jasper 
257a252f5d5SDaniel Jasper   // Only recognize the first #include with a matching basename as main include.
258a252f5d5SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
259a252f5d5SDaniel Jasper             "#include \"b.h\"\n"
260a252f5d5SDaniel Jasper             "#include \"c.h\"\n"
261a252f5d5SDaniel Jasper             "#include \"llvm/a.h\"\n",
262a252f5d5SDaniel Jasper             sort("#include \"b.h\"\n"
263a252f5d5SDaniel Jasper                  "#include \"a.h\"\n"
264a252f5d5SDaniel Jasper                  "#include \"c.h\"\n"
265a252f5d5SDaniel Jasper                  "#include \"llvm/a.h\"\n",
266a252f5d5SDaniel Jasper                  "a.cc"));
26785c472dcSDaniel Jasper }
26885c472dcSDaniel Jasper 
269d2629dc8SDaniel Jasper TEST_F(SortIncludesTest, NegativePriorities) {
270d2629dc8SDaniel Jasper   Style.IncludeCategories = {{".*important_os_header.*", -1}, {".*", 1}};
271d2629dc8SDaniel Jasper   EXPECT_EQ("#include \"important_os_header.h\"\n"
2720bfdeb4bSDaniel Jasper             "#include \"c_main.h\"\n"
2730bfdeb4bSDaniel Jasper             "#include \"a_other.h\"\n",
2740bfdeb4bSDaniel Jasper             sort("#include \"c_main.h\"\n"
2750bfdeb4bSDaniel Jasper                  "#include \"a_other.h\"\n"
2760bfdeb4bSDaniel Jasper                  "#include \"important_os_header.h\"\n",
2770bfdeb4bSDaniel Jasper                  "c_main.cc"));
2780bfdeb4bSDaniel Jasper 
2790bfdeb4bSDaniel Jasper   // check stable when re-run
2800bfdeb4bSDaniel Jasper   EXPECT_EQ("#include \"important_os_header.h\"\n"
2810bfdeb4bSDaniel Jasper             "#include \"c_main.h\"\n"
2820bfdeb4bSDaniel Jasper             "#include \"a_other.h\"\n",
2830bfdeb4bSDaniel Jasper             sort("#include \"important_os_header.h\"\n"
2840bfdeb4bSDaniel Jasper                  "#include \"c_main.h\"\n"
2850bfdeb4bSDaniel Jasper                  "#include \"a_other.h\"\n",
2860bfdeb4bSDaniel Jasper                  "c_main.cc"));
287d2629dc8SDaniel Jasper }
288d2629dc8SDaniel Jasper 
289b68aabf7SDaniel Jasper TEST_F(SortIncludesTest, CalculatesCorrectCursorPosition) {
290b68aabf7SDaniel Jasper   std::string Code = "#include <ccc>\n"    // Start of line: 0
291b68aabf7SDaniel Jasper                      "#include <bbbbbb>\n" // Start of line: 15
292b68aabf7SDaniel Jasper                      "#include <a>\n";     // Start of line: 33
293b68aabf7SDaniel Jasper   EXPECT_EQ(31u, newCursor(Code, 0));
294b68aabf7SDaniel Jasper   EXPECT_EQ(13u, newCursor(Code, 15));
295b68aabf7SDaniel Jasper   EXPECT_EQ(0u, newCursor(Code, 33));
296b68aabf7SDaniel Jasper 
297b68aabf7SDaniel Jasper   EXPECT_EQ(41u, newCursor(Code, 10));
298b68aabf7SDaniel Jasper   EXPECT_EQ(23u, newCursor(Code, 25));
299b68aabf7SDaniel Jasper   EXPECT_EQ(10u, newCursor(Code, 43));
300b68aabf7SDaniel Jasper }
301b68aabf7SDaniel Jasper 
302a992afe8SEric Liu TEST_F(SortIncludesTest, DeduplicateIncludes) {
303a992afe8SEric Liu   EXPECT_EQ("#include <a>\n"
304a992afe8SEric Liu             "#include <b>\n"
305a992afe8SEric Liu             "#include <c>\n",
306a992afe8SEric Liu             sort("#include <a>\n"
307a992afe8SEric Liu                  "#include <b>\n"
308a992afe8SEric Liu                  "#include <b>\n"
309a992afe8SEric Liu                  "#include <b>\n"
310a992afe8SEric Liu                  "#include <b>\n"
311a992afe8SEric Liu                  "#include <c>\n"));
312a992afe8SEric Liu }
313a992afe8SEric Liu 
314a992afe8SEric Liu TEST_F(SortIncludesTest, SortAndDeduplicateIncludes) {
315a992afe8SEric Liu   EXPECT_EQ("#include <a>\n"
316a992afe8SEric Liu             "#include <b>\n"
317a992afe8SEric Liu             "#include <c>\n",
318a992afe8SEric Liu             sort("#include <b>\n"
319a992afe8SEric Liu                  "#include <a>\n"
320a992afe8SEric Liu                  "#include <b>\n"
321a992afe8SEric Liu                  "#include <b>\n"
322a992afe8SEric Liu                  "#include <c>\n"
323a992afe8SEric Liu                  "#include <b>\n"));
324a992afe8SEric Liu }
325a992afe8SEric Liu 
326a992afe8SEric Liu TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionAfterDeduplicate) {
327a992afe8SEric Liu   std::string Code = "#include <b>\n"      // Start of line: 0
328a992afe8SEric Liu                      "#include <a>\n"      // Start of line: 13
329a992afe8SEric Liu                      "#include <b>\n"      // Start of line: 26
330a992afe8SEric Liu                      "#include <b>\n"      // Start of line: 39
331a992afe8SEric Liu                      "#include <c>\n"      // Start of line: 52
332a992afe8SEric Liu                      "#include <b>\n";     // Start of line: 65
333a992afe8SEric Liu   std::string Expected = "#include <a>\n"  // Start of line: 0
334a992afe8SEric Liu                          "#include <b>\n"  // Start of line: 13
335a992afe8SEric Liu                          "#include <c>\n"; // Start of line: 26
336a992afe8SEric Liu   EXPECT_EQ(Expected, sort(Code));
337a992afe8SEric Liu   // Cursor on 'i' in "#include <a>".
338a992afe8SEric Liu   EXPECT_EQ(1u, newCursor(Code, 14));
339a992afe8SEric Liu   // Cursor on 'b' in "#include <b>".
340a992afe8SEric Liu   EXPECT_EQ(23u, newCursor(Code, 10));
341a992afe8SEric Liu   EXPECT_EQ(23u, newCursor(Code, 36));
342a992afe8SEric Liu   EXPECT_EQ(23u, newCursor(Code, 49));
343a992afe8SEric Liu   EXPECT_EQ(23u, newCursor(Code, 36));
344a992afe8SEric Liu   EXPECT_EQ(23u, newCursor(Code, 75));
345a992afe8SEric Liu   // Cursor on '#' in "#include <c>".
346a992afe8SEric Liu   EXPECT_EQ(26u, newCursor(Code, 52));
347a992afe8SEric Liu }
348a992afe8SEric Liu 
349a992afe8SEric Liu TEST_F(SortIncludesTest, DeduplicateLocallyInEachBlock) {
350a992afe8SEric Liu   EXPECT_EQ("#include <a>\n"
351a992afe8SEric Liu             "#include <b>\n"
352a992afe8SEric Liu             "\n"
353a992afe8SEric Liu             "#include <b>\n"
354a992afe8SEric Liu             "#include <c>\n",
355a992afe8SEric Liu             sort("#include <a>\n"
356a992afe8SEric Liu                  "#include <b>\n"
357a992afe8SEric Liu                  "\n"
358a992afe8SEric Liu                  "#include <c>\n"
359a992afe8SEric Liu                  "#include <b>\n"
360a992afe8SEric Liu                  "#include <b>\n"));
361a992afe8SEric Liu }
362a992afe8SEric Liu 
363a992afe8SEric Liu TEST_F(SortIncludesTest, ValidAffactedRangesAfterDeduplicatingIncludes) {
364a992afe8SEric Liu   std::string Code = "#include <a>\n"
365a992afe8SEric Liu                      "#include <b>\n"
366a992afe8SEric Liu                      "#include <a>\n"
367a992afe8SEric Liu                      "#include <a>\n"
368a992afe8SEric Liu                      "\n"
369a992afe8SEric Liu                      "   int     x ;";
370a992afe8SEric Liu   std::vector<tooling::Range> Ranges = {tooling::Range(0, 52)};
371a992afe8SEric Liu   auto Replaces = sortIncludes(Style, Code, Ranges, "input.cpp");
372a992afe8SEric Liu   Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
373a992afe8SEric Liu   EXPECT_EQ(1u, Ranges.size());
374a992afe8SEric Liu   EXPECT_EQ(0u, Ranges[0].getOffset());
375a992afe8SEric Liu   EXPECT_EQ(26u, Ranges[0].getLength());
376a992afe8SEric Liu }
377a992afe8SEric Liu 
378d89ae9d3SDaniel Jasper } // end namespace
379d89ae9d3SDaniel Jasper } // end namespace format
380d89ae9d3SDaniel Jasper } // end namespace clang
381