|
10 | 10 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_TWEAKS_TWEAKTESTING_H
|
11 | 11 |
|
12 | 12 | #include "ParsedAST.h"
|
| 13 | +#include "TestWorkspace.h" |
13 | 14 | #include "index/Index.h"
|
14 | 15 | #include "llvm/ADT/StringMap.h"
|
15 | 16 | #include "llvm/ADT/StringRef.h"
|
@@ -123,6 +124,73 @@ MATCHER_P2(FileWithContents, FileName, Contents, "") {
|
123 | 124 | #define EXPECT_AVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, true)
|
124 | 125 | #define EXPECT_UNAVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, false)
|
125 | 126 |
|
| 127 | +// A helper class to represent the return value of TweakWorkspaceTest::apply(). |
| 128 | +struct TweakResult { |
| 129 | + // A string representation the status of the operation. |
| 130 | + // For failure cases, this is the same as the return value of |
| 131 | + // TweakTest::apply() (see the comment above that for details). |
| 132 | + // For success cases, this is "success". |
| 133 | + std::string Status; |
| 134 | + // The contents of all files changed by the tweak, including |
| 135 | + // the file in which it was invoked. Keys are absolute paths. |
| 136 | + llvm::StringMap<std::string> EditedFiles = {}; |
| 137 | +}; |
| 138 | + |
| 139 | +// GTest matchers to allow more easily writing assertions about the |
| 140 | +// expected value of a TweakResult. |
| 141 | +MATCHER_P(withStatus, S, "") { return arg.Status == S; } |
| 142 | +template <class EditedFilesMatcher> |
| 143 | +::testing::Matcher<TweakResult> editedFiles(EditedFilesMatcher M) { |
| 144 | + return ::testing::Field(&TweakResult::EditedFiles, M); |
| 145 | +} |
| 146 | + |
| 147 | +// Used for formatting TweakResult objects in assertion failure messages, |
| 148 | +// so it's easier to understand what didn't match. |
| 149 | +inline llvm::raw_ostream &operator<<(llvm::raw_ostream &Stream, |
| 150 | + const TweakResult &Result) { |
| 151 | + Stream << "{ status: " << Result.Status << ", editedFiles: ["; |
| 152 | + for (const auto &F : Result.EditedFiles) { |
| 153 | + Stream << F.first() << ":\n"; |
| 154 | + Stream << F.second; |
| 155 | + } |
| 156 | + return Stream << "] }"; |
| 157 | +} |
| 158 | + |
| 159 | +// A version of TweakTest that makes it easier to create test cases that |
| 160 | +// involve multiple files which are indexed. |
| 161 | +// Usage: |
| 162 | +// - Call `Workspace.addMainFile(filename, contents)` to add |
| 163 | +// source files which are indexer entry points (e.g. would show |
| 164 | +// up in `compile_commands.json`). |
| 165 | +// - Call `Workspace.addSource(filename, contents)` to add other |
| 166 | +// source files (e.g. header files). |
| 167 | +// - Call `apply(filename, range)` to invoke the tweak on the |
| 168 | +// indicated file with the given range selected. Can be called |
| 169 | +// multiple times for the same set of added files. |
| 170 | +// The implementation takes care of building an index reflecting |
| 171 | +// all added source files, and making it available to the tweak. |
| 172 | +// Unlike TweakTest, this does not have a notion of a `CodeContext` |
| 173 | +// (i.e. the contents of all added files are interpreted as being |
| 174 | +// in a File context). |
| 175 | +class TweakWorkspaceTest : public ::testing::Test { |
| 176 | + const char *TweakID; |
| 177 | + |
| 178 | +public: |
| 179 | + TweakWorkspaceTest(const char *TweakID) : TweakID(TweakID) {} |
| 180 | + |
| 181 | + TweakResult apply(StringRef InvocationFile, |
| 182 | + llvm::Annotations::Range InvocationRange); |
| 183 | + |
| 184 | +protected: |
| 185 | + TestWorkspace Workspace; |
| 186 | +}; |
| 187 | + |
| 188 | +#define TWEAK_WORKSPACE_TEST(TweakID) \ |
| 189 | + class TweakID##WorkspaceTest : public ::clang::clangd::TweakWorkspaceTest { \ |
| 190 | + protected: \ |
| 191 | + TweakID##WorkspaceTest() : TweakWorkspaceTest(#TweakID) {} \ |
| 192 | + } |
| 193 | + |
126 | 194 | } // namespace clangd
|
127 | 195 | } // namespace clang
|
128 | 196 |
|
|
0 commit comments