diff options
author | Alex Lorenz <arphaman@gmail.com> | 2017-10-02 18:42:43 +0000 |
---|---|---|
committer | Alex Lorenz <arphaman@gmail.com> | 2017-10-02 18:42:43 +0000 |
commit | 7b93fc9054c185272b5e4b3c4bc9cd8f512fd9f4 (patch) | |
tree | 0477a6207fc1e8b415fd99b1d4837874017db4e7 /unittests | |
parent | c97363b2c14a50be033ae6af4e360114a234e752 (diff) |
[refactor] Simplify the refactoring interface
This commit simplifies the interface for the refactoring action rules and the
refactoring requirements. It merges the selection constraints and the selection
requirements into one class. The refactoring actions rules must now be
implemented using subclassing instead of raw function / lambda pointers. This
change also removes a bunch of template-based traits and other
template definitions that are now redundant.
Differential Revision: https://reviews.llvm.org/D37681
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@314704 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/Tooling/RefactoringActionRulesTest.cpp | 129 |
1 files changed, 57 insertions, 72 deletions
diff --git a/unittests/Tooling/RefactoringActionRulesTest.cpp b/unittests/Tooling/RefactoringActionRulesTest.cpp index cf91b5fc8f..afab34ef10 100644 --- a/unittests/Tooling/RefactoringActionRulesTest.cpp +++ b/unittests/Tooling/RefactoringActionRulesTest.cpp @@ -18,7 +18,6 @@ using namespace clang; using namespace tooling; -using namespace refactoring_action_rules; namespace { @@ -56,29 +55,39 @@ createReplacements(const std::unique_ptr<RefactoringActionRule> &Rule, } TEST_F(RefactoringActionRulesTest, MyFirstRefactoringRule) { - auto ReplaceAWithB = - [](const RefactoringRuleContext &, - std::pair<selection::SourceSelectionRange, int> Selection) - -> Expected<AtomicChanges> { - const SourceManager &SM = Selection.first.getSources(); - SourceLocation Loc = Selection.first.getRange().getBegin().getLocWithOffset( - Selection.second); - AtomicChange Change(SM, Loc); - llvm::Error E = Change.replace(SM, Loc, 1, "b"); - if (E) - return std::move(E); - return AtomicChanges{Change}; + class ReplaceAWithB : public SourceChangeRefactoringRule { + std::pair<SourceRange, int> Selection; + + public: + ReplaceAWithB(std::pair<SourceRange, int> Selection) + : Selection(Selection) {} + + Expected<AtomicChanges> + createSourceReplacements(RefactoringRuleContext &Context) { + const SourceManager &SM = Context.getSources(); + SourceLocation Loc = + Selection.first.getBegin().getLocWithOffset(Selection.second); + AtomicChange Change(SM, Loc); + llvm::Error E = Change.replace(SM, Loc, 1, "b"); + if (E) + return std::move(E); + return AtomicChanges{Change}; + } }; - class SelectionRequirement : public selection::Requirement { + + class SelectionRequirement : public SourceRangeSelectionRequirement { public: - std::pair<selection::SourceSelectionRange, int> - evaluateSelection(const RefactoringRuleContext &, - selection::SourceSelectionRange Selection) const { - return std::make_pair(Selection, 20); + Expected<std::pair<SourceRange, int>> + evaluate(RefactoringRuleContext &Context) const { + Expected<SourceRange> R = + SourceRangeSelectionRequirement::evaluate(Context); + if (!R) + return R.takeError(); + return std::make_pair(*R, 20); } }; - auto Rule = createRefactoringRule(ReplaceAWithB, - requiredSelection(SelectionRequirement())); + auto Rule = + createRefactoringActionRule<ReplaceAWithB>(SelectionRequirement()); // When the requirements are satisifed, the rule's function must be invoked. { @@ -123,54 +132,24 @@ TEST_F(RefactoringActionRulesTest, MyFirstRefactoringRule) { llvm::handleAllErrors( ErrorOrResult.takeError(), [&](llvm::StringError &Error) { Message = Error.getMessage(); }); - EXPECT_EQ(Message, "refactoring action can't be initiated with the " - "specified selection range"); + EXPECT_EQ(Message, + "refactoring action can't be initiated without a selection"); } } TEST_F(RefactoringActionRulesTest, ReturnError) { - Expected<AtomicChanges> (*Func)(const RefactoringRuleContext &, - selection::SourceSelectionRange) = - [](const RefactoringRuleContext &, - selection::SourceSelectionRange) -> Expected<AtomicChanges> { - return llvm::make_error<llvm::StringError>( - "Error", llvm::make_error_code(llvm::errc::invalid_argument)); - }; - auto Rule = createRefactoringRule( - Func, requiredSelection( - selection::identity<selection::SourceSelectionRange>())); - - RefactoringRuleContext RefContext(Context.Sources); - SourceLocation Cursor = - Context.Sources.getLocForStartOfFile(Context.Sources.getMainFileID()); - RefContext.setSelectionRange({Cursor, Cursor}); - Expected<AtomicChanges> Result = createReplacements(Rule, RefContext); - - ASSERT_TRUE(!Result); - std::string Message; - llvm::handleAllErrors(Result.takeError(), [&](llvm::StringError &Error) { - Message = Error.getMessage(); - }); - EXPECT_EQ(Message, "Error"); -} - -TEST_F(RefactoringActionRulesTest, ReturnInitiationDiagnostic) { - RefactoringRuleContext RefContext(Context.Sources); - class SelectionRequirement : public selection::Requirement { + class ErrorRule : public SourceChangeRefactoringRule { public: - Expected<Optional<int>> - evaluateSelection(const RefactoringRuleContext &, - selection::SourceSelectionRange Selection) const { + ErrorRule(SourceRange R) {} + Expected<AtomicChanges> createSourceReplacements(RefactoringRuleContext &) { return llvm::make_error<llvm::StringError>( - "bad selection", llvm::make_error_code(llvm::errc::invalid_argument)); + "Error", llvm::make_error_code(llvm::errc::invalid_argument)); } }; - auto Rule = createRefactoringRule( - [](const RefactoringRuleContext &, int) -> Expected<AtomicChanges> { - llvm::report_fatal_error("Should not run!"); - }, - requiredSelection(SelectionRequirement())); + auto Rule = + createRefactoringActionRule<ErrorRule>(SourceRangeSelectionRequirement()); + RefactoringRuleContext RefContext(Context.Sources); SourceLocation Cursor = Context.Sources.getLocForStartOfFile(Context.Sources.getMainFileID()); RefContext.setSelectionRange({Cursor, Cursor}); @@ -181,7 +160,7 @@ TEST_F(RefactoringActionRulesTest, ReturnInitiationDiagnostic) { llvm::handleAllErrors(Result.takeError(), [&](llvm::StringError &Error) { Message = Error.getMessage(); }); - EXPECT_EQ(Message, "bad selection"); + EXPECT_EQ(Message, "Error"); } Optional<SymbolOccurrences> findOccurrences(RefactoringActionRule &Rule, @@ -205,18 +184,24 @@ Optional<SymbolOccurrences> findOccurrences(RefactoringActionRule &Rule, } TEST_F(RefactoringActionRulesTest, ReturnSymbolOccurrences) { - auto Rule = createRefactoringRule( - [](const RefactoringRuleContext &, - selection::SourceSelectionRange Selection) - -> Expected<SymbolOccurrences> { - SymbolOccurrences Occurrences; - Occurrences.push_back(SymbolOccurrence( - SymbolName("test"), SymbolOccurrence::MatchingSymbol, - Selection.getRange().getBegin())); - return std::move(Occurrences); - }, - requiredSelection( - selection::identity<selection::SourceSelectionRange>())); + class FindOccurrences : public FindSymbolOccurrencesRefactoringRule { + SourceRange Selection; + + public: + FindOccurrences(SourceRange Selection) : Selection(Selection) {} + + Expected<SymbolOccurrences> + findSymbolOccurrences(RefactoringRuleContext &) override { + SymbolOccurrences Occurrences; + Occurrences.push_back(SymbolOccurrence(SymbolName("test"), + SymbolOccurrence::MatchingSymbol, + Selection.getBegin())); + return Occurrences; + } + }; + + auto Rule = createRefactoringActionRule<FindOccurrences>( + SourceRangeSelectionRequirement()); RefactoringRuleContext RefContext(Context.Sources); SourceLocation Cursor = |