https://github.com/seranu created https://github.com/llvm/llvm-project/pull/78108
Extract utility functions outside of test fixture for DefinitionBlockSeparatorTest so that these functions can be used by other test fixtures. There are no functional changes. This is a refactoring in preparation of the fix for #42112 >From 952c782d7e7067fb1e33f24525fe1bcae67edf00 Mon Sep 17 00:00:00 2001 From: Serban Ungureanu <serban.ungure...@randstaddigital.com> Date: Sun, 14 Jan 2024 17:40:02 +0200 Subject: [PATCH] [clang-format] Extract utility functions outside of test fixture so that they can be used by other test fixtures in this file --- .../Format/DefinitionBlockSeparatorTest.cpp | 119 +++++++++--------- 1 file changed, 59 insertions(+), 60 deletions(-) diff --git a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp index f5489498a93b9e..3939f856638545 100644 --- a/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp +++ b/clang/unittests/Format/DefinitionBlockSeparatorTest.cpp @@ -17,73 +17,72 @@ namespace clang { namespace format { namespace { +std::string +separateDefinitionBlocks(llvm::StringRef Code, + const std::vector<tooling::Range> &Ranges, + const FormatStyle &Style = getLLVMStyle()) { + LLVM_DEBUG(llvm::errs() << "---\n"); + LLVM_DEBUG(llvm::errs() << Code << "\n\n"); + tooling::Replacements Replaces = reformat(Style, Code, Ranges, "<stdin>"); + auto Result = applyAllReplacements(Code, Replaces); + EXPECT_TRUE(static_cast<bool>(Result)); + LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); + return *Result; +} -class DefinitionBlockSeparatorTest : public ::testing::Test { -protected: - static std::string - separateDefinitionBlocks(llvm::StringRef Code, - const std::vector<tooling::Range> &Ranges, - const FormatStyle &Style = getLLVMStyle()) { - LLVM_DEBUG(llvm::errs() << "---\n"); - LLVM_DEBUG(llvm::errs() << Code << "\n\n"); - tooling::Replacements Replaces = reformat(Style, Code, Ranges, "<stdin>"); - auto Result = applyAllReplacements(Code, Replaces); - EXPECT_TRUE(static_cast<bool>(Result)); - LLVM_DEBUG(llvm::errs() << "\n" << *Result << "\n\n"); - return *Result; - } - - static std::string - separateDefinitionBlocks(llvm::StringRef Code, - const FormatStyle &Style = getLLVMStyle()) { - return separateDefinitionBlocks( - Code, - /*Ranges=*/{1, tooling::Range(0, Code.size())}, Style); +std::string removeEmptyLines(llvm::StringRef Code) { + std::string Result = ""; + for (auto Char : Code.str()) { + if (Result.size()) { + auto LastChar = Result.back(); + if ((Char == '\n' && LastChar == '\n') || + (Char == '\r' && (LastChar == '\r' || LastChar == '\n'))) { + continue; + } + } + Result.push_back(Char); } + return Result; +} - static void _verifyFormat(const char *File, int Line, llvm::StringRef Code, - const FormatStyle &Style = getLLVMStyle(), - llvm::StringRef ExpectedCode = "", - bool Inverse = true) { - ::testing::ScopedTrace t(File, Line, ::testing::Message() << Code.str()); - bool HasOriginalCode = true; - if (ExpectedCode == "") { - ExpectedCode = Code; - HasOriginalCode = false; - } +std::string +separateDefinitionBlocks(llvm::StringRef Code, + const FormatStyle &Style = getLLVMStyle()) { + return separateDefinitionBlocks( + Code, + /*Ranges=*/{1, tooling::Range(0, Code.size())}, Style); +} - EXPECT_EQ(ExpectedCode, separateDefinitionBlocks(ExpectedCode, Style)) - << "Expected code is not stable"; - if (Inverse) { - FormatStyle InverseStyle = Style; - if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always) - InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never; - else - InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always; - EXPECT_NE(ExpectedCode, - separateDefinitionBlocks(ExpectedCode, InverseStyle)) - << "Inverse formatting makes no difference"; - } - std::string CodeToFormat = - HasOriginalCode ? Code.str() : removeEmptyLines(Code); - std::string Result = separateDefinitionBlocks(CodeToFormat, Style); - EXPECT_EQ(ExpectedCode, Result) << "Test failed. Formatted:\n" << Result; +void _verifyFormat(const char *File, int Line, llvm::StringRef Code, + const FormatStyle &Style = getLLVMStyle(), + llvm::StringRef ExpectedCode = "", bool Inverse = true) { + ::testing::ScopedTrace t(File, Line, ::testing::Message() << Code.str()); + bool HasOriginalCode = true; + if (ExpectedCode == "") { + ExpectedCode = Code; + HasOriginalCode = false; } - static std::string removeEmptyLines(llvm::StringRef Code) { - std::string Result = ""; - for (auto Char : Code.str()) { - if (Result.size()) { - auto LastChar = Result.back(); - if ((Char == '\n' && LastChar == '\n') || - (Char == '\r' && (LastChar == '\r' || LastChar == '\n'))) { - continue; - } - } - Result.push_back(Char); - } - return Result; + EXPECT_EQ(ExpectedCode, separateDefinitionBlocks(ExpectedCode, Style)) + << "Expected code is not stable"; + if (Inverse) { + FormatStyle InverseStyle = Style; + if (Style.SeparateDefinitionBlocks == FormatStyle::SDS_Always) + InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Never; + else + InverseStyle.SeparateDefinitionBlocks = FormatStyle::SDS_Always; + EXPECT_NE(ExpectedCode, + separateDefinitionBlocks(ExpectedCode, InverseStyle)) + << "Inverse formatting makes no difference"; } + std::string CodeToFormat = + HasOriginalCode ? Code.str() : removeEmptyLines(Code); + std::string Result = separateDefinitionBlocks(CodeToFormat, Style); + EXPECT_EQ(ExpectedCode, Result) << "Test failed. Formatted:\n" << Result; +} + +class DefinitionBlockSeparatorTest : public ::testing::Test { +protected: }; #define verifyFormat(...) _verifyFormat(__FILE__, __LINE__, __VA_ARGS__) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits