matcher_test_base.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. ASSERT_TRUE(ct::runToolOnCodeWithArgs(
  15. factory->create(), before, {}, Filename, "clang-tool",
  16. std::make_shared<clang::PCHContainerOperations>(),
  17. ct::FileContentMappings()));
  18. auto it = replacements.find(Filename);
  19. if (it != replacements.end()) {
  20. auto actual = ct::applyAllReplacements(before, it->second);
  21. // Split lines to get gmock to get an easier-to-read error.
  22. llvm::SmallVector<llvm::StringRef, 0> actual_lines;
  23. llvm::SplitString(*actual, actual_lines, "\n");
  24. llvm::SmallVector<llvm::StringRef, 0> after_lines;
  25. llvm::SplitString(after, after_lines, "\n");
  26. EXPECT_THAT(actual_lines, testing::ContainerEq(after_lines));
  27. } else {
  28. // No replacements; before and after should match.
  29. EXPECT_THAT(before, testing::Eq(after));
  30. }
  31. }
  32. } // namespace Carbon