blob: da8dd94ea39cc5b201ea939459f2371795eaadc7 [file] [log] [blame]
[email protected]5290aaa2009-05-28 06:02:561// 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.
13using testing::AnyOf;
14using testing::Eq;
15using testing::Return;
thestige147d682017-05-05 07:31:3116using testing::SetArgPointee;
[email protected]5290aaa2009-05-28 06:02:5617using testing::WithArg;
18using testing::_;
19
20namespace {
21
22// Simple class that we can mock out the behavior for. Everything is virtual
23// for easy mocking.
24class SampleClass {
25 public:
26 SampleClass() {}
27 virtual ~SampleClass() {}
28
29 virtual int ReturnSomething() {
30 return -1;
31 }
32
33 virtual void ReturnNothingConstly() const {
34 }
35
36 virtual void OutputParam(int* a) {
37 }
38
39 virtual int ReturnSecond(int a, int b) {
40 return b;
41 }
42};
43
44// Declare a mock for the class.
45class 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.
56ACTION(ReturnVal) {
57 // Return the first argument received.
58 return arg0;
59}
60ACTION(ReturnSecond) {
61 // Returns the second argument. This basically implemetns ReturnSecond.
62 return arg1;
63}
64
65TEST(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
83TEST(GmockTest, AssignArgument) {
84 // Capture an argument for examination.
85 MockSampleClass mock;
86
thestige147d682017-05-05 07:31:3187 EXPECT_CALL(mock, OutputParam(_)).WillRepeatedly(SetArgPointee<0>(5));
[email protected]5290aaa2009-05-28 06:02:5688
89 int arg = 0;
90 mock.OutputParam(&arg);
91 EXPECT_EQ(5, arg);
92}
93
94TEST(GmockTest, SideEffects) {
95 // Capture an argument for examination.
96 MockSampleClass mock;
97
thestige147d682017-05-05 07:31:3198 EXPECT_CALL(mock, OutputParam(_)).WillRepeatedly(SetArgPointee<0>(5));
[email protected]5290aaa2009-05-28 06:02:5699
100 int arg = 0;
101 mock.OutputParam(&arg);
102 EXPECT_EQ(5, arg);
103}
104
105TEST(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
121TEST(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