1 //===- UsingDeclarationsSorterTest.cpp - Formatting unit tests ------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "clang/Format/Format.h"
11 #include "llvm/Support/Debug.h"
12 #include "gtest/gtest.h"
14 #define DEBUG_TYPE "using-declarations-sorter-test"
20 class UsingDeclarationsSorterTest
: public testing::Test
{
22 std::string
sortUsingDeclarations(StringRef Code
,
23 const std::vector
<tooling::Range
> &Ranges
,
24 const FormatStyle
&Style
= getLLVMStyle()) {
25 LLVM_DEBUG(llvm::errs() << "---\n");
26 LLVM_DEBUG(llvm::errs() << Code
<< "\n\n");
27 tooling::Replacements Replaces
=
28 format::sortUsingDeclarations(Style
, Code
, Ranges
, "<stdin>");
29 auto Result
= applyAllReplacements(Code
, Replaces
);
30 EXPECT_TRUE(static_cast<bool>(Result
));
31 LLVM_DEBUG(llvm::errs() << "\n" << *Result
<< "\n\n");
35 std::string
sortUsingDeclarations(StringRef Code
,
36 const FormatStyle
&Style
= getLLVMStyle()) {
37 return sortUsingDeclarations(Code
,
38 /*Ranges=*/{1, tooling::Range(0, Code
.size())},
43 TEST_F(UsingDeclarationsSorterTest
, SwapsTwoConsecutiveUsingDeclarations
) {
44 FormatStyle Style
= getLLVMStyle();
45 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
46 EXPECT_EQ("using a;\n"
48 sortUsingDeclarations("using a;\n"
51 EXPECT_EQ("using a;\n"
53 sortUsingDeclarations("using aa;\n"
56 EXPECT_EQ("using a;\n"
58 sortUsingDeclarations("using a;\n"
62 EXPECT_EQ("using a::bcd;\n"
64 sortUsingDeclarations("using a::cd;\n"
68 EXPECT_EQ("using a;\n"
70 sortUsingDeclarations("using a::a;\n"
74 EXPECT_EQ("using a::ba::aa;\n"
76 sortUsingDeclarations("using a::bb::ccc;\n"
80 EXPECT_EQ("using a;\n"
82 sortUsingDeclarations("using typename a;\n"
86 EXPECT_EQ("using typename z;\n"
88 sortUsingDeclarations("using typenamea;\n"
92 EXPECT_EQ("using a, b;\n"
94 sortUsingDeclarations("using aa;\n"
98 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
99 EXPECT_EQ("using a;\n"
101 sortUsingDeclarations("using a;\n"
104 EXPECT_EQ("using a;\n"
106 sortUsingDeclarations("using aa;\n"
109 EXPECT_EQ("using a;\n"
111 sortUsingDeclarations("using a;\n"
115 EXPECT_EQ("using a::bcd;\n"
117 sortUsingDeclarations("using a::cd;\n"
121 EXPECT_EQ("using a;\n"
123 sortUsingDeclarations("using a::a;\n"
127 EXPECT_EQ("using a::ba::aa;\n"
129 sortUsingDeclarations("using a::bb::ccc;\n"
133 EXPECT_EQ("using a;\n"
135 sortUsingDeclarations("using typename a;\n"
139 EXPECT_EQ("using typename z;\n"
141 sortUsingDeclarations("using typenamea;\n"
145 EXPECT_EQ("using a, b;\n"
147 sortUsingDeclarations("using aa;\n"
152 TEST_F(UsingDeclarationsSorterTest
, UsingDeclarationOrder
) {
153 FormatStyle Style
= getLLVMStyle();
154 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
155 EXPECT_EQ("using A;\n"
157 sortUsingDeclarations("using A;\n"
160 EXPECT_EQ("using a;\n"
162 sortUsingDeclarations("using a;\n"
165 EXPECT_EQ("using a;\n"
167 sortUsingDeclarations("using B;\n"
171 // Ignores leading '::'.
172 EXPECT_EQ("using ::a;\n"
174 sortUsingDeclarations("using ::a;\n"
178 EXPECT_EQ("using ::A;\n"
180 sortUsingDeclarations("using ::A;\n"
184 // Sorts '_' before 'a' and 'A'.
185 EXPECT_EQ("using _;\n"
187 sortUsingDeclarations("using A;\n"
190 EXPECT_EQ("using _;\n"
192 sortUsingDeclarations("using a;\n"
195 EXPECT_EQ("using a::_;\n"
197 sortUsingDeclarations("using a::a;\n"
201 // Sorts non-namespace names before namespace names at the same level.
202 EXPECT_EQ("using ::testing::_;\n"
203 "using ::testing::Aardvark;\n"
204 "using ::testing::kMax;\n"
205 "using ::testing::Xylophone;\n"
206 "using ::testing::apple::Honeycrisp;\n"
207 "using ::testing::zebra::Stripes;",
208 sortUsingDeclarations("using ::testing::Aardvark;\n"
209 "using ::testing::Xylophone;\n"
210 "using ::testing::kMax;\n"
211 "using ::testing::_;\n"
212 "using ::testing::apple::Honeycrisp;\n"
213 "using ::testing::zebra::Stripes;",
216 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
217 EXPECT_EQ("using A;\n"
219 sortUsingDeclarations("using A;\n"
222 EXPECT_EQ("using a;\n"
224 sortUsingDeclarations("using a;\n"
227 EXPECT_EQ("using a;\n"
229 sortUsingDeclarations("using B;\n"
233 // Ignores leading '::'.
234 EXPECT_EQ("using ::a;\n"
236 sortUsingDeclarations("using ::a;\n"
240 EXPECT_EQ("using ::A;\n"
242 sortUsingDeclarations("using ::A;\n"
246 // Sorts '_' before 'a' and 'A'.
247 EXPECT_EQ("using _;\n"
249 sortUsingDeclarations("using A;\n"
252 EXPECT_EQ("using _;\n"
254 sortUsingDeclarations("using a;\n"
257 EXPECT_EQ("using a::_;\n"
259 sortUsingDeclarations("using a::a;\n"
263 // Sorts non-namespace names before namespace names at the same level.
264 EXPECT_EQ("using ::testing::_;\n"
265 "using ::testing::Aardvark;\n"
266 "using ::testing::apple::Honeycrisp;\n"
267 "using ::testing::kMax;\n"
268 "using ::testing::Xylophone;\n"
269 "using ::testing::zebra::Stripes;",
270 sortUsingDeclarations("using ::testing::Aardvark;\n"
271 "using ::testing::Xylophone;\n"
272 "using ::testing::kMax;\n"
273 "using ::testing::_;\n"
274 "using ::testing::apple::Honeycrisp;\n"
275 "using ::testing::zebra::Stripes;",
279 TEST_F(UsingDeclarationsSorterTest
, SortsStably
) {
280 FormatStyle Style
= getLLVMStyle();
281 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
282 EXPECT_EQ("using a;\n"
295 sortUsingDeclarations("using a;\n"
314 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
315 EXPECT_EQ("using a;\n"
328 sortUsingDeclarations("using a;\n"
348 TEST_F(UsingDeclarationsSorterTest
, SortsMultipleTopLevelDeclarations
) {
349 FormatStyle Style
= getLLVMStyle();
350 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
351 EXPECT_EQ("using a;\n"
356 sortUsingDeclarations("using d;\n"
363 EXPECT_EQ("#include <iostream>\n"
366 "using ::std::endl;\n"
368 sortUsingDeclarations("#include <iostream>\n"
370 "using ::std::endl;\n"
375 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
376 EXPECT_EQ("using a;\n"
381 sortUsingDeclarations("using d;\n"
388 EXPECT_EQ("#include <iostream>\n"
391 "using ::std::endl;\n"
393 sortUsingDeclarations("#include <iostream>\n"
395 "using ::std::endl;\n"
401 TEST_F(UsingDeclarationsSorterTest
, BreaksOnEmptyLines
) {
402 FormatStyle Style
= getLLVMStyle();
403 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
404 EXPECT_EQ("using b;\n"
409 sortUsingDeclarations("using c;\n"
416 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
417 EXPECT_EQ("using b;\n"
422 sortUsingDeclarations("using c;\n"
430 TEST_F(UsingDeclarationsSorterTest
, BreaksOnUsingNamespace
) {
431 FormatStyle Style
= getLLVMStyle();
432 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
433 EXPECT_EQ("using b;\n"
434 "using namespace std;\n"
436 sortUsingDeclarations("using b;\n"
437 "using namespace std;\n"
441 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
442 EXPECT_EQ("using b;\n"
443 "using namespace std;\n"
445 sortUsingDeclarations("using b;\n"
446 "using namespace std;\n"
451 TEST_F(UsingDeclarationsSorterTest
, KeepsUsingDeclarationsInPPDirectives
) {
452 FormatStyle Style
= getLLVMStyle();
453 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
454 EXPECT_EQ("#define A \\\n"
457 sortUsingDeclarations("#define A \\\n"
462 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
463 EXPECT_EQ("#define A \\\n"
466 sortUsingDeclarations("#define A \\\n"
472 TEST_F(UsingDeclarationsSorterTest
, KeepsTypeAliases
) {
473 auto Code
= "struct C { struct B { struct A; }; };\n"
477 FormatStyle Style
= getLLVMStyle();
478 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
479 EXPECT_EQ(Code
, sortUsingDeclarations(Code
, Style
));
481 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
482 EXPECT_EQ(Code
, sortUsingDeclarations(Code
, Style
));
485 TEST_F(UsingDeclarationsSorterTest
, MovesTrailingCommentsWithDeclarations
) {
486 FormatStyle Style
= getLLVMStyle();
487 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
488 EXPECT_EQ("using a; // line a1\n"
489 "using b; /* line b1\n"
492 "using c; // line c1\n"
494 sortUsingDeclarations("using c; // line c1\n"
496 "using b; /* line b1\n"
499 "using a; // line a1",
502 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
503 EXPECT_EQ("using a; // line a1\n"
504 "using b; /* line b1\n"
507 "using c; // line c1\n"
509 sortUsingDeclarations("using c; // line c1\n"
511 "using b; /* line b1\n"
514 "using a; // line a1",
518 TEST_F(UsingDeclarationsSorterTest
, SortsInStructScope
) {
519 FormatStyle Style
= getLLVMStyle();
520 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
521 EXPECT_EQ("struct pt3 : pt2 {\n"
526 sortUsingDeclarations("struct pt3 : pt2 {\n"
533 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
534 EXPECT_EQ("struct pt3 : pt2 {\n"
539 sortUsingDeclarations("struct pt3 : pt2 {\n"
547 TEST_F(UsingDeclarationsSorterTest
, KeepsOperators
) {
548 FormatStyle Style
= getLLVMStyle();
549 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
550 EXPECT_EQ("using a::operator();\n"
551 "using a::operator-;\n"
552 "using a::operator+;",
553 sortUsingDeclarations("using a::operator();\n"
554 "using a::operator-;\n"
555 "using a::operator+;",
558 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
559 EXPECT_EQ("using a::operator();\n"
560 "using a::operator-;\n"
561 "using a::operator+;",
562 sortUsingDeclarations("using a::operator();\n"
563 "using a::operator-;\n"
564 "using a::operator+;",
568 TEST_F(UsingDeclarationsSorterTest
, SortsUsingDeclarationsInsideNamespaces
) {
569 FormatStyle Style
= getLLVMStyle();
570 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
571 EXPECT_EQ("namespace A {\n"
579 sortUsingDeclarations("namespace A {\n"
589 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
590 EXPECT_EQ("namespace A {\n"
598 sortUsingDeclarations("namespace A {\n"
609 TEST_F(UsingDeclarationsSorterTest
, SupportsClangFormatOff
) {
610 FormatStyle Style
= getLLVMStyle();
611 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
612 EXPECT_EQ("// clang-format off\n"
615 "// clang-format on\n"
618 sortUsingDeclarations("// clang-format off\n"
621 "// clang-format on\n"
626 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
627 EXPECT_EQ("// clang-format off\n"
630 "// clang-format on\n"
633 sortUsingDeclarations("// clang-format off\n"
636 "// clang-format on\n"
642 TEST_F(UsingDeclarationsSorterTest
, SortsPartialRangeOfUsingDeclarations
) {
643 FormatStyle Style
= getLLVMStyle();
644 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
645 // Sorts the whole block of using declarations surrounding the range.
646 EXPECT_EQ("using a;\n"
649 sortUsingDeclarations("using b;\n"
650 "using c;\n" // starts at offset 10
652 {tooling::Range(10, 15)}, Style
));
653 EXPECT_EQ("using a;\n"
657 sortUsingDeclarations("using b;\n"
658 "using c;\n" // starts at offset 10
661 {tooling::Range(10, 15)}, Style
));
663 EXPECT_EQ("using d;\n"
671 sortUsingDeclarations("using d;\n"
674 "using b;\n" // starts at offset 19
679 {tooling::Range(19, 1)}, Style
));
681 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
682 // Sorts the whole block of using declarations surrounding the range.
683 EXPECT_EQ("using a;\n"
686 sortUsingDeclarations("using b;\n"
687 "using c;\n" // starts at offset 10
689 {tooling::Range(10, 15)}, Style
));
690 EXPECT_EQ("using a;\n"
694 sortUsingDeclarations("using b;\n"
695 "using c;\n" // starts at offset 10
698 {tooling::Range(10, 15)}, Style
));
700 EXPECT_EQ("using d;\n"
708 sortUsingDeclarations("using d;\n"
711 "using b;\n" // starts at offset 19
716 {tooling::Range(19, 1)}, Style
));
719 TEST_F(UsingDeclarationsSorterTest
,
720 SortsUsingDeclarationsWithLeadingkComments
) {
721 FormatStyle Style
= getLLVMStyle();
722 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
723 EXPECT_EQ("/* comment */ using a;\n"
724 "/* comment */ using b;",
725 sortUsingDeclarations("/* comment */ using b;\n"
726 "/* comment */ using a;",
729 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
730 EXPECT_EQ("/* comment */ using a;\n"
731 "/* comment */ using b;",
732 sortUsingDeclarations("/* comment */ using b;\n"
733 "/* comment */ using a;",
737 TEST_F(UsingDeclarationsSorterTest
, DeduplicatesUsingDeclarations
) {
738 FormatStyle Style
= getLLVMStyle();
739 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
740 EXPECT_EQ("using a;\n"
746 sortUsingDeclarations("using c;\n"
757 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
758 EXPECT_EQ("using a;\n"
764 sortUsingDeclarations("using c;\n"
776 TEST_F(UsingDeclarationsSorterTest
,
777 SortsUsingDeclarationsWithDifferentCountsOfScopes
) {
778 FormatStyle Style
= getLLVMStyle();
779 EXPECT_EQ(FormatStyle::SUD_LexicographicNumeric
, Style
.SortUsingDeclarations
);
780 EXPECT_EQ("using boost::regex;\n"
781 "using boost::regex_constants::icase;\n"
783 "using std::string;\n"
784 "using std::chrono::duration_cast;\n"
785 "using std::chrono::microseconds;\n"
786 "using std::chrono::seconds;\n"
787 "using std::chrono::steady_clock;",
788 sortUsingDeclarations("using boost::regex;\n"
789 "using boost::regex_constants::icase;\n"
790 "using std::chrono::duration_cast;\n"
791 "using std::chrono::microseconds;\n"
792 "using std::chrono::seconds;\n"
793 "using std::chrono::steady_clock;\n"
795 "using std::string;",
798 Style
.SortUsingDeclarations
= FormatStyle::SUD_Lexicographic
;
799 EXPECT_EQ("using boost::regex;\n"
800 "using boost::regex_constants::icase;\n"
801 "using std::chrono::duration_cast;\n"
802 "using std::chrono::microseconds;\n"
803 "using std::chrono::seconds;\n"
804 "using std::chrono::steady_clock;\n"
806 "using std::string;",
807 sortUsingDeclarations("using boost::regex;\n"
808 "using boost::regex_constants::icase;\n"
810 "using std::string;\n"
811 "using std::chrono::duration_cast;\n"
812 "using std::chrono::microseconds;\n"
813 "using std::chrono::seconds;\n"
814 "using std::chrono::steady_clock;",
819 } // end namespace format
820 } // end namespace clang