Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / base / gmock_unittest.cc
blob855380a97596903f1571739b884b29bc545cc04b
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.
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.
13 using testing::AnyOf;
14 using testing::Eq;
15 using testing::Return;
16 using testing::SetArgumentPointee;
17 using testing::WithArg;
18 using testing::_;
20 namespace {
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() {}
27 virtual ~SampleClass() {}
29 virtual int ReturnSomething() {
30 return -1;
33 virtual void ReturnNothingConstly() const {
36 virtual void OutputParam(int* a) {
39 virtual int ReturnSecond(int a, int b) {
40 return b;
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));
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.
56 ACTION(ReturnVal) {
57 // Return the first argument received.
58 return arg0;
60 ACTION(ReturnSecond) {
61 // Returns the second argument. This basically implemetns ReturnSecond.
62 return arg1;
65 TEST(GmockTest, SimpleMatchAndActions) {
66 // Basic test of some simple gmock matchers, actions, and cardinality
67 // expectations.
68 MockSampleClass mock;
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());
78 EXPECT_CALL(mock, ReturnNothingConstly()).Times(2);
79 mock.ReturnNothingConstly();
80 mock.ReturnNothingConstly();
83 TEST(GmockTest, AssignArgument) {
84 // Capture an argument for examination.
85 MockSampleClass mock;
87 EXPECT_CALL(mock, OutputParam(_))
88 .WillRepeatedly(SetArgumentPointee<0>(5));
90 int arg = 0;
91 mock.OutputParam(&arg);
92 EXPECT_EQ(5, arg);
95 TEST(GmockTest, SideEffects) {
96 // Capture an argument for examination.
97 MockSampleClass mock;
99 EXPECT_CALL(mock, OutputParam(_))
100 .WillRepeatedly(SetArgumentPointee<0>(5));
102 int arg = 0;
103 mock.OutputParam(&arg);
104 EXPECT_EQ(5, 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));
137 } // namespace