clang-tools 22.0.0git
FeatureModulesRegistryTests.cpp
Go to the documentation of this file.
1//===--- FeatureModulesRegistryTests.cpp ---------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "FeatureModule.h"
10#include "refactor/Tweak.h"
11#include "support/Logger.h"
12
13#include "gmock/gmock.h"
14#include "gtest/gtest.h"
15
16using testing::ElementsAre;
17
18namespace llvm {
19raw_ostream &operator<<(raw_ostream &OS,
20 const clang::clangd::FeatureModuleRegistry::entry &E) {
21 OS << "(name = " << E.getName() << ", description = '" << E.getDesc() << "')";
22 return OS;
23}
24
25raw_ostream &operator<<(
26 raw_ostream &OS,
27 const iterator_range<Registry<clang::clangd::FeatureModule>::iterator>
28 &Rng) {
29 OS << "{ ";
30 bool First = true;
31 for (clang::clangd::FeatureModuleRegistry::entry E : Rng) {
32 if (First)
33 First = false;
34 else
35 OS << ", ";
36 OS << E;
37 }
38 OS << " }";
39 return OS;
40}
41
42raw_ostream &operator<<(raw_ostream &OS, const clang::clangd::Tweak &T) {
43 OS << "(id = " << T.id() << ", "
44 << "title = " << T.title() << ")";
45 return OS;
46}
47} // namespace llvm
48
49namespace clang::clangd {
50namespace {
51
52class Dummy final : public FeatureModule {
53 static constexpr const char *TweakID = "DummyTweak";
54 struct DummyTweak final : public Tweak {
55 const char *id() const override { return TweakID; }
56 bool prepare(const Selection &) override { return true; }
57 Expected<Effect> apply(const Selection &) override {
58 return error("not implemented");
59 }
60 std::string title() const override { return id(); }
61 llvm::StringLiteral kind() const override {
62 return llvm::StringLiteral("");
63 };
64 };
65
66 void contributeTweaks(std::vector<std::unique_ptr<Tweak>> &Out) override {
67 Out.emplace_back(new DummyTweak);
68 }
69};
70
71static FeatureModuleRegistry::Add<Dummy>
72 X("dummy", "Dummy feature module with dummy tweak");
73
74MATCHER_P(moduleName, Name, "") { return arg.getName() == Name; }
75MATCHER_P(tweakID, ID, "") { return arg->id() == llvm::StringRef(ID); }
76
77// In this test, it is assumed that for unittests executable, all feature
78// modules are added to the registry only here (in this file). To implement
79// modules for clangd tool, one need to link them directly to the clangd
80// executable in clangd/tool/CMakeLists.txt.
81TEST(FeatureModulesRegistryTest, DummyModule) {
82 EXPECT_THAT(FeatureModuleRegistry::entries(),
83 ElementsAre(moduleName("dummy")));
85 ASSERT_EQ(Set.end() - Set.begin(), 1u);
86 std::vector<std::unique_ptr<Tweak>> Tweaks;
87 Set.begin()->contributeTweaks(Tweaks);
88 EXPECT_THAT(Tweaks, ElementsAre(tweakID("DummyTweak")));
89}
90
91} // namespace
92} // namespace clang::clangd
A FeatureModuleSet is a collection of feature modules installed in clangd.
static FeatureModuleSet fromRegistry()
A FeatureModule contributes a vertical feature to clangd.
An interface base for small context-sensitive refactoring actions.
Definition Tweak.h:46
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:45
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&... Vals)
Definition Logger.h:79
MATCHER_P(named, N, "")
static URISchemeRegistry::Add< TestScheme > X(TestScheme::Scheme, "Test schema")
TEST(BackgroundQueueTest, Priority)
Some operations such as code completion produce a set of candidates.
Definition Generators.h:66
raw_ostream & operator<<(raw_ostream &OS, const clang::clangd::FeatureModuleRegistry::entry &E)