-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathGMock.cpp
More file actions
154 lines (121 loc) · 3.8 KB
/
Copy pathGMock.cpp
File metadata and controls
154 lines (121 loc) · 3.8 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
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
//
// Copyright (c) 2016-2017 Kris Jusiak (kris at jusiak dot net)
//
// 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)
//
#include <gtest/gtest.h>
#include <stdexcept>
#include "GUnit/GMake.h"
#include "GUnit/GMock.h"
class interface {
public:
virtual bool get() const = 0;
virtual ~interface() = default;
};
class extended_interface : public interface {
public:
virtual void foo(bool) = 0;
virtual void bar(bool) = 0;
};
class example {
public:
example(const interface& i, extended_interface& ei) : i(i), ei(ei) {}
void update() {
const auto value = i.get();
if (value) {
ei.foo(value);
} else {
ei.bar(value);
}
}
private:
const interface& i;
extended_interface& ei;
};
////////////////////////////////////////////////////////////////////////////////
TEST(GMock, ShouldMockSimpleInterface) {
using namespace testing;
GMock<interface> mock;
EXPECT_CALL(mock, (get)()).WillOnce(Return(true));
EXPECT_TRUE(static_cast<interface&>(mock).get());
}
TEST(GMock, ShouldMockExtendedInterface) {
using namespace testing;
GMock<interface> imock;
GMock<extended_interface> emock;
EXPECT_CALL(imock, (get)()).WillOnce(Return(true));
EXPECT_CALL(emock, (foo)(true)).Times(1);
example e{static_cast<const interface&>(imock),
static_cast<extended_interface&>(emock)};
e.update();
}
TEST(GMock, ShouldMockExtendedInterfaceDifferentMockTypes) {
using namespace testing;
NiceGMock<interface> imock;
StrictGMock<extended_interface> emock;
EXPECT_CALL(imock, (get)()).WillOnce(Return(false));
EXPECT_CALL(emock, (bar)(false)).Times(1);
example e{static_cast<const interface&>(imock),
static_cast<extended_interface&>(emock)};
e.update();
}
TEST(GMock, ShouldWorkWithMacroDefinedMocks) {
using namespace testing;
NiceGMock<interface> imock;
struct mock_extended_interface : extended_interface {
MOCK_CONST_METHOD0(get, bool());
MOCK_METHOD(void, foo, (bool));
MOCK_METHOD(void, bar, (bool));
} emock;
EXPECT_CALL(imock, (get)()).WillOnce(Return(false));
EXPECT_CALL(emock, bar(false)).Times(1);
example e{static_cast<const interface&>(imock),
static_cast<extended_interface&>(emock)};
e.update();
}
TEST(GMock, ShouldMakeExample) {
using namespace testing;
GMock<interface> imock;
GMock<extended_interface> emock;
EXPECT_CALL(imock, (get)()).WillOnce(Return(false));
EXPECT_CALL(emock, (bar)(false)).Times(1);
auto sut = make<example>(imock, emock);
sut.update();
}
TEST(GMock, ShouldMakeUniquePtrExample) {
using namespace testing;
GMock<interface> imock;
GMock<extended_interface> emock;
EXPECT_CALL(imock, (get)()).WillOnce(Return(false));
EXPECT_CALL(emock, (bar)(false)).Times(1);
auto sut = make<std::unique_ptr<example>>(imock, emock);
sut->update();
}
TEST(GMock, ShouldMakeSharedPtrExample) {
using namespace testing;
GMock<interface> imock;
GMock<extended_interface> emock;
EXPECT_CALL(imock, (get)()).WillOnce(Return(false));
EXPECT_CALL(emock, (bar)(false)).Times(1);
auto sut = make<std::shared_ptr<example>>(imock, emock);
sut->update();
}
TEST(GMock, ShouldFailDueToUninterestingGetCall) {
using namespace testing;
StrictGMock<interface> mock;
EXPECT_CALL(mock, (get)())
.WillOnce(Return(true)); // Comment to get an UNINTERESTING CALL
static_cast<interface&>(mock).get();
}
TEST(GMock, ShouldNotCompileWhenMethodParametersDontMatch) {
using namespace testing;
GMock<interface> mock;
// EXPECT_CALL(mock, (get)(_, _)).WillOnce(Return(true)); // COMPILE ERROR
}
TEST(GMock, ShouldNotCompileWhenGMockNotUsedWithBrackets) {
using namespace testing;
GMock<interface> mock;
// EXPECT_CALL(mock, get()).WillOnce(Return(false)); // COMPILE ERROR
}