Files
carbon-lang/migrate_cpp/cpp_refactoring/matcher_test_base.h
T
Josh Soref 066b103881 Spelling (#1580)
This PR corrects misspellings identified by the [check-spelling action](https://github.com/marketplace/actions/check-spelling).

The misspellings have been reported at https://github.com/jsoref/carbon-lang/commit/38a1c1640151899fd6da0442a92557f9543b6280#commitcomment-79197316

The action reports that the changes in this PR would make it happy: https://github.com/jsoref/carbon-lang/commit/173c8f9083a68aa61f7cfe94f720f1e5dc7f1ea3

Note: this PR does not include the action. If you're interested in running a spell check on every PR and push, that can be offered separately.

Signed-off-by: Josh Soref <2119212+jsoref@users.noreply.github.com>
2022-07-22 16:14:21 -07:00

67 lines
2.5 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_MIGRATE_CPP_CPP_REFACTORING_MATCHER_TEST_BASE_H_
#define CARBON_MIGRATE_CPP_CPP_REFACTORING_MATCHER_TEST_BASE_H_
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Tooling/Core/Replacement.h"
#include "clang/Tooling/Tooling.h"
#include "migrate_cpp/cpp_refactoring/matcher_manager.h"
namespace Carbon::Testing {
// Matcher test framework.
template <typename MatcherFactoryType>
class MatcherTestBase : public ::testing::Test {
protected:
MatcherTestBase() : matchers(&replacements) {
matchers.Register(std::make_unique<MatcherFactoryType>());
}
// Expects that the replacements produced by running the finder result in
// the specified code transformation.
void ExpectReplacement(llvm::StringRef before, llvm::StringRef after) {
auto factory =
clang::tooling::newFrontendActionFactory(matchers.GetFinder());
constexpr char Filename[] = "test.cc";
replacements.clear();
replacements.insert({Filename, {}});
ASSERT_TRUE(clang::tooling::runToolOnCodeWithArgs(
factory->create(), before, {}, Filename, "clang-tool",
std::make_shared<clang::PCHContainerOperations>(),
clang::tooling::FileContentMappings()));
EXPECT_THAT(replacements, testing::ElementsAre(testing::Key(Filename)));
llvm::Expected<std::string> actual =
clang::tooling::applyAllReplacements(before, replacements[Filename]);
// Make a specific note if the matcher didn't make any changes.
std::string unchanged;
if (before == *actual) {
unchanged = "NOTE: Actual matches original text, no changes made.";
}
if (after.find('\n') == std::string::npos) {
EXPECT_THAT(*actual, testing::Eq(after.str())) << unchanged;
} else {
// Split lines to get gmock to get an easier-to-read error.
llvm::SmallVector<llvm::StringRef, 0> actual_lines;
llvm::SplitString(*actual, actual_lines, "\n");
llvm::SmallVector<llvm::StringRef, 0> after_lines;
llvm::SplitString(after, after_lines, "\n");
EXPECT_THAT(actual_lines, testing::ContainerEq(after_lines)) << unchanged;
}
}
Matcher::ReplacementMap replacements;
MatcherManager matchers;
};
} // namespace Carbon::Testing
#endif // CARBON_MIGRATE_CPP_CPP_REFACTORING_MATCHER_TEST_BASE_H_