Commit ed6c9277 by zhanyong.wan

Makes Google Mock compile much faster and use much less memory; reviewed by Nico…

Makes Google Mock compile much faster and use much less memory; reviewed by Nico Weber. This fixes issue 68.
parent b3e90422
......@@ -59,9 +59,6 @@ namespace testing {
namespace internal {
template <typename F>
class MonomorphicDoDefaultActionImpl;
template <typename F1, typename F2>
class ActionAdaptor;
......@@ -255,8 +252,7 @@ class ActionInterface {
typedef typename internal::Function<F>::Result Result;
typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple;
ActionInterface() : is_do_default_(false) {}
ActionInterface() {}
virtual ~ActionInterface() {}
// Performs the action. This method is not const, as in general an
......@@ -265,21 +261,7 @@ class ActionInterface {
// remember the current element.
virtual Result Perform(const ArgumentTuple& args) = 0;
// Returns true iff this is the DoDefault() action.
bool IsDoDefault() const { return is_do_default_; }
private:
template <typename Function>
friend class internal::MonomorphicDoDefaultActionImpl;
// This private constructor is reserved for implementing
// DoDefault(), the default action for a given mock function.
explicit ActionInterface(bool is_do_default)
: is_do_default_(is_do_default) {}
// True iff this action is DoDefault().
const bool is_do_default_;
GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface);
};
......@@ -302,7 +284,8 @@ class Action {
// STL containers.
Action() : impl_(NULL) {}
// Constructs an Action from its implementation.
// Constructs an Action from its implementation. A NULL impl is
// used to represent the "do-default" action.
explicit Action(ActionInterface<F>* impl) : impl_(impl) {}
// Copy constructor.
......@@ -316,7 +299,7 @@ class Action {
explicit Action(const Action<Func>& action);
// Returns true iff this is the DoDefault() action.
bool IsDoDefault() const { return impl_->IsDoDefault(); }
bool IsDoDefault() const { return impl_.get() == NULL; }
// Performs the action. Note that this method is const even though
// the corresponding method in ActionInterface is not. The reason
......@@ -325,6 +308,13 @@ class Action {
// cannot change state. (Think of the difference between a const
// pointer and a pointer to const.)
Result Perform(const ArgumentTuple& args) const {
internal::Assert(
!IsDoDefault(), __FILE__, __LINE__,
"You are using DoDefault() inside a composite action like "
"DoAll() or WithArgs(). This is not supported for technical "
"reasons. Please instead spell out the default action, or "
"assign the default action to an Action variable and use "
"the variable in various places.");
return impl_->Perform(args);
}
......@@ -633,42 +623,13 @@ class ReturnRefOfCopyAction {
GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction);
};
// Implements the DoDefault() action for a particular function type F.
template <typename F>
class MonomorphicDoDefaultActionImpl : public ActionInterface<F> {
public:
typedef typename Function<F>::Result Result;
typedef typename Function<F>::ArgumentTuple ArgumentTuple;
MonomorphicDoDefaultActionImpl() : ActionInterface<F>(true) {}
// For technical reasons, DoDefault() cannot be used inside a
// composite action (e.g. DoAll(...)). It can only be used at the
// top level in an EXPECT_CALL(). If this function is called, the
// user must be using DoDefault() inside a composite action, and we
// have to generate a run-time error.
virtual Result Perform(const ArgumentTuple&) {
Assert(false, __FILE__, __LINE__,
"You are using DoDefault() inside a composite action like "
"DoAll() or WithArgs(). This is not supported for technical "
"reasons. Please instead spell out the default action, or "
"assign the default action to an Action variable and use "
"the variable in various places.");
return internal::Invalid<Result>();
// The above statement will never be reached, but is required in
// order for this function to compile.
}
};
// Implements the polymorphic DoDefault() action.
class DoDefaultAction {
public:
// This template type conversion operator allows DoDefault() to be
// used in any function.
template <typename F>
operator Action<F>() const {
return Action<F>(new MonomorphicDoDefaultActionImpl<F>);
}
operator Action<F>() const { return Action<F>(NULL); }
};
// Implements the Assign action to set a given pointer referent to a
......
// This file was GENERATED by a script. DO NOT EDIT BY HAND!!!
// This file was GENERATED by command:
// pump.py gmock-generated-function-mockers.h.pump
// DO NOT EDIT BY HAND!!!
// Copyright 2007, Google Inc.
// All rights reserved.
......@@ -352,7 +354,8 @@ using internal::FunctionMocker;
} \
::testing::MockSpec<F>& \
gmock_##Method() constness { \
return GMOCK_MOCKER_(0, constness, Method).RegisterOwner(this).With(); \
GMOCK_MOCKER_(0, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_(0, constness, Method).With(); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_(0, constness, Method)
......@@ -367,8 +370,8 @@ using internal::FunctionMocker;
} \
::testing::MockSpec<F>& \
gmock_##Method(GMOCK_MATCHER_(tn, F, 1) gmock_a1) constness { \
return GMOCK_MOCKER_(1, constness, \
Method).RegisterOwner(this).With(gmock_a1); \
GMOCK_MOCKER_(1, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_(1, constness, Method).With(gmock_a1); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_(1, constness, Method)
......@@ -385,8 +388,8 @@ using internal::FunctionMocker;
::testing::MockSpec<F>& \
gmock_##Method(GMOCK_MATCHER_(tn, F, 1) gmock_a1, \
GMOCK_MATCHER_(tn, F, 2) gmock_a2) constness { \
return GMOCK_MOCKER_(2, constness, \
Method).RegisterOwner(this).With(gmock_a1, gmock_a2); \
GMOCK_MOCKER_(2, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_(2, constness, Method).With(gmock_a1, gmock_a2); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_(2, constness, Method)
......@@ -406,8 +409,9 @@ using internal::FunctionMocker;
gmock_##Method(GMOCK_MATCHER_(tn, F, 1) gmock_a1, \
GMOCK_MATCHER_(tn, F, 2) gmock_a2, \
GMOCK_MATCHER_(tn, F, 3) gmock_a3) constness { \
return GMOCK_MOCKER_(3, constness, \
Method).RegisterOwner(this).With(gmock_a1, gmock_a2, gmock_a3); \
GMOCK_MOCKER_(3, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_(3, constness, Method).With(gmock_a1, gmock_a2, \
gmock_a3); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_(3, constness, Method)
......@@ -429,9 +433,9 @@ using internal::FunctionMocker;
GMOCK_MATCHER_(tn, F, 2) gmock_a2, \
GMOCK_MATCHER_(tn, F, 3) gmock_a3, \
GMOCK_MATCHER_(tn, F, 4) gmock_a4) constness { \
return GMOCK_MOCKER_(4, constness, \
Method).RegisterOwner(this).With(gmock_a1, gmock_a2, gmock_a3, \
gmock_a4); \
GMOCK_MOCKER_(4, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_(4, constness, Method).With(gmock_a1, gmock_a2, \
gmock_a3, gmock_a4); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_(4, constness, Method)
......@@ -455,9 +459,9 @@ using internal::FunctionMocker;
GMOCK_MATCHER_(tn, F, 3) gmock_a3, \
GMOCK_MATCHER_(tn, F, 4) gmock_a4, \
GMOCK_MATCHER_(tn, F, 5) gmock_a5) constness { \
return GMOCK_MOCKER_(5, constness, \
Method).RegisterOwner(this).With(gmock_a1, gmock_a2, gmock_a3, \
gmock_a4, gmock_a5); \
GMOCK_MOCKER_(5, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_(5, constness, Method).With(gmock_a1, gmock_a2, \
gmock_a3, gmock_a4, gmock_a5); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_(5, constness, Method)
......@@ -483,9 +487,9 @@ using internal::FunctionMocker;
GMOCK_MATCHER_(tn, F, 4) gmock_a4, \
GMOCK_MATCHER_(tn, F, 5) gmock_a5, \
GMOCK_MATCHER_(tn, F, 6) gmock_a6) constness { \
return GMOCK_MOCKER_(6, constness, \
Method).RegisterOwner(this).With(gmock_a1, gmock_a2, gmock_a3, \
gmock_a4, gmock_a5, gmock_a6); \
GMOCK_MOCKER_(6, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_(6, constness, Method).With(gmock_a1, gmock_a2, \
gmock_a3, gmock_a4, gmock_a5, gmock_a6); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_(6, constness, Method)
......@@ -513,9 +517,9 @@ using internal::FunctionMocker;
GMOCK_MATCHER_(tn, F, 5) gmock_a5, \
GMOCK_MATCHER_(tn, F, 6) gmock_a6, \
GMOCK_MATCHER_(tn, F, 7) gmock_a7) constness { \
return GMOCK_MOCKER_(7, constness, \
Method).RegisterOwner(this).With(gmock_a1, gmock_a2, gmock_a3, \
gmock_a4, gmock_a5, gmock_a6, gmock_a7); \
GMOCK_MOCKER_(7, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_(7, constness, Method).With(gmock_a1, gmock_a2, \
gmock_a3, gmock_a4, gmock_a5, gmock_a6, gmock_a7); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_(7, constness, Method)
......@@ -545,9 +549,9 @@ using internal::FunctionMocker;
GMOCK_MATCHER_(tn, F, 6) gmock_a6, \
GMOCK_MATCHER_(tn, F, 7) gmock_a7, \
GMOCK_MATCHER_(tn, F, 8) gmock_a8) constness { \
return GMOCK_MOCKER_(8, constness, \
Method).RegisterOwner(this).With(gmock_a1, gmock_a2, gmock_a3, \
gmock_a4, gmock_a5, gmock_a6, gmock_a7, gmock_a8); \
GMOCK_MOCKER_(8, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_(8, constness, Method).With(gmock_a1, gmock_a2, \
gmock_a3, gmock_a4, gmock_a5, gmock_a6, gmock_a7, gmock_a8); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_(8, constness, Method)
......@@ -580,9 +584,10 @@ using internal::FunctionMocker;
GMOCK_MATCHER_(tn, F, 7) gmock_a7, \
GMOCK_MATCHER_(tn, F, 8) gmock_a8, \
GMOCK_MATCHER_(tn, F, 9) gmock_a9) constness { \
return GMOCK_MOCKER_(9, constness, \
Method).RegisterOwner(this).With(gmock_a1, gmock_a2, gmock_a3, \
gmock_a4, gmock_a5, gmock_a6, gmock_a7, gmock_a8, gmock_a9); \
GMOCK_MOCKER_(9, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_(9, constness, Method).With(gmock_a1, gmock_a2, \
gmock_a3, gmock_a4, gmock_a5, gmock_a6, gmock_a7, gmock_a8, \
gmock_a9); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_(9, constness, Method)
......@@ -617,9 +622,9 @@ using internal::FunctionMocker;
GMOCK_MATCHER_(tn, F, 8) gmock_a8, \
GMOCK_MATCHER_(tn, F, 9) gmock_a9, \
GMOCK_MATCHER_(tn, F, 10) gmock_a10) constness { \
return GMOCK_MOCKER_(10, constness, \
Method).RegisterOwner(this).With(gmock_a1, gmock_a2, gmock_a3, \
gmock_a4, gmock_a5, gmock_a6, gmock_a7, gmock_a8, gmock_a9, \
GMOCK_MOCKER_(10, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_(10, constness, Method).With(gmock_a1, gmock_a2, \
gmock_a3, gmock_a4, gmock_a5, gmock_a6, gmock_a7, gmock_a8, gmock_a9, \
gmock_a10); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_(10, constness, Method)
......
......@@ -140,7 +140,8 @@ $var matcher_as = [[$for j, \
} \
::testing::MockSpec<F>& \
gmock_##Method($matcher_as) constness { \
return GMOCK_MOCKER_($i, constness, Method).RegisterOwner(this).With($as); \
GMOCK_MOCKER_($i, constness, Method).RegisterOwner(this); \
return GMOCK_MOCKER_($i, constness, Method).With($as); \
} \
mutable ::testing::FunctionMocker<F> GMOCK_MOCKER_($i, constness, Method)
......
......@@ -261,8 +261,8 @@ def _IncompleteByReferenceArgumentDiagnoser(msg):
r'(.*\n)*?' +
_CLANG_NON_GMOCK_FILE_LINE_RE +
r'note: in instantiation of member function '
r'\'testing::internal::FunctionMocker<.*>::Invoke\' '
r'requested here')
r'\'testing::internal2::TypeWithoutFormatter<.*>::'
r'PrintValue\' requested here')
diagnosis = """
In order to mock this function, Google Mock needs to see the definition
of type "%(type)s" - declaration alone is not enough. Either #include
......
......@@ -334,8 +334,7 @@ class MyActionImpl : public ActionInterface<MyFunction> {
TEST(ActionInterfaceTest, CanBeImplementedByDefiningPerform) {
MyActionImpl my_action_impl;
EXPECT_FALSE(my_action_impl.IsDoDefault());
(void)my_action_impl;
}
TEST(ActionInterfaceTest, MakeAction) {
......
......@@ -103,6 +103,34 @@ using testing::internal::CaptureStdout;
using testing::internal::GetCapturedStdout;
#endif
class Incomplete;
class MockIncomplete {
public:
// This line verifies that a mock method can take a by-reference
// argument of an incomplete type.
MOCK_METHOD1(ByRefFunc, void(const Incomplete& x));
};
// Tells Google Mock how to print a value of type Incomplete.
void PrintTo(const Incomplete& x, ::std::ostream* os);
TEST(MockMethodTest, CanInstantiateWithIncompleteArgType) {
// Even though this mock class contains a mock method that takes
// by-reference an argument whose type is incomplete, we can still
// use the mock, as long as Google Mock knows how to print the
// argument.
MockIncomplete incomplete;
EXPECT_CALL(incomplete, ByRefFunc(_))
.Times(AnyNumber());
}
// The definition of the printer for the argument type doesn't have to
// be visible where the mock is used.
void PrintTo(const Incomplete& /* x */, ::std::ostream* os) {
*os << "incomplete";
}
class Result {};
class MockA {
......@@ -1327,12 +1355,33 @@ TEST(SequenceTest, Retirement) {
TEST(ExpectationTest, ConstrutorsWork) {
MockA a;
Expectation e1; // Default ctor.
Expectation e2 = EXPECT_CALL(a, DoA(1)); // Ctor from EXPECT_CALL.
Expectation e3 = e2; // Copy ctor.
// Ctor from various forms of EXPECT_CALL.
Expectation e2 = EXPECT_CALL(a, DoA(2));
Expectation e3 = EXPECT_CALL(a, DoA(3)).With(_);
{
Sequence s;
Expectation e4 = EXPECT_CALL(a, DoA(4)).Times(1);
Expectation e5 = EXPECT_CALL(a, DoA(5)).InSequence(s);
}
Expectation e6 = EXPECT_CALL(a, DoA(6)).After(e2);
Expectation e7 = EXPECT_CALL(a, DoA(7)).WillOnce(Return());
Expectation e8 = EXPECT_CALL(a, DoA(8)).WillRepeatedly(Return());
Expectation e9 = EXPECT_CALL(a, DoA(9)).RetiresOnSaturation();
Expectation e10 = e2; // Copy ctor.
EXPECT_THAT(e1, Ne(e2));
EXPECT_THAT(e2, Eq(e3));
a.DoA(1);
EXPECT_THAT(e2, Eq(e10));
a.DoA(2);
a.DoA(3);
a.DoA(4);
a.DoA(5);
a.DoA(6);
a.DoA(7);
a.DoA(8);
a.DoA(9);
}
TEST(ExpectationTest, AssignmentWorks) {
......
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