diff options
author | Daniel Burrows <dburrows@debian.org> | 2010-07-10 12:06:10 -0700 |
---|---|---|
committer | Daniel Burrows <dburrows@debian.org> | 2010-07-10 12:06:10 -0700 |
commit | 19f2defc238845957ee00406da93c4cff538007b (patch) | |
tree | 1fde4dfa49f09f0923dd59b39c4d0b848c7fe46e | |
parent | 5eee1e057cf9423774c76eee31cfeaf2a1909f2d (diff) | |
download | aptitude-19f2defc238845957ee00406da93c4cff538007b.tar.gz |
Add a utility class that makes it easy to use strict/nice mocks.
The idea is to formalize the friending/named constructor patterns
used in the existing mocks, and extend them to support creating
nice and strict mocks in addition to the default ones.
I have deliberately changed the default mock constructor
from create() to create_default(); I want it to be explicit
which variant is being used, and I also want existing code
to fail so I can update it. Strict mocks should really be
the default in most cases.
-rw-r--r-- | src/generic/util/mocks/Makefile.am | 2 | ||||
-rw-r--r-- | src/generic/util/mocks/SConscript | 2 | ||||
-rw-r--r-- | src/generic/util/mocks/mock_util.h | 93 |
3 files changed, 95 insertions, 2 deletions
diff --git a/src/generic/util/mocks/Makefile.am b/src/generic/util/mocks/Makefile.am index 6b88c48d..67c8ed5f 100644 --- a/src/generic/util/mocks/Makefile.am +++ b/src/generic/util/mocks/Makefile.am @@ -1 +1 @@ -EXTRA_DIST = throttle.h
\ No newline at end of file +EXTRA_DIST = mock_util.h throttle.h
\ No newline at end of file diff --git a/src/generic/util/mocks/SConscript b/src/generic/util/mocks/SConscript index 025e0f15..fffd9451 100644 --- a/src/generic/util/mocks/SConscript +++ b/src/generic/util/mocks/SConscript @@ -1,3 +1,3 @@ Import('programs_env') -programs_env.Dist('throttle.h') +programs_env.Dist('mock_util.h', 'throttle.h') diff --git a/src/generic/util/mocks/mock_util.h b/src/generic/util/mocks/mock_util.h new file mode 100644 index 00000000..e8cf4d9e --- /dev/null +++ b/src/generic/util/mocks/mock_util.h @@ -0,0 +1,93 @@ +// \file mock_util.h -*-c++-*- +// +// Copyright (C) 2010 Daniel Burrows +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License as +// published by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, but +// WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; see the file COPYING. If not, write to +// the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +// Boston, MA 02111-1307, USA. + +#ifndef APTITUDE_GENERIC_MOCK_UTIL_H +#define APTITUDE_GENERIC_MOCK_UTIL_H + +// System includes: +#include <boost/make_shared.hpp> +#include <boost/shared_ptr.hpp> + +#include <gmock/gmock.h> + +namespace aptitude +{ + namespace util + { + namespace mocks + { + /** \brief Convenience mixin that instruments a mock class with + * a standard set of static constructors: one for nice mocks, + * one for default mocks, and one for strict mocks. + * + * \tparam SubMockType The class to instrument; must be a subclass + * of this class. Must be default-constructable. + * + * If the constructor of MockType is hidden, then + * NiceMock<MockType> and StrictMock<MockType> must be friends + * of Mock (so that they can subclass it). See MOCK_FRIENDS() + * for a convenient way to declare this. + */ + template<typename SubMockType> + class Mock + { + public: + virtual ~Mock() + { + } + + typedef SubMockType MockType; + typedef testing::NiceMock<MockType> NiceMockType; + typedef testing::StrictMock<MockType> StrictMockType; + + /** \brief Create a default-style mock. */ + static boost::shared_ptr<MockType> create_default() + { + return boost::make_shared<MockType>(); + } + + /** \brief Create a nice mock. */ + static boost::shared_ptr<MockType> create_nice() + { + return boost::make_shared<NiceMockType>(); + } + + /** \brief Create a strict mock. */ + static boost::shared_ptr<MockType> create_strict() + { + return boost::make_shared<StrictMockType>(); + } + }; + + /** \brief Convenience macro to support mocks with hidden + * constructors. + * + * This expands to the friend declarations that are required + * for Mock to function. It must be invoked in the class body + * of a subclass of Mock that does not declare the name + * MockType. + */ +#define MOCK_FRIENDS() \ + friend class testing::NiceMock<MockType>; \ + friend class testing::StrictMock<MockType>; + } + } +} + +#endif // APTITUDE_GENERIC_MOCK_UTIL_H |