1 // Copyright 2009 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
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.
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 // Gmock matchers and actions that we use below.
15 using testing::Return;
16 using testing::SetArgPointee;
17 using testing::WithArg;
22 // Simple class that we can mock out the behavior for. Everything is virtual
26 SampleClass() = default;
27 virtual ~SampleClass() = default;
29 virtual int ReturnSomething() {
33 virtual void ReturnNothingConstly() const {
36 virtual void OutputParam(int* a) {
39 virtual int ReturnSecond(int a, int b) {
44 // Declare a mock for the class.
45 class MockSampleClass : public SampleClass {
47 MOCK_METHOD(int, ReturnSomething, ());
48 MOCK_METHOD(void, ReturnNothingConstly, (), (const));
49 MOCK_METHOD(void, OutputParam, (int* a));
50 MOCK_METHOD(int, ReturnSecond, (int a, int b));
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.
57 // Return the first argument received.
60 ACTION(ReturnSecond) {
61 // Returns the second argument. This basically implemetns ReturnSecond.
65 TEST(GmockTest, SimpleMatchAndActions) {
66 // Basic test of some simple gmock matchers, actions, and cardinality
70 EXPECT_CALL(mock, ReturnSomething())
74 EXPECT_EQ(1, mock.ReturnSomething());
75 EXPECT_EQ(2, mock.ReturnSomething());
76 EXPECT_EQ(3, mock.ReturnSomething());
78 EXPECT_CALL(mock, ReturnNothingConstly()).Times(2);
79 mock.ReturnNothingConstly();
80 mock.ReturnNothingConstly();
83 TEST(GmockTest, AssignArgument) {
84 // Capture an argument for examination.
87 EXPECT_CALL(mock, OutputParam(_)).WillRepeatedly(SetArgPointee<0>(5));
90 mock.OutputParam(&arg);
94 TEST(GmockTest, SideEffects) {
95 // Capture an argument for examination.
98 EXPECT_CALL(mock, OutputParam(_)).WillRepeatedly(SetArgPointee<0>(5));
101 mock.OutputParam(&arg);
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;
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));
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;
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));