15#include "clang/AST/DeclBase.h"
16#include "clang/AST/DeclObjC.h"
17#include "llvm/Testing/Support/Error.h"
18#include "gmock/gmock.h"
19#include "gtest/gtest.h"
26TEST(InsertionPointTests, Generic) {
32 $c^int c1; // trailing comment
39 [&](llvm::StringLiteral S) -> std::function<
bool(
const Decl *)> {
40 return [S](
const Decl *
D) {
41 if (
const auto *ND = llvm::dyn_cast<NamedDecl>(D))
42 return llvm::StringRef(ND->getNameAsString()).starts_with(S);
48 auto &NS = cast<NamespaceDecl>(
findDecl(
AST,
"ns"));
51 auto Point = [&](llvm::StringLiteral Prefix,
Anchor::Dir Direction) {
66 auto Chain = [&](llvm::StringLiteral P1, llvm::StringLiteral P2) {
71 EXPECT_EQ(Chain(
"a",
"b"), Code.point(
"a"));
72 EXPECT_EQ(Chain(
"b",
"a"), Code.point(
"b"));
73 EXPECT_EQ(Chain(
"no_match",
"a"), Code.point(
"a"));
77 ASSERT_THAT_EXPECTED(
Edit, llvm::Succeeded());
79 EXPECT_EQ(
Edit->getReplacementText(),
"foo;");
82 ASSERT_THAT_EXPECTED(
Edit, llvm::Succeeded());
90TEST(InsertionPointTests, CXX) {
94 $Method^void pubMethod();
99 $method^void privMethod();
100 template <typename T> void privTemplateMethod();
105 const CXXRecordDecl &
C = cast<CXXRecordDecl>(
findDecl(
AST,
"C"));
107 auto IsMethod = [](
const Decl *
D) {
return llvm::isa<CXXMethodDecl>(D); };
108 auto Any = [](
const Decl *
D) {
return true; };
111 auto Point = [&](
Anchor A, AccessSpecifier Protection) {
115 EXPECT_EQ(Point({IsMethod,
Anchor::Above}, AS_public), Code.point(
"Method"));
116 EXPECT_EQ(Point({IsMethod,
Anchor::Below}, AS_public), Code.point(
"Field"));
117 EXPECT_EQ(Point({Any,
Anchor::Above}, AS_public), Code.point(
"Method"));
118 EXPECT_EQ(Point({Any,
Anchor::Below}, AS_public), Code.point(
"private"));
119 EXPECT_EQ(Point({IsMethod,
Anchor::Above}, AS_private), Code.point(
"method"));
120 EXPECT_EQ(Point({IsMethod,
Anchor::Below}, AS_private), Code.point(
"end"));
121 EXPECT_EQ(Point({Any,
Anchor::Above}, AS_private), Code.point(
"field"));
122 EXPECT_EQ(Point({Any,
Anchor::Below}, AS_private), Code.point(
"end"));
130 ASSERT_THAT_EXPECTED(
Edit, llvm::Succeeded());
132 Code.point(
"private"));
135 ASSERT_THAT_EXPECTED(
Edit, llvm::Succeeded());
140 ASSERT_THAT_EXPECTED(
Edit, llvm::Succeeded());
143 EXPECT_EQ(
Edit->getReplacementText(),
"protected:\nx");
147 if (arg.getReplacementText() !=
Text) {
148 *result_listener <<
"replacement is " << arg.getReplacementText().str();
154TEST(InsertionPointTests, CXXAccessProtection) {
157 const CXXRecordDecl &S = cast<CXXRecordDecl>(
findDecl(
AST,
"S"));
158 ASSERT_THAT_EXPECTED(
insertDecl(
"x", S, {}, AS_public),
159 HasValue(replacementText(
"x")));
160 ASSERT_THAT_EXPECTED(
insertDecl(
"x", S, {}, AS_private),
161 HasValue(replacementText(
"private:\nx")));
165 const CXXRecordDecl &
T = cast<CXXRecordDecl>(
findDecl(
AST,
"T"));
166 ASSERT_THAT_EXPECTED(
insertDecl(
"x", T, {}, AS_public),
167 HasValue(replacementText(
"public:\nx")));
168 ASSERT_THAT_EXPECTED(
insertDecl(
"x", T, {}, AS_private),
169 HasValue(replacementText(
"x")));
173 const CXXRecordDecl &U = cast<CXXRecordDecl>(
findDecl(
AST,
"U"));
174 ASSERT_THAT_EXPECTED(
insertDecl(
"x", U, {}, AS_public),
175 HasValue(replacementText(
"x")));
176 ASSERT_THAT_EXPECTED(
insertDecl(
"x", U, {}, AS_private),
177 HasValue(replacementText(
"x")));
181TEST(InsertionPointTests, ObjC) {
191 TU.Filename =
"TestTU.m";
192 auto AST = TU.build();
195 cast<ObjCImplementationDecl>(
findDecl(
AST, [&](
const NamedDecl &D) {
196 return llvm::isa<ObjCImplementationDecl>(D);
198 auto &Iface = *Impl.getClassInterface();
201 const auto &SM =
AST.getSourceManager();
203 Code.point(
"endIface"));
205 Code.point(
"endImpl"));
Same as llvm::Annotations, but adjusts functions to LSP-specific types for positions and ranges.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
const NamedDecl & findDecl(ParsedAST &AST, llvm::StringRef QName)
Position offsetToPosition(llvm::StringRef Code, size_t Offset)
Turn an offset in Code into a [line, column] pair.
Position sourceLocToPosition(const SourceManager &SM, SourceLocation Loc)
Turn a SourceLocation into a [line, column] pair.
TEST(BackgroundQueueTest, Priority)
SourceLocation insertionPoint(const DeclContext &DC, llvm::ArrayRef< Anchor > Anchors)
llvm::Expected< tooling::Replacement > insertDecl(llvm::StringRef Code, const DeclContext &DC, llvm::ArrayRef< Anchor > Anchors)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
A set of edits generated for a single file.
static TestTU withCode(llvm::StringRef Code)