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 
2794a96fceSDaniel Jasper   std::string sort(StringRef Code, StringRef FileName = "input.cpp") {
2894a96fceSDaniel Jasper     auto Ranges = GetCodeRange(Code);
29*a992afe8SEric Liu     auto Replaces = sortIncludes(Style, Code, Ranges, FileName);
30*a992afe8SEric Liu     Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
31*a992afe8SEric 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 
39b68aabf7SDaniel Jasper   unsigned newCursor(llvm::StringRef Code, unsigned Cursor) {
4094a96fceSDaniel Jasper     sortIncludes(Style, Code, GetCodeRange(Code), "input.cpp", &Cursor);
41b68aabf7SDaniel Jasper     return Cursor;
42b68aabf7SDaniel Jasper   }
43b68aabf7SDaniel Jasper 
4485c472dcSDaniel Jasper   FormatStyle Style = getLLVMStyle();
45b68aabf7SDaniel Jasper 
46d89ae9d3SDaniel Jasper };
47d89ae9d3SDaniel Jasper 
48d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, BasicSorting) {
49d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
50d89ae9d3SDaniel Jasper             "#include \"b.h\"\n"
51d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
52d89ae9d3SDaniel Jasper             sort("#include \"a.h\"\n"
53d89ae9d3SDaniel Jasper                  "#include \"c.h\"\n"
54d89ae9d3SDaniel Jasper                  "#include \"b.h\"\n"));
55d89ae9d3SDaniel Jasper }
56d89ae9d3SDaniel Jasper 
5794a96fceSDaniel Jasper TEST_F(SortIncludesTest, NoReplacementsForValidIncludes) {
5894a96fceSDaniel Jasper   // Identical #includes have led to a failure with an unstable sort.
5994a96fceSDaniel Jasper   std::string Code = "#include <a>\n"
6094a96fceSDaniel Jasper                      "#include <b>\n"
61*a992afe8SEric Liu                      "#include <c>\n"
62*a992afe8SEric Liu                      "#include <d>\n"
63*a992afe8SEric Liu                      "#include <e>\n"
64*a992afe8SEric Liu                      "#include <f>\n";
6594a96fceSDaniel Jasper   EXPECT_TRUE(sortIncludes(Style, Code, GetCodeRange(Code), "a.cc").empty());
6694a96fceSDaniel Jasper }
6794a96fceSDaniel Jasper 
689b8c7c72SDaniel Jasper TEST_F(SortIncludesTest, SupportClangFormatOff) {
699b8c7c72SDaniel Jasper   EXPECT_EQ("#include <a>\n"
709b8c7c72SDaniel Jasper             "#include <b>\n"
719b8c7c72SDaniel Jasper             "#include <c>\n"
729b8c7c72SDaniel Jasper             "// clang-format off\n"
739b8c7c72SDaniel Jasper             "#include <b>\n"
749b8c7c72SDaniel Jasper             "#include <a>\n"
759b8c7c72SDaniel Jasper             "#include <c>\n"
769b8c7c72SDaniel Jasper             "// clang-format on\n",
779b8c7c72SDaniel Jasper             sort("#include <b>\n"
789b8c7c72SDaniel Jasper                  "#include <a>\n"
799b8c7c72SDaniel Jasper                  "#include <c>\n"
809b8c7c72SDaniel Jasper                  "// clang-format off\n"
819b8c7c72SDaniel Jasper                  "#include <b>\n"
829b8c7c72SDaniel Jasper                  "#include <a>\n"
839b8c7c72SDaniel Jasper                  "#include <c>\n"
849b8c7c72SDaniel Jasper                  "// clang-format on\n"));
859b8c7c72SDaniel Jasper }
869b8c7c72SDaniel Jasper 
87da446770SDaniel Jasper TEST_F(SortIncludesTest, IncludeSortingCanBeDisabled) {
88da446770SDaniel Jasper   Style.SortIncludes = false;
89da446770SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
90da446770SDaniel Jasper             "#include \"c.h\"\n"
91da446770SDaniel Jasper             "#include \"b.h\"\n",
92da446770SDaniel Jasper             sort("#include \"a.h\"\n"
93da446770SDaniel Jasper                  "#include \"c.h\"\n"
94da446770SDaniel Jasper                  "#include \"b.h\"\n"));
95da446770SDaniel Jasper }
96da446770SDaniel Jasper 
97ff06370eSNico Weber TEST_F(SortIncludesTest, MixIncludeAndImport) {
98ff06370eSNico Weber   EXPECT_EQ("#include \"a.h\"\n"
99ff06370eSNico Weber             "#import \"b.h\"\n"
100ff06370eSNico Weber             "#include \"c.h\"\n",
101ff06370eSNico Weber             sort("#include \"a.h\"\n"
102ff06370eSNico Weber                  "#include \"c.h\"\n"
103ff06370eSNico Weber                  "#import \"b.h\"\n"));
104ff06370eSNico Weber }
105ff06370eSNico Weber 
106d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, FixTrailingComments) {
107d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"  // comment\n"
108d89ae9d3SDaniel Jasper             "#include \"bb.h\" // comment\n"
109d89ae9d3SDaniel Jasper             "#include \"ccc.h\"\n",
110d89ae9d3SDaniel Jasper             sort("#include \"a.h\" // comment\n"
111d89ae9d3SDaniel Jasper                  "#include \"ccc.h\"\n"
112d89ae9d3SDaniel Jasper                  "#include \"bb.h\" // comment\n"));
113d89ae9d3SDaniel Jasper }
114d89ae9d3SDaniel Jasper 
115d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, LeadingWhitespace) {
116d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
117d89ae9d3SDaniel Jasper             "#include \"b.h\"\n"
118d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
119d89ae9d3SDaniel Jasper             sort(" #include \"a.h\"\n"
120d89ae9d3SDaniel Jasper                  "  #include \"c.h\"\n"
121d89ae9d3SDaniel Jasper                  "   #include \"b.h\"\n"));
122d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
123d89ae9d3SDaniel Jasper             "#include \"b.h\"\n"
124d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
125d89ae9d3SDaniel Jasper             sort("# include \"a.h\"\n"
126d89ae9d3SDaniel Jasper                  "#  include \"c.h\"\n"
127d89ae9d3SDaniel Jasper                  "#   include \"b.h\"\n"));
128d89ae9d3SDaniel Jasper }
129d89ae9d3SDaniel Jasper 
130d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, GreaterInComment) {
131d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
132d89ae9d3SDaniel Jasper             "#include \"b.h\" // >\n"
133d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
134d89ae9d3SDaniel Jasper             sort("#include \"a.h\"\n"
135d89ae9d3SDaniel Jasper                  "#include \"c.h\"\n"
136d89ae9d3SDaniel Jasper                  "#include \"b.h\" // >\n"));
137d89ae9d3SDaniel Jasper }
138d89ae9d3SDaniel Jasper 
139d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, SortsLocallyInEachBlock) {
140d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
141d89ae9d3SDaniel Jasper             "#include \"c.h\"\n"
142d89ae9d3SDaniel Jasper             "\n"
143d89ae9d3SDaniel Jasper             "#include \"b.h\"\n",
14485c472dcSDaniel Jasper             sort("#include \"a.h\"\n"
14585c472dcSDaniel Jasper                  "#include \"c.h\"\n"
146d89ae9d3SDaniel Jasper                  "\n"
147d89ae9d3SDaniel Jasper                  "#include \"b.h\"\n"));
148d89ae9d3SDaniel Jasper }
149d89ae9d3SDaniel Jasper 
150d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, HandlesAngledIncludesAsSeparateBlocks) {
15185c472dcSDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
15285c472dcSDaniel Jasper             "#include \"c.h\"\n"
15385c472dcSDaniel Jasper             "#include <b.h>\n"
15485c472dcSDaniel Jasper             "#include <d.h>\n",
15585c472dcSDaniel Jasper             sort("#include <d.h>\n"
15685c472dcSDaniel Jasper                  "#include <b.h>\n"
15785c472dcSDaniel Jasper                  "#include \"c.h\"\n"
15885c472dcSDaniel Jasper                  "#include \"a.h\"\n"));
15985c472dcSDaniel Jasper 
16085c472dcSDaniel Jasper   Style = getGoogleStyle(FormatStyle::LK_Cpp);
161d89ae9d3SDaniel Jasper   EXPECT_EQ("#include <b.h>\n"
162d89ae9d3SDaniel Jasper             "#include <d.h>\n"
163d89ae9d3SDaniel Jasper             "#include \"a.h\"\n"
164d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
165d89ae9d3SDaniel Jasper             sort("#include <d.h>\n"
166d89ae9d3SDaniel Jasper                  "#include <b.h>\n"
167d89ae9d3SDaniel Jasper                  "#include \"c.h\"\n"
168d89ae9d3SDaniel Jasper                  "#include \"a.h\"\n"));
169d89ae9d3SDaniel Jasper }
170d89ae9d3SDaniel Jasper 
171d89ae9d3SDaniel Jasper TEST_F(SortIncludesTest, HandlesMultilineIncludes) {
172d89ae9d3SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
173d89ae9d3SDaniel Jasper             "#include \"b.h\"\n"
174d89ae9d3SDaniel Jasper             "#include \"c.h\"\n",
175d89ae9d3SDaniel Jasper             sort("#include \"a.h\"\n"
176d89ae9d3SDaniel Jasper                  "#include \\\n"
177d89ae9d3SDaniel Jasper                  "\"c.h\"\n"
178d89ae9d3SDaniel Jasper                  "#include \"b.h\"\n"));
179d89ae9d3SDaniel Jasper }
180d89ae9d3SDaniel Jasper 
18185c472dcSDaniel Jasper TEST_F(SortIncludesTest, LeavesMainHeaderFirst) {
1829c8ff355SDaniel Jasper   Style.IncludeIsMainRegex = "([-_](test|unittest))?$";
18385c472dcSDaniel Jasper   EXPECT_EQ("#include \"llvm/a.h\"\n"
18485c472dcSDaniel Jasper             "#include \"b.h\"\n"
18585c472dcSDaniel Jasper             "#include \"c.h\"\n",
18685c472dcSDaniel Jasper             sort("#include \"llvm/a.h\"\n"
18785c472dcSDaniel Jasper                  "#include \"c.h\"\n"
1880bfdeb4bSDaniel Jasper                  "#include \"b.h\"\n",
1890bfdeb4bSDaniel Jasper                  "a.cc"));
190afa62faeSNico Weber   EXPECT_EQ("#include \"llvm/a.h\"\n"
191afa62faeSNico Weber             "#include \"b.h\"\n"
192afa62faeSNico Weber             "#include \"c.h\"\n",
193afa62faeSNico Weber             sort("#include \"llvm/a.h\"\n"
194afa62faeSNico Weber                  "#include \"c.h\"\n"
195afa62faeSNico Weber                  "#include \"b.h\"\n",
1969c8ff355SDaniel Jasper                  "a_test.cc"));
1970bfdeb4bSDaniel Jasper   EXPECT_EQ("#include \"llvm/input.h\"\n"
1980bfdeb4bSDaniel Jasper             "#include \"b.h\"\n"
1990bfdeb4bSDaniel Jasper             "#include \"c.h\"\n",
2000bfdeb4bSDaniel Jasper             sort("#include \"llvm/input.h\"\n"
2010bfdeb4bSDaniel Jasper                  "#include \"c.h\"\n"
2020bfdeb4bSDaniel Jasper                  "#include \"b.h\"\n",
203afa62faeSNico Weber                  "input.mm"));
20485c472dcSDaniel Jasper 
2059c8ff355SDaniel Jasper   // Don't allow prefixes.
2069c8ff355SDaniel Jasper   EXPECT_EQ("#include \"b.h\"\n"
2079c8ff355SDaniel Jasper             "#include \"c.h\"\n"
2089c8ff355SDaniel Jasper             "#include \"llvm/not_a.h\"\n",
2099c8ff355SDaniel Jasper             sort("#include \"llvm/not_a.h\"\n"
2109c8ff355SDaniel Jasper                  "#include \"c.h\"\n"
2119c8ff355SDaniel Jasper                  "#include \"b.h\"\n",
2129c8ff355SDaniel Jasper                  "a.cc"));
2139c8ff355SDaniel Jasper 
2149c8ff355SDaniel Jasper   // Don't do this for _main and other suffixes.
2159c8ff355SDaniel Jasper   EXPECT_EQ("#include \"b.h\"\n"
2169c8ff355SDaniel Jasper             "#include \"c.h\"\n"
2179c8ff355SDaniel Jasper             "#include \"llvm/a.h\"\n",
2189c8ff355SDaniel Jasper             sort("#include \"llvm/a.h\"\n"
2199c8ff355SDaniel Jasper                  "#include \"c.h\"\n"
2209c8ff355SDaniel Jasper                  "#include \"b.h\"\n",
2219c8ff355SDaniel Jasper                  "a_main.cc"));
2229c8ff355SDaniel Jasper 
22385c472dcSDaniel Jasper   // Don't do this in headers.
22485c472dcSDaniel Jasper   EXPECT_EQ("#include \"b.h\"\n"
22585c472dcSDaniel Jasper             "#include \"c.h\"\n"
22685c472dcSDaniel Jasper             "#include \"llvm/a.h\"\n",
22785c472dcSDaniel Jasper             sort("#include \"llvm/a.h\"\n"
22885c472dcSDaniel Jasper                  "#include \"c.h\"\n"
22985c472dcSDaniel Jasper                  "#include \"b.h\"\n",
2300bfdeb4bSDaniel Jasper                  "a.h"));
23132d75fa2SDaniel Jasper 
23232d75fa2SDaniel Jasper   // Only do this in the first #include block.
23332d75fa2SDaniel Jasper   EXPECT_EQ("#include <a>\n"
23432d75fa2SDaniel Jasper             "\n"
23532d75fa2SDaniel Jasper             "#include \"b.h\"\n"
23632d75fa2SDaniel Jasper             "#include \"c.h\"\n"
23732d75fa2SDaniel Jasper             "#include \"llvm/a.h\"\n",
23832d75fa2SDaniel Jasper             sort("#include <a>\n"
23932d75fa2SDaniel Jasper                  "\n"
24032d75fa2SDaniel Jasper                  "#include \"llvm/a.h\"\n"
24132d75fa2SDaniel Jasper                  "#include \"c.h\"\n"
24232d75fa2SDaniel Jasper                  "#include \"b.h\"\n",
24332d75fa2SDaniel Jasper                  "a.cc"));
244a252f5d5SDaniel Jasper 
245a252f5d5SDaniel Jasper   // Only recognize the first #include with a matching basename as main include.
246a252f5d5SDaniel Jasper   EXPECT_EQ("#include \"a.h\"\n"
247a252f5d5SDaniel Jasper             "#include \"b.h\"\n"
248a252f5d5SDaniel Jasper             "#include \"c.h\"\n"
249a252f5d5SDaniel Jasper             "#include \"llvm/a.h\"\n",
250a252f5d5SDaniel Jasper             sort("#include \"b.h\"\n"
251a252f5d5SDaniel Jasper                  "#include \"a.h\"\n"
252a252f5d5SDaniel Jasper                  "#include \"c.h\"\n"
253a252f5d5SDaniel Jasper                  "#include \"llvm/a.h\"\n",
254a252f5d5SDaniel Jasper                  "a.cc"));
25585c472dcSDaniel Jasper }
25685c472dcSDaniel Jasper 
257d2629dc8SDaniel Jasper TEST_F(SortIncludesTest, NegativePriorities) {
258d2629dc8SDaniel Jasper   Style.IncludeCategories = {{".*important_os_header.*", -1}, {".*", 1}};
259d2629dc8SDaniel Jasper   EXPECT_EQ("#include \"important_os_header.h\"\n"
2600bfdeb4bSDaniel Jasper             "#include \"c_main.h\"\n"
2610bfdeb4bSDaniel Jasper             "#include \"a_other.h\"\n",
2620bfdeb4bSDaniel Jasper             sort("#include \"c_main.h\"\n"
2630bfdeb4bSDaniel Jasper                  "#include \"a_other.h\"\n"
2640bfdeb4bSDaniel Jasper                  "#include \"important_os_header.h\"\n",
2650bfdeb4bSDaniel Jasper                  "c_main.cc"));
2660bfdeb4bSDaniel Jasper 
2670bfdeb4bSDaniel Jasper   // check stable when re-run
2680bfdeb4bSDaniel Jasper   EXPECT_EQ("#include \"important_os_header.h\"\n"
2690bfdeb4bSDaniel Jasper             "#include \"c_main.h\"\n"
2700bfdeb4bSDaniel Jasper             "#include \"a_other.h\"\n",
2710bfdeb4bSDaniel Jasper             sort("#include \"important_os_header.h\"\n"
2720bfdeb4bSDaniel Jasper                  "#include \"c_main.h\"\n"
2730bfdeb4bSDaniel Jasper                  "#include \"a_other.h\"\n",
2740bfdeb4bSDaniel Jasper                  "c_main.cc"));
275d2629dc8SDaniel Jasper }
276d2629dc8SDaniel Jasper 
277b68aabf7SDaniel Jasper TEST_F(SortIncludesTest, CalculatesCorrectCursorPosition) {
278b68aabf7SDaniel Jasper   std::string Code = "#include <ccc>\n"    // Start of line: 0
279b68aabf7SDaniel Jasper                      "#include <bbbbbb>\n" // Start of line: 15
280b68aabf7SDaniel Jasper                      "#include <a>\n";     // Start of line: 33
281b68aabf7SDaniel Jasper   EXPECT_EQ(31u, newCursor(Code, 0));
282b68aabf7SDaniel Jasper   EXPECT_EQ(13u, newCursor(Code, 15));
283b68aabf7SDaniel Jasper   EXPECT_EQ(0u, newCursor(Code, 33));
284b68aabf7SDaniel Jasper 
285b68aabf7SDaniel Jasper   EXPECT_EQ(41u, newCursor(Code, 10));
286b68aabf7SDaniel Jasper   EXPECT_EQ(23u, newCursor(Code, 25));
287b68aabf7SDaniel Jasper   EXPECT_EQ(10u, newCursor(Code, 43));
288b68aabf7SDaniel Jasper }
289b68aabf7SDaniel Jasper 
290*a992afe8SEric Liu TEST_F(SortIncludesTest, DeduplicateIncludes) {
291*a992afe8SEric Liu   EXPECT_EQ("#include <a>\n"
292*a992afe8SEric Liu             "#include <b>\n"
293*a992afe8SEric Liu             "#include <c>\n",
294*a992afe8SEric Liu             sort("#include <a>\n"
295*a992afe8SEric Liu                  "#include <b>\n"
296*a992afe8SEric Liu                  "#include <b>\n"
297*a992afe8SEric Liu                  "#include <b>\n"
298*a992afe8SEric Liu                  "#include <b>\n"
299*a992afe8SEric Liu                  "#include <c>\n"));
300*a992afe8SEric Liu }
301*a992afe8SEric Liu 
302*a992afe8SEric Liu TEST_F(SortIncludesTest, SortAndDeduplicateIncludes) {
303*a992afe8SEric Liu   EXPECT_EQ("#include <a>\n"
304*a992afe8SEric Liu             "#include <b>\n"
305*a992afe8SEric Liu             "#include <c>\n",
306*a992afe8SEric Liu             sort("#include <b>\n"
307*a992afe8SEric Liu                  "#include <a>\n"
308*a992afe8SEric Liu                  "#include <b>\n"
309*a992afe8SEric Liu                  "#include <b>\n"
310*a992afe8SEric Liu                  "#include <c>\n"
311*a992afe8SEric Liu                  "#include <b>\n"));
312*a992afe8SEric Liu }
313*a992afe8SEric Liu 
314*a992afe8SEric Liu TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionAfterDeduplicate) {
315*a992afe8SEric Liu   std::string Code = "#include <b>\n"      // Start of line: 0
316*a992afe8SEric Liu                      "#include <a>\n"      // Start of line: 13
317*a992afe8SEric Liu                      "#include <b>\n"      // Start of line: 26
318*a992afe8SEric Liu                      "#include <b>\n"      // Start of line: 39
319*a992afe8SEric Liu                      "#include <c>\n"      // Start of line: 52
320*a992afe8SEric Liu                      "#include <b>\n";     // Start of line: 65
321*a992afe8SEric Liu   std::string Expected = "#include <a>\n"  // Start of line: 0
322*a992afe8SEric Liu                          "#include <b>\n"  // Start of line: 13
323*a992afe8SEric Liu                          "#include <c>\n"; // Start of line: 26
324*a992afe8SEric Liu   EXPECT_EQ(Expected, sort(Code));
325*a992afe8SEric Liu   // Cursor on 'i' in "#include <a>".
326*a992afe8SEric Liu   EXPECT_EQ(1u, newCursor(Code, 14));
327*a992afe8SEric Liu   // Cursor on 'b' in "#include <b>".
328*a992afe8SEric Liu   EXPECT_EQ(23u, newCursor(Code, 10));
329*a992afe8SEric Liu   EXPECT_EQ(23u, newCursor(Code, 36));
330*a992afe8SEric Liu   EXPECT_EQ(23u, newCursor(Code, 49));
331*a992afe8SEric Liu   EXPECT_EQ(23u, newCursor(Code, 36));
332*a992afe8SEric Liu   EXPECT_EQ(23u, newCursor(Code, 75));
333*a992afe8SEric Liu   // Cursor on '#' in "#include <c>".
334*a992afe8SEric Liu   EXPECT_EQ(26u, newCursor(Code, 52));
335*a992afe8SEric Liu }
336*a992afe8SEric Liu 
337*a992afe8SEric Liu TEST_F(SortIncludesTest, DeduplicateLocallyInEachBlock) {
338*a992afe8SEric Liu   EXPECT_EQ("#include <a>\n"
339*a992afe8SEric Liu             "#include <b>\n"
340*a992afe8SEric Liu             "\n"
341*a992afe8SEric Liu             "#include <b>\n"
342*a992afe8SEric Liu             "#include <c>\n",
343*a992afe8SEric Liu             sort("#include <a>\n"
344*a992afe8SEric Liu                  "#include <b>\n"
345*a992afe8SEric Liu                  "\n"
346*a992afe8SEric Liu                  "#include <c>\n"
347*a992afe8SEric Liu                  "#include <b>\n"
348*a992afe8SEric Liu                  "#include <b>\n"));
349*a992afe8SEric Liu }
350*a992afe8SEric Liu 
351*a992afe8SEric Liu TEST_F(SortIncludesTest, ValidAffactedRangesAfterDeduplicatingIncludes) {
352*a992afe8SEric Liu   std::string Code = "#include <a>\n"
353*a992afe8SEric Liu                      "#include <b>\n"
354*a992afe8SEric Liu                      "#include <a>\n"
355*a992afe8SEric Liu                      "#include <a>\n"
356*a992afe8SEric Liu                      "\n"
357*a992afe8SEric Liu                      "   int     x ;";
358*a992afe8SEric Liu   std::vector<tooling::Range> Ranges = {tooling::Range(0, 52)};
359*a992afe8SEric Liu   auto Replaces = sortIncludes(Style, Code, Ranges, "input.cpp");
360*a992afe8SEric Liu   Ranges = tooling::calculateRangesAfterReplacements(Replaces, Ranges);
361*a992afe8SEric Liu   EXPECT_EQ(1u, Ranges.size());
362*a992afe8SEric Liu   EXPECT_EQ(0u, Ranges[0].getOffset());
363*a992afe8SEric Liu   EXPECT_EQ(26u, Ranges[0].getLength());
364*a992afe8SEric Liu }
365*a992afe8SEric Liu 
366d89ae9d3SDaniel Jasper } // end namespace
367d89ae9d3SDaniel Jasper } // end namespace format
368d89ae9d3SDaniel Jasper } // end namespace clang
369