matcher_test_base.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "migrate_cpp/cpp_refactoring/matcher_test_base.h"
  5. #include "clang/Tooling/Tooling.h"
  6. #include "gmock/gmock.h"
  7. #include "gtest/gtest.h"
  8. namespace ct = ::clang::tooling;
  9. namespace Carbon {
  10. void MatcherTestBase::ExpectReplacement(llvm::StringRef before,
  11. llvm::StringRef after) {
  12. auto factory = ct::newFrontendActionFactory(&finder);
  13. constexpr char Filename[] = "test.cc";
  14. replacements.clear();
  15. replacements.insert({Filename, {}});
  16. ASSERT_TRUE(ct::runToolOnCodeWithArgs(
  17. factory->create(), before, {}, Filename, "clang-tool",
  18. std::make_shared<clang::PCHContainerOperations>(),
  19. ct::FileContentMappings()));
  20. EXPECT_THAT(replacements, testing::ElementsAre(testing::Key(Filename)));
  21. auto actual = ct::applyAllReplacements(before, replacements[Filename]);
  22. if (after.find('\n') == std::string::npos) {
  23. EXPECT_THAT(*actual, testing::Eq(after.str()));
  24. } else {
  25. // Split lines to get gmock to get an easier-to-read error.
  26. llvm::SmallVector<llvm::StringRef, 0> actual_lines;
  27. llvm::SplitString(*actual, actual_lines, "\n");
  28. llvm::SmallVector<llvm::StringRef, 0> after_lines;
  29. llvm::SplitString(after, after_lines, "\n");
  30. EXPECT_THAT(actual_lines, testing::ContainerEq(after_lines));
  31. }
  32. }
  33. } // namespace Carbon