Debian Codegraph @

clang-tools-extra/clangd/unittests/FeatureModulesRegistryTests.cpp

raw
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
16 using testing::ElementsAre;
17
18 namespace llvm {
19 raw_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
25 raw_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
42 raw_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
49 namespace clang::clangd {
50 namespace {
51
52 class 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
71 static FeatureModuleRegistry::Add<Dummy>
72 X("dummy", "Dummy feature module with dummy tweak");
73
74 MATCHER_P(moduleName, Name, "") { return arg.getName() == Name; }
75 MATCHER_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.
81 TEST(FeatureModulesRegistryTest, DummyModule) {
82 EXPECT_THAT(FeatureModuleRegistry::entries(),
83 ElementsAre(moduleName("dummy")));
84 FeatureModuleSet Set = FeatureModuleSet::fromRegistry();
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