forked from snakster/cpp.react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObserverTest.h
More file actions
110 lines (87 loc) · 2.5 KB
/
ObserverTest.h
File metadata and controls
110 lines (87 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright Sebastian Jeckel 2014.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#pragma once
#include "gtest/gtest.h"
#include "react/Signal.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace {
using namespace react;
///////////////////////////////////////////////////////////////////////////////////////////////////
/// ObserverTest fixture
///////////////////////////////////////////////////////////////////////////////////////////////////
template <typename TEngine>
class ObserverTest : public testing::Test
{
public:
REACTIVE_DOMAIN(MyDomain, TEngine);
};
TYPED_TEST_CASE_P(ObserverTest);
///////////////////////////////////////////////////////////////////////////////////////////////////
/// Detach test
///////////////////////////////////////////////////////////////////////////////////////////////////
TYPED_TEST_P(ObserverTest, Detach)
{
auto a1 = MyDomain::MakeVar(1);
auto a2 = MyDomain::MakeVar(1);
auto result = a1 + a2;
int observeCount1 = 0;
int observeCount2 = 0;
int observeCount3 = 0;
int phase;
auto obs1 = Observe(result, [&] (int v)
{
observeCount1++;
if (phase == 0)
ASSERT_EQ(v,3);
else if (phase == 1)
ASSERT_EQ(v,4);
else
ASSERT_TRUE(false);
});
Observe(result, [&] (int v)
{
observeCount2++;
if (phase == 0)
ASSERT_EQ(v,3);
else if (phase == 1)
ASSERT_EQ(v,4);
else
ASSERT_TRUE(false);
});
Observe(result, [&] (int v)
{
observeCount3++;
if (phase == 0)
ASSERT_EQ(v,3);
else if (phase == 1)
ASSERT_EQ(v,4);
else
ASSERT_TRUE(false);
});
phase = 0;
a1 <<= 2;
ASSERT_EQ(observeCount1,1);
ASSERT_EQ(observeCount2,1);
ASSERT_EQ(observeCount3,1);
phase = 1;
obs1.Detach();
a1 <<= 3;
ASSERT_EQ(observeCount1,1);
ASSERT_EQ(observeCount2,2);
ASSERT_EQ(observeCount3,2);
phase = 2;
DetachAllObservers(result);
a1 <<= 4;
ASSERT_EQ(observeCount1,1);
ASSERT_EQ(observeCount2,2);
ASSERT_EQ(observeCount3,2);
}
///////////////////////////////////////////////////////////////////////////////////////////////////
REGISTER_TYPED_TEST_CASE_P
(
ObserverTest,
Detach
);
} // ~namespace