| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_TWEAKS_TWEAKTESTING_H |
| 10 |
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_UNITTESTS_TWEAKS_TWEAKTESTING_H |
| 11 |
|
| 12 |
#include "ParsedAST.h" |
| 13 |
#include "index/Index.h" |
| 14 |
#include "llvm/ADT/StringMap.h" |
| 15 |
#include "llvm/ADT/StringRef.h" |
| 16 |
#include "llvm/Testing/Annotations/Annotations.h" |
| 17 |
#include "gmock/gmock.h" |
| 18 |
#include "gtest/gtest.h" |
| 19 |
#include <memory> |
| 20 |
#include <string> |
| 21 |
|
| 22 |
namespace clang { |
| 23 |
namespace clangd { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class TweakTest : public ::testing::Test { |
| 42 |
const char *TweakID; |
| 43 |
|
| 44 |
public: |
| 45 |
|
| 46 |
|
| 47 |
enum CodeContext { |
| 48 |
|
| 49 |
File, |
| 50 |
|
| 51 |
Function, |
| 52 |
|
| 53 |
Expression, |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
llvm::StringMap<std::string> ExtraFiles; |
| 58 |
|
| 59 |
protected: |
| 60 |
TweakTest(const char *TweakID) : TweakID(TweakID) {} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
std::string Header; |
| 66 |
|
| 67 |
llvm::StringRef FileName = "TestTU.cpp"; |
| 68 |
|
| 69 |
|
| 70 |
std::vector<std::string> ExtraArgs; |
| 71 |
|
| 72 |
|
| 73 |
CodeContext Context = File; |
| 74 |
|
| 75 |
|
| 76 |
std::unique_ptr<const SymbolIndex> Index = nullptr; |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
std::string apply(llvm::StringRef MarkedCode, |
| 92 |
llvm::StringMap<std::string> *EditedFiles = nullptr) const; |
| 93 |
|
| 94 |
|
| 95 |
using WrappedAST = std::pair<ParsedAST, unsigned>; |
| 96 |
WrappedAST build(llvm::StringRef) const; |
| 97 |
bool isAvailable(WrappedAST &, llvm::Annotations::Range) const; |
| 98 |
|
| 99 |
static std::string decorate(llvm::StringRef, unsigned); |
| 100 |
static std::string decorate(llvm::StringRef, llvm::Annotations::Range); |
| 101 |
}; |
| 102 |
|
| 103 |
MATCHER_P2(FileWithContents, FileName, Contents, "") { |
| 104 |
return arg.first() == FileName && arg.second == Contents; |
| 105 |
} |
| 106 |
|
| 107 |
#define TWEAK_TEST(TweakID) \ |
| 108 |
class TweakID##Test : public ::clang::clangd::TweakTest { \ |
| 109 |
protected: \ |
| 110 |
TweakID##Test() : TweakTest(#TweakID) {} \ |
| 111 |
} |
| 112 |
|
| 113 |
#define EXPECT_AVAILABLE_(MarkedCode, Available) \ |
| 114 |
do { \ |
| 115 |
llvm::Annotations A{llvm::StringRef(MarkedCode)}; \ |
| 116 |
auto AST = build(A.code()); \ |
| 117 |
assert(!A.points().empty() || !A.ranges().empty()); \ |
| 118 |
for (const auto &P : A.points()) \ |
| 119 |
EXPECT_EQ(Available, isAvailable(AST, {P, P})) << decorate(A.code(), P); \ |
| 120 |
for (const auto &R : A.ranges()) \ |
| 121 |
EXPECT_EQ(Available, isAvailable(AST, R)) << decorate(A.code(), R); \ |
| 122 |
} while (0) |
| 123 |
#define EXPECT_AVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, true) |
| 124 |
#define EXPECT_UNAVAILABLE(MarkedCode) EXPECT_AVAILABLE_(MarkedCode, false) |
| 125 |
|
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
#endif |