[clang] [clang-format] Separate License text and include blocks (PR #77918)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 15 06:11:51 PST 2024


================
@@ -610,6 +626,113 @@ TEST_F(DefinitionBlockSeparatorTest, JavaScript) {
                "}",
                Style);
 }
+
+TEST_P(LicenseTest, SeparateLicenseFromBlock) {
+  constexpr StringRef LicenseSingleLineCommentStyle = {"// start license\n"
+                                                       "// license text\n"
+                                                       "// more license text\n"
+                                                       "// end license\n"};
+  constexpr StringRef LicenseMultipleLineCommentStyle{"/*\n"
+                                                      "start license\n"
+                                                      "license text\n"
+                                                      "more license text\n"
+                                                      "end license */\n"};
+
+  const auto Block = GetParam();
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_One;
+  Style.MaxEmptyLinesToKeep = 2;
+  verifyFormat(LicenseSingleLineCommentStyle.str() + "\n" + Block, Style);
+  verifyFormat(LicenseMultipleLineCommentStyle.str() + "\n" + Block, Style);
+
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Two;
+  verifyFormat(LicenseSingleLineCommentStyle.str() + "\n\n" + Block, Style);
+  verifyFormat(LicenseMultipleLineCommentStyle.str() + "\n\n" + Block, Style);
+}
+
+INSTANTIATE_TEST_SUITE_P(
+    DefinitionSeparator, LicenseTest,
+    ::testing::Values(std::string{"class Test {};"},
+                      std::string{"class Test {\n"
+                                  "public:\n"
+                                  "  void test() const {}\n"
+                                  "};\n"},
+                      std::string{"namespace tests {};"},
+                      std::string{"static int variable = 10;"},
+                      std::string{"#ifnef __TEST__\n"
+                                  "#define __TEST__\n"
+                                  "#endif"}));
+
+TEST_P(IncludesTest, SeparateIncludeFromBlock) {
+  constexpr StringRef Includes = {"#include <string>\n"
+                                  "#include <cstdio>\n"};
+
+  const auto Block = GetParam();
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_One;
+  Style.MaxEmptyLinesToKeep = 2;
+  verifyFormat(Includes.str() + "\n" + Block, Style);
+
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Two;
+  verifyFormat(Includes.str() + "\n\n" + Block, Style);
+}
+
+INSTANTIATE_TEST_SUITE_P(
+    DefinitionSeparator, IncludesTest,
+    ::testing::Values(std::string{"class Test {};"},
+                      std::string{"class Test {\n"
+                                  "public:\n"
+                                  "  void test() const {}\n"
+                                  "};\n"},
+                      std::string{"namespace tests {};"},
+                      std::string{"static int variable = 10;"},
+                      std::string{"#ifnef __TEST__\n"
+                                  "#define __TEST__\n"
+                                  "#endif"}));
+
+TEST_P(NoNewLineAtEofTest, NoNewLineAfterBlock) {
+  FormatStyle Style = getLLVMStyle();
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_One;
+  Style.MaxEmptyLinesToKeep = 2;
+  const auto Code = GetParam();
+  verifyFormat(Code, Style, Code,
+               /* Inverse = */ false);
+
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Two;
+  verifyFormat(Code, Style, Code,
+               /* Inverse = */ false);
+}
+
+INSTANTIATE_TEST_SUITE_P(DefinitionSeparator, NoNewLineAtEofTest,
+                         ::testing::Values(std::string{"// start license\n"
+                                                       "// license text\n"
+                                                       "// more license text\n"
+                                                       "// end license\n"},
+                                           std::string{"// start license"},
+                                           std::string{"#include <string>"},
+                                           std::string{"#include <string>\n"
+                                                       "#include <cstdio>"}));
+
+TEST_F(DefinitionBlockSeparatorTest,
+       NoNewLinesWhenThereIsNoCodeAfterLicenseText) {
+  FormatStyle Style = getLLVMStyle();
+  constexpr StringRef Code = {"// start license\n"
+                              "// license text\n"
+                              "// end license"};
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_One;
+  verifyFormat(Code, Style,
+               "// start license\n"
+               "// license text\n"
+               "// end license",
+               /* Inverse = */ false);
+
+  Style.SeparateDefinitionBlocks = FormatStyle::SDS_Two;
+  verifyFormat(Code, Style,
+               "// start license\n"
+               "// license text\n"
+               "// end license",
+               /* Inverse = */ false);
+}
----------------
mydeveloperday wrote:

These tests are overly complex for what in my view you are trying to do... sorry I think its hard to read and doesn't matrch the style of the other tests in clang-format.

https://github.com/llvm/llvm-project/pull/77918


More information about the cfe-commits mailing list