forked from CxxTest/cxxtest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MockTest.h
182 lines (152 loc) · 3.77 KB
/
MockTest.h
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//
// This is a test of CxxTest's Mock framework (not a mock test).
//
#include <cxxtest/TestSuite.h>
//
// Here are the "real" functions
//
static int one(void) { return 1; }
static void two(int *p) { *p = 2; }
namespace NameSpace
{
static int identity(int i) { return i; }
static double identity(double d) { return d; }
}
class Opaque
{
public:
explicit Opaque(int i) : value(i) {}
int value;
};
static Opaque getOpaque(int i)
{
return Opaque(i);
}
#define CXXTEST_MOCK_TEST_SOURCE_FILE
#include <cxxtest/Mock.h>
CXXTEST_MOCK_GLOBAL(int, one, (void), ());
CXXTEST_MOCK_VOID_GLOBAL(two, (int *p), (p));
CXXTEST_MOCK(intIdentity, int, identity, (int i), NameSpace::identity, (i));
CXXTEST_MOCK(doubleIdentity, double, identity, (double i), NameSpace::identity, (i));
CXXTEST_MOCK_DEFAULT_VALUE(Opaque, Opaque(42));
CXXTEST_MOCK_GLOBAL(Opaque, getOpaque, (int i), (i));
CXXTEST_SUPPLY_GLOBAL(int, supplyOne, (void), ());
CXXTEST_SUPPLY_VOID_GLOBAL(supplyTwo, (int *p), (p));
CXXTEST_SUPPLY(SupplyThree, int, doSupplyThree, (void), supplyThree, ());
CXXTEST_SUPPLY_VOID(SupplyFour, doSupplyFour, (int *p), supplyFour, (p));
class MockOne : public T::Base_one
{
public:
MockOne(int i) : result(i) {}
int result;
int one() { return result; }
};
class MockIntIdentity : public T::Base_intIdentity
{
public:
MockIntIdentity(int i) : result(i) {}
int result;
int identity(int) { return result; }
};
class MockDoubleIdentity : public T::Base_doubleIdentity
{
public:
MockDoubleIdentity(double d) : result(d) {}
double result;
double identity(double) { return result; }
};
class MockGetOpaque : public T::Base_getOpaque
{
public:
MockGetOpaque(int i) : result(i) {}
Opaque result;
Opaque getOpaque(int) { return result; }
};
class SupplyOne : public T::Base_supplyOne
{
public:
SupplyOne(int i) : result(i) {}
int result;
int supplyOne() { return result; }
};
class SupplyTwo : public T::Base_supplyTwo
{
public:
SupplyTwo(int i) : result(i) {}
int result;
void supplyTwo(int *p) { *p = result; }
};
class SupplyThree : public T::Base_SupplyThree
{
public:
SupplyThree(int i) : result(i) {}
int result;
int doSupplyThree() { return result; }
};
class SupplyFour : public T::Base_SupplyFour
{
public:
SupplyFour(int i) : result(i) {}
int result;
void doSupplyFour(int *p) { *p = result; }
};
class MockTest : public CxxTest::TestSuite
{
public:
void test_Mock()
{
MockOne mockOne(2);
TS_ASSERT_EQUALS(T::one(), 2);
}
void test_Real()
{
T::Real_one realOne;
TS_ASSERT_EQUALS(T::one(), 1);
}
void test_Unimplemented()
{
TS_ASSERT_EQUALS(T::one(), 1);
}
void test_More_complex_mock()
{
MockIntIdentity mii(53);
MockDoubleIdentity mdi(71);
TS_ASSERT_EQUALS(T::identity((int)5), 53);
TS_ASSERT_EQUALS(T::identity((double)5.0), 71);
}
void test_Mock_traits()
{
TS_ASSERT_EQUALS(T::getOpaque(3).value, 72);
}
void test_Override()
{
MockOne *two = new MockOne(2);
MockOne *three = new MockOne(3);
MockOne *four = new MockOne(4);
TS_ASSERT_EQUALS(T::one(), 4);
delete three;
TS_ASSERT_EQUALS(T::one(), 4);
delete four;
TS_ASSERT_EQUALS(T::one(), 2);
delete two;
TS_ASSERT_EQUALS(T::one(), 1);
}
void test_Supply()
{
SupplyOne s(2);
TS_ASSERT_EQUALS(supplyOne(), 2);
}
void test_Unimplemented_supply()
{
TS_ASSERT_EQUALS(supplyOne(), 1);
}
void test_More_complex_supply()
{
SupplyThree st(28);
SupplyFour sf(53);
TS_ASSERT_EQUALS(supplyThree(), 28);
int i;
supplyFour(&i);
TS_ASSERT_EQUALS(i, 53);
}
};