ReplacementTest.h 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. //===- unittest/Tooling/ReplacementTest.h - Replacements related test------===//
  2. //
  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
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file defines utility class and function for Replacement related tests.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_CLANG_UNITTESTS_TOOLING_REPLACEMENTTESTBASE_H
  13. #define LLVM_CLANG_UNITTESTS_TOOLING_REPLACEMENTTESTBASE_H
  14. #include "RewriterTestContext.h"
  15. #include "clang/Tooling/Core/Replacement.h"
  16. #include "gtest/gtest.h"
  17. namespace clang {
  18. namespace tooling {
  19. /// \brief Converts a set of replacements to Replacements class.
  20. /// \return A Replacements class containing \p Replaces on success; otherwise,
  21. /// an empty Replacements is returned.
  22. inline tooling::Replacements
  23. toReplacements(const std::set<tooling::Replacement> &Replaces) {
  24. tooling::Replacements Result;
  25. for (const auto &R : Replaces) {
  26. auto Err = Result.add(R);
  27. EXPECT_TRUE(!Err);
  28. if (Err) {
  29. llvm::errs() << llvm::toString(std::move(Err)) << "\n";
  30. return tooling::Replacements();
  31. }
  32. }
  33. return Result;
  34. }
  35. /// \brief A utility class for replacement related tests.
  36. class ReplacementTest : public ::testing::Test {
  37. protected:
  38. tooling::Replacement createReplacement(SourceLocation Start, unsigned Length,
  39. llvm::StringRef ReplacementText) {
  40. return tooling::Replacement(Context.Sources, Start, Length,
  41. ReplacementText);
  42. }
  43. RewriterTestContext Context;
  44. };
  45. } // namespace tooling
  46. } // namespace clang
  47. #endif // LLVM_CLANG_UNITTESTS_TOOLING_REPLACEMENTTESTBASE_H