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.
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::SetArgumentPointee;
17 using testing::WithArg;
22 // Simple class that we can mock out the behavior for. Everything is virtual
27 virtual ~SampleClass() {}
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_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));
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(_))
88 .WillRepeatedly(SetArgumentPointee<0>(5));
91 mock.OutputParam(&arg);
95 TEST(GmockTest, SideEffects) {
96 // Capture an argument for examination.
99 EXPECT_CALL(mock, OutputParam(_))
100 .WillRepeatedly(SetArgumentPointee<0>(5));
103 mock.OutputParam(&arg);
107 TEST(GmockTest, CustomAction_ReturnSecond) {
108 // Test a mock of the ReturnSecond behavior using an action that provides an
109 // alternate implementation of the function. Danger here though, this is
110 // starting to add too much behavior of the mock, which means the mock
111 // implementation might start to have bugs itself.
112 MockSampleClass mock;
114 EXPECT_CALL(mock, ReturnSecond(_, AnyOf(Eq(4), Eq(5))))
115 .WillRepeatedly(ReturnSecond());
116 EXPECT_EQ(4, mock.ReturnSecond(-1, 4));
117 EXPECT_EQ(5, mock.ReturnSecond(0, 5));
118 EXPECT_EQ(4, mock.ReturnSecond(0xdeadbeef, 4));
119 EXPECT_EQ(4, mock.ReturnSecond(112358, 4));
120 EXPECT_EQ(5, mock.ReturnSecond(1337, 5));
123 TEST(GmockTest, CustomAction_ReturnVal) {
124 // Alternate implemention of ReturnSecond using a more general custom action,
125 // and a WithArg adapter to bridge the interfaces.
126 MockSampleClass mock;
128 EXPECT_CALL(mock, ReturnSecond(_, AnyOf(Eq(4), Eq(5))))
129 .WillRepeatedly(WithArg<1>(ReturnVal()));
130 EXPECT_EQ(4, mock.ReturnSecond(-1, 4));
131 EXPECT_EQ(5, mock.ReturnSecond(0, 5));
132 EXPECT_EQ(4, mock.ReturnSecond(0xdeadbeef, 4));
133 EXPECT_EQ(4, mock.ReturnSecond(112358, 4));
134 EXPECT_EQ(5, mock.ReturnSecond(1337, 5));