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(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 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(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 format::fixNamespaceEndComments(Style
, Code
, Ranges
, "<stdin>")
50 bool isFormatted(StringRef Code
,
51 const FormatStyle
&Style
= getLLVMStyle()) const {
52 return isFormatted(Code
, {1, tooling::Range(0, Code
.size())}, Style
);
56 TEST_F(NamespaceEndCommentsFixerTest
, AddsEndComment
) {
57 EXPECT_EQ("namespace {\n"
61 fixNamespaceEndComments("namespace {\n"
66 EXPECT_EQ("namespace {\n"
70 fixNamespaceEndComments("namespace {\n"
74 EXPECT_EQ("namespace A {\n"
78 fixNamespaceEndComments("namespace A {\n"
82 EXPECT_EQ("#define M(x) x##x\n"
86 "} // namespace M(x)",
87 fixNamespaceEndComments("#define M(x) x##x\n"
92 EXPECT_EQ("#define M(x) x##x\n"
93 "namespace A::M(x) {\n"
96 "} // namespace A::M(x)",
97 fixNamespaceEndComments("#define M(x) x##x\n"
98 "namespace A::M(x) {\n"
102 EXPECT_EQ("#define M(x) x##x\n"
103 "namespace M(x)::A {\n"
106 "} // namespace M(x)::A",
107 fixNamespaceEndComments("#define M(x) x##x\n"
108 "namespace M(x)::A {\n"
112 EXPECT_EQ("#define M(x) x##x\n"
113 "namespace A::inline M(x)::B {\n"
116 "} // namespace A::inline M(x)::B",
117 fixNamespaceEndComments("#define M(x) x##x\n"
118 "namespace A::inline M(x)::B {\n"
122 EXPECT_EQ("#define M(x) x##x\n"
123 "namespace [[deprecated(\"foo\")]] A::inline M(x)::A {\n"
126 "} // namespace A::inline M(x)::A",
127 fixNamespaceEndComments(
128 "#define M(x) x##x\n"
129 "namespace [[deprecated(\"foo\")]] A::inline M(x)::A {\n"
134 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ A {\n"
138 fixNamespaceEndComments(
139 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ A {\n"
143 EXPECT_EQ("namespace /* comment */ [[deprecated(\"foo\")]] A {\n"
147 fixNamespaceEndComments(
148 "namespace /* comment */ [[deprecated(\"foo\")]] A {\n"
153 "#define M(x) x##x\n"
154 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ M(x) {\n"
157 "} // namespace M(x)",
158 fixNamespaceEndComments("#define M(x) x##x\n"
159 "namespace /* comment */ "
160 "[[deprecated(\"foo\")]] /* comment */ M(x) {\n"
164 EXPECT_EQ("#define M(x) x##x\n"
165 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ "
169 "} // namespace A::M(x)",
170 fixNamespaceEndComments(
171 "#define M(x) x##x\n"
172 "namespace /* comment */ "
173 "[[deprecated(\"foo\")]] /* comment */ A::M(x) {\n"
177 EXPECT_EQ("#define M(x) x##x\n"
178 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ "
179 "M(x) /* comment */ {\n"
182 "} // namespace M(x)",
183 fixNamespaceEndComments(
184 "#define M(x) x##x\n"
185 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment "
186 "*/ M(x) /* comment */ {\n"
190 EXPECT_EQ("#define M(x) x##x\n"
191 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment */ "
192 "A::M(x) /* comment */ {\n"
195 "} // namespace A::M(x)",
196 fixNamespaceEndComments(
197 "#define M(x) x##x\n"
198 "namespace /* comment */ [[deprecated(\"foo\")]] /* comment "
199 "*/ A::M(x) /* comment */ {\n"
203 EXPECT_EQ("#define M(x) x##x\n"
204 "namespace A M(x) {\n"
207 "} // namespace A M(x)",
208 fixNamespaceEndComments("#define M(x) x##x\n"
209 "namespace A M(x) {\n"
214 "#define B __attribute__((availability(macos, introduced=10.15)))\n"
218 "} // namespace A B",
219 fixNamespaceEndComments(
220 "#define B __attribute__((availability(macos, introduced=10.15)))\n"
225 EXPECT_EQ("#define M(x) x##x\n"
226 "namespace A::B M(x) {\n"
229 "} // namespace A::B",
230 fixNamespaceEndComments("#define M(x) x##x\n"
231 "namespace A::B M(x) {\n"
236 "namespace A __attribute__((availability(macos, introduced=10.15))) {\n"
240 fixNamespaceEndComments(
241 "namespace A __attribute__((availability(macos, introduced=10.15))) "
246 EXPECT_EQ("inline namespace A {\n"
250 fixNamespaceEndComments("inline namespace A {\n"
255 EXPECT_EQ("namespace [[deprecated(\"foo\")]] A::B {\n"
258 "} // namespace A::B",
259 fixNamespaceEndComments("namespace [[deprecated(\"foo\")]] A::B {\n"
264 EXPECT_EQ("namespace [[deprecated(\"foo\")]] A::inline B::inline C {\n"
267 "} // namespace A::inline B::inline C",
268 fixNamespaceEndComments(
269 "namespace [[deprecated(\"foo\")]] A::inline B::inline C {\n"
274 EXPECT_EQ("namespace DEPRECATED A::B {\n"
277 "} // namespace A::B",
278 fixNamespaceEndComments("namespace DEPRECATED A::B {\n"
283 EXPECT_EQ("inline namespace [[deprecated]] A {\n"
287 fixNamespaceEndComments("inline namespace [[deprecated]] A {\n"
292 EXPECT_EQ("namespace ::A {\n"
295 "} // namespace ::A",
296 fixNamespaceEndComments("namespace ::A {\n"
300 EXPECT_EQ("namespace ::A::B {\n"
303 "} // namespace ::A::B",
304 fixNamespaceEndComments("namespace ::A::B {\n"
308 EXPECT_EQ("namespace /**/::/**/A/**/::/**/B/**/ {\n"
311 "} // namespace ::A::B",
312 fixNamespaceEndComments("namespace /**/::/**/A/**/::/**/B/**/ {\n"
316 EXPECT_EQ("namespace A {\n"
321 fixNamespaceEndComments("namespace A {\n"
326 EXPECT_EQ("namespace A {\n"
332 fixNamespaceEndComments("namespace A {\n"
338 EXPECT_EQ("namespace A {\n"
346 fixNamespaceEndComments("namespace A {\n"
354 EXPECT_EQ("namespace A {\n"
362 fixNamespaceEndComments("namespace A {\n"
370 EXPECT_EQ("namespace A {\n"
374 "// comment about b\n"
376 fixNamespaceEndComments("namespace A {\n"
380 "// comment about b\n"
383 EXPECT_EQ("namespace A {\n"
391 fixNamespaceEndComments("namespace A {\n"
400 // Add comment for namespaces which will be 'compacted'
401 FormatStyle CompactNamespacesStyle
= getLLVMStyle();
402 CompactNamespacesStyle
.CompactNamespaces
= true;
403 EXPECT_EQ("namespace out { namespace in {\n"
406 "}} // namespace out::in",
407 fixNamespaceEndComments("namespace out { namespace in {\n"
411 CompactNamespacesStyle
));
412 EXPECT_EQ("namespace out {\n"
417 "} // namespace out::in",
418 fixNamespaceEndComments("namespace out {\n"
424 CompactNamespacesStyle
));
425 EXPECT_EQ("namespace out { namespace in {\n"
428 "};} // namespace out::in",
429 fixNamespaceEndComments("namespace out { namespace in {\n"
433 CompactNamespacesStyle
));
434 EXPECT_EQ("namespace out { namespace {\n"
437 "}} // namespace out",
438 fixNamespaceEndComments("namespace out { namespace {\n"
442 CompactNamespacesStyle
));
444 // Adds an end comment after a semicolon.
445 EXPECT_EQ("namespace {\n"
449 fixNamespaceEndComments("namespace {\n"
453 EXPECT_EQ("namespace A {\n"
457 fixNamespaceEndComments("namespace A {\n"
461 EXPECT_EQ("namespace A {\n"
464 "}; // namespace A\n"
466 fixNamespaceEndComments("namespace A {\n"
473 TEST_F(NamespaceEndCommentsFixerTest
, WorksForObjCpp
) {
474 FormatStyle ObjCppStyle
= getLLVMStyle();
475 ObjCppStyle
.Language
= FormatStyle::LK_ObjC
;
476 EXPECT_EQ("namespace {\n"
480 fixNamespaceEndComments("namespace {\n"
487 TEST_F(NamespaceEndCommentsFixerTest
, AddsMacroEndComment
) {
488 FormatStyle Style
= getLLVMStyle();
489 Style
.NamespaceMacros
.push_back("TESTSUITE");
491 EXPECT_EQ("TESTSUITE() {\n"
495 fixNamespaceEndComments("TESTSUITE() {\n"
501 EXPECT_EQ("TESTSUITE(A) {\n"
505 fixNamespaceEndComments("TESTSUITE(A) {\n"
510 EXPECT_EQ("inline TESTSUITE(A) {\n"
514 fixNamespaceEndComments("inline TESTSUITE(A) {\n"
519 EXPECT_EQ("TESTSUITE(::A) {\n"
522 "} // TESTSUITE(::A)",
523 fixNamespaceEndComments("TESTSUITE(::A) {\n"
528 EXPECT_EQ("TESTSUITE(::A::B) {\n"
531 "} // TESTSUITE(::A::B)",
532 fixNamespaceEndComments("TESTSUITE(::A::B) {\n"
537 EXPECT_EQ("TESTSUITE(/**/::/**/A/**/::/**/B/**/) {\n"
540 "} // TESTSUITE(::A::B)",
541 fixNamespaceEndComments("TESTSUITE(/**/::/**/A/**/::/**/B/**/) {\n"
546 EXPECT_EQ("TESTSUITE(A, B) {\n"
550 fixNamespaceEndComments("TESTSUITE(A, B) {\n"
555 EXPECT_EQ("TESTSUITE(\"Test1\") {\n"
558 "} // TESTSUITE(\"Test1\")",
559 fixNamespaceEndComments("TESTSUITE(\"Test1\") {\n"
566 TEST_F(NamespaceEndCommentsFixerTest
, AddsNewlineIfNeeded
) {
567 EXPECT_EQ("namespace A {\n"
572 fixNamespaceEndComments("namespace A {\n"
576 EXPECT_EQ("namespace {\n"
581 fixNamespaceEndComments("namespace {\n"
585 EXPECT_EQ("namespace A {\n"
593 fixNamespaceEndComments("namespace A {\n"
600 EXPECT_EQ("namespace {\n"
605 fixNamespaceEndComments("namespace {\n"
609 EXPECT_EQ("namespace {\n"
614 fixNamespaceEndComments("namespace {\n"
620 TEST_F(NamespaceEndCommentsFixerTest
, DoesNotAddEndCommentForShortNamespace
) {
621 EXPECT_EQ("namespace {}", fixNamespaceEndComments("namespace {}"));
622 EXPECT_EQ("namespace A {}", fixNamespaceEndComments("namespace A {}"));
623 EXPECT_EQ("namespace A { a }", fixNamespaceEndComments("namespace A { a }"));
624 EXPECT_EQ("namespace A { a };",
625 fixNamespaceEndComments("namespace A { a };"));
628 TEST_F(NamespaceEndCommentsFixerTest
, DoesNotAddCommentAfterUnaffectedRBrace
) {
629 EXPECT_EQ("namespace A {\n"
632 fixNamespaceEndComments("namespace A {\n"
635 // The range (16, 3) spans the 'int' above.
636 /*Ranges=*/{1, tooling::Range(16, 3)}));
637 EXPECT_EQ("namespace A {\n"
640 fixNamespaceEndComments("namespace A {\n"
643 // The range (16, 3) spans the 'int' above.
644 /*Ranges=*/{1, tooling::Range(16, 3)}));
647 TEST_F(NamespaceEndCommentsFixerTest
,
648 DoesNotAddCommentAfterRBraceInPPDirective
) {
649 EXPECT_EQ("#define SAD \\\n"
653 fixNamespaceEndComments("#define SAD \\\n"
659 TEST_F(NamespaceEndCommentsFixerTest
, KeepsValidEndComment
) {
660 EXPECT_TRUE(isFormatted("namespace {\n"
662 "} // end anonymous namespace"));
663 EXPECT_TRUE(isFormatted("namespace A {\n"
665 "} /* end of namespace A */"));
666 EXPECT_TRUE(isFormatted("namespace A {\n"
668 "} // namespace A"));
669 EXPECT_TRUE(isFormatted("namespace A::B {\n"
671 "} // end namespace A::B"));
672 EXPECT_TRUE(isFormatted("namespace A {\n"
674 "}; // end namespace A"));
675 EXPECT_TRUE(isFormatted("namespace {\n"
677 "}; /* unnamed namespace */"));
678 EXPECT_TRUE(isFormatted("namespace a::inline b {\n"
680 "}; // namespace a::inline b"));
683 TEST_F(NamespaceEndCommentsFixerTest
, KeepsValidMacroEndComment
) {
684 FormatStyle Style
= getLLVMStyle();
685 Style
.NamespaceMacros
.push_back("TESTSUITE");
687 EXPECT_TRUE(isFormatted("TESTSUITE() {\n"
689 "} // end anonymous TESTSUITE()",
691 EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
693 "} /* end of TESTSUITE(A) */",
695 EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
699 EXPECT_TRUE(isFormatted("TESTSUITE(A::B) {\n"
701 "} // end TESTSUITE(A::B)",
703 EXPECT_TRUE(isFormatted("TESTSUITE(A) {\n"
705 "}; // end TESTSUITE(A)",
707 EXPECT_TRUE(isFormatted("TESTSUITE() {\n"
709 "}; /* unnamed TESTSUITE() */",
711 EXPECT_TRUE(isFormatted("TESTSUITE(\"foo\") {\n"
713 "} // TESTSUITE(\"foo\")",
717 TEST_F(NamespaceEndCommentsFixerTest
, UpdatesInvalidEndLineComment
) {
718 EXPECT_EQ("namespace {\n"
721 fixNamespaceEndComments("namespace {\n"
723 "} // namespace A"));
724 EXPECT_EQ("namespace A {\n"
727 fixNamespaceEndComments("namespace A {\n"
730 EXPECT_EQ("namespace A {\n"
733 fixNamespaceEndComments("namespace A {\n"
736 EXPECT_EQ("namespace A {\n"
739 fixNamespaceEndComments("namespace A {\n"
743 EXPECT_EQ("namespace A {\n"
746 fixNamespaceEndComments("namespace A {\n"
748 "} // banamespace A"));
749 EXPECT_EQ("namespace A {\n"
752 fixNamespaceEndComments("namespace A {\n"
754 "}; // banamespace A"));
755 // Updates invalid line comments even for short namespaces.
756 EXPECT_EQ("namespace A {} // namespace A",
757 fixNamespaceEndComments("namespace A {} // namespace"));
758 EXPECT_EQ("namespace A {}; // namespace A",
759 fixNamespaceEndComments("namespace A {}; // namespace"));
761 // Update invalid comments for compacted namespaces.
762 FormatStyle CompactNamespacesStyle
= getLLVMStyle();
763 CompactNamespacesStyle
.CompactNamespaces
= true;
764 EXPECT_EQ("namespace out { namespace in {\n"
765 "}} // namespace out::in",
766 fixNamespaceEndComments("namespace out { namespace in {\n"
767 "}} // namespace out",
768 CompactNamespacesStyle
));
769 EXPECT_EQ("namespace out { namespace in {\n"
770 "}} // namespace out::in",
771 fixNamespaceEndComments("namespace out { namespace in {\n"
772 "}} // namespace in",
773 CompactNamespacesStyle
));
774 EXPECT_EQ("namespace out { namespace in {\n"
776 "} // namespace out::in",
777 fixNamespaceEndComments("namespace out { namespace in {\n"
778 "}// banamespace in\n"
779 "} // namespace out",
780 CompactNamespacesStyle
));
781 EXPECT_EQ("namespace out { namespace {\n"
782 "}} // namespace out",
783 fixNamespaceEndComments("namespace out { namespace {\n"
784 "}} // namespace out::",
785 CompactNamespacesStyle
));
788 TEST_F(NamespaceEndCommentsFixerTest
, UpdatesInvalidMacroEndLineComment
) {
789 FormatStyle Style
= getLLVMStyle();
790 Style
.NamespaceMacros
.push_back("TESTSUITE");
792 EXPECT_EQ("TESTSUITE() {\n"
795 fixNamespaceEndComments("TESTSUITE() {\n"
799 EXPECT_EQ("TESTSUITE(A) {\n"
802 fixNamespaceEndComments("TESTSUITE(A) {\n"
806 EXPECT_EQ("TESTSUITE(A) {\n"
809 fixNamespaceEndComments("TESTSUITE(A) {\n"
813 EXPECT_EQ("TESTSUITE(A) {\n"
815 "}; // TESTSUITE(A)",
816 fixNamespaceEndComments("TESTSUITE(A) {\n"
820 EXPECT_EQ("TESTSUITE(A) {\n"
823 fixNamespaceEndComments("TESTSUITE(A) {\n"
827 EXPECT_EQ("TESTSUITE() {\n"
830 fixNamespaceEndComments("TESTSUITE() {\n"
834 EXPECT_EQ("TESTSUITE(A) {\n"
837 fixNamespaceEndComments("TESTSUITE(A) {\n"
839 "} // TOASTSUITE(A)",
841 EXPECT_EQ("TESTSUITE(A) {\n"
843 "}; // TESTSUITE(A)",
844 fixNamespaceEndComments("TESTSUITE(A) {\n"
846 "}; // TOASTSUITE(A)",
848 // Updates invalid line comments even for short namespaces.
849 EXPECT_EQ("TESTSUITE(A) {} // TESTSUITE(A)",
850 fixNamespaceEndComments("TESTSUITE(A) {} // TESTSUITE()", Style
));
851 EXPECT_EQ("TESTSUITE(A) {}; // TESTSUITE(A)",
852 fixNamespaceEndComments("TESTSUITE(A) {}; // TESTSUITE()", Style
));
854 // Update invalid comments for compacted namespaces.
855 FormatStyle CompactNamespacesStyle
= getLLVMStyle();
856 CompactNamespacesStyle
.CompactNamespaces
= true;
857 CompactNamespacesStyle
.NamespaceMacros
.push_back("TESTSUITE");
859 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
860 "}} // TESTSUITE(out::in)",
861 fixNamespaceEndComments("TESTSUITE(out) { TESTSUITE(in) {\n"
862 "}} // TESTSUITE(out)",
863 CompactNamespacesStyle
));
864 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
865 "}} // TESTSUITE(out::in)",
866 fixNamespaceEndComments("TESTSUITE(out) { TESTSUITE(in) {\n"
867 "}} // TESTSUITE(in)",
868 CompactNamespacesStyle
));
869 EXPECT_EQ("TESTSUITE(out) { TESTSUITE(in) {\n"
871 "} // TESTSUITE(out::in)",
872 fixNamespaceEndComments("TESTSUITE(out) { TESTSUITE(in) {\n"
873 "}// TAOSTSUITE(in)\n"
874 "} // TESTSUITE(out)",
875 CompactNamespacesStyle
));
878 TEST_F(NamespaceEndCommentsFixerTest
, UpdatesInvalidEndBlockComment
) {
879 EXPECT_EQ("namespace {\n"
882 fixNamespaceEndComments("namespace {\n"
884 "} /* namespace A */"));
885 EXPECT_EQ("namespace A {\n"
888 fixNamespaceEndComments("namespace A {\n"
890 "} /* end namespace */"));
891 EXPECT_EQ("namespace A {\n"
894 fixNamespaceEndComments("namespace A {\n"
897 EXPECT_EQ("namespace A {\n"
900 fixNamespaceEndComments("namespace A {\n"
902 "} /* end unnamed namespace */"));
903 EXPECT_EQ("namespace A {\n"
906 fixNamespaceEndComments("namespace A {\n"
908 "} /* banamespace A */"));
909 EXPECT_EQ("namespace A {\n"
912 fixNamespaceEndComments("namespace A {\n"
914 "}; /* banamespace A */"));
915 EXPECT_EQ("namespace A {} // namespace A",
916 fixNamespaceEndComments("namespace A {} /**/"));
917 EXPECT_EQ("namespace A {}; // namespace A",
918 fixNamespaceEndComments("namespace A {}; /**/"));
921 TEST_F(NamespaceEndCommentsFixerTest
, UpdatesInvalidMacroEndBlockComment
) {
922 FormatStyle Style
= getLLVMStyle();
923 Style
.NamespaceMacros
.push_back("TESTSUITE");
925 EXPECT_EQ("TESTSUITE() {\n"
928 fixNamespaceEndComments("TESTSUITE() {\n"
930 "} /* TESTSUITE(A) */",
932 EXPECT_EQ("TESTSUITE(A) {\n"
935 fixNamespaceEndComments("TESTSUITE(A) {\n"
937 "} /* end TESTSUITE() */",
939 EXPECT_EQ("TESTSUITE(A) {\n"
942 fixNamespaceEndComments("TESTSUITE(A) {\n"
946 EXPECT_EQ("TESTSUITE(A) {\n"
949 fixNamespaceEndComments("TESTSUITE(A) {\n"
951 "} /* end unnamed TESTSUITE() */",
953 EXPECT_EQ("TESTSUITE(A) {\n"
956 fixNamespaceEndComments("TESTSUITE(A) {\n"
958 "} /* TOASTSUITE(A) */",
960 EXPECT_EQ("TESTSUITE(A) {\n"
962 "}; // TESTSUITE(A)",
963 fixNamespaceEndComments("TESTSUITE(A) {\n"
965 "}; /* TAOSTSUITE(A) */",
967 EXPECT_EQ("TESTSUITE(A) {} // TESTSUITE(A)",
968 fixNamespaceEndComments("TESTSUITE(A) {} /**/", Style
));
969 EXPECT_EQ("TESTSUITE(A) {}; // TESTSUITE(A)",
970 fixNamespaceEndComments("TESTSUITE(A) {}; /**/", Style
));
973 TEST_F(NamespaceEndCommentsFixerTest
,
974 DoesNotAddEndCommentForNamespacesControlledByMacros
) {
975 EXPECT_EQ("#ifdef 1\n"
983 fixNamespaceEndComments("#ifdef 1\n"
993 TEST_F(NamespaceEndCommentsFixerTest
, AddsEndCommentForNamespacesAroundMacros
) {
994 // Conditional blocks around are fine
995 EXPECT_EQ("namespace A {\n"
1000 fixNamespaceEndComments("namespace A {\n"
1011 fixNamespaceEndComments("#if 1\n"
1017 EXPECT_EQ("namespace A {\n"
1020 "} // namespace A\n"
1023 fixNamespaceEndComments("namespace A {\n"
1033 "} // namespace A\n"
1035 fixNamespaceEndComments("#if 1\n"
1042 // Macro definition has no impact
1043 EXPECT_EQ("namespace A {\n"
1047 fixNamespaceEndComments("namespace A {\n"
1051 EXPECT_EQ("#define FOO\n"
1056 fixNamespaceEndComments("#define FOO\n"
1061 EXPECT_EQ("namespace A {\n"
1064 "} // namespace A\n"
1066 fixNamespaceEndComments("namespace A {\n"
1072 // No replacement if open & close in different conditional blocks
1081 fixNamespaceEndComments("#if 1\n"
1089 EXPECT_EQ("#ifdef A\n"
1097 fixNamespaceEndComments("#ifdef A\n"
1106 // No replacement inside unreachable conditional block
1113 fixNamespaceEndComments("#if 0\n"
1121 TEST_F(NamespaceEndCommentsFixerTest
,
1122 DoesNotAddEndCommentForNamespacesInMacroDeclarations
) {
1123 EXPECT_EQ("#ifdef 1\n"
1131 fixNamespaceEndComments("#ifdef 1\n"
1139 EXPECT_EQ("namespace {\n"
1148 fixNamespaceEndComments("namespace {\n"
1165 fixNamespaceEndComments("#if A\n"
1181 fixNamespaceEndComments("#if A\n"
1188 "} // namespace A"));
1197 fixNamespaceEndComments("#if A\n"
1204 "} // namespace B"));
1205 EXPECT_EQ("namespace A\n"
1213 fixNamespaceEndComments("namespace A\n"
1221 EXPECT_EQ("namespace A\n"
1225 "} // namespace A\n"
1227 "} // namespace A\n"
1229 fixNamespaceEndComments("namespace A\n"
1233 "} // namespace A\n"
1235 "} // namespace A\n"
1239 TEST_F(NamespaceEndCommentsFixerTest
,
1240 DoesNotAddEndCommentForUnbalancedRBracesAfterNamespaceEnd
) {
1241 EXPECT_EQ("namespace {\n"
1245 fixNamespaceEndComments("namespace {\n"
1251 TEST_F(NamespaceEndCommentsFixerTest
, HandlesInlineAtEndOfLine_PR32438
) {
1252 EXPECT_EQ("template <int> struct a {};\n"
1253 "struct a<bool{}> b() {\n"
1255 "#define c inline\n"
1258 fixNamespaceEndComments("template <int> struct a {};\n"
1259 "struct a<bool{}> b() {\n"
1261 "#define c inline\n"
1266 TEST_F(NamespaceEndCommentsFixerTest
, IgnoreUnbalanced
) {
1267 EXPECT_EQ("namespace A {\n"
1271 fixNamespaceEndComments("namespace A {\n"
1275 EXPECT_EQ("namespace A {\n"
1278 fixNamespaceEndComments("namespace A {\n"
1282 EXPECT_EQ("namespace A {\n"
1287 fixNamespaceEndComments("namespace A {\n"
1294 using ShortNamespaceLinesTest
= NamespaceEndCommentsFixerTest
;
1296 TEST_F(ShortNamespaceLinesTest
, ZeroUnwrappedLines
) {
1297 auto Style
= getLLVMStyle();
1298 Style
.ShortNamespaceLines
= 0u;
1300 EXPECT_EQ("namespace OneLinerNamespace {}",
1301 fixNamespaceEndComments("namespace OneLinerNamespace {}", Style
));
1302 EXPECT_EQ("namespace ShortNamespace {\n"
1304 fixNamespaceEndComments("namespace ShortNamespace {\n"
1307 EXPECT_EQ("namespace LongNamespace {\n"
1309 "} // namespace LongNamespace",
1310 fixNamespaceEndComments("namespace LongNamespace {\n"
1316 TEST_F(ShortNamespaceLinesTest
, OneUnwrappedLine
) {
1317 constexpr auto DefaultUnwrappedLines
= 1u;
1318 auto const Style
= getLLVMStyle();
1320 EXPECT_EQ(DefaultUnwrappedLines
, Style
.ShortNamespaceLines
);
1321 EXPECT_EQ("namespace ShortNamespace {\n"
1324 fixNamespaceEndComments("namespace ShortNamespace {\n"
1327 EXPECT_EQ("namespace LongNamespace {\n"
1330 "} // namespace LongNamespace",
1331 fixNamespaceEndComments("namespace LongNamespace {\n"
1337 TEST_F(ShortNamespaceLinesTest
, MultipleUnwrappedLine
) {
1338 auto Style
= getLLVMStyle();
1339 Style
.ShortNamespaceLines
= 2u;
1341 EXPECT_EQ("namespace ShortNamespace {\n"
1345 fixNamespaceEndComments("namespace ShortNamespace {\n"
1350 EXPECT_EQ("namespace LongNamespace {\n"
1354 "} // namespace LongNamespace",
1355 fixNamespaceEndComments("namespace LongNamespace {\n"
1362 // The namespace body has 5 unwrapped/annotated lines.
1363 const std::string NestedLambdas
{"namespace foo {\n"
1364 "auto bar = [] {\n" // line 1
1365 " int i;\n" // line 2
1366 " return [] {\n" // line 3
1368 " return 0;\n" // line 5
1369 " };\n" // part of line 3
1370 "};\n" // part of line 1
1372 Style
.ShortNamespaceLines
= 4;
1373 EXPECT_EQ(NestedLambdas
+ " // namespace foo",
1374 fixNamespaceEndComments(NestedLambdas
, Style
));
1375 ++Style
.ShortNamespaceLines
;
1376 EXPECT_EQ(NestedLambdas
, fixNamespaceEndComments(NestedLambdas
, Style
));
1379 TEST_F(ShortNamespaceLinesTest
, NamespaceAlias
) {
1380 auto Style
= getLLVMStyle();
1382 EXPECT_EQ("namespace n = nn;\n"
1387 fixNamespaceEndComments("namespace n = nn;\n"
1394 EXPECT_EQ("namespace n = nn; // comment\n"
1399 fixNamespaceEndComments("namespace n = nn; // comment\n"
1406 EXPECT_EQ("namespace n = nn; /* comment */\n"
1411 fixNamespaceEndComments("namespace n = nn; /* comment */\n"
1419 "namespace n = nn; /* comment */ /* comment2 */\n"
1424 fixNamespaceEndComments("namespace n = nn; /* comment */ /* comment2 */\n"
1431 EXPECT_EQ("namespace n = nn; {\n"
1435 fixNamespaceEndComments("namespace n = nn; {\n"
1440 EXPECT_EQ("int foo;\n"
1446 fixNamespaceEndComments("int foo;\n"
1455 } // end namespace format
1456 } // end namespace clang