Unverified Commit b640d874 by Gennadiy Civil Committed by GitHub

Merge pull request #1557 from pwnall/gmock-fix-ub

Remove multiple inheritance from "unintesting call" mock classes.
parents fdb57f85 1324e2d7
......@@ -83,79 +83,61 @@ $var method=[[$if kind==0 [[AllowUninterestingCalls]]
$elif kind==1 [[WarnUninterestingCalls]]
$else [[FailUninterestingCalls]]]]
namespace internal {
// $clazz[[]]Base serves as a mix-in to establish the "uninteresting call"
// behavior for $clazz on construction. It accomplishes this via CRTP to get
// access to the derived MockClass.
template <class MockClass>
class $clazz[[]]Base {
protected:
$clazz[[]]Base();
~$clazz[[]]Base();
};
} // namespace internal
template <class MockClass>
class $clazz : public MockClass, public internal::$clazz[[]]Base<MockClass> {
class $clazz : public MockClass {
public:
$clazz() : MockClass() {}
$clazz() : MockClass() {
::testing::Mock::$method(
internal::ImplicitCast_<MockClass*>(this));
}
#if GTEST_LANG_CXX11
// Ideally, we would inherit base class's constructors through a using
// declaration, which would preserve their visibility. However, many existing
// tests rely on the fact that current implementation reexports protected
// constructors as public. These tests would need to be cleaned up first.
// Single argument constructor is special-cased so that it can be
// made explicit.
template <typename A>
explicit $clazz(A&& arg) : MockClass(std::forward<A>(arg)) {}
explicit $clazz(A&& arg) : MockClass(std::forward<A>(arg)) {
::testing::Mock::$method(
internal::ImplicitCast_<MockClass*>(this));
}
template <typename A1, typename A2, typename... An>
$clazz(A1&& arg1, A2&& arg2, An&&... args)
: MockClass(std::forward<A1>(arg1), std::forward<A2>(arg2),
std::forward<An>(args)...) {}
std::forward<An>(args)...) {
::testing::Mock::$method(
internal::ImplicitCast_<MockClass*>(this));
}
#else
// C++98 doesn't have variadic templates, so we have to define one
// for each arity.
template <typename A1>
explicit $clazz(const A1& a1) : MockClass(a1) {}
explicit $clazz(const A1& a1) : MockClass(a1) {
::testing::Mock::$method(
internal::ImplicitCast_<MockClass*>(this));
}
$range i 2..n
$for i [[
$range j 1..i
template <$for j, [[typename A$j]]>
$clazz($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) {}
$clazz($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) {
::testing::Mock::$method(
internal::ImplicitCast_<MockClass*>(this));
}
]]
#endif // GTEST_LANG_CXX11
~$clazz() {
::testing::Mock::UnregisterCallReaction(
internal::ImplicitCast_<MockClass*>(this));
}
private:
GTEST_DISALLOW_COPY_AND_ASSIGN_($clazz);
};
namespace internal {
template <typename MockClass>
$clazz[[]]Base<MockClass>::$clazz[[]]Base() {
::testing::Mock::$method(
internal::ImplicitCast_<MockClass*>(
static_cast<$clazz<MockClass> *>(this)));
}
template <typename MockClass>
$clazz[[]]Base<MockClass>::~$clazz[[]]Base() {
::testing::Mock::UnregisterCallReaction(
internal::ImplicitCast_<MockClass*>(
static_cast<$clazz<MockClass>*>(this)));
}
} // namespace internal
]]
// The following specializations catch some (relatively more common)
......
......@@ -103,11 +103,6 @@ class ExpectationTester;
// Base class for function mockers.
template <typename F> class FunctionMockerBase;
// Uninteresting call behavior mixins.
template <typename M> class NiceMockBase;
template <typename M> class NaggyMockBase;
template <typename M> class StrictMockBase;
// Protects the mock object registry (in class Mock), all function
// mockers, and all expectations.
//
......@@ -408,13 +403,13 @@ class GTEST_API_ Mock {
friend class internal::FunctionMockerBase;
template <typename M>
friend class internal::NiceMockBase;
friend class NiceMock;
template <typename M>
friend class internal::NaggyMockBase;
friend class NaggyMock;
template <typename M>
friend class internal::StrictMockBase;
friend class StrictMock;
// Tells Google Mock to allow uninteresting calls on the given mock
// object.
......
......@@ -259,6 +259,13 @@ TEST(NiceMockTest, NonDefaultConstructor10) {
nice_bar.That(5, true);
}
TEST(NiceMockTest, AllowLeak) {
NiceMock<MockFoo>* leaked = new NiceMock<MockFoo>;
Mock::AllowLeak(leaked);
EXPECT_CALL(*leaked, DoThis());
leaked->DoThis();
}
#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
// Tests that NiceMock<Mock> compiles where Mock is a user-defined
// class (as opposed to ::testing::Mock). We had to work around an
......@@ -352,6 +359,13 @@ TEST(NaggyMockTest, NonDefaultConstructor10) {
naggy_bar.That(5, true);
}
TEST(NaggyMockTest, AllowLeak) {
NaggyMock<MockFoo>* leaked = new NaggyMock<MockFoo>;
Mock::AllowLeak(leaked);
EXPECT_CALL(*leaked, DoThis());
leaked->DoThis();
}
#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
// Tests that NaggyMock<Mock> compiles where Mock is a user-defined
// class (as opposed to ::testing::Mock). We had to work around an
......@@ -426,6 +440,13 @@ TEST(StrictMockTest, NonDefaultConstructor10) {
"Uninteresting mock function call");
}
TEST(StrictMockTest, AllowLeak) {
StrictMock<MockFoo>* leaked = new StrictMock<MockFoo>;
Mock::AllowLeak(leaked);
EXPECT_CALL(*leaked, DoThis());
leaked->DoThis();
}
#if !GTEST_OS_SYMBIAN && !GTEST_OS_WINDOWS_MOBILE
// Tests that StrictMock<Mock> compiles where Mock is a user-defined
// class (as opposed to ::testing::Mock). We had to work around an
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment