1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This test is a simple sanity check to make sure gmock is able to build/link
6 // correctly. It just instantiates a mock object and runs through a couple of
7 // the basic mock features.
8
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 // Gmock matchers and actions that we use below.
13 using testing::AnyOf;
14 using testing::Eq;
15 using testing::Return;
16 using testing::SetArgPointee;
17 using testing::WithArg;
18 using testing::_;
19
20 namespace {
21
22 // Simple class that we can mock out the behavior for. Everything is virtual
23 // for easy mocking.
24 class SampleClass {
25 public:
26 SampleClass() = default;
27 virtual ~SampleClass() = default;
28
ReturnSomething()29 virtual int ReturnSomething() {
30 return -1;
31 }
32
ReturnNothingConstly() const33 virtual void ReturnNothingConstly() const {
34 }
35
OutputParam(int * a)36 virtual void OutputParam(int* a) {
37 }
38
ReturnSecond(int a,int b)39 virtual int ReturnSecond(int a, int b) {
40 return b;
41 }
42 };
43
44 // Declare a mock for the class.
45 class MockSampleClass : public SampleClass {
46 public:
47 MOCK_METHOD0(ReturnSomething, int());
48 MOCK_CONST_METHOD0(ReturnNothingConstly, void());
49 MOCK_METHOD1(OutputParam, void(int* a));
50 MOCK_METHOD2(ReturnSecond, int(int a, int b));
51 };
52
53 // Create a couple of custom actions. Custom actions can be used for adding
54 // more complex behavior into your mock...though if you start needing these, ask
55 // if you're asking your mock to do too much.
ACTION(ReturnVal)56 ACTION(ReturnVal) {
57 // Return the first argument received.
58 return arg0;
59 }
ACTION(ReturnSecond)60 ACTION(ReturnSecond) {
61 // Returns the second argument. This basically implemetns ReturnSecond.
62 return arg1;
63 }
64
TEST(GmockTest,SimpleMatchAndActions)65 TEST(GmockTest, SimpleMatchAndActions) {
66 // Basic test of some simple gmock matchers, actions, and cardinality
67 // expectations.
68 MockSampleClass mock;
69
70 EXPECT_CALL(mock, ReturnSomething())
71 .WillOnce(Return(1))
72 .WillOnce(Return(2))
73 .WillOnce(Return(3));
74 EXPECT_EQ(1, mock.ReturnSomething());
75 EXPECT_EQ(2, mock.ReturnSomething());
76 EXPECT_EQ(3, mock.ReturnSomething());
77
78 EXPECT_CALL(mock, ReturnNothingConstly()).Times(2);
79 mock.ReturnNothingConstly();
80 mock.ReturnNothingConstly();
81 }
82
TEST(GmockTest,AssignArgument)83 TEST(GmockTest, AssignArgument) {
84 // Capture an argument for examination.
85 MockSampleClass mock;
86
87 EXPECT_CALL(mock, OutputParam(_)).WillRepeatedly(SetArgPointee<0>(5));
88
89 int arg = 0;
90 mock.OutputParam(&arg);
91 EXPECT_EQ(5, arg);
92 }
93
TEST(GmockTest,SideEffects)94 TEST(GmockTest, SideEffects) {
95 // Capture an argument for examination.
96 MockSampleClass mock;
97
98 EXPECT_CALL(mock, OutputParam(_)).WillRepeatedly(SetArgPointee<0>(5));
99
100 int arg = 0;
101 mock.OutputParam(&arg);
102 EXPECT_EQ(5, arg);
103 }
104
TEST(GmockTest,CustomAction_ReturnSecond)105 TEST(GmockTest, CustomAction_ReturnSecond) {
106 // Test a mock of the ReturnSecond behavior using an action that provides an
107 // alternate implementation of the function. Danger here though, this is
108 // starting to add too much behavior of the mock, which means the mock
109 // implementation might start to have bugs itself.
110 MockSampleClass mock;
111
112 EXPECT_CALL(mock, ReturnSecond(_, AnyOf(Eq(4), Eq(5))))
113 .WillRepeatedly(ReturnSecond());
114 EXPECT_EQ(4, mock.ReturnSecond(-1, 4));
115 EXPECT_EQ(5, mock.ReturnSecond(0, 5));
116 EXPECT_EQ(4, mock.ReturnSecond(0xdeadbeef, 4));
117 EXPECT_EQ(4, mock.ReturnSecond(112358, 4));
118 EXPECT_EQ(5, mock.ReturnSecond(1337, 5));
119 }
120
TEST(GmockTest,CustomAction_ReturnVal)121 TEST(GmockTest, CustomAction_ReturnVal) {
122 // Alternate implemention of ReturnSecond using a more general custom action,
123 // and a WithArg adapter to bridge the interfaces.
124 MockSampleClass mock;
125
126 EXPECT_CALL(mock, ReturnSecond(_, AnyOf(Eq(4), Eq(5))))
127 .WillRepeatedly(WithArg<1>(ReturnVal()));
128 EXPECT_EQ(4, mock.ReturnSecond(-1, 4));
129 EXPECT_EQ(5, mock.ReturnSecond(0, 5));
130 EXPECT_EQ(4, mock.ReturnSecond(0xdeadbeef, 4));
131 EXPECT_EQ(4, mock.ReturnSecond(112358, 4));
132 EXPECT_EQ(5, mock.ReturnSecond(1337, 5));
133 }
134
135 } // namespace
136