clang-tools 20.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
13namespace clang {
14namespace clangd {
15
16TEST(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
26TEST(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
36TEST(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
A context is an immutable container for per-request data that must be propagated through layers that ...
Definition: Context.h:69
Context derive(const Key< Type > &Key, std::decay_t< Type > Value) const &
Derives a child context It is safe to move or destroy a parent context after calling derive().
Definition: Context.h:119
const Type * get(const Key< Type > &Key) const
Get data stored for a typed Key.
Definition: Context.h:98
static Context empty()
Returns an empty root context that contains no data.
Definition: Context.cpp:15
Values in a Context are indexed by typed keys.
Definition: Context.h:40
TEST(BackgroundQueueTest, Priority)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//