1 | //===-- FunctionTests.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 "support/Function.h" |
10 | #include "gmock/gmock.h" |
11 | #include "gtest/gtest.h" |
12 | |
13 | namespace clang { |
14 | namespace clangd { |
15 | namespace { |
16 | |
17 | TEST(EventTest, Subscriptions) { |
18 | Event<int> E; |
19 | int N = 0; |
20 | { |
21 | Event<int>::Subscription SubA; |
22 | // No subscriptions are active. |
23 | E.broadcast(V: 42); |
24 | EXPECT_EQ(0, N); |
25 | |
26 | Event<int>::Subscription SubB = E.observe(L: [&](int) { ++N; }); |
27 | // Now one is active. |
28 | E.broadcast(V: 42); |
29 | EXPECT_EQ(1, N); |
30 | |
31 | SubA = E.observe(L: [&](int) { ++N; }); |
32 | // Both are active. |
33 | EXPECT_EQ(1, N); |
34 | E.broadcast(V: 42); |
35 | EXPECT_EQ(3, N); |
36 | |
37 | SubA = std::move(SubB); |
38 | // One is active. |
39 | EXPECT_EQ(3, N); |
40 | E.broadcast(V: 42); |
41 | EXPECT_EQ(4, N); |
42 | } |
43 | // None are active. |
44 | EXPECT_EQ(4, N); |
45 | E.broadcast(V: 42); |
46 | EXPECT_EQ(4, N); |
47 | } |
48 | |
49 | } // namespace |
50 | } // namespace clangd |
51 | } // namespace clang |
52 | |