1 //===--- FeatureModulesTests.cpp  -------------------------------*- C++ -*-===//
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 "Annotations.h"
10 #include "FeatureModule.h"
11 #include "Selection.h"
12 #include "TestTU.h"
13 #include "refactor/Tweak.h"
14 #include "support/Logger.h"
15 #include "llvm/Support/Error.h"
16 #include "gmock/gmock.h"
17 #include "gtest/gtest.h"
18 #include <memory>
19 
20 namespace clang {
21 namespace clangd {
22 namespace {
23 
24 TEST(FeatureModulesTest, ContributesTweak) {
25   static constexpr const char *TweakID = "ModuleTweak";
26   struct TweakContributingModule final : public FeatureModule {
27     struct ModuleTweak final : public Tweak {
28       const char *id() const override { return TweakID; }
29       bool prepare(const Selection &Sel) override { return true; }
30       Expected<Effect> apply(const Selection &Sel) override {
31         return error("not implemented");
32       }
33       std::string title() const override { return id(); }
34       llvm::StringLiteral kind() const override {
35         return llvm::StringLiteral("");
36       };
37     };
38 
39     void contributeTweaks(std::vector<std::unique_ptr<Tweak>> &Out) override {
40       Out.emplace_back(new ModuleTweak);
41     }
42   };
43 
44   FeatureModuleSet Set;
45   Set.add(std::make_unique<TweakContributingModule>());
46 
47   auto AST = TestTU::withCode("").build();
48   auto Tree =
49       SelectionTree::createRight(AST.getASTContext(), AST.getTokens(), 0, 0);
50   auto Actual = prepareTweak(
51       TweakID, Tweak::Selection(nullptr, AST, 0, 0, std::move(Tree), nullptr),
52       &Set);
53   ASSERT_TRUE(bool(Actual));
54   EXPECT_EQ(Actual->get()->id(), TweakID);
55 }
56 
57 TEST(FeatureModulesTest, SuppressDiags) {
58   struct DiagModifierModule final : public FeatureModule {
59     struct Listener : public FeatureModule::ASTListener {
60       void sawDiagnostic(const clang::Diagnostic &Info,
61                          clangd::Diag &Diag) override {
62         Diag.Severity = DiagnosticsEngine::Ignored;
63       }
64     };
65     std::unique_ptr<ASTListener> astListeners() override {
66       return std::make_unique<Listener>();
67     };
68   };
69   FeatureModuleSet FMS;
70   FMS.add(std::make_unique<DiagModifierModule>());
71 
72   Annotations Code("[[test]]; /* error-ok */");
73   TestTU TU;
74   TU.Code = Code.code().str();
75 
76   {
77     auto AST = TU.build();
78     EXPECT_THAT(*AST.getDiagnostics(), testing::Not(testing::IsEmpty()));
79   }
80 
81   TU.FeatureModules = &FMS;
82   {
83     auto AST = TU.build();
84     EXPECT_THAT(*AST.getDiagnostics(), testing::IsEmpty());
85   }
86 }
87 
88 } // namespace
89 } // namespace clangd
90 } // namespace clang
91