-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgmock_example.cpp
More file actions
52 lines (43 loc) · 1.13 KB
/
Copy pathgmock_example.cpp
File metadata and controls
52 lines (43 loc) · 1.13 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
// This example comes from:
// https://code.google.com/p/googlemock/wiki/V1_7_ForDummies
#include <gmock/gmock.h>
#include <gtest/gtest.h>
class Turtle {
public:
virtual ~Turtle() {}
virtual void PenUp() = 0;
virtual void PenDown() = 0;
virtual void Forward(int distance) = 0;
virtual void Turn(int degrees) = 0;
virtual void GoTo(int x, int y) = 0;
virtual int GetX() const = 0;
virtual int GetY() const = 0;
};
class Painter {
public:
Painter(Turtle *turtle) : _turtle(turtle) { }
bool DrawCircle(int, int, int) {
_turtle->PenDown();
return true;
}
private:
Turtle *_turtle;
};
class MockTurtle : public Turtle {
public:
MOCK_METHOD0(PenUp, void());
MOCK_METHOD0(PenDown, void());
MOCK_METHOD1(Forward, void(int distance));
MOCK_METHOD1(Turn, void(int degrees));
MOCK_METHOD2(GoTo, void(int x, int y));
MOCK_CONST_METHOD0(GetX, int());
MOCK_CONST_METHOD0(GetY, int());
};
using ::testing::AtLeast;
TEST(PainterTest, CanDrawSomething) {
MockTurtle turtle;
EXPECT_CALL(turtle, PenDown())
.Times(AtLeast(1));
Painter painter(&turtle);
EXPECT_TRUE(painter.DrawCircle(0, 0, 10));
}