| 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> ; |
| 19 | |
| 20 | Context Ctx = Context::empty().derive(Key: IntParam, Value: 10).derive(Key: ExtraIntParam, Value: 20); |
| 21 | |
| 22 | EXPECT_EQ(*Ctx.get(IntParam), 10); |
| 23 | EXPECT_EQ(*Ctx.get(ExtraIntParam), 20); |
| 24 | } |
| 25 | |
| 26 | TEST(ContextTests, MoveOps) { |
| 27 | Key<std::unique_ptr<int>> Param; |
| 28 | |
| 29 | Context Ctx = Context::empty().derive(Key: Param, Value: std::make_unique<int>(args: 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(Key: ParentParam, Value: 10).derive(Key: ParentAndChildParam, Value: 20); |
| 43 | Context ChildCtx = |
| 44 | ParentCtx.derive(Key: ParentAndChildParam, Value: 30).derive(Key: ChildParam, Value: 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 |
| 57 | |