1 //===- NamespaceEndCommentsFixerTest.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 "namespace-end-comments-fixer-test"
20 class NamespaceEndCommentsFixerTest
: public ::testing::Test
{
23 fixNamespaceEndComments(llvm::StringRef Code
,
24 const std::vector
<tooling::Range
> &Ranges
,
25 const FormatStyle
&Style
= getLLVMStyle()) {
26 LLVM_DEBUG(llvm::errs() << "---\n");
27 LLVM_DEBUG(llvm::errs() << Code
<< "\n\n");
28 tooling::Replacements Replaces
=
29 clang::format::fixNamespaceEndComments(Style
, Code
, Ranges
, "<stdin>");
30 auto Result
= applyAllReplacements(Code
, Replaces
);
31 EXPECT_TRUE(static_cast<bool>(Result
));
32 LLVM_DEBUG(llvm::errs() << "\n" << *Result
<< "\n\n");
37 fixNamespaceEndComments(llvm::StringRef Code
,
38 const FormatStyle
&Style
= getLLVMStyle()) {
39 return fixNamespaceEndComments(
41 /*Ranges=*/{1, tooling::Range(0, Code
.size())}, Style
);
44 bool isFormatted(StringRef Code
, const std::vector
<tooling::Range
> &Ranges
,
45 const FormatStyle
&Style
= getLLVMStyle()) const {
46 return clang::format::fixNamespaceEndComments(Style
, Code
, Ranges
,
51 bool isFormatted(StringRef Code
,
52 const FormatStyle
&Style
= getLLVMStyle()) const {
53 return isFormatted(Code
, {1, tooling::Range(0, Code
.size())}, Style
);
57 TEST_F(NamespaceEndCommentsFixerTest
, AddsEndComment
) {
58 EXPECT_EQ("namespace {\n"
62 fixNamespaceEndComments("namespace {\n"
67 EXPECT_EQ("namespace {\n"
71 fixNamespaceEndComments("namespace {\n"
75 EXPECT_EQ("namespace A {\n"
79 fixNamespaceEndComments("namespace A {\n"
83 EXPECT_EQ("#define M(x) x##x\n"
87 "} // namespace M(x)",
88 fixNamespaceEndComments("#define M(x) x##x\n"
93 EXPECT_EQ("#define M(x) x##x\n"
94 "namespace A::M(x) {\n"
97 "} // namespace A::M(x)",
98 fixNamespaceEndComments("#define M(x) x##x\n"
99 "namespace A::M(x) {\n"
103 EXPECT_EQ("#define M(x) x##x\n"
104 "namespace M(x)::A {\n"
107 "} // namespace M(x)::A",
108 fixNamespaceEndComments("#define M(x) x##x\n"
109 "namespace M(x)::A {\n"
113 EXPECT_EQ("#define M(x) x##x\n"
114 "namespace A::inline M(x)::B {\n"
117 "} // namespace A::inline M(x)::B",
118 fixNamespaceEndComments("#define M(x) x##x\n"
119 "namespace A::inline M(x)::B {\n"
123 EXPECT_EQ("#define M(x) x##x\n"
124 "namespace [[deprecated(\"foo\")]] A::inline M(x)::A {\n"
127 "} // namespace A::inline M(x)::A",
128 fixNamespaceEndComments(
129 "#define M(x) x##x\n"
130 "namespace [[deprecated(\"foo\")]] A::inline M(x)::A {\n"
135 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ A {\n"
139 fixNamespaceEndComments(
140 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ A {\n"
144 EXPECT_EQ("namespace /* comment */ [[deprecated(\"foo\")]] A {\n"
148 fixNamespaceEndComments(
149 "namespace /* comment */ [[deprecated(\"foo\")]] A {\n"
154 "#define M(x) x##x\n"
155 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ M(x) {\n"
158 "} // namespace M(x)",
159 fixNamespaceEndComments("#define M(x) x##x\n"
160 "namespace /* comment */ "
161 "[[deprecated(\"foo\")]] /* comment */ M(x) {\n"
165 EXPECT_EQ("#define M(x) x##x\n"
166 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ "
170 "} // namespace A::M(x)",
171 fixNamespaceEndComments(
172 "#define M(x) x##x\n"
173 "namespace /* comment */ "
174 "[[deprecated(\"foo\")]] /* comment */ A::M(x) {\n"
178 EXPECT_EQ("#define M(x) x##x\n"
179 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ "
180 "M(x) /* comment */ {\n"
183 "} // namespace M(x)",
184 fixNamespaceEndComments(
185 "#define M(x) x##x\n"
186 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment "
187 "*/ M(x) /* comment */ {\n"
191 EXPECT_EQ("#define M(x) x##x\n"
192 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ "
193 "A::M(x) /* comment */ {\n"
196 "} // namespace A::M(x)",
197 fixNamespaceEndComments(
198 "#define M(x) x##x\n"
199 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment "
200 "*/ A::M(x) /* comment */ {\n"
204 EXPECT_EQ("#define M(x) x##x\n"
205 "namespace A M(x) {\n"
208 "} // namespace A M(x)",
209 fixNamespaceEndComments("#define M(x) x##x\n"
210 "namespace A M(x) {\n"
215 "#define B __attribute__((availability(macos, introduced=10.15)))\n"
219 "} // namespace A B",
220 fixNamespaceEndComments(
221 "#define B __attribute__((availability(macos, introduced=10.15)))\n"
226 EXPECT_EQ("#define M(x) x##x\n"
227 "namespace A::B M(x) {\n"
230 "} // namespace A::B",
231 fixNamespaceEndComments("#define M(x) x##x\n"
232 "namespace A::B M(x) {\n"
237 "namespace A __attribute__((availability(macos, introduced=10.15))) {\n"
241 fixNamespaceEndComments(
242 "namespace A __attribute__((availability(macos, introduced=10.15))) "
247 EXPECT_EQ("inline namespace A {\n"
251 fixNamespaceEndComments("inline namespace A {\n"
256 EXPECT_EQ("namespace [[deprecated(\"foo\")]] A::B {\n"
259 "} // namespace A::B",
260 fixNamespaceEndComments("namespace [[deprecated(\"foo\")]] A::B {\n"
265 EXPECT_EQ("namespace [[deprecated(\"foo\")]] A::inline B::inline C {\n"
268 "} // namespace A::inline B::inline C",
269 fixNamespaceEndComments(
270 "namespace [[deprecated(\"foo\")]] A::inline B::inline C {\n"
275 EXPECT_EQ("namespace DEPRECATED A::B {\n"
278 "} // namespace A::B",
279 fixNamespaceEndComments("namespace DEPRECATED A::B {\n"
284 EXPECT_EQ("inline namespace [[deprecated]] A {\n"
288 fixNamespaceEndComments("inline namespace [[deprecated]] A {\n"
293 EXPECT_EQ("namespace ::A {\n"
296 "} // namespace ::A",
297 fixNamespaceEndComments("namespace ::A {\n"
301 EXPECT_EQ("namespace ::A::B {\n"
304 "} // namespace ::A::B",
305 fixNamespaceEndComments("namespace ::A::B {\n"
309 EXPECT_EQ("namespace /**/::/**/A/**/::/**/B/**/ {\n"
312 "} // namespace ::A::B",
313 fixNamespaceEndComments("namespace /**/::/**/A/**/::/**/B/**/ {\n"
317 EXPECT_EQ("namespace A {\n"
322 fixNamespaceEndComments("namespace A {\n"
327 EXPECT_EQ("namespace A {\n"
333 fixNamespaceEndComments("namespace A {\n"
339 EXPECT_EQ("namespace A {\n"
347 fixNamespaceEndComments("namespace A {\n"
355 EXPECT_EQ("namespace A {\n"
363 fixNamespaceEndComments("namespace A {\n"
371 EXPECT_EQ("namespace A {\n"
375 "// comment about b\n"
377 fixNamespaceEndComments("namespace A {\n"
381 "// comment about b\n"
384 EXPECT_EQ("namespace A {\n"
392 fixNamespaceEndComments("namespace A {\n"
401 // Add comment for namespaces which will be 'compacted'
402 FormatStyle CompactNamespacesStyle
= getLLVMStyle();
403 CompactNamespacesStyle
.CompactNamespaces
= true;
404 EXPECT_EQ("namespace out { namespace in {\n"
407 "}} // namespace out::in",
408 fixNamespaceEndComments("namespace out { namespace in {\n"
412 CompactNamespacesStyle
));
413 EXPECT_EQ("namespace out {\n"
418 "} // namespace out::in",
419 fixNamespaceEndComments("namespace out {\n"
425 CompactNamespacesStyle
));
426 EXPECT_EQ("namespace out { namespace in {\n"
429 "};} // namespace out::in",
430 fixNamespaceEndComments("namespace out { namespace in {\n"
434 CompactNamespacesStyle
));
435 EXPECT_EQ("namespace out { namespace {\n"
438 "}} // namespace out",
439 fixNamespaceEndComments("namespace out { namespace {\n"
443 CompactNamespacesStyle
));
445 // Adds an end comment after a semicolon.
446 EXPECT_EQ("namespace {\n"
450 fixNamespaceEndComments("namespace {\n"
454 EXPECT_EQ("namespace A {\n"
458 fixNamespaceEndComments("namespace A {\n"
462 EXPECT_EQ("namespace A {\n"
465 "}; // namespace A\n"
467 fixNamespaceEndComments("namespace A {\n"
474 TEST_F(NamespaceEndCommentsFixerTest
, WorksForObjCpp
) {
475 FormatStyle ObjCppStyle
= getLLVMStyle();
476 ObjCppStyle
.Language
= FormatStyle::LK_ObjC
;
477 EXPECT_EQ("namespace {\n"
481 fixNamespaceEndComments("namespace {\n"
488 TEST_F(NamespaceEndCommentsFixerTest
, AddsMacroEndComment
) {
489 FormatStyle Style
= getLLVMStyle();
490 Style
.NamespaceMacros
.push_back("TESTSUITE");
492 EXPECT_EQ("TESTSUITE() {\n"
496 fixNamespaceEndComments("TESTSUITE() {\n"
502 EXPECT_EQ("TESTSUITE(A) {\n"
506 fixNamespaceEndComments("TESTSUITE(A) {\n"
511 EXPECT_EQ("inline TESTSUITE(A) {\n"
515 fixNamespaceEndComments("inline TESTSUITE(A) {\n"
520 EXPECT_EQ("TESTSUITE(::A) {\n"
523 "} // TESTSUITE(::A)",
524 fixNamespaceEndComments("TESTSUITE(::A) {\n"
529 EXPECT_EQ("TESTSUITE(::A::B) {\n"
532 "} // TESTSUITE(::A::B)",
533 fixNamespaceEndComments("TESTSUITE(::A::B) {\n"
538 EXPECT_EQ("TESTSUITE(/**/::/**/A/**/::/**/B/**/) {\n"
541 "} // TESTSUITE(::A::B)",
542 fixNamespaceEndComments("TESTSUITE(/**/::/**/A/**/::/**/B/**/) {\n"
547 EXPECT_EQ("TESTSUITE(A, B) {\n"
551 fixNamespaceEndComments("TESTSUITE(A, B) {\n"
556 EXPECT_EQ("TESTSUITE(\"Test1\") {\n"
559 "} // TESTSUITE(\"Test1\")",
560 fixNamespaceEndComments("TESTSUITE(\"Test1\") {\n"
567 TEST_F(NamespaceEndCommentsFixerTest
, AddsNewlineIfNeeded
) {
568 EXPECT_EQ("namespace A {\n"
573 fixNamespaceEndComments("namespace A {\n"
577 EXPECT_EQ("namespace {\n"
582 fixNamespaceEndComments("namespace {\n"
586 EXPECT_EQ("namespace A {\n"
594 fixNamespaceEndComments("namespace A {\n"
601 EXPECT_EQ("namespace {\n"
606 fixNamespaceEndComments("namespace {\n"
610 EXPECT_EQ("namespace {\n"
615 fixNamespaceEndComments("namespace {\n"
621 TEST_F(NamespaceEndCommentsFixerTest
, DoesNotAddEndCommentForShortNamespace
) {
622 EXPECT_EQ("namespace {}", fixNamespaceEndComments("namespace {}"));
623 EXPECT_EQ("namespace A {}", fixNamespaceEndComments("namespace A {}"));
624 EXPECT_EQ("namespace A { a }", fixNamespaceEndComments("namespace A { a }"));
625 EXPECT_EQ("namespace A { a };",
626 fixNamespaceEndComments("namespace A { a };"));
629 TEST_F(NamespaceEndCommentsFixerTest
, DoesNotAddCommentAfterUnaffectedRBrace
) {
630 EXPECT_EQ("namespace A {\n"
633 fixNamespaceEndComments("namespace A {\n"
636 // The range (16, 3) spans the 'int' above.
637 /*Ranges=*/{1, tooling::Range(16, 3)}));
638 EXPECT_EQ("namespace A {\n"
641 fixNamespaceEndComments("namespace A {\n"
644 // The range (16, 3) spans the 'int' above.
645 /*Ranges=*/{1, tooling::Range(16, 3)}));
648 TEST_F(NamespaceEndCommentsFixerTest
,
649 DoesNotAddCommentAfterRBraceInPPDirective
) {
650 EXPECT_EQ("#define SAD \\\n"
654 fixNamespaceEndComments("#define SAD \\\n"
660 TEST_F(NamespaceEndCommentsFixerTest
, KeepsValidEndComment
) {
661 EXPECT_TRUE(isFormatted("namespace {\n"
663 "} // end anonymous namespace"));
664 EXPECT_TRUE(isFormatted("namespace A {\n"
666 "} /* end of namespace A */"));
667 EXPECT_TRUE(isFormatted("namespace A {\n"
669 "} // namespace A"));
670 EXPECT_TRUE(isFormatted("namespace A::B {\n"
672 "} // end namespace A::B"));
673 EXPECT_TRUE(isFormatted("namespace A {\n"
675 "}; // end namespace A"));
676 EXPECT_TRUE(isFormatted("namespace {\n"
678 "}; /* unnamed namespace */"));
679 EXPECT_TRUE(isFormatted("namespace a::inline b {\n"
681 "}; // namespace a::inline b"));
684 TEST_F(NamespaceEndCommentsFixerTest
, KeepsValidMacroEndComment
) {
685 FormatStyle Style
= getLLVMStyle();
686 Style
.NamespaceMacros
.push_back("TESTSUITE");
688 EXPECT_TRUE(isFormatted("TESTSUITE() {\n"
690 "} // end anonymous TESTSUITE()",
692 EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
694 "} /* end of TESTSUITE(A) */",
696 EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
700 EXPECT_TRUE(isFormatted("TESTSUITE(A::B) {\n"
702 "} // end TESTSUITE(A::B)",
704 EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
706 "}; // end TESTSUITE(A)",
708 EXPECT_TRUE(isFormatted("TESTSUITE() {\n"
710 "}; /* unnamed TESTSUITE() */",
712 EXPECT_TRUE(isFormatted("TESTSUITE(\"foo\") {\n"
714 "} // TESTSUITE(\"foo\")",
718 TEST_F(NamespaceEndCommentsFixerTest
, UpdatesInvalidEndLineComment
) {
719 EXPECT_EQ("namespace {\n"
722 fixNamespaceEndComments("namespace {\n"
724 "} // namespace A"));
725 EXPECT_EQ("namespace A {\n"
728 fixNamespaceEndComments("namespace A {\n"
731 EXPECT_EQ("namespace A {\n"
734 fixNamespaceEndComments("namespace A {\n"
737 EXPECT_EQ("namespace A {\n"
740 fixNamespaceEndComments("namespace A {\n"
744 EXPECT_EQ("namespace A {\n"
747 fixNamespaceEndComments("namespace A {\n"
749 "} // banamespace A"));
750 EXPECT_EQ("namespace A {\n"
753 fixNamespaceEndComments("namespace A {\n"
755 "}; // banamespace A"));
756 // Updates invalid line comments even for short namespaces.
757 EXPECT_EQ("namespace A {} // namespace A",
758 fixNamespaceEndComments("namespace A {} // namespace"));
759 EXPECT_EQ("namespace A {}; // namespace A",
760 fixNamespaceEndComments("namespace A {}; // namespace"));
762 // Update invalid comments for compacted namespaces.
763 FormatStyle CompactNamespacesStyle
= getLLVMStyle();
764 CompactNamespacesStyle
.CompactNamespaces
= true;
765 EXPECT_EQ("namespace out { namespace in {\n"
766 "}} // namespace out::in",
767 fixNamespaceEndComments("namespace out { namespace in {\n"
768 "}} // namespace out",
769 CompactNamespacesStyle
));
770 EXPECT_EQ("namespace out { namespace in {\n"
771 "}} // namespace out::in",
772 fixNamespaceEndComments("namespace out { namespace in {\n"
773 "}} // namespace in",
774 CompactNamespacesStyle
));
775 EXPECT_EQ("namespace out { namespace in {\n"
777 "} // namespace out::in",
778 fixNamespaceEndComments("namespace out { namespace in {\n"
779 "}// banamespace in\n"
780 "} // namespace out",
781 CompactNamespacesStyle
));
782 EXPECT_EQ("namespace out { namespace {\n"
783 "}} // namespace out",
784 fixNamespaceEndComments("namespace out { namespace {\n"
785 "}} // namespace out::",
786 CompactNamespacesStyle
));
789 TEST_F(NamespaceEndCommentsFixerTest
, UpdatesInvalidMacroEndLineComment
) {
790 FormatStyle Style
= getLLVMStyle();
791 Style
.NamespaceMacros
.push_back("TESTSUITE");
793 EXPECT_EQ("TESTSUITE() {\n"
796 fixNamespaceEndComments("TESTSUITE() {\n"
800 EXPECT_EQ("TESTSUITE(A) {\n"
803 fixNamespaceEndComments("TESTSUITE(A) {\n"
807 EXPECT_EQ("TESTSUITE(A) {\n"
810 fixNamespaceEndComments("TESTSUITE(A) {\n"
814 EXPECT_EQ("TESTSUITE(A) {\n"
816 "}; // TESTSUITE(A)",
817 fixNamespaceEndComments("TESTSUITE(A) {\n"
821 EXPECT_EQ("TESTSUITE(A) {\n"
824 fixNamespaceEndComments("TESTSUITE(A) {\n"
828 EXPECT_EQ("TESTSUITE() {\n"
831 fixNamespaceEndComments("TESTSUITE() {\n"
835 EXPECT_EQ("TESTSUITE(A) {\n"
838 fixNamespaceEndComments("TESTSUITE(A) {\n"
840 "} // TOASTSUITE(A)",
842 EXPECT_EQ("TESTSUITE(A) {\n"
844 "}; // TESTSUITE(A)",
845 fixNamespaceEndComments("TESTSUITE(A) {\n"
847 "}; // TOASTSUITE(A)",
849 // Updates invalid line comments even for short namespaces.
850 EXPECT_EQ("TESTSUITE(A) {} // TESTSUITE(A)",
851 fixNamespaceEndComments("TESTSUITE(A) {} // TESTSUITE()", Style
));
852 EXPECT_EQ("TESTSUITE(A) {}; // TESTSUITE(A)",
853 fixNamespaceEndComments("TESTSUITE(A) {}; // TESTSUITE()", Style
));
855 // Update invalid comments for compacted namespaces.
856 FormatStyle CompactNamespacesStyle
= getLLVMStyle();
857 CompactNamespacesStyle
.CompactNamespaces
= true;
858 CompactNamespacesStyle
.NamespaceMacros
.push_back("TESTSUITE");
860 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
861 "}} // TESTSUITE(out::in)",
862 fixNamespaceEndComments("TESTSUITE(out) { TESTSUITE(in) {\n"
863 "}} // TESTSUITE(out)",
864 CompactNamespacesStyle
));
865 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
866 "}} // TESTSUITE(out::in)",
867 fixNamespaceEndComments("TESTSUITE(out) { TESTSUITE(in) {\n"
868 "}} // TESTSUITE(in)",
869 CompactNamespacesStyle
));
870 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
872 "} // TESTSUITE(out::in)",
873 fixNamespaceEndComments("TESTSUITE(out) { TESTSUITE(in) {\n"
874 "}// TAOSTSUITE(in)\n"
875 "} // TESTSUITE(out)",
876 CompactNamespacesStyle
));
879 TEST_F(NamespaceEndCommentsFixerTest
, UpdatesInvalidEndBlockComment
) {
880 EXPECT_EQ("namespace {\n"
883 fixNamespaceEndComments("namespace {\n"
885 "} /* namespace A */"));
886 EXPECT_EQ("namespace A {\n"
889 fixNamespaceEndComments("namespace A {\n"
891 "} /* end namespace */"));
892 EXPECT_EQ("namespace A {\n"
895 fixNamespaceEndComments("namespace A {\n"
898 EXPECT_EQ("namespace A {\n"
901 fixNamespaceEndComments("namespace A {\n"
903 "} /* end unnamed namespace */"));
904 EXPECT_EQ("namespace A {\n"
907 fixNamespaceEndComments("namespace A {\n"
909 "} /* banamespace A */"));
910 EXPECT_EQ("namespace A {\n"
913 fixNamespaceEndComments("namespace A {\n"
915 "}; /* banamespace A */"));
916 EXPECT_EQ("namespace A {} // namespace A",
917 fixNamespaceEndComments("namespace A {} /**/"));
918 EXPECT_EQ("namespace A {}; // namespace A",
919 fixNamespaceEndComments("namespace A {}; /**/"));
922 TEST_F(NamespaceEndCommentsFixerTest
, UpdatesInvalidMacroEndBlockComment
) {
923 FormatStyle Style
= getLLVMStyle();
924 Style
.NamespaceMacros
.push_back("TESTSUITE");
926 EXPECT_EQ("TESTSUITE() {\n"
929 fixNamespaceEndComments("TESTSUITE() {\n"
931 "} /* TESTSUITE(A) */",
933 EXPECT_EQ("TESTSUITE(A) {\n"
936 fixNamespaceEndComments("TESTSUITE(A) {\n"
938 "} /* end TESTSUITE() */",
940 EXPECT_EQ("TESTSUITE(A) {\n"
943 fixNamespaceEndComments("TESTSUITE(A) {\n"
947 EXPECT_EQ("TESTSUITE(A) {\n"
950 fixNamespaceEndComments("TESTSUITE(A) {\n"
952 "} /* end unnamed TESTSUITE() */",
954 EXPECT_EQ("TESTSUITE(A) {\n"
957 fixNamespaceEndComments("TESTSUITE(A) {\n"
959 "} /* TOASTSUITE(A) */",
961 EXPECT_EQ("TESTSUITE(A) {\n"
963 "}; // TESTSUITE(A)",
964 fixNamespaceEndComments("TESTSUITE(A) {\n"
966 "}; /* TAOSTSUITE(A) */",
968 EXPECT_EQ("TESTSUITE(A) {} // TESTSUITE(A)",
969 fixNamespaceEndComments("TESTSUITE(A) {} /**/", Style
));
970 EXPECT_EQ("TESTSUITE(A) {}; // TESTSUITE(A)",
971 fixNamespaceEndComments("TESTSUITE(A) {}; /**/", Style
));
974 TEST_F(NamespaceEndCommentsFixerTest
,
975 DoesNotAddEndCommentForNamespacesControlledByMacros
) {
976 EXPECT_EQ("#ifdef 1\n"
984 fixNamespaceEndComments("#ifdef 1\n"
994 TEST_F(NamespaceEndCommentsFixerTest
, AddsEndCommentForNamespacesAroundMacros
) {
995 // Conditional blocks around are fine
996 EXPECT_EQ("namespace A {\n"
1001 fixNamespaceEndComments("namespace A {\n"
1012 fixNamespaceEndComments("#if 1\n"
1018 EXPECT_EQ("namespace A {\n"
1021 "} // namespace A\n"
1024 fixNamespaceEndComments("namespace A {\n"
1034 "} // namespace A\n"
1036 fixNamespaceEndComments("#if 1\n"
1043 // Macro definition has no impact
1044 EXPECT_EQ("namespace A {\n"
1048 fixNamespaceEndComments("namespace A {\n"
1052 EXPECT_EQ("#define FOO\n"
1057 fixNamespaceEndComments("#define FOO\n"
1062 EXPECT_EQ("namespace A {\n"
1065 "} // namespace A\n"
1067 fixNamespaceEndComments("namespace A {\n"
1073 // No replacement if open & close in different conditional blocks
1082 fixNamespaceEndComments("#if 1\n"
1090 EXPECT_EQ("#ifdef A\n"
1098 fixNamespaceEndComments("#ifdef A\n"
1107 // No replacement inside unreachable conditional block
1114 fixNamespaceEndComments("#if 0\n"
1122 TEST_F(NamespaceEndCommentsFixerTest
,
1123 DoesNotAddEndCommentForNamespacesInMacroDeclarations
) {
1124 EXPECT_EQ("#ifdef 1\n"
1132 fixNamespaceEndComments("#ifdef 1\n"
1140 EXPECT_EQ("namespace {\n"
1149 fixNamespaceEndComments("namespace {\n"
1166 fixNamespaceEndComments("#if A\n"
1182 fixNamespaceEndComments("#if A\n"
1189 "} // namespace A"));
1198 fixNamespaceEndComments("#if A\n"
1205 "} // namespace B"));
1206 EXPECT_EQ("namespace A\n"
1214 fixNamespaceEndComments("namespace A\n"
1222 EXPECT_EQ("namespace A\n"
1226 "} // namespace A\n"
1228 "} // namespace A\n"
1230 fixNamespaceEndComments("namespace A\n"
1234 "} // namespace A\n"
1236 "} // namespace A\n"
1240 TEST_F(NamespaceEndCommentsFixerTest
,
1241 DoesNotAddEndCommentForUnbalancedRBracesAfterNamespaceEnd
) {
1242 EXPECT_EQ("namespace {\n"
1246 fixNamespaceEndComments("namespace {\n"
1252 TEST_F(NamespaceEndCommentsFixerTest
, HandlesInlineAtEndOfLine_PR32438
) {
1253 EXPECT_EQ("template <int> struct a {};\n"
1254 "struct a<bool{}> b() {\n"
1256 "#define c inline\n"
1259 fixNamespaceEndComments("template <int> struct a {};\n"
1260 "struct a<bool{}> b() {\n"
1262 "#define c inline\n"
1267 TEST_F(NamespaceEndCommentsFixerTest
, IgnoreUnbalanced
) {
1268 EXPECT_EQ("namespace A {\n"
1272 fixNamespaceEndComments("namespace A {\n"
1276 EXPECT_EQ("namespace A {\n"
1279 fixNamespaceEndComments("namespace A {\n"
1283 EXPECT_EQ("namespace A {\n"
1288 fixNamespaceEndComments("namespace A {\n"
1295 using ShortNamespaceLinesTest
= NamespaceEndCommentsFixerTest
;
1297 TEST_F(ShortNamespaceLinesTest
, ZeroUnwrappedLines
) {
1298 auto Style
= getLLVMStyle();
1299 Style
.ShortNamespaceLines
= 0u;
1301 EXPECT_EQ("namespace OneLinerNamespace {}",
1302 fixNamespaceEndComments("namespace OneLinerNamespace {}", Style
));
1303 EXPECT_EQ("namespace ShortNamespace {\n"
1305 fixNamespaceEndComments("namespace ShortNamespace {\n"
1308 EXPECT_EQ("namespace LongNamespace {\n"
1310 "} // namespace LongNamespace",
1311 fixNamespaceEndComments("namespace LongNamespace {\n"
1317 TEST_F(ShortNamespaceLinesTest
, OneUnwrappedLine
) {
1318 constexpr auto DefaultUnwrappedLines
= 1u;
1319 auto const Style
= getLLVMStyle();
1321 EXPECT_EQ(DefaultUnwrappedLines
, Style
.ShortNamespaceLines
);
1322 EXPECT_EQ("namespace ShortNamespace {\n"
1325 fixNamespaceEndComments("namespace ShortNamespace {\n"
1328 EXPECT_EQ("namespace LongNamespace {\n"
1331 "} // namespace LongNamespace",
1332 fixNamespaceEndComments("namespace LongNamespace {\n"
1338 TEST_F(ShortNamespaceLinesTest
, MultipleUnwrappedLine
) {
1339 auto Style
= getLLVMStyle();
1340 Style
.ShortNamespaceLines
= 2u;
1342 EXPECT_EQ("namespace ShortNamespace {\n"
1346 fixNamespaceEndComments("namespace ShortNamespace {\n"
1351 EXPECT_EQ("namespace LongNamespace {\n"
1355 "} // namespace LongNamespace",
1356 fixNamespaceEndComments("namespace LongNamespace {\n"
1363 // The namespace body has 5 unwrapped/annotated lines.
1364 const std::string NestedLambdas
{"namespace foo {\n"
1365 "auto bar = [] {\n" // line 1
1366 " int i;\n" // line 2
1367 " return [] {\n" // line 3
1369 " return 0;\n" // line 5
1370 " };\n" // part of line 3
1371 "};\n" // part of line 1
1373 Style
.ShortNamespaceLines
= 4;
1374 EXPECT_EQ(NestedLambdas
+ " // namespace foo",
1375 fixNamespaceEndComments(NestedLambdas
, Style
));
1376 ++Style
.ShortNamespaceLines
;
1377 EXPECT_EQ(NestedLambdas
, fixNamespaceEndComments(NestedLambdas
, Style
));
1380 TEST_F(ShortNamespaceLinesTest
, NamespaceAlias
) {
1381 auto Style
= getLLVMStyle();
1383 EXPECT_EQ("namespace n = nn;\n"
1388 fixNamespaceEndComments("namespace n = nn;\n"
1395 EXPECT_EQ("namespace n = nn; // comment\n"
1400 fixNamespaceEndComments("namespace n = nn; // comment\n"
1407 EXPECT_EQ("namespace n = nn; /* comment */\n"
1412 fixNamespaceEndComments("namespace n = nn; /* comment */\n"
1420 "namespace n = nn; /* comment */ /* comment2 */\n"
1425 fixNamespaceEndComments("namespace n = nn; /* comment */ /* comment2 */\n"
1432 EXPECT_EQ("namespace n = nn; {\n"
1436 fixNamespaceEndComments("namespace n = nn; {\n"
1441 EXPECT_EQ("int foo;\n"
1447 fixNamespaceEndComments("int foo;\n"
1456 } // end namespace format
1457 } // end namespace clang