From 5290aaa39b51aa27d3f64fba3bc5fff6a08e3a26 Mon Sep 17 00:00:00 2001 From: "ajwong@chromium.org" Date: Thu, 28 May 2009 06:02:56 +0000 Subject: Retry to checkin a version of gmock, modified to use our boost_tuple in VS2005. This checkin adds gmock, and a small example of how to write a gmock-based unittest. Original Review URL: http://codereview.chromium.org/113807 Review URL: http://codereview.chromium.org/115846 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@17066 0039d316-1c4b-4281-b951-d872f2087c98 --- base/base.gyp | 12 +++-- base/gmock_unittest.cc | 137 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 5 deletions(-) create mode 100644 base/gmock_unittest.cc (limited to 'base') diff --git a/base/base.gyp b/base/base.gyp index aa168c1..a294726 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -590,16 +590,12 @@ 'type': 'executable', 'msvs_guid': '27A30967-4BBA-48D1-8522-CDE95F7B1CEC', 'sources': [ - 'crypto/signature_verifier_unittest.cc', - 'gfx/jpeg_codec_unittest.cc', - 'gfx/native_theme_unittest.cc', - 'gfx/png_codec_unittest.cc', - 'gfx/rect_unittest.cc', 'at_exit_unittest.cc', 'atomicops_unittest.cc', 'clipboard_unittest.cc', 'command_line_unittest.cc', 'condition_variable_unittest.cc', + 'crypto/signature_verifier_unittest.cc', 'data_pack_unittest.cc', 'debug_util_unittest.cc', 'directory_watcher_unittest.cc', @@ -608,6 +604,11 @@ 'file_path_unittest.cc', 'file_util_unittest.cc', 'file_version_info_unittest.cc', + 'gfx/jpeg_codec_unittest.cc', + 'gfx/native_theme_unittest.cc', + 'gfx/png_codec_unittest.cc', + 'gfx/rect_unittest.cc', + 'gmock_unittest.cc', 'histogram_unittest.cc', 'hmac_unittest.cc', 'idletimer_unittest.cc', @@ -673,6 +674,7 @@ 'base', 'base_gfx', '../skia/skia.gyp:skia', + '../testing/gmock.gyp:gmock', '../testing/gtest.gyp:gtest', ], 'conditions': [ diff --git a/base/gmock_unittest.cc b/base/gmock_unittest.cc new file mode 100644 index 0000000..855380a --- /dev/null +++ b/base/gmock_unittest.cc @@ -0,0 +1,137 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. +// +// This test is a simple sanity check to make sure gmock is able to build/link +// correctly. It just instantiates a mock object and runs through a couple of +// the basic mock features. + +#include "testing/gmock/include/gmock/gmock.h" +#include "testing/gtest/include/gtest/gtest.h" + +// Gmock matchers and actions that we use below. +using testing::AnyOf; +using testing::Eq; +using testing::Return; +using testing::SetArgumentPointee; +using testing::WithArg; +using testing::_; + +namespace { + +// Simple class that we can mock out the behavior for. Everything is virtual +// for easy mocking. +class SampleClass { + public: + SampleClass() {} + virtual ~SampleClass() {} + + virtual int ReturnSomething() { + return -1; + } + + virtual void ReturnNothingConstly() const { + } + + virtual void OutputParam(int* a) { + } + + virtual int ReturnSecond(int a, int b) { + return b; + } +}; + +// Declare a mock for the class. +class MockSampleClass : public SampleClass { + public: + MOCK_METHOD0(ReturnSomething, int()); + MOCK_CONST_METHOD0(ReturnNothingConstly, void()); + MOCK_METHOD1(OutputParam, void(int* a)); + MOCK_METHOD2(ReturnSecond, int(int a, int b)); +}; + +// Create a couple of custom actions. Custom actions can be used for adding +// more complex behavior into your mock...though if you start needing these, ask +// if you're asking your mock to do too much. +ACTION(ReturnVal) { + // Return the first argument received. + return arg0; +} +ACTION(ReturnSecond) { + // Returns the second argument. This basically implemetns ReturnSecond. + return arg1; +} + +TEST(GmockTest, SimpleMatchAndActions) { + // Basic test of some simple gmock matchers, actions, and cardinality + // expectations. + MockSampleClass mock; + + EXPECT_CALL(mock, ReturnSomething()) + .WillOnce(Return(1)) + .WillOnce(Return(2)) + .WillOnce(Return(3)); + EXPECT_EQ(1, mock.ReturnSomething()); + EXPECT_EQ(2, mock.ReturnSomething()); + EXPECT_EQ(3, mock.ReturnSomething()); + + EXPECT_CALL(mock, ReturnNothingConstly()).Times(2); + mock.ReturnNothingConstly(); + mock.ReturnNothingConstly(); +} + +TEST(GmockTest, AssignArgument) { + // Capture an argument for examination. + MockSampleClass mock; + + EXPECT_CALL(mock, OutputParam(_)) + .WillRepeatedly(SetArgumentPointee<0>(5)); + + int arg = 0; + mock.OutputParam(&arg); + EXPECT_EQ(5, arg); +} + +TEST(GmockTest, SideEffects) { + // Capture an argument for examination. + MockSampleClass mock; + + EXPECT_CALL(mock, OutputParam(_)) + .WillRepeatedly(SetArgumentPointee<0>(5)); + + int arg = 0; + mock.OutputParam(&arg); + EXPECT_EQ(5, arg); +} + +TEST(GmockTest, CustomAction_ReturnSecond) { + // Test a mock of the ReturnSecond behavior using an action that provides an + // alternate implementation of the function. Danger here though, this is + // starting to add too much behavior of the mock, which means the mock + // implementation might start to have bugs itself. + MockSampleClass mock; + + EXPECT_CALL(mock, ReturnSecond(_, AnyOf(Eq(4), Eq(5)))) + .WillRepeatedly(ReturnSecond()); + EXPECT_EQ(4, mock.ReturnSecond(-1, 4)); + EXPECT_EQ(5, mock.ReturnSecond(0, 5)); + EXPECT_EQ(4, mock.ReturnSecond(0xdeadbeef, 4)); + EXPECT_EQ(4, mock.ReturnSecond(112358, 4)); + EXPECT_EQ(5, mock.ReturnSecond(1337, 5)); +} + +TEST(GmockTest, CustomAction_ReturnVal) { + // Alternate implemention of ReturnSecond using a more general custom action, + // and a WithArg adapter to bridge the interfaces. + MockSampleClass mock; + + EXPECT_CALL(mock, ReturnSecond(_, AnyOf(Eq(4), Eq(5)))) + .WillRepeatedly(WithArg<1>(ReturnVal())); + EXPECT_EQ(4, mock.ReturnSecond(-1, 4)); + EXPECT_EQ(5, mock.ReturnSecond(0, 5)); + EXPECT_EQ(4, mock.ReturnSecond(0xdeadbeef, 4)); + EXPECT_EQ(4, mock.ReturnSecond(112358, 4)); + EXPECT_EQ(5, mock.ReturnSecond(1337, 5)); +} + +} // namespace -- cgit v1.1