clang-tools  14.0.0git
ContextTests.cpp
Go to the documentation of this file.
1 //===-- ContextTests.cpp - Context tests ------------------------*- 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 "support/Context.h"
10 
11 #include "gtest/gtest.h"
12 
13 namespace clang {
14 namespace clangd {
15 
16 TEST(ContextTests, Simple) {
17  Key<int> IntParam;
18  Key<int> ExtraIntParam;
19 
20  Context Ctx = Context::empty().derive(IntParam, 10).derive(ExtraIntParam, 20);
21 
22  EXPECT_EQ(*Ctx.get(IntParam), 10);
23  EXPECT_EQ(*Ctx.get(ExtraIntParam), 20);
24 }
25 
26 TEST(ContextTests, MoveOps) {
28 
29  Context Ctx = Context::empty().derive(Param, std::make_unique<int>(10));
30  EXPECT_EQ(**Ctx.get(Param), 10);
31 
32  Context NewCtx = std::move(Ctx);
33  EXPECT_EQ(**NewCtx.get(Param), 10);
34 }
35 
36 TEST(ContextTests, Builders) {
37  Key<int> ParentParam;
38  Key<int> ParentAndChildParam;
39  Key<int> ChildParam;
40 
41  Context ParentCtx =
42  Context::empty().derive(ParentParam, 10).derive(ParentAndChildParam, 20);
43  Context ChildCtx =
44  ParentCtx.derive(ParentAndChildParam, 30).derive(ChildParam, 40);
45 
46  EXPECT_EQ(*ParentCtx.get(ParentParam), 10);
47  EXPECT_EQ(*ParentCtx.get(ParentAndChildParam), 20);
48  EXPECT_EQ(ParentCtx.get(ChildParam), nullptr);
49 
50  EXPECT_EQ(*ChildCtx.get(ParentParam), 10);
51  EXPECT_EQ(*ChildCtx.get(ParentAndChildParam), 30);
52  EXPECT_EQ(*ChildCtx.get(ChildParam), 40);
53 }
54 
55 } // namespace clangd
56 } // namespace clang
clang::clangd::TEST
TEST(BackgroundQueueTest, Priority)
Definition: BackgroundIndexTests.cpp:751
clang::clangd::Context::get
const Type * get(const Key< Type > &Key) const
Get data stored for a typed Key.
Definition: Context.h:98
Ctx
Context Ctx
Definition: TUScheduler.cpp:454
clang::clangd::Key
Values in a Context are indexed by typed keys.
Definition: Context.h:40
clang::clangd::Context::derive
Context derive(const Key< Type > &Key, typename std::decay< Type >::type Value) const &
Derives a child context It is safe to move or destroy a parent context after calling derive().
Definition: Context.h:119
clang::clangd::Context::empty
static Context empty()
Returns an empty root context that contains no data.
Definition: Context.cpp:15
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::Context
A context is an immutable container for per-request data that must be propagated through layers that ...
Definition: Context.h:69
Context.h