diff options
author | nsylvain@chromium.org <nsylvain@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-03 20:47:21 +0000 |
---|---|---|
committer | nsylvain@chromium.org <nsylvain@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-03 20:47:21 +0000 |
commit | c836d7a0a05a8e5326494dcbd6c1bc73ad496c20 (patch) | |
tree | f9fa326e901385dfbfd19e7d488dc86b9ceca310 | |
parent | b8913f0d456e385a470e4ec157978eacd8a91a91 (diff) | |
download | chromium_src-c836d7a0a05a8e5326494dcbd6c1bc73ad496c20.zip chromium_src-c836d7a0a05a8e5326494dcbd6c1bc73ad496c20.tar.gz chromium_src-c836d7a0a05a8e5326494dcbd6c1bc73ad496c20.tar.bz2 |
Update gmock and gtest.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27953 0039d316-1c4b-4281-b951-d872f2087c98
55 files changed, 2976 insertions, 2013 deletions
@@ -19,7 +19,7 @@ deps = { "http://open-vcdiff.googlecode.com/svn/trunk@26", "src/testing/gtest": - "http://googletest.googlecode.com/svn/trunk@267", + "http://googletest.googlecode.com/svn/trunk@329", "src/third_party/WebKit": "/trunk/deps/third_party/WebKit@27313", diff --git a/testing/README.chromium b/testing/README.chromium index ed305ee..2fd8517 100644 --- a/testing/README.chromium +++ b/testing/README.chromium @@ -1,7 +1,7 @@ We include a snapshot of gmock from http://googlemock.googlecode.com/svn/trunk with chromium.patch applied. -Current revision: 173 +Current revision: 222 -- HOW TO USE -- diff --git a/testing/gmock/CHANGES b/testing/gmock/CHANGES index 8b62def..f703ac2 100644 --- a/testing/gmock/CHANGES +++ b/testing/gmock/CHANGES @@ -1,3 +1,28 @@ +Changes for 1.4.0 (we skipped 1.2.* and 1.3.* to match the version of +Google Test): + + * Works in more environments: Symbian and minGW, Visual C++ 7.1. + * Lighter weight: comes with our own implementation of TR1 tuple (no + more dependency on Boost!). + * New feature: --gmock_catch_leaked_mocks for detecting leaked mocks. + * New feature: ACTION_TEMPLATE for defining templatized actions. + * New feature: the .After() clause for specifying expectation order. + * New feature: the .With() clause for for specifying inter-argument + constraints. + * New feature: actions ReturnArg<k>(), ReturnNew<T>(...), and + DeleteArg<k>(). + * New feature: matchers Key(), Pair(), Args<...>(), AllArgs(), IsNull(), + and Contains(). + * New feature: utility class MockFunction<F>, useful for checkpoints, etc. + * New feature: functions Value(x, m) and SafeMatcherCast<T>(m). + * New feature: copying a mock object is rejected at compile time. + * New feature: a script for fusing all Google Mock and Google Test + source files for easy deployment. + * Improved the Google Mock doctor to diagnose more diseases. + * Improved the Google Mock generator script. + * Compatibility fixes for Mac OS X and gcc. + * Bug fixes and implementation clean-ups. + Changes for 1.1.0: * New feature: ability to use Google Mock with any testing framework. @@ -7,7 +32,7 @@ Changes for 1.1.0: * New feature: actions for accessing function arguments and throwing exceptions. * Improved the Google Mock doctor script for diagnosing compiler errors. - * Bug fixes and implementation clean-up. + * Bug fixes and implementation clean-ups. Changes for 1.0.0: diff --git a/testing/gmock/Makefile.am b/testing/gmock/Makefile.am index 196b927..e176bbe 100644 --- a/testing/gmock/Makefile.am +++ b/testing/gmock/Makefile.am @@ -34,6 +34,7 @@ pkginclude_HEADERS = include/gmock/gmock.h \ include/gmock/gmock-generated-matchers.h \ include/gmock/gmock-generated-nice-strict.h \ include/gmock/gmock-matchers.h \ + include/gmock/gmock-more-actions.h \ include/gmock/gmock-printers.h \ include/gmock/gmock-spec-builders.h @@ -110,6 +111,11 @@ check_PROGRAMS += test/gmock-matchers_test test_gmock_matchers_test_SOURCES = test/gmock-matchers_test.cc test_gmock_matchers_test_LDADD = $(GTEST_LIBS) lib/libgmock_main.la +TESTS += test/gmock-more-actions_test +check_PROGRAMS += test/gmock-more-actions_test +test_gmock_more_actions_test_SOURCES = test/gmock-more-actions_test.cc +test_gmock_more_actions_test_LDADD = $(GTEST_LIBS) lib/libgmock_main.la + TESTS += test/gmock-nice-strict_test check_PROGRAMS += test/gmock-nice-strict_test test_gmock_nice_strict_test_SOURCES = test/gmock-nice-strict_test.cc diff --git a/testing/gmock/configure.ac b/testing/gmock/configure.ac index 78ff30d..25ab6f3 100644 --- a/testing/gmock/configure.ac +++ b/testing/gmock/configure.ac @@ -1,5 +1,5 @@ AC_INIT([Google C++ Mocking Framework], - [1.1.0], + [1.4.0], [googlemock@googlegroups.com], [gmock]) @@ -80,7 +80,7 @@ AC_ARG_VAR([GTEST_VERSION], [The version of Google Test available.]) HAVE_BUILT_GTEST="no" -GTEST_MIN_VERSION="1.3.0" +GTEST_MIN_VERSION="1.4.0" AS_IF([test "x${enable_external_gtest}" = "xyes"], [# Begin filling in variables as we are able. diff --git a/testing/gmock/include/gmock/gmock-actions.h b/testing/gmock/include/gmock/gmock-actions.h index a283ed7..7f21a7d 100644 --- a/testing/gmock/include/gmock/gmock-actions.h +++ b/testing/gmock/include/gmock/gmock-actions.h @@ -43,6 +43,7 @@ #include <errno.h> #endif +#include <gmock/gmock-printers.h> #include <gmock/internal/gmock-internal-utils.h> #include <gmock/internal/gmock-port.h> @@ -124,32 +125,13 @@ GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0'); GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0'); GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0'); -// signed wchar_t and unsigned wchar_t are NOT in the C++ standard. -// Using them is a bad practice and not portable. So don't use them. -// -// Still, Google Mock is designed to work even if the user uses signed -// wchar_t or unsigned wchar_t (obviously, assuming the compiler -// supports them). -// -// To gcc, -// -// wchar_t == signed wchar_t != unsigned wchar_t == unsigned int -// -// MSVC does not recognize signed wchar_t or unsigned wchar_t. It -// treats wchar_t as a native type usually, but treats it as the same -// as unsigned short when the compiler option /Zc:wchar_t- is -// specified. -// -// Therefore we provide a default action for wchar_t when compiled -// with gcc or _NATIVE_WCHAR_T_DEFINED is defined. -// // There's no need for a default action for signed wchar_t, as that // type is the same as wchar_t for gcc, and invalid for MSVC. // // There's also no need for a default action for unsigned wchar_t, as // that type is the same as unsigned int for gcc, and invalid for // MSVC. -#if defined(__GNUC__) || defined(_NATIVE_WCHAR_T_DEFINED) +#if GMOCK_WCHAR_T_IS_NATIVE_ GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); // NOLINT #endif @@ -605,7 +587,7 @@ class AssignAction { const T2 value_; }; -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE // Implements the SetErrnoAndReturn action to simulate return from // various system calls and libc functions. @@ -625,7 +607,7 @@ class SetErrnoAndReturnAction { const T result_; }; -#endif // _WIN32_WCE +#endif // !GTEST_OS_WINDOWS_MOBILE // Implements the SetArgumentPointee<N>(x) action for any function // whose N-th argument (0-based) is a pointer to x's type. The @@ -668,39 +650,6 @@ class SetArgumentPointeeAction<N, Proto, true> { const internal::linked_ptr<Proto> proto_; }; -// Implements the SetArrayArgument<N>(first, last) action for any function -// whose N-th argument (0-based) is a pointer or iterator to a type that can be -// implicitly converted from *first. -template <size_t N, typename InputIterator> -class SetArrayArgumentAction { - public: - // Constructs an action that sets the variable pointed to by the - // N-th function argument to 'value'. - explicit SetArrayArgumentAction(InputIterator first, InputIterator last) - : first_(first), last_(last) { - } - - template <typename Result, typename ArgumentTuple> - void Perform(const ArgumentTuple& args) const { - CompileAssertTypesEqual<void, Result>(); - - // Microsoft compiler deprecates ::std::copy, so we want to suppress warning - // 4996 (Function call with parameters that may be unsafe) there. -#if GTEST_OS_WINDOWS -#pragma warning(push) // Saves the current warning state. -#pragma warning(disable:4996) // Temporarily disables warning 4996. -#endif // GTEST_OS_WINDOWS - ::std::copy(first_, last_, ::std::tr1::get<N>(args)); -#if GTEST_OS_WINDOWS -#pragma warning(pop) // Restores the warning state. -#endif // GTEST_OS_WINDOWS - } - - private: - const InputIterator first_; - const InputIterator last_; -}; - // Implements the InvokeWithoutArgs(f) action. The template argument // FunctionImpl is the implementation type of f, which can be either a // function pointer or a functor. InvokeWithoutArgs(f) can be used as an @@ -787,6 +736,74 @@ class IgnoreResultAction { const A action_; }; +// A ReferenceWrapper<T> object represents a reference to type T, +// which can be either const or not. It can be explicitly converted +// from, and implicitly converted to, a T&. Unlike a reference, +// ReferenceWrapper<T> can be copied and can survive template type +// inference. This is used to support by-reference arguments in the +// InvokeArgument<N>(...) action. The idea was from "reference +// wrappers" in tr1, which we don't have in our source tree yet. +template <typename T> +class ReferenceWrapper { + public: + // Constructs a ReferenceWrapper<T> object from a T&. + explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT + + // Allows a ReferenceWrapper<T> object to be implicitly converted to + // a T&. + operator T&() const { return *pointer_; } + private: + T* pointer_; +}; + +// Allows the expression ByRef(x) to be printed as a reference to x. +template <typename T> +void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) { + T& value = ref; + UniversalPrinter<T&>::Print(value, os); +} + +// Does two actions sequentially. Used for implementing the DoAll(a1, +// a2, ...) action. +template <typename Action1, typename Action2> +class DoBothAction { + public: + DoBothAction(Action1 action1, Action2 action2) + : action1_(action1), action2_(action2) {} + + // This template type conversion operator allows DoAll(a1, ..., a_n) + // to be used in ANY function of compatible type. + template <typename F> + operator Action<F>() const { + return Action<F>(new Impl<F>(action1_, action2_)); + } + + private: + // Implements the DoAll(...) action for a particular function type F. + template <typename F> + class Impl : public ActionInterface<F> { + public: + typedef typename Function<F>::Result Result; + typedef typename Function<F>::ArgumentTuple ArgumentTuple; + typedef typename Function<F>::MakeResultVoid VoidResult; + + Impl(const Action<VoidResult>& action1, const Action<F>& action2) + : action1_(action1), action2_(action2) {} + + virtual Result Perform(const ArgumentTuple& args) { + action1_.Perform(args); + return action2_.Perform(args); + } + + private: + const Action<VoidResult> action1_; + const Action<F> action2_; + }; + + Action1 action1_; + Action2 action2_; +}; + } // namespace internal // An Unused object can be implicitly constructed from ANY value. @@ -870,23 +887,13 @@ SetArgumentPointee(const T& x) { N, T, internal::IsAProtocolMessage<T>::value>(x)); } -// Creates an action that sets the elements of the array pointed to by the N-th -// (0-based) function argument, which can be either a pointer or an iterator, -// to the values of the elements in the source range [first, last). -template <size_t N, typename InputIterator> -PolymorphicAction<internal::SetArrayArgumentAction<N, InputIterator> > -SetArrayArgument(InputIterator first, InputIterator last) { - return MakePolymorphicAction(internal::SetArrayArgumentAction< - N, InputIterator>(first, last)); -} - // Creates an action that sets a pointer referent to a given value. template <typename T1, typename T2> PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) { return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val)); } -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE // Creates an action that sets errno and returns the appropriate error. template <typename T> @@ -896,7 +903,7 @@ SetErrnoAndReturn(int errval, T result) { internal::SetErrnoAndReturnAction<T>(errval, result)); } -#endif // _WIN32_WCE +#endif // !GTEST_OS_WINDOWS_MOBILE // Various overloads for InvokeWithoutArgs(). @@ -926,6 +933,18 @@ inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) { return internal::IgnoreResultAction<A>(an_action); } +// Creates a reference wrapper for the given L-value. If necessary, +// you can explicitly specify the type of the reference. For example, +// suppose 'derived' is an object of type Derived, ByRef(derived) +// would wrap a Derived&. If you want to wrap a const Base& instead, +// where Base is a base class of Derived, just write: +// +// ByRef<const Base>(derived) +template <typename T> +inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT + return internal::ReferenceWrapper<T>(l_value); +} + } // namespace testing #endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ diff --git a/testing/gmock/include/gmock/gmock-generated-actions.h b/testing/gmock/include/gmock/gmock-generated-actions.h index fa02faa..143a99b 100644 --- a/testing/gmock/include/gmock/gmock-generated-actions.h +++ b/testing/gmock/include/gmock/gmock-generated-actions.h @@ -282,65 +282,6 @@ class InvokeHelper<R, ::std::tr1::tuple<A1, A2, A3, A4, A5, A6, A7, A8, A9, } }; - -// Implements the Invoke(f) action. The template argument -// FunctionImpl is the implementation type of f, which can be either a -// function pointer or a functor. Invoke(f) can be used as an -// Action<F> as long as f's type is compatible with F (i.e. f can be -// assigned to a tr1::function<F>). -template <typename FunctionImpl> -class InvokeAction { - public: - // The c'tor makes a copy of function_impl (either a function - // pointer or a functor). - explicit InvokeAction(FunctionImpl function_impl) - : function_impl_(function_impl) {} - - template <typename Result, typename ArgumentTuple> - Result Perform(const ArgumentTuple& args) { - return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args); - } - private: - FunctionImpl function_impl_; -}; - -// Implements the Invoke(object_ptr, &Class::Method) action. -template <class Class, typename MethodPtr> -class InvokeMethodAction { - public: - InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr) - : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {} - - template <typename Result, typename ArgumentTuple> - Result Perform(const ArgumentTuple& args) const { - return InvokeHelper<Result, ArgumentTuple>::InvokeMethod( - obj_ptr_, method_ptr_, args); - } - private: - Class* const obj_ptr_; - const MethodPtr method_ptr_; -}; - -// A ReferenceWrapper<T> object represents a reference to type T, -// which can be either const or not. It can be explicitly converted -// from, and implicitly converted to, a T&. Unlike a reference, -// ReferenceWrapper<T> can be copied and can survive template type -// inference. This is used to support by-reference arguments in the -// InvokeArgument<N>(...) action. The idea was from "reference -// wrappers" in tr1, which we don't have in our source tree yet. -template <typename T> -class ReferenceWrapper { - public: - // Constructs a ReferenceWrapper<T> object from a T&. - explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT - - // Allows a ReferenceWrapper<T> object to be implicitly converted to - // a T&. - operator T&() const { return *pointer_; } - private: - T* pointer_; -}; - // CallableHelper has static methods for invoking "callables", // i.e. function pointers and functors. It uses overloading to // provide a uniform interface for invoking different kinds of @@ -676,47 +617,6 @@ class WithArgsAction { const InnerAction action_; }; -// Does two actions sequentially. Used for implementing the DoAll(a1, -// a2, ...) action. -template <typename Action1, typename Action2> -class DoBothAction { - public: - DoBothAction(Action1 action1, Action2 action2) - : action1_(action1), action2_(action2) {} - - // This template type conversion operator allows DoAll(a1, ..., a_n) - // to be used in ANY function of compatible type. - template <typename F> - operator Action<F>() const { - return Action<F>(new Impl<F>(action1_, action2_)); - } - - private: - // Implements the DoAll(...) action for a particular function type F. - template <typename F> - class Impl : public ActionInterface<F> { - public: - typedef typename Function<F>::Result Result; - typedef typename Function<F>::ArgumentTuple ArgumentTuple; - typedef typename Function<F>::MakeResultVoid VoidResult; - - Impl(const Action<VoidResult>& action1, const Action<F>& action2) - : action1_(action1), action2_(action2) {} - - virtual Result Perform(const ArgumentTuple& args) { - action1_.Perform(args); - return action2_.Perform(args); - } - - private: - const Action<VoidResult> action1_; - const Action<F> action2_; - }; - - Action1 action1_; - Action2 action2_; -}; - // A macro from the ACTION* family (defined later in this file) // defines an action that can be used in a mock function. Typically, // these actions only care about a subset of the arguments of the mock @@ -852,57 +752,6 @@ class ActionHelper { // Various overloads for Invoke(). -// Creates an action that invokes 'function_impl' with the mock -// function's arguments. -template <typename FunctionImpl> -PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke( - FunctionImpl function_impl) { - return MakePolymorphicAction( - internal::InvokeAction<FunctionImpl>(function_impl)); -} - -// Creates an action that invokes the given method on the given object -// with the mock function's arguments. -template <class Class, typename MethodPtr> -PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke( - Class* obj_ptr, MethodPtr method_ptr) { - return MakePolymorphicAction( - internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr)); -} - -// Creates a reference wrapper for the given L-value. If necessary, -// you can explicitly specify the type of the reference. For example, -// suppose 'derived' is an object of type Derived, ByRef(derived) -// would wrap a Derived&. If you want to wrap a const Base& instead, -// where Base is a base class of Derived, just write: -// -// ByRef<const Base>(derived) -template <typename T> -inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT - return internal::ReferenceWrapper<T>(l_value); -} - -// WithoutArgs(inner_action) can be used in a mock function with a -// non-empty argument list to perform inner_action, which takes no -// argument. In other words, it adapts an action accepting no -// argument to one that accepts (and ignores) arguments. -template <typename InnerAction> -inline internal::WithArgsAction<InnerAction> -WithoutArgs(const InnerAction& action) { - return internal::WithArgsAction<InnerAction>(action); -} - -// WithArg<k>(an_action) creates an action that passes the k-th -// (0-based) argument of the mock function to an_action and performs -// it. It adapts an action accepting one argument to one that accepts -// multiple arguments. For convenience, we also provide -// WithArgs<k>(an_action) (defined below) as a synonym. -template <int k, typename InnerAction> -inline internal::WithArgsAction<InnerAction, k> -WithArg(const InnerAction& action) { - return internal::WithArgsAction<InnerAction, k>(action); -} - // WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes // the selected arguments of the mock function to an_action and // performs it. It serves as an adaptor between actions with @@ -2430,48 +2279,6 @@ ACTION_TEMPLATE(InvokeArgument, ::std::tr1::get<k>(args), p0, p1, p2, p3, p4, p5, p6, p7, p8, p9); } -// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the -// mock function to *pointer. -ACTION_TEMPLATE(SaveArg, - HAS_1_TEMPLATE_PARAMS(int, k), - AND_1_VALUE_PARAMS(pointer)) { - *pointer = ::std::tr1::get<k>(args); -} - -// Action SetArgReferee<k>(value) assigns 'value' to the variable -// referenced by the k-th (0-based) argument of the mock function. -ACTION_TEMPLATE(SetArgReferee, - HAS_1_TEMPLATE_PARAMS(int, k), - AND_1_VALUE_PARAMS(value)) { - typedef typename ::std::tr1::tuple_element<k, args_type>::type argk_type; - // Ensures that argument #k is a reference. If you get a compiler - // error on the next line, you are using SetArgReferee<k>(value) in - // a mock function whose k-th (0-based) argument is not a reference. - GMOCK_COMPILE_ASSERT_(internal::is_reference<argk_type>::value, - SetArgReferee_must_be_used_with_a_reference_argument); - ::std::tr1::get<k>(args) = value; -} - -// Action SetArrayArgument<k>(first, last) copies the elements in -// source range [first, last) to the array pointed to by the k-th -// (0-based) argument, which can be either a pointer or an -// iterator. The action does not take ownership of the elements in the -// source range. -ACTION_TEMPLATE(SetArrayArgument, - HAS_1_TEMPLATE_PARAMS(int, k), - AND_2_VALUE_PARAMS(first, last)) { - // Microsoft compiler deprecates ::std::copy, so we want to suppress warning - // 4996 (Function call with parameters that may be unsafe) there. -#ifdef _MSC_VER -#pragma warning(push) // Saves the current warning state. -#pragma warning(disable:4996) // Temporarily disables warning 4996. -#endif - ::std::copy(first, last, ::std::tr1::get<k>(args)); -#ifdef _MSC_VER -#pragma warning(pop) // Restores the warning state. -#endif -} - // Various overloads for ReturnNew<T>(). // // The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new @@ -2543,20 +2350,6 @@ ACTION_TEMPLATE(ReturnNew, return new T(p0, p1, p2, p3, p4, p5, p6, p7, p8, p9); } -// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock -// function. -ACTION_TEMPLATE(DeleteArg, - HAS_1_TEMPLATE_PARAMS(int, k), - AND_0_VALUE_PARAMS()) { - delete ::std::tr1::get<k>(args); -} - -// Action Throw(exception) can be used in a mock function of any type -// to throw the given exception. Any copyable value can be thrown. -#if GTEST_HAS_EXCEPTIONS -ACTION_P(Throw, exception) { throw exception; } -#endif // GTEST_HAS_EXCEPTIONS - } // namespace testing #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ diff --git a/testing/gmock/include/gmock/gmock-generated-actions.h.pump b/testing/gmock/include/gmock/gmock-generated-actions.h.pump index b5223a34..05bf3db 100644 --- a/testing/gmock/include/gmock/gmock-generated-actions.h.pump +++ b/testing/gmock/include/gmock/gmock-generated-actions.h.pump @@ -1,6 +1,6 @@ $$ -*- mode: c++; -*- $$ This is a Pump source file. Please use Pump to convert it to -$$ gmock-generated-variadic-actions.h. +$$ gmock-generated-actions.h. $$ $var n = 10 $$ The maximum arity we support. $$}} This meta comment fixes auto-indentation in editors. @@ -84,65 +84,6 @@ $import return (obj_ptr->*method_ptr)($gets); ]] - -// Implements the Invoke(f) action. The template argument -// FunctionImpl is the implementation type of f, which can be either a -// function pointer or a functor. Invoke(f) can be used as an -// Action<F> as long as f's type is compatible with F (i.e. f can be -// assigned to a tr1::function<F>). -template <typename FunctionImpl> -class InvokeAction { - public: - // The c'tor makes a copy of function_impl (either a function - // pointer or a functor). - explicit InvokeAction(FunctionImpl function_impl) - : function_impl_(function_impl) {} - - template <typename Result, typename ArgumentTuple> - Result Perform(const ArgumentTuple& args) { - return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args); - } - private: - FunctionImpl function_impl_; -}; - -// Implements the Invoke(object_ptr, &Class::Method) action. -template <class Class, typename MethodPtr> -class InvokeMethodAction { - public: - InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr) - : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {} - - template <typename Result, typename ArgumentTuple> - Result Perform(const ArgumentTuple& args) const { - return InvokeHelper<Result, ArgumentTuple>::InvokeMethod( - obj_ptr_, method_ptr_, args); - } - private: - Class* const obj_ptr_; - const MethodPtr method_ptr_; -}; - -// A ReferenceWrapper<T> object represents a reference to type T, -// which can be either const or not. It can be explicitly converted -// from, and implicitly converted to, a T&. Unlike a reference, -// ReferenceWrapper<T> can be copied and can survive template type -// inference. This is used to support by-reference arguments in the -// InvokeArgument<N>(...) action. The idea was from "reference -// wrappers" in tr1, which we don't have in our source tree yet. -template <typename T> -class ReferenceWrapper { - public: - // Constructs a ReferenceWrapper<T> object from a T&. - explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT - - // Allows a ReferenceWrapper<T> object to be implicitly converted to - // a T&. - operator T&() const { return *pointer_; } - private: - T* pointer_; -}; - // CallableHelper has static methods for invoking "callables", // i.e. function pointers and functors. It uses overloading to // provide a uniform interface for invoking different kinds of @@ -289,47 +230,6 @@ class WithArgsAction { const InnerAction action_; }; -// Does two actions sequentially. Used for implementing the DoAll(a1, -// a2, ...) action. -template <typename Action1, typename Action2> -class DoBothAction { - public: - DoBothAction(Action1 action1, Action2 action2) - : action1_(action1), action2_(action2) {} - - // This template type conversion operator allows DoAll(a1, ..., a_n) - // to be used in ANY function of compatible type. - template <typename F> - operator Action<F>() const { - return Action<F>(new Impl<F>(action1_, action2_)); - } - - private: - // Implements the DoAll(...) action for a particular function type F. - template <typename F> - class Impl : public ActionInterface<F> { - public: - typedef typename Function<F>::Result Result; - typedef typename Function<F>::ArgumentTuple ArgumentTuple; - typedef typename Function<F>::MakeResultVoid VoidResult; - - Impl(const Action<VoidResult>& action1, const Action<F>& action2) - : action1_(action1), action2_(action2) {} - - virtual Result Perform(const ArgumentTuple& args) { - action1_.Perform(args); - return action2_.Perform(args); - } - - private: - const Action<VoidResult> action1_; - const Action<F> action2_; - }; - - Action1 action1_; - Action2 action2_; -}; - // A macro from the ACTION* family (defined later in this file) // defines an action that can be used in a mock function. Typically, // these actions only care about a subset of the arguments of the mock @@ -377,57 +277,6 @@ $template // Various overloads for Invoke(). -// Creates an action that invokes 'function_impl' with the mock -// function's arguments. -template <typename FunctionImpl> -PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke( - FunctionImpl function_impl) { - return MakePolymorphicAction( - internal::InvokeAction<FunctionImpl>(function_impl)); -} - -// Creates an action that invokes the given method on the given object -// with the mock function's arguments. -template <class Class, typename MethodPtr> -PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke( - Class* obj_ptr, MethodPtr method_ptr) { - return MakePolymorphicAction( - internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr)); -} - -// Creates a reference wrapper for the given L-value. If necessary, -// you can explicitly specify the type of the reference. For example, -// suppose 'derived' is an object of type Derived, ByRef(derived) -// would wrap a Derived&. If you want to wrap a const Base& instead, -// where Base is a base class of Derived, just write: -// -// ByRef<const Base>(derived) -template <typename T> -inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT - return internal::ReferenceWrapper<T>(l_value); -} - -// WithoutArgs(inner_action) can be used in a mock function with a -// non-empty argument list to perform inner_action, which takes no -// argument. In other words, it adapts an action accepting no -// argument to one that accepts (and ignores) arguments. -template <typename InnerAction> -inline internal::WithArgsAction<InnerAction> -WithoutArgs(const InnerAction& action) { - return internal::WithArgsAction<InnerAction>(action); -} - -// WithArg<k>(an_action) creates an action that passes the k-th -// (0-based) argument of the mock function to an_action and performs -// it. It adapts an action accepting one argument to one that accepts -// multiple arguments. For convenience, we also provide -// WithArgs<k>(an_action) (defined below) as a synonym. -template <int k, typename InnerAction> -inline internal::WithArgsAction<InnerAction, k> -WithArg(const InnerAction& action) { - return internal::WithArgsAction<InnerAction, k>(action); -} - // WithArgs<N1, N2, ..., Nk>(an_action) creates an action that passes // the selected arguments of the mock function to an_action and // performs it. It serves as an adaptor between actions with @@ -929,48 +778,6 @@ ACTION_TEMPLATE(InvokeArgument, ]] -// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the -// mock function to *pointer. -ACTION_TEMPLATE(SaveArg, - HAS_1_TEMPLATE_PARAMS(int, k), - AND_1_VALUE_PARAMS(pointer)) { - *pointer = ::std::tr1::get<k>(args); -} - -// Action SetArgReferee<k>(value) assigns 'value' to the variable -// referenced by the k-th (0-based) argument of the mock function. -ACTION_TEMPLATE(SetArgReferee, - HAS_1_TEMPLATE_PARAMS(int, k), - AND_1_VALUE_PARAMS(value)) { - typedef typename ::std::tr1::tuple_element<k, args_type>::type argk_type; - // Ensures that argument #k is a reference. If you get a compiler - // error on the next line, you are using SetArgReferee<k>(value) in - // a mock function whose k-th (0-based) argument is not a reference. - GMOCK_COMPILE_ASSERT_(internal::is_reference<argk_type>::value, - SetArgReferee_must_be_used_with_a_reference_argument); - ::std::tr1::get<k>(args) = value; -} - -// Action SetArrayArgument<k>(first, last) copies the elements in -// source range [first, last) to the array pointed to by the k-th -// (0-based) argument, which can be either a pointer or an -// iterator. The action does not take ownership of the elements in the -// source range. -ACTION_TEMPLATE(SetArrayArgument, - HAS_1_TEMPLATE_PARAMS(int, k), - AND_2_VALUE_PARAMS(first, last)) { - // Microsoft compiler deprecates ::std::copy, so we want to suppress warning - // 4996 (Function call with parameters that may be unsafe) there. -#ifdef _MSC_VER -#pragma warning(push) // Saves the current warning state. -#pragma warning(disable:4996) // Temporarily disables warning 4996. -#endif - ::std::copy(first, last, ::std::tr1::get<k>(args)); -#ifdef _MSC_VER -#pragma warning(pop) // Restores the warning state. -#endif -} - // Various overloads for ReturnNew<T>(). // // The ReturnNew<T>(a1, a2, ..., a_k) action returns a pointer to a new @@ -989,20 +796,6 @@ ACTION_TEMPLATE(ReturnNew, ]] -// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock -// function. -ACTION_TEMPLATE(DeleteArg, - HAS_1_TEMPLATE_PARAMS(int, k), - AND_0_VALUE_PARAMS()) { - delete ::std::tr1::get<k>(args); -} - -// Action Throw(exception) can be used in a mock function of any type -// to throw the given exception. Any copyable value can be thrown. -#if GTEST_HAS_EXCEPTIONS -ACTION_P(Throw, exception) { throw exception; } -#endif // GTEST_HAS_EXCEPTIONS - } // namespace testing #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_ACTIONS_H_ diff --git a/testing/gmock/include/gmock/gmock-generated-function-mockers.h b/testing/gmock/include/gmock/gmock-generated-function-mockers.h index b6c1d82..3002b6c 100644 --- a/testing/gmock/include/gmock/gmock-generated-function-mockers.h +++ b/testing/gmock/include/gmock/gmock-generated-function-mockers.h @@ -712,6 +712,117 @@ using internal::FunctionMocker; #define MOCK_CONST_METHOD10_T_WITH_CALLTYPE(ct, m, F) \ GMOCK_METHOD10_(typename, const, ct, m, F) +// A MockFunction<F> class has one mock method whose type is F. It is +// useful when you just want your test code to emit some messages and +// have Google Mock verify the right messages are sent (and perhaps at +// the right times). For example, if you are exercising code: +// +// Foo(1); +// Foo(2); +// Foo(3); +// +// and want to verify that Foo(1) and Foo(3) both invoke +// mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write: +// +// TEST(FooTest, InvokesBarCorrectly) { +// MyMock mock; +// MockFunction<void(string check_point_name)> check; +// { +// InSequence s; +// +// EXPECT_CALL(mock, Bar("a")); +// EXPECT_CALL(check, Call("1")); +// EXPECT_CALL(check, Call("2")); +// EXPECT_CALL(mock, Bar("a")); +// } +// Foo(1); +// check.Call("1"); +// Foo(2); +// check.Call("2"); +// Foo(3); +// } +// +// The expectation spec says that the first Bar("a") must happen +// before check point "1", the second Bar("a") must happen after check +// point "2", and nothing should happen between the two check +// points. The explicit check points make it easy to tell which +// Bar("a") is called by which call to Foo(). +template <typename F> +class MockFunction; + +template <typename R> +class MockFunction<R()> { + public: + MOCK_METHOD0_T(Call, R()); +}; + +template <typename R, typename A0> +class MockFunction<R(A0)> { + public: + MOCK_METHOD1_T(Call, R(A0)); +}; + +template <typename R, typename A0, typename A1> +class MockFunction<R(A0, A1)> { + public: + MOCK_METHOD2_T(Call, R(A0, A1)); +}; + +template <typename R, typename A0, typename A1, typename A2> +class MockFunction<R(A0, A1, A2)> { + public: + MOCK_METHOD3_T(Call, R(A0, A1, A2)); +}; + +template <typename R, typename A0, typename A1, typename A2, typename A3> +class MockFunction<R(A0, A1, A2, A3)> { + public: + MOCK_METHOD4_T(Call, R(A0, A1, A2, A3)); +}; + +template <typename R, typename A0, typename A1, typename A2, typename A3, + typename A4> +class MockFunction<R(A0, A1, A2, A3, A4)> { + public: + MOCK_METHOD5_T(Call, R(A0, A1, A2, A3, A4)); +}; + +template <typename R, typename A0, typename A1, typename A2, typename A3, + typename A4, typename A5> +class MockFunction<R(A0, A1, A2, A3, A4, A5)> { + public: + MOCK_METHOD6_T(Call, R(A0, A1, A2, A3, A4, A5)); +}; + +template <typename R, typename A0, typename A1, typename A2, typename A3, + typename A4, typename A5, typename A6> +class MockFunction<R(A0, A1, A2, A3, A4, A5, A6)> { + public: + MOCK_METHOD7_T(Call, R(A0, A1, A2, A3, A4, A5, A6)); +}; + +template <typename R, typename A0, typename A1, typename A2, typename A3, + typename A4, typename A5, typename A6, typename A7> +class MockFunction<R(A0, A1, A2, A3, A4, A5, A6, A7)> { + public: + MOCK_METHOD8_T(Call, R(A0, A1, A2, A3, A4, A5, A6, A7)); +}; + +template <typename R, typename A0, typename A1, typename A2, typename A3, + typename A4, typename A5, typename A6, typename A7, typename A8> +class MockFunction<R(A0, A1, A2, A3, A4, A5, A6, A7, A8)> { + public: + MOCK_METHOD9_T(Call, R(A0, A1, A2, A3, A4, A5, A6, A7, A8)); +}; + +template <typename R, typename A0, typename A1, typename A2, typename A3, + typename A4, typename A5, typename A6, typename A7, typename A8, + typename A9> +class MockFunction<R(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9)> { + public: + MOCK_METHOD10_T(Call, R(A0, A1, A2, A3, A4, A5, A6, A7, A8, A9)); +}; + } // namespace testing #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_ diff --git a/testing/gmock/include/gmock/gmock-generated-function-mockers.h.pump b/testing/gmock/include/gmock/gmock-generated-function-mockers.h.pump index 54b848f..3c84563 100644 --- a/testing/gmock/include/gmock/gmock-generated-function-mockers.h.pump +++ b/testing/gmock/include/gmock/gmock-generated-function-mockers.h.pump @@ -198,6 +198,55 @@ $for i [[ ]] +// A MockFunction<F> class has one mock method whose type is F. It is +// useful when you just want your test code to emit some messages and +// have Google Mock verify the right messages are sent (and perhaps at +// the right times). For example, if you are exercising code: +// +// Foo(1); +// Foo(2); +// Foo(3); +// +// and want to verify that Foo(1) and Foo(3) both invoke +// mock.Bar("a"), but Foo(2) doesn't invoke anything, you can write: +// +// TEST(FooTest, InvokesBarCorrectly) { +// MyMock mock; +// MockFunction<void(string check_point_name)> check; +// { +// InSequence s; +// +// EXPECT_CALL(mock, Bar("a")); +// EXPECT_CALL(check, Call("1")); +// EXPECT_CALL(check, Call("2")); +// EXPECT_CALL(mock, Bar("a")); +// } +// Foo(1); +// check.Call("1"); +// Foo(2); +// check.Call("2"); +// Foo(3); +// } +// +// The expectation spec says that the first Bar("a") must happen +// before check point "1", the second Bar("a") must happen after check +// point "2", and nothing should happen between the two check +// points. The explicit check points make it easy to tell which +// Bar("a") is called by which call to Foo(). +template <typename F> +class MockFunction; + + +$for i [[ +$range j 0..i-1 +template <typename R$for j [[, typename A$j]]> +class MockFunction<R($for j, [[A$j]])> { + public: + MOCK_METHOD$i[[]]_T(Call, R($for j, [[A$j]])); +}; + + +]] } // namespace testing #endif // GMOCK_INCLUDE_GMOCK_GMOCK_GENERATED_FUNCTION_MOCKERS_H_ diff --git a/testing/gmock/include/gmock/gmock-generated-matchers.h b/testing/gmock/include/gmock/gmock-generated-matchers.h index 1a3e60b..a59e457 100644 --- a/testing/gmock/include/gmock/gmock-generated-matchers.h +++ b/testing/gmock/include/gmock/gmock-generated-matchers.h @@ -290,163 +290,7 @@ class ArgsMatcher { const InnerMatcher inner_matcher_; }; -// Implements ElementsAre() and ElementsAreArray(). -template <typename Container> -class ElementsAreMatcherImpl : public MatcherInterface<Container> { - public: - typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer; - typedef internal::StlContainerView<RawContainer> View; - typedef typename View::type StlContainer; - typedef typename View::const_reference StlContainerReference; - typedef typename StlContainer::value_type Element; - - // Constructs the matcher from a sequence of element values or - // element matchers. - template <typename InputIter> - ElementsAreMatcherImpl(InputIter first, size_t count) { - matchers_.reserve(count); - InputIter it = first; - for (size_t i = 0; i != count; ++i, ++it) { - matchers_.push_back(MatcherCast<const Element&>(*it)); - } - } - - // Returns true iff 'container' matches. - virtual bool Matches(Container container) const { - StlContainerReference stl_container = View::ConstReference(container); - if (stl_container.size() != count()) - return false; - - typename StlContainer::const_iterator it = stl_container.begin(); - for (size_t i = 0; i != count(); ++it, ++i) { - if (!matchers_[i].Matches(*it)) - return false; - } - - return true; - } - - // Describes what this matcher does. - virtual void DescribeTo(::std::ostream* os) const { - if (count() == 0) { - *os << "is empty"; - } else if (count() == 1) { - *os << "has 1 element that "; - matchers_[0].DescribeTo(os); - } else { - *os << "has " << Elements(count()) << " where\n"; - for (size_t i = 0; i != count(); ++i) { - *os << "element " << i << " "; - matchers_[i].DescribeTo(os); - if (i + 1 < count()) { - *os << ",\n"; - } - } - } - } - - // Describes what the negation of this matcher does. - virtual void DescribeNegationTo(::std::ostream* os) const { - if (count() == 0) { - *os << "is not empty"; - return; - } - - *os << "does not have " << Elements(count()) << ", or\n"; - for (size_t i = 0; i != count(); ++i) { - *os << "element " << i << " "; - matchers_[i].DescribeNegationTo(os); - if (i + 1 < count()) { - *os << ", or\n"; - } - } - } - - // Explains why 'container' matches, or doesn't match, this matcher. - virtual void ExplainMatchResultTo(Container container, - ::std::ostream* os) const { - StlContainerReference stl_container = View::ConstReference(container); - if (Matches(container)) { - // We need to explain why *each* element matches (the obvious - // ones can be skipped). - - bool reason_printed = false; - typename StlContainer::const_iterator it = stl_container.begin(); - for (size_t i = 0; i != count(); ++it, ++i) { - ::std::stringstream ss; - matchers_[i].ExplainMatchResultTo(*it, &ss); - - const string s = ss.str(); - if (!s.empty()) { - if (reason_printed) { - *os << ",\n"; - } - *os << "element " << i << " " << s; - reason_printed = true; - } - } - } else { - // We need to explain why the container doesn't match. - const size_t actual_count = stl_container.size(); - if (actual_count != count()) { - // The element count doesn't match. If the container is - // empty, there's no need to explain anything as Google Mock - // already prints the empty container. Otherwise we just need - // to show how many elements there actually are. - if (actual_count != 0) { - *os << "has " << Elements(actual_count); - } - return; - } - - // The container has the right size but at least one element - // doesn't match expectation. We need to find this element and - // explain why it doesn't match. - typename StlContainer::const_iterator it = stl_container.begin(); - for (size_t i = 0; i != count(); ++it, ++i) { - if (matchers_[i].Matches(*it)) { - continue; - } - - *os << "element " << i << " doesn't match"; - - ::std::stringstream ss; - matchers_[i].ExplainMatchResultTo(*it, &ss); - const string s = ss.str(); - if (!s.empty()) { - *os << " (" << s << ")"; - } - return; - } - } - } - - private: - static Message Elements(size_t count) { - return Message() << count << (count == 1 ? " element" : " elements"); - } - - size_t count() const { return matchers_.size(); } - std::vector<Matcher<const Element&> > matchers_; -}; - -// Implements ElementsAre() of 0-10 arguments. - -class ElementsAreMatcher0 { - public: - ElementsAreMatcher0() {} - - template <typename Container> - operator Matcher<Container>() const { - typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) - RawContainer; - typedef typename internal::StlContainerView<RawContainer>::type::value_type - Element; - - const Matcher<const Element&>* const matchers = NULL; - return MakeMatcher(new ElementsAreMatcherImpl<Container>(matchers, 0)); - } -}; +// Implements ElementsAre() of 1-10 arguments. template <typename T1> class ElementsAreMatcher1 { @@ -460,11 +304,15 @@ class ElementsAreMatcher1 { typedef typename internal::StlContainerView<RawContainer>::type::value_type Element; - const Matcher<const Element&> matchers[] = { - MatcherCast<const Element&>(e1_), - }; - - return MakeMatcher(new ElementsAreMatcherImpl<Container>(matchers, 1)); + // Nokia's Symbian Compiler has a nasty bug where the object put + // in a one-element local array is not destructed when the array + // goes out of scope. This leads to obvious badness as we've + // added the linked_ptr in it to our other linked_ptrs list. + // Hence we implement ElementsAreMatcher1 specially to avoid using + // a local array. + const Matcher<const Element&> matcher = + MatcherCast<const Element&>(e1_); + return MakeMatcher(new ElementsAreMatcherImpl<Container>(&matcher, 1)); } private: @@ -788,28 +636,6 @@ class ElementsAreMatcher10 { const T10& e10_; }; -// Implements ElementsAreArray(). -template <typename T> -class ElementsAreArrayMatcher { - public: - ElementsAreArrayMatcher(const T* first, size_t count) : - first_(first), count_(count) {} - - template <typename Container> - operator Matcher<Container>() const { - typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) - RawContainer; - typedef typename internal::StlContainerView<RawContainer>::type::value_type - Element; - - return MakeMatcher(new ElementsAreMatcherImpl<Container>(first_, count_)); - } - - private: - const T* const first_; - const size_t count_; -}; - } // namespace internal // Args<N1, N2, ..., Nk>(a_matcher) matches a tuple if the selected @@ -1169,48 +995,6 @@ ElementsAreArray(const T (&array)[N]) { // To learn more about using these macros, please search for 'MATCHER' // on http://code.google.com/p/googlemock/wiki/CookBook. -namespace testing { -namespace internal { - -// Constants denoting interpolations in a matcher description string. -const int kTupleInterpolation = -1; // "%(*)s" -const int kPercentInterpolation = -2; // "%%" -const int kInvalidInterpolation = -3; // "%" followed by invalid text - -// Records the location and content of an interpolation. -struct Interpolation { - Interpolation(const char* start, const char* end, int param) - : start_pos(start), end_pos(end), param_index(param) {} - - // Points to the start of the interpolation (the '%' character). - const char* start_pos; - // Points to the first character after the interpolation. - const char* end_pos; - // 0-based index of the interpolated matcher parameter; - // kTupleInterpolation for "%(*)s"; kPercentInterpolation for "%%". - int param_index; -}; - -typedef ::std::vector<Interpolation> Interpolations; - -// Parses a matcher description string and returns a vector of -// interpolations that appear in the string; generates non-fatal -// failures iff 'description' is an invalid matcher description. -// 'param_names' is a NULL-terminated array of parameter names in the -// order they appear in the MATCHER_P*() parameter list. -Interpolations ValidateMatcherDescription( - const char* param_names[], const char* description); - -// Returns the actual matcher description, given the matcher name, -// user-supplied description template string, interpolations in the -// string, and the printed values of the matcher parameters. -string FormatMatcherDescription( - const char* matcher_name, const char* description, - const Interpolations& interp, const Strings& param_values); - -} // namespace internal -} // namespace testing - #define MATCHER(name, description)\ class name##Matcher {\ public:\ diff --git a/testing/gmock/include/gmock/gmock-generated-matchers.h.pump b/testing/gmock/include/gmock/gmock-generated-matchers.h.pump index 653a2e8..c43aa87 100644 --- a/testing/gmock/include/gmock/gmock-generated-matchers.h.pump +++ b/testing/gmock/include/gmock/gmock-generated-matchers.h.pump @@ -1,6 +1,6 @@ $$ -*- mode: c++; -*- $$ This is a Pump source file. Please use Pump to convert it to -$$ gmock-generated-variadic-actions.h. +$$ gmock-generated-actions.h. $$ $var n = 10 $$ The maximum arity we support. $$ }} This line fixes auto-indentation of the following code in Emacs. @@ -173,163 +173,7 @@ class ArgsMatcher { const InnerMatcher inner_matcher_; }; -// Implements ElementsAre() and ElementsAreArray(). -template <typename Container> -class ElementsAreMatcherImpl : public MatcherInterface<Container> { - public: - typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer; - typedef internal::StlContainerView<RawContainer> View; - typedef typename View::type StlContainer; - typedef typename View::const_reference StlContainerReference; - typedef typename StlContainer::value_type Element; - - // Constructs the matcher from a sequence of element values or - // element matchers. - template <typename InputIter> - ElementsAreMatcherImpl(InputIter first, size_t count) { - matchers_.reserve(count); - InputIter it = first; - for (size_t i = 0; i != count; ++i, ++it) { - matchers_.push_back(MatcherCast<const Element&>(*it)); - } - } - - // Returns true iff 'container' matches. - virtual bool Matches(Container container) const { - StlContainerReference stl_container = View::ConstReference(container); - if (stl_container.size() != count()) - return false; - - typename StlContainer::const_iterator it = stl_container.begin(); - for (size_t i = 0; i != count(); ++it, ++i) { - if (!matchers_[i].Matches(*it)) - return false; - } - - return true; - } - - // Describes what this matcher does. - virtual void DescribeTo(::std::ostream* os) const { - if (count() == 0) { - *os << "is empty"; - } else if (count() == 1) { - *os << "has 1 element that "; - matchers_[0].DescribeTo(os); - } else { - *os << "has " << Elements(count()) << " where\n"; - for (size_t i = 0; i != count(); ++i) { - *os << "element " << i << " "; - matchers_[i].DescribeTo(os); - if (i + 1 < count()) { - *os << ",\n"; - } - } - } - } - - // Describes what the negation of this matcher does. - virtual void DescribeNegationTo(::std::ostream* os) const { - if (count() == 0) { - *os << "is not empty"; - return; - } - - *os << "does not have " << Elements(count()) << ", or\n"; - for (size_t i = 0; i != count(); ++i) { - *os << "element " << i << " "; - matchers_[i].DescribeNegationTo(os); - if (i + 1 < count()) { - *os << ", or\n"; - } - } - } - - // Explains why 'container' matches, or doesn't match, this matcher. - virtual void ExplainMatchResultTo(Container container, - ::std::ostream* os) const { - StlContainerReference stl_container = View::ConstReference(container); - if (Matches(container)) { - // We need to explain why *each* element matches (the obvious - // ones can be skipped). - - bool reason_printed = false; - typename StlContainer::const_iterator it = stl_container.begin(); - for (size_t i = 0; i != count(); ++it, ++i) { - ::std::stringstream ss; - matchers_[i].ExplainMatchResultTo(*it, &ss); - - const string s = ss.str(); - if (!s.empty()) { - if (reason_printed) { - *os << ",\n"; - } - *os << "element " << i << " " << s; - reason_printed = true; - } - } - } else { - // We need to explain why the container doesn't match. - const size_t actual_count = stl_container.size(); - if (actual_count != count()) { - // The element count doesn't match. If the container is - // empty, there's no need to explain anything as Google Mock - // already prints the empty container. Otherwise we just need - // to show how many elements there actually are. - if (actual_count != 0) { - *os << "has " << Elements(actual_count); - } - return; - } - - // The container has the right size but at least one element - // doesn't match expectation. We need to find this element and - // explain why it doesn't match. - typename StlContainer::const_iterator it = stl_container.begin(); - for (size_t i = 0; i != count(); ++it, ++i) { - if (matchers_[i].Matches(*it)) { - continue; - } - - *os << "element " << i << " doesn't match"; - - ::std::stringstream ss; - matchers_[i].ExplainMatchResultTo(*it, &ss); - const string s = ss.str(); - if (!s.empty()) { - *os << " (" << s << ")"; - } - return; - } - } - } - - private: - static Message Elements(size_t count) { - return Message() << count << (count == 1 ? " element" : " elements"); - } - - size_t count() const { return matchers_.size(); } - std::vector<Matcher<const Element&> > matchers_; -}; - -// Implements ElementsAre() of 0-10 arguments. - -class ElementsAreMatcher0 { - public: - ElementsAreMatcher0() {} - - template <typename Container> - operator Matcher<Container>() const { - typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) - RawContainer; - typedef typename internal::StlContainerView<RawContainer>::type::value_type - Element; - - const Matcher<const Element&>* const matchers = NULL; - return MakeMatcher(new ElementsAreMatcherImpl<Container>(matchers, 0)); - } -}; +// Implements ElementsAre() of 1-$n arguments. $range i 1..n @@ -348,6 +192,19 @@ class ElementsAreMatcher$i { typedef typename internal::StlContainerView<RawContainer>::type::value_type Element; +$if i==1 [[ + + // Nokia's Symbian Compiler has a nasty bug where the object put + // in a one-element local array is not destructed when the array + // goes out of scope. This leads to obvious badness as we've + // added the linked_ptr in it to our other linked_ptrs list. + // Hence we implement ElementsAreMatcher1 specially to avoid using + // a local array. + const Matcher<const Element&> matcher = + MatcherCast<const Element&>(e1_); + return MakeMatcher(new ElementsAreMatcherImpl<Container>(&matcher, 1)); +]] $else [[ + const Matcher<const Element&> matchers[] = { $for j [[ @@ -357,6 +214,8 @@ $for j [[ }; return MakeMatcher(new ElementsAreMatcherImpl<Container>(matchers, $i)); +]] + } private: @@ -369,28 +228,6 @@ $for j [[ ]] -// Implements ElementsAreArray(). -template <typename T> -class ElementsAreArrayMatcher { - public: - ElementsAreArrayMatcher(const T* first, size_t count) : - first_(first), count_(count) {} - - template <typename Container> - operator Matcher<Container>() const { - typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) - RawContainer; - typedef typename internal::StlContainerView<RawContainer>::type::value_type - Element; - - return MakeMatcher(new ElementsAreMatcherImpl<Container>(first_, count_)); - } - - private: - const T* const first_; - const size_t count_; -}; - } // namespace internal // Args<N1, N2, ..., Nk>(a_matcher) matches a tuple if the selected @@ -625,48 +462,6 @@ $$ // show up in the generated code. // To learn more about using these macros, please search for 'MATCHER' // on http://code.google.com/p/googlemock/wiki/CookBook. -namespace testing { -namespace internal { - -// Constants denoting interpolations in a matcher description string. -const int kTupleInterpolation = -1; // "%(*)s" -const int kPercentInterpolation = -2; // "%%" -const int kInvalidInterpolation = -3; // "%" followed by invalid text - -// Records the location and content of an interpolation. -struct Interpolation { - Interpolation(const char* start, const char* end, int param) - : start_pos(start), end_pos(end), param_index(param) {} - - // Points to the start of the interpolation (the '%' character). - const char* start_pos; - // Points to the first character after the interpolation. - const char* end_pos; - // 0-based index of the interpolated matcher parameter; - // kTupleInterpolation for "%(*)s"; kPercentInterpolation for "%%". - int param_index; -}; - -typedef ::std::vector<Interpolation> Interpolations; - -// Parses a matcher description string and returns a vector of -// interpolations that appear in the string; generates non-fatal -// failures iff 'description' is an invalid matcher description. -// 'param_names' is a NULL-terminated array of parameter names in the -// order they appear in the MATCHER_P*() parameter list. -Interpolations ValidateMatcherDescription( - const char* param_names[], const char* description); - -// Returns the actual matcher description, given the matcher name, -// user-supplied description template string, interpolations in the -// string, and the printed values of the matcher parameters. -string FormatMatcherDescription( - const char* matcher_name, const char* description, - const Interpolations& interp, const Strings& param_values); - -} // namespace internal -} // namespace testing - $range i 0..n $for i diff --git a/testing/gmock/include/gmock/gmock-generated-nice-strict.h b/testing/gmock/include/gmock/gmock-generated-nice-strict.h index f961d79..fc9a81b 100644 --- a/testing/gmock/include/gmock/gmock-generated-nice-strict.h +++ b/testing/gmock/include/gmock/gmock-generated-nice-strict.h @@ -70,42 +70,49 @@ class NiceMock : public MockClass { // We don't factor out the constructor body to a common method, as // we have to avoid a possible clash with members of MockClass. NiceMock() { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } // C++ doesn't (yet) allow inheritance of constructors, so we have // to define it for each arity. template <typename A1> explicit NiceMock(const A1& a1) : MockClass(a1) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2> NiceMock(const A1& a1, const A2& a2) : MockClass(a1, a2) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3> NiceMock(const A1& a1, const A2& a2, const A3& a3) : MockClass(a1, a2, a3) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4> NiceMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4) : MockClass(a1, a2, a3, a4) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5> NiceMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5) : MockClass(a1, a2, a3, a4, a5) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> NiceMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6) : MockClass(a1, a2, a3, a4, a5, a6) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5, @@ -113,7 +120,8 @@ class NiceMock : public MockClass { NiceMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7) : MockClass(a1, a2, a3, a4, a5, a6, a7) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5, @@ -121,7 +129,8 @@ class NiceMock : public MockClass { NiceMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8) : MockClass(a1, a2, a3, a4, a5, a6, a7, a8) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5, @@ -129,7 +138,8 @@ class NiceMock : public MockClass { NiceMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9) : MockClass(a1, a2, a3, a4, a5, a6, a7, a8, a9) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5, @@ -137,11 +147,13 @@ class NiceMock : public MockClass { NiceMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10) : MockClass(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } virtual ~NiceMock() { - Mock::UnregisterCallReaction(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::UnregisterCallReaction( + internal::implicit_cast<MockClass*>(this)); } }; @@ -151,40 +163,47 @@ class StrictMock : public MockClass { // We don't factor out the constructor body to a common method, as // we have to avoid a possible clash with members of MockClass. StrictMock() { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1> explicit StrictMock(const A1& a1) : MockClass(a1) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2> StrictMock(const A1& a1, const A2& a2) : MockClass(a1, a2) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3> StrictMock(const A1& a1, const A2& a2, const A3& a3) : MockClass(a1, a2, a3) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4> StrictMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4) : MockClass(a1, a2, a3, a4) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5> StrictMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5) : MockClass(a1, a2, a3, a4, a5) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5, typename A6> StrictMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6) : MockClass(a1, a2, a3, a4, a5, a6) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5, @@ -192,7 +211,8 @@ class StrictMock : public MockClass { StrictMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7) : MockClass(a1, a2, a3, a4, a5, a6, a7) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5, @@ -200,7 +220,8 @@ class StrictMock : public MockClass { StrictMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8) : MockClass(a1, a2, a3, a4, a5, a6, a7, a8) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5, @@ -208,7 +229,8 @@ class StrictMock : public MockClass { StrictMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9) : MockClass(a1, a2, a3, a4, a5, a6, a7, a8, a9) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1, typename A2, typename A3, typename A4, typename A5, @@ -216,11 +238,13 @@ class StrictMock : public MockClass { StrictMock(const A1& a1, const A2& a2, const A3& a3, const A4& a4, const A5& a5, const A6& a6, const A7& a7, const A8& a8, const A9& a9, const A10& a10) : MockClass(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } virtual ~StrictMock() { - Mock::UnregisterCallReaction(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::UnregisterCallReaction( + internal::implicit_cast<MockClass*>(this)); } }; diff --git a/testing/gmock/include/gmock/gmock-generated-nice-strict.h.pump b/testing/gmock/include/gmock/gmock-generated-nice-strict.h.pump index 580e79f..b265c2e4 100644 --- a/testing/gmock/include/gmock/gmock-generated-nice-strict.h.pump +++ b/testing/gmock/include/gmock/gmock-generated-nice-strict.h.pump @@ -73,14 +73,16 @@ class NiceMock : public MockClass { // We don't factor out the constructor body to a common method, as // we have to avoid a possible clash with members of MockClass. NiceMock() { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } // C++ doesn't (yet) allow inheritance of constructors, so we have // to define it for each arity. template <typename A1> explicit NiceMock(const A1& a1) : MockClass(a1) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } $range i 2..n @@ -88,13 +90,15 @@ $for i [[ $range j 1..i template <$for j, [[typename A$j]]> NiceMock($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) { - Mock::AllowUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::AllowUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } ]] virtual ~NiceMock() { - Mock::UnregisterCallReaction(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::UnregisterCallReaction( + internal::implicit_cast<MockClass*>(this)); } }; @@ -104,25 +108,29 @@ class StrictMock : public MockClass { // We don't factor out the constructor body to a common method, as // we have to avoid a possible clash with members of MockClass. StrictMock() { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } template <typename A1> explicit StrictMock(const A1& a1) : MockClass(a1) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } $for i [[ $range j 1..i template <$for j, [[typename A$j]]> StrictMock($for j, [[const A$j& a$j]]) : MockClass($for j, [[a$j]]) { - Mock::FailUninterestingCalls(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::FailUninterestingCalls( + internal::implicit_cast<MockClass*>(this)); } ]] virtual ~StrictMock() { - Mock::UnregisterCallReaction(internal::implicit_cast<MockClass*>(this)); + ::testing::Mock::UnregisterCallReaction( + internal::implicit_cast<MockClass*>(this)); } }; diff --git a/testing/gmock/include/gmock/gmock-matchers.h b/testing/gmock/include/gmock/gmock-matchers.h index dc252e3..3d82279 100644 --- a/testing/gmock/include/gmock/gmock-matchers.h +++ b/testing/gmock/include/gmock/gmock-matchers.h @@ -236,6 +236,14 @@ class PolymorphicMatcher { public: explicit PolymorphicMatcher(const Impl& impl) : impl_(impl) {} + // Returns a mutable reference to the underlying matcher + // implementation object. + Impl& mutable_impl() { return impl_; } + + // Returns an immutable reference to the underlying matcher + // implementation object. + const Impl& impl() const { return impl_; } + template <typename T> operator Matcher<T>() const { return Matcher<T>(new MonomorphicImpl<T>(impl_)); @@ -273,11 +281,12 @@ class PolymorphicMatcher { // doesn't need to customize it. ExplainMatchResultTo(impl_, x, os); } + private: const Impl impl_; }; - const Impl impl_; + Impl impl_; }; // Creates a matcher from its implementation. This is easier to use @@ -311,47 +320,59 @@ inline PolymorphicMatcher<Impl> MakePolymorphicMatcher(const Impl& impl) { template <typename T, typename M> Matcher<T> MatcherCast(M m); -// TODO(vladl@google.com): Modify the implementation to reject casting -// Matcher<int> to Matcher<double>. // Implements SafeMatcherCast(). // -// This overload handles polymorphic matchers only since monomorphic -// matchers are handled by the next one. -template <typename T, typename M> -inline Matcher<T> SafeMatcherCast(M polymorphic_matcher) { - return Matcher<T>(polymorphic_matcher); -} +// We use an intermediate class to do the actual safe casting as Nokia's +// Symbian compiler cannot decide between +// template <T, M> ... (M) and +// template <T, U> ... (const Matcher<U>&) +// for function templates but can for member function templates. +template <typename T> +class SafeMatcherCastImpl { + public: + // This overload handles polymorphic matchers only since monomorphic + // matchers are handled by the next one. + template <typename M> + static inline Matcher<T> Cast(M polymorphic_matcher) { + return Matcher<T>(polymorphic_matcher); + } -// This overload handles monomorphic matchers. -// -// In general, if type T can be implicitly converted to type U, we can -// safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is -// contravariant): just keep a copy of the original Matcher<U>, convert the -// argument from type T to U, and then pass it to the underlying Matcher<U>. -// The only exception is when U is a reference and T is not, as the -// underlying Matcher<U> may be interested in the argument's address, which -// is not preserved in the conversion from T to U. -template <typename T, typename U> -Matcher<T> SafeMatcherCast(const Matcher<U>& matcher) { - // Enforce that T can be implicitly converted to U. - GMOCK_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value), - T_must_be_implicitly_convertible_to_U); - // Enforce that we are not converting a non-reference type T to a reference - // type U. - GMOCK_COMPILE_ASSERT_( - internal::is_reference<T>::value || !internal::is_reference<U>::value, - cannot_convert_non_referentce_arg_to_reference); - // In case both T and U are arithmetic types, enforce that the - // conversion is not lossy. - typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(T)) RawT; - typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(U)) RawU; - const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther; - const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther; - GMOCK_COMPILE_ASSERT_( - kTIsOther || kUIsOther || - (internal::LosslessArithmeticConvertible<RawT, RawU>::value), - conversion_of_arithmetic_types_must_be_lossless); - return MatcherCast<T>(matcher); + // This overload handles monomorphic matchers. + // + // In general, if type T can be implicitly converted to type U, we can + // safely convert a Matcher<U> to a Matcher<T> (i.e. Matcher is + // contravariant): just keep a copy of the original Matcher<U>, convert the + // argument from type T to U, and then pass it to the underlying Matcher<U>. + // The only exception is when U is a reference and T is not, as the + // underlying Matcher<U> may be interested in the argument's address, which + // is not preserved in the conversion from T to U. + template <typename U> + static inline Matcher<T> Cast(const Matcher<U>& matcher) { + // Enforce that T can be implicitly converted to U. + GMOCK_COMPILE_ASSERT_((internal::ImplicitlyConvertible<T, U>::value), + T_must_be_implicitly_convertible_to_U); + // Enforce that we are not converting a non-reference type T to a reference + // type U. + GMOCK_COMPILE_ASSERT_( + internal::is_reference<T>::value || !internal::is_reference<U>::value, + cannot_convert_non_referentce_arg_to_reference); + // In case both T and U are arithmetic types, enforce that the + // conversion is not lossy. + typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(T)) RawT; + typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(U)) RawU; + const bool kTIsOther = GMOCK_KIND_OF_(RawT) == internal::kOther; + const bool kUIsOther = GMOCK_KIND_OF_(RawU) == internal::kOther; + GMOCK_COMPILE_ASSERT_( + kTIsOther || kUIsOther || + (internal::LosslessArithmeticConvertible<RawT, RawU>::value), + conversion_of_arithmetic_types_must_be_lossless); + return MatcherCast<T>(matcher); + } +}; + +template <typename T, typename M> +inline Matcher<T> SafeMatcherCast(const M& polymorphic_matcher) { + return SafeMatcherCastImpl<T>::Cast(polymorphic_matcher); } // A<T>() returns a matcher that matches any value of type T. @@ -612,6 +633,19 @@ GMOCK_IMPLEMENT_COMPARISON_MATCHER_(Ne, !=, "not equal to"); #undef GMOCK_IMPLEMENT_COMPARISON_MATCHER_ +// Implements the polymorphic IsNull() matcher, which matches any +// pointer that is NULL. +class IsNullMatcher { + public: + template <typename T> + bool Matches(T* p) const { return p == NULL; } + + void DescribeTo(::std::ostream* os) const { *os << "is NULL"; } + void DescribeNegationTo(::std::ostream* os) const { + *os << "is not NULL"; + } +}; + // Implements the polymorphic NotNull() matcher, which matches any // pointer that is not NULL. class NotNullMatcher { @@ -1614,7 +1648,7 @@ struct CallableTraits<ResType(*)(ArgType)> { typedef ResType(*StorageType)(ArgType); static void CheckIsValid(ResType(*f)(ArgType)) { - GMOCK_CHECK_(f != NULL) + GTEST_CHECK_(f != NULL) << "NULL function pointer is passed into ResultOf()."; } template <typename T> @@ -1867,6 +1901,367 @@ class ContainsMatcher { const M inner_matcher_; }; +// Implements Key(inner_matcher) for the given argument pair type. +// Key(inner_matcher) matches an std::pair whose 'first' field matches +// inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an +// std::map that contains at least one element whose key is >= 5. +template <typename PairType> +class KeyMatcherImpl : public MatcherInterface<PairType> { + public: + typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(PairType)) RawPairType; + typedef typename RawPairType::first_type KeyType; + + template <typename InnerMatcher> + explicit KeyMatcherImpl(InnerMatcher inner_matcher) + : inner_matcher_( + testing::SafeMatcherCast<const KeyType&>(inner_matcher)) { + } + + // Returns true iff 'key_value.first' (the key) matches the inner matcher. + virtual bool Matches(PairType key_value) const { + return inner_matcher_.Matches(key_value.first); + } + + // Describes what this matcher does. + virtual void DescribeTo(::std::ostream* os) const { + *os << "has a key that "; + inner_matcher_.DescribeTo(os); + } + + // Describes what the negation of this matcher does. + virtual void DescribeNegationTo(::std::ostream* os) const { + *os << "doesn't have a key that "; + inner_matcher_.DescribeTo(os); + } + + // Explains why 'key_value' matches, or doesn't match, this matcher. + virtual void ExplainMatchResultTo(PairType key_value, + ::std::ostream* os) const { + inner_matcher_.ExplainMatchResultTo(key_value.first, os); + } + + private: + const Matcher<const KeyType&> inner_matcher_; +}; + +// Implements polymorphic Key(matcher_for_key). +template <typename M> +class KeyMatcher { + public: + explicit KeyMatcher(M m) : matcher_for_key_(m) {} + + template <typename PairType> + operator Matcher<PairType>() const { + return MakeMatcher(new KeyMatcherImpl<PairType>(matcher_for_key_)); + } + + private: + const M matcher_for_key_; +}; + +// Implements Pair(first_matcher, second_matcher) for the given argument pair +// type with its two matchers. See Pair() function below. +template <typename PairType> +class PairMatcherImpl : public MatcherInterface<PairType> { + public: + typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(PairType)) RawPairType; + typedef typename RawPairType::first_type FirstType; + typedef typename RawPairType::second_type SecondType; + + template <typename FirstMatcher, typename SecondMatcher> + PairMatcherImpl(FirstMatcher first_matcher, SecondMatcher second_matcher) + : first_matcher_( + testing::SafeMatcherCast<const FirstType&>(first_matcher)), + second_matcher_( + testing::SafeMatcherCast<const SecondType&>(second_matcher)) { + } + + // Returns true iff 'a_pair.first' matches first_matcher and 'a_pair.second' + // matches second_matcher. + virtual bool Matches(PairType a_pair) const { + return first_matcher_.Matches(a_pair.first) && + second_matcher_.Matches(a_pair.second); + } + + // Describes what this matcher does. + virtual void DescribeTo(::std::ostream* os) const { + *os << "has a first field that "; + first_matcher_.DescribeTo(os); + *os << ", and has a second field that "; + second_matcher_.DescribeTo(os); + } + + // Describes what the negation of this matcher does. + virtual void DescribeNegationTo(::std::ostream* os) const { + *os << "has a first field that "; + first_matcher_.DescribeNegationTo(os); + *os << ", or has a second field that "; + second_matcher_.DescribeNegationTo(os); + } + + // Explains why 'a_pair' matches, or doesn't match, this matcher. + virtual void ExplainMatchResultTo(PairType a_pair, + ::std::ostream* os) const { + ::std::stringstream ss1; + first_matcher_.ExplainMatchResultTo(a_pair.first, &ss1); + internal::string s1 = ss1.str(); + if (s1 != "") { + s1 = "the first field " + s1; + } + + ::std::stringstream ss2; + second_matcher_.ExplainMatchResultTo(a_pair.second, &ss2); + internal::string s2 = ss2.str(); + if (s2 != "") { + s2 = "the second field " + s2; + } + + *os << s1; + if (s1 != "" && s2 != "") { + *os << ", and "; + } + *os << s2; + } + + private: + const Matcher<const FirstType&> first_matcher_; + const Matcher<const SecondType&> second_matcher_; +}; + +// Implements polymorphic Pair(first_matcher, second_matcher). +template <typename FirstMatcher, typename SecondMatcher> +class PairMatcher { + public: + PairMatcher(FirstMatcher first_matcher, SecondMatcher second_matcher) + : first_matcher_(first_matcher), second_matcher_(second_matcher) {} + + template <typename PairType> + operator Matcher<PairType> () const { + return MakeMatcher( + new PairMatcherImpl<PairType>( + first_matcher_, second_matcher_)); + } + + private: + const FirstMatcher first_matcher_; + const SecondMatcher second_matcher_; +}; + +// Implements ElementsAre() and ElementsAreArray(). +template <typename Container> +class ElementsAreMatcherImpl : public MatcherInterface<Container> { + public: + typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) RawContainer; + typedef internal::StlContainerView<RawContainer> View; + typedef typename View::type StlContainer; + typedef typename View::const_reference StlContainerReference; + typedef typename StlContainer::value_type Element; + + // Constructs the matcher from a sequence of element values or + // element matchers. + template <typename InputIter> + ElementsAreMatcherImpl(InputIter first, size_t count) { + matchers_.reserve(count); + InputIter it = first; + for (size_t i = 0; i != count; ++i, ++it) { + matchers_.push_back(MatcherCast<const Element&>(*it)); + } + } + + // Returns true iff 'container' matches. + virtual bool Matches(Container container) const { + StlContainerReference stl_container = View::ConstReference(container); + if (stl_container.size() != count()) + return false; + + typename StlContainer::const_iterator it = stl_container.begin(); + for (size_t i = 0; i != count(); ++it, ++i) { + if (!matchers_[i].Matches(*it)) + return false; + } + + return true; + } + + // Describes what this matcher does. + virtual void DescribeTo(::std::ostream* os) const { + if (count() == 0) { + *os << "is empty"; + } else if (count() == 1) { + *os << "has 1 element that "; + matchers_[0].DescribeTo(os); + } else { + *os << "has " << Elements(count()) << " where\n"; + for (size_t i = 0; i != count(); ++i) { + *os << "element " << i << " "; + matchers_[i].DescribeTo(os); + if (i + 1 < count()) { + *os << ",\n"; + } + } + } + } + + // Describes what the negation of this matcher does. + virtual void DescribeNegationTo(::std::ostream* os) const { + if (count() == 0) { + *os << "is not empty"; + return; + } + + *os << "does not have " << Elements(count()) << ", or\n"; + for (size_t i = 0; i != count(); ++i) { + *os << "element " << i << " "; + matchers_[i].DescribeNegationTo(os); + if (i + 1 < count()) { + *os << ", or\n"; + } + } + } + + // Explains why 'container' matches, or doesn't match, this matcher. + virtual void ExplainMatchResultTo(Container container, + ::std::ostream* os) const { + StlContainerReference stl_container = View::ConstReference(container); + if (Matches(container)) { + // We need to explain why *each* element matches (the obvious + // ones can be skipped). + + bool reason_printed = false; + typename StlContainer::const_iterator it = stl_container.begin(); + for (size_t i = 0; i != count(); ++it, ++i) { + ::std::stringstream ss; + matchers_[i].ExplainMatchResultTo(*it, &ss); + + const string s = ss.str(); + if (!s.empty()) { + if (reason_printed) { + *os << ",\n"; + } + *os << "element " << i << " " << s; + reason_printed = true; + } + } + } else { + // We need to explain why the container doesn't match. + const size_t actual_count = stl_container.size(); + if (actual_count != count()) { + // The element count doesn't match. If the container is + // empty, there's no need to explain anything as Google Mock + // already prints the empty container. Otherwise we just need + // to show how many elements there actually are. + if (actual_count != 0) { + *os << "has " << Elements(actual_count); + } + return; + } + + // The container has the right size but at least one element + // doesn't match expectation. We need to find this element and + // explain why it doesn't match. + typename StlContainer::const_iterator it = stl_container.begin(); + for (size_t i = 0; i != count(); ++it, ++i) { + if (matchers_[i].Matches(*it)) { + continue; + } + + *os << "element " << i << " doesn't match"; + + ::std::stringstream ss; + matchers_[i].ExplainMatchResultTo(*it, &ss); + const string s = ss.str(); + if (!s.empty()) { + *os << " (" << s << ")"; + } + return; + } + } + } + + private: + static Message Elements(size_t count) { + return Message() << count << (count == 1 ? " element" : " elements"); + } + + size_t count() const { return matchers_.size(); } + std::vector<Matcher<const Element&> > matchers_; +}; + +// Implements ElementsAre() of 0 arguments. +class ElementsAreMatcher0 { + public: + ElementsAreMatcher0() {} + + template <typename Container> + operator Matcher<Container>() const { + typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) + RawContainer; + typedef typename internal::StlContainerView<RawContainer>::type::value_type + Element; + + const Matcher<const Element&>* const matchers = NULL; + return MakeMatcher(new ElementsAreMatcherImpl<Container>(matchers, 0)); + } +}; + +// Implements ElementsAreArray(). +template <typename T> +class ElementsAreArrayMatcher { + public: + ElementsAreArrayMatcher(const T* first, size_t count) : + first_(first), count_(count) {} + + template <typename Container> + operator Matcher<Container>() const { + typedef GMOCK_REMOVE_CONST_(GMOCK_REMOVE_REFERENCE_(Container)) + RawContainer; + typedef typename internal::StlContainerView<RawContainer>::type::value_type + Element; + + return MakeMatcher(new ElementsAreMatcherImpl<Container>(first_, count_)); + } + + private: + const T* const first_; + const size_t count_; +}; + +// Constants denoting interpolations in a matcher description string. +const int kTupleInterpolation = -1; // "%(*)s" +const int kPercentInterpolation = -2; // "%%" +const int kInvalidInterpolation = -3; // "%" followed by invalid text + +// Records the location and content of an interpolation. +struct Interpolation { + Interpolation(const char* start, const char* end, int param) + : start_pos(start), end_pos(end), param_index(param) {} + + // Points to the start of the interpolation (the '%' character). + const char* start_pos; + // Points to the first character after the interpolation. + const char* end_pos; + // 0-based index of the interpolated matcher parameter; + // kTupleInterpolation for "%(*)s"; kPercentInterpolation for "%%". + int param_index; +}; + +typedef ::std::vector<Interpolation> Interpolations; + +// Parses a matcher description string and returns a vector of +// interpolations that appear in the string; generates non-fatal +// failures iff 'description' is an invalid matcher description. +// 'param_names' is a NULL-terminated array of parameter names in the +// order they appear in the MATCHER_P*() parameter list. +Interpolations ValidateMatcherDescription( + const char* param_names[], const char* description); + +// Returns the actual matcher description, given the matcher name, +// user-supplied description template string, interpolations in the +// string, and the printed values of the matcher parameters. +string FormatMatcherDescription( + const char* matcher_name, const char* description, + const Interpolations& interp, const Strings& param_values); + } // namespace internal // Implements MatcherCast(). @@ -1949,6 +2344,11 @@ inline internal::NeMatcher<Rhs> Ne(Rhs x) { return internal::NeMatcher<Rhs>(x); } +// Creates a polymorphic matcher that matches any NULL pointer. +inline PolymorphicMatcher<internal::IsNullMatcher > IsNull() { + return MakePolymorphicMatcher(internal::IsNullMatcher()); +} + // Creates a polymorphic matcher that matches any non-NULL pointer. // This is convenient as Not(NULL) doesn't compile (the compiler // thinks that that expression is comparing a pointer with an integer). @@ -2332,7 +2732,8 @@ inline PolymorphicMatcher<internal::ContainerEqMatcher< // // ::std::map<int, size_t> page_lengths; // page_lengths[1] = 100; -// EXPECT_THAT(map_int, Contains(::std::pair<const int, size_t>(1, 100))); +// EXPECT_THAT(page_lengths, +// Contains(::std::pair<const int, size_t>(1, 100))); // // const char* user_ids[] = { "joe", "mike", "tom" }; // EXPECT_THAT(user_ids, Contains(Eq(::std::string("tom")))); @@ -2341,6 +2742,26 @@ inline internal::ContainsMatcher<M> Contains(M matcher) { return internal::ContainsMatcher<M>(matcher); } +// Key(inner_matcher) matches an std::pair whose 'first' field matches +// inner_matcher. For example, Contains(Key(Ge(5))) can be used to match an +// std::map that contains at least one element whose key is >= 5. +template <typename M> +inline internal::KeyMatcher<M> Key(M inner_matcher) { + return internal::KeyMatcher<M>(inner_matcher); +} + +// Pair(first_matcher, second_matcher) matches a std::pair whose 'first' field +// matches first_matcher and whose 'second' field matches second_matcher. For +// example, EXPECT_THAT(map_type, ElementsAre(Pair(Ge(5), "foo"))) can be used +// to match a std::map<int, string> that contains exactly one element whose key +// is >= 5 and whose value equals "foo". +template <typename FirstMatcher, typename SecondMatcher> +inline internal::PairMatcher<FirstMatcher, SecondMatcher> +Pair(FirstMatcher first_matcher, SecondMatcher second_matcher) { + return internal::PairMatcher<FirstMatcher, SecondMatcher>( + first_matcher, second_matcher); +} + // Returns a predicate that is satisfied by anything that matches the // given matcher. template <typename M> diff --git a/testing/gmock/include/gmock/gmock-more-actions.h b/testing/gmock/include/gmock/gmock-more-actions.h new file mode 100644 index 0000000..c6fa6bb --- /dev/null +++ b/testing/gmock/include/gmock/gmock-more-actions.h @@ -0,0 +1,190 @@ +// Copyright 2007, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Author: wan@google.com (Zhanyong Wan) + +// Google Mock - a framework for writing C++ mock classes. +// +// This file implements some actions that depend on gmock-generated-actions.h. + +#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ +#define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ + +#include <gmock/gmock-generated-actions.h> + +namespace testing { +namespace internal { + +// Implements the Invoke(f) action. The template argument +// FunctionImpl is the implementation type of f, which can be either a +// function pointer or a functor. Invoke(f) can be used as an +// Action<F> as long as f's type is compatible with F (i.e. f can be +// assigned to a tr1::function<F>). +template <typename FunctionImpl> +class InvokeAction { + public: + // The c'tor makes a copy of function_impl (either a function + // pointer or a functor). + explicit InvokeAction(FunctionImpl function_impl) + : function_impl_(function_impl) {} + + template <typename Result, typename ArgumentTuple> + Result Perform(const ArgumentTuple& args) { + return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args); + } + private: + FunctionImpl function_impl_; +}; + +// Implements the Invoke(object_ptr, &Class::Method) action. +template <class Class, typename MethodPtr> +class InvokeMethodAction { + public: + InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr) + : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {} + + template <typename Result, typename ArgumentTuple> + Result Perform(const ArgumentTuple& args) const { + return InvokeHelper<Result, ArgumentTuple>::InvokeMethod( + obj_ptr_, method_ptr_, args); + } + private: + Class* const obj_ptr_; + const MethodPtr method_ptr_; +}; + +} // namespace internal + +// Various overloads for Invoke(). + +// Creates an action that invokes 'function_impl' with the mock +// function's arguments. +template <typename FunctionImpl> +PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke( + FunctionImpl function_impl) { + return MakePolymorphicAction( + internal::InvokeAction<FunctionImpl>(function_impl)); +} + +// Creates an action that invokes the given method on the given object +// with the mock function's arguments. +template <class Class, typename MethodPtr> +PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke( + Class* obj_ptr, MethodPtr method_ptr) { + return MakePolymorphicAction( + internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr)); +} + +// WithoutArgs(inner_action) can be used in a mock function with a +// non-empty argument list to perform inner_action, which takes no +// argument. In other words, it adapts an action accepting no +// argument to one that accepts (and ignores) arguments. +template <typename InnerAction> +inline internal::WithArgsAction<InnerAction> +WithoutArgs(const InnerAction& action) { + return internal::WithArgsAction<InnerAction>(action); +} + +// WithArg<k>(an_action) creates an action that passes the k-th +// (0-based) argument of the mock function to an_action and performs +// it. It adapts an action accepting one argument to one that accepts +// multiple arguments. For convenience, we also provide +// WithArgs<k>(an_action) (defined below) as a synonym. +template <int k, typename InnerAction> +inline internal::WithArgsAction<InnerAction, k> +WithArg(const InnerAction& action) { + return internal::WithArgsAction<InnerAction, k>(action); +} + +// Action ReturnArg<k>() returns the k-th argument of the mock function. +ACTION_TEMPLATE(ReturnArg, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_0_VALUE_PARAMS()) { + return std::tr1::get<k>(args); +} + +// Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the +// mock function to *pointer. +ACTION_TEMPLATE(SaveArg, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_1_VALUE_PARAMS(pointer)) { + *pointer = ::std::tr1::get<k>(args); +} + +// Action SetArgReferee<k>(value) assigns 'value' to the variable +// referenced by the k-th (0-based) argument of the mock function. +ACTION_TEMPLATE(SetArgReferee, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_1_VALUE_PARAMS(value)) { + typedef typename ::std::tr1::tuple_element<k, args_type>::type argk_type; + // Ensures that argument #k is a reference. If you get a compiler + // error on the next line, you are using SetArgReferee<k>(value) in + // a mock function whose k-th (0-based) argument is not a reference. + GMOCK_COMPILE_ASSERT_(internal::is_reference<argk_type>::value, + SetArgReferee_must_be_used_with_a_reference_argument); + ::std::tr1::get<k>(args) = value; +} + +// Action SetArrayArgument<k>(first, last) copies the elements in +// source range [first, last) to the array pointed to by the k-th +// (0-based) argument, which can be either a pointer or an +// iterator. The action does not take ownership of the elements in the +// source range. +ACTION_TEMPLATE(SetArrayArgument, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_2_VALUE_PARAMS(first, last)) { + // Microsoft compiler deprecates ::std::copy, so we want to suppress warning + // 4996 (Function call with parameters that may be unsafe) there. +#ifdef _MSC_VER +#pragma warning(push) // Saves the current warning state. +#pragma warning(disable:4996) // Temporarily disables warning 4996. +#endif + ::std::copy(first, last, ::std::tr1::get<k>(args)); +#ifdef _MSC_VER +#pragma warning(pop) // Restores the warning state. +#endif +} + +// Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock +// function. +ACTION_TEMPLATE(DeleteArg, + HAS_1_TEMPLATE_PARAMS(int, k), + AND_0_VALUE_PARAMS()) { + delete ::std::tr1::get<k>(args); +} + +// Action Throw(exception) can be used in a mock function of any type +// to throw the given exception. Any copyable value can be thrown. +#if GTEST_HAS_EXCEPTIONS +ACTION_P(Throw, exception) { throw exception; } +#endif // GTEST_HAS_EXCEPTIONS + +} // namespace testing + +#endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_ diff --git a/testing/gmock/include/gmock/gmock-printers.h b/testing/gmock/include/gmock/gmock-printers.h index 561de3d..e07d92a 100644 --- a/testing/gmock/include/gmock/gmock-printers.h +++ b/testing/gmock/include/gmock/gmock-printers.h @@ -129,14 +129,21 @@ class TypeWithoutFormatter { sizeof(value), os); } }; + +// We print a protobuf using its ShortDebugString() when the string +// doesn't exceed this many characters; otherwise we print it using +// DebugString() for better readability. +const size_t kProtobufOneLinerMaxLength = 50; + template <typename T> class TypeWithoutFormatter<T, true> { public: static void PrintValue(const T& value, ::std::ostream* os) { - // Both ProtocolMessage and proto2::Message have the - // ShortDebugString() method, so the same implementation works for - // both. - ::std::operator<<(*os, "<" + value.ShortDebugString() + ">"); + const ::testing::internal::string short_str = value.ShortDebugString(); + const ::testing::internal::string pretty_str = + short_str.length() <= kProtobufOneLinerMaxLength ? + short_str : ("\n" + value.DebugString()); + ::std::operator<<(*os, "<" + pretty_str + ">"); } }; @@ -272,9 +279,12 @@ void DefaultPrintTo(IsNotContainer /* dummy */, if (p == NULL) { *os << "NULL"; } else { - // We cannot use implicit_cast or static_cast here, as they don't - // work when p is a function pointer. - *os << reinterpret_cast<const void*>(p); + // We want to print p as a const void*. However, we cannot cast + // it to const void* directly, even using reinterpret_cast, as + // earlier versions of gcc (e.g. 3.4.5) cannot compile the cast + // when p is a function pointer. Casting to UInt64 first solves + // the problem. + *os << reinterpret_cast<const void*>(reinterpret_cast<internal::UInt64>(p)); } } diff --git a/testing/gmock/include/gmock/gmock-spec-builders.h b/testing/gmock/include/gmock/gmock-spec-builders.h index a22bcd1..765e06d 100644 --- a/testing/gmock/include/gmock/gmock-spec-builders.h +++ b/testing/gmock/include/gmock/gmock-spec-builders.h @@ -49,13 +49,13 @@ // .With(multi-argument-matchers) // .Times(cardinality) // .InSequence(sequences) +// .After(expectations) // .WillOnce(action) // .WillRepeatedly(action) // .RetiresOnSaturation(); // -// where all clauses are optional, .InSequence() and .WillOnce() can -// appear any number of times, and .Times() can be omitted only if -// .WillOnce() or .WillRepeatedly() is present. +// where all clauses are optional, and .InSequence()/.After()/ +// .WillOnce() can appear any number of times. #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ #define GMOCK_INCLUDE_GMOCK_GMOCK_SPEC_BUILDERS_H_ @@ -76,22 +76,30 @@ namespace testing { +// An abstract handle of an expectation. +class Expectation; + +// A set of expectation handles. +class ExpectationSet; + // Anything inside the 'internal' namespace IS INTERNAL IMPLEMENTATION // and MUST NOT BE USED IN USER CODE!!! namespace internal { -template <typename F> -class FunctionMocker; +// Implements a mock function. +template <typename F> class FunctionMocker; // Base class for expectations. class ExpectationBase; +// Implements an expectation. +template <typename F> class TypedExpectation; + // Helper class for testing the Expectation class template. class ExpectationTester; // Base class for function mockers. -template <typename F> -class FunctionMockerBase; +template <typename F> class FunctionMockerBase; // Protects the mock object registry (in class Mock), all function // mockers, and all expectations. @@ -232,7 +240,8 @@ class DefaultActionSpec { Clause last_clause_; }; // class DefaultActionSpec -// Possible reactions on uninteresting calls. +// Possible reactions on uninteresting calls. TODO(wan@google.com): +// rename the enum values to the kFoo style. enum CallReaction { ALLOW, WARN, @@ -327,31 +336,173 @@ class Mock { static void UnregisterLocked(internal::UntypedFunctionMockerBase* mocker); }; // class Mock +// An abstract handle of an expectation. Useful in the .After() +// clause of EXPECT_CALL() for setting the (partial) order of +// expectations. The syntax: +// +// Expectation e1 = EXPECT_CALL(...)...; +// EXPECT_CALL(...).After(e1)...; +// +// sets two expectations where the latter can only be matched after +// the former has been satisfied. +// +// Notes: +// - This class is copyable and has value semantics. +// - Constness is shallow: a const Expectation object itself cannot +// be modified, but the mutable methods of the ExpectationBase +// object it references can be called via expectation_base(). +// - The constructors and destructor are defined out-of-line because +// the Symbian WINSCW compiler wants to otherwise instantiate them +// when it sees this class definition, at which point it doesn't have +// ExpectationBase available yet, leading to incorrect destruction +// in the linked_ptr (or compilation errors if using a checking +// linked_ptr). +class Expectation { + public: + // Constructs a null object that doesn't reference any expectation. + Expectation(); + + ~Expectation(); + + // This single-argument ctor must not be explicit, in order to support the + // Expectation e = EXPECT_CALL(...); + // syntax. + // + // A TypedExpectation object stores its pre-requisites as + // Expectation objects, and needs to call the non-const Retire() + // method on the ExpectationBase objects they reference. Therefore + // Expectation must receive a *non-const* reference to the + // ExpectationBase object. + Expectation(internal::ExpectationBase& exp); // NOLINT + + // The compiler-generated copy ctor and operator= work exactly as + // intended, so we don't need to define our own. + + // Returns true iff rhs references the same expectation as this object does. + bool operator==(const Expectation& rhs) const { + return expectation_base_ == rhs.expectation_base_; + } + + bool operator!=(const Expectation& rhs) const { return !(*this == rhs); } + + private: + friend class ExpectationSet; + friend class Sequence; + friend class ::testing::internal::ExpectationBase; + + template <typename F> + friend class ::testing::internal::FunctionMockerBase; + + template <typename F> + friend class ::testing::internal::TypedExpectation; + + // This comparator is needed for putting Expectation objects into a set. + class Less { + public: + bool operator()(const Expectation& lhs, const Expectation& rhs) const { + return lhs.expectation_base_.get() < rhs.expectation_base_.get(); + } + }; + + typedef ::std::set<Expectation, Less> Set; + + Expectation( + const internal::linked_ptr<internal::ExpectationBase>& expectation_base); + + // Returns the expectation this object references. + const internal::linked_ptr<internal::ExpectationBase>& + expectation_base() const { + return expectation_base_; + } + + // A linked_ptr that co-owns the expectation this handle references. + internal::linked_ptr<internal::ExpectationBase> expectation_base_; +}; + +// A set of expectation handles. Useful in the .After() clause of +// EXPECT_CALL() for setting the (partial) order of expectations. The +// syntax: +// +// ExpectationSet es; +// es += EXPECT_CALL(...)...; +// es += EXPECT_CALL(...)...; +// EXPECT_CALL(...).After(es)...; +// +// sets three expectations where the last one can only be matched +// after the first two have both been satisfied. +// +// This class is copyable and has value semantics. +class ExpectationSet { + public: + // A bidirectional iterator that can read a const element in the set. + typedef Expectation::Set::const_iterator const_iterator; + + // An object stored in the set. This is an alias of Expectation. + typedef Expectation::Set::value_type value_type; + + // Constructs an empty set. + ExpectationSet() {} + + // This single-argument ctor must not be explicit, in order to support the + // ExpectationSet es = EXPECT_CALL(...); + // syntax. + ExpectationSet(internal::ExpectationBase& exp) { // NOLINT + *this += Expectation(exp); + } + + // This single-argument ctor implements implicit conversion from + // Expectation and thus must not be explicit. This allows either an + // Expectation or an ExpectationSet to be used in .After(). + ExpectationSet(const Expectation& e) { // NOLINT + *this += e; + } + + // The compiler-generator ctor and operator= works exactly as + // intended, so we don't need to define our own. + + // Returns true iff rhs contains the same set of Expectation objects + // as this does. + bool operator==(const ExpectationSet& rhs) const { + return expectations_ == rhs.expectations_; + } + + bool operator!=(const ExpectationSet& rhs) const { return !(*this == rhs); } + + // Implements the syntax + // expectation_set += EXPECT_CALL(...); + ExpectationSet& operator+=(const Expectation& e) { + expectations_.insert(e); + return *this; + } + + int size() const { return static_cast<int>(expectations_.size()); } + + const_iterator begin() const { return expectations_.begin(); } + const_iterator end() const { return expectations_.end(); } + + private: + Expectation::Set expectations_; +}; + + // Sequence objects are used by a user to specify the relative order // in which the expectations should match. They are copyable (we rely // on the compiler-defined copy constructor and assignment operator). class Sequence { public: // Constructs an empty sequence. - Sequence() - : last_expectation_( - new internal::linked_ptr<internal::ExpectationBase>(NULL)) {} + Sequence() : last_expectation_(new Expectation) {} // Adds an expectation to this sequence. The caller must ensure // that no other thread is accessing this Sequence object. - void AddExpectation( - const internal::linked_ptr<internal::ExpectationBase>& expectation) const; + void AddExpectation(const Expectation& expectation) const; + private: - // The last expectation in this sequence. We use a nested - // linked_ptr here because: - // - Sequence objects are copyable, and we want the copies to act - // as aliases. The outer linked_ptr allows the copies to co-own - // and share the same state. - // - An Expectation object is co-owned (via linked_ptr) by its - // FunctionMocker and its successors (other Expectation objects). - // Hence the inner linked_ptr. - internal::linked_ptr<internal::linked_ptr<internal::ExpectationBase> > - last_expectation_; + // The last expectation in this sequence. We use a linked_ptr here + // because Sequence objects are copyable and we want the copies to + // be aliases. The linked_ptr allows the copies to co-own and share + // the same Expectation object. + internal::linked_ptr<Expectation> last_expectation_; }; // class Sequence // An object of this type causes all EXPECT_CALL() statements @@ -431,9 +582,7 @@ class ExpectationBase { // L >= g_gmock_mutex virtual void DescribeCallCountTo(::std::ostream* os) const = 0; protected: - typedef std::set<linked_ptr<ExpectationBase>, - LinkedPtrLessThan<ExpectationBase> > - ExpectationBaseSet; + friend class ::testing::Expectation; enum Clause { // Don't change the order of the enum members! @@ -441,11 +590,16 @@ class ExpectationBase { kWith, kTimes, kInSequence, + kAfter, kWillOnce, kWillRepeatedly, kRetiresOnSaturation, }; + // Returns an Expectation object that references and co-owns this + // expectation. + virtual Expectation GetHandle() = 0; + // Asserts that the EXPECT_CALL() statement has the given property. void AssertSpecProperty(bool property, const string& failure_message) const { Assert(property, file_, line_, failure_message); @@ -518,7 +672,7 @@ class ExpectationBase { // Adds unsatisfied pre-requisites of this expectation to 'result'. // L >= g_gmock_mutex - void FindUnsatisfiedPrerequisites(ExpectationBaseSet* result) const; + void FindUnsatisfiedPrerequisites(ExpectationSet* result) const; // Returns the number this expectation has been invoked. // L >= g_gmock_mutex @@ -539,7 +693,7 @@ class ExpectationBase { friend class ::testing::internal::ExpectationTester; template <typename Function> - friend class Expectation; + friend class TypedExpectation; // This group of fields are part of the spec and won't change after // an EXPECT_CALL() statement finishes. @@ -548,11 +702,13 @@ class ExpectationBase { // True iff the cardinality is specified explicitly. bool cardinality_specified_; Cardinality cardinality_; // The cardinality of the expectation. - // The immediate pre-requisites of this expectation. We use - // linked_ptr in the set because we want an Expectation object to be - // co-owned by its FunctionMocker and its successors. This allows - // multiple mock objects to be deleted at different times. - ExpectationBaseSet immediate_prerequisites_; + // The immediate pre-requisites (i.e. expectations that must be + // satisfied before this expectation can be matched) of this + // expectation. We use linked_ptr in the set because we want an + // Expectation object to be co-owned by its FunctionMocker and its + // successors. This allows multiple mock objects to be deleted at + // different times. + ExpectationSet immediate_prerequisites_; // This group of fields are the current state of the expectation, // and can change as the mock function is called. @@ -562,14 +718,14 @@ class ExpectationBase { // Impements an expectation for the given function type. template <typename F> -class Expectation : public ExpectationBase { +class TypedExpectation : public ExpectationBase { public: typedef typename Function<F>::ArgumentTuple ArgumentTuple; typedef typename Function<F>::ArgumentMatcherTuple ArgumentMatcherTuple; typedef typename Function<F>::Result Result; - Expectation(FunctionMockerBase<F>* owner, const char* file, int line, - const ArgumentMatcherTuple& m) + TypedExpectation(FunctionMockerBase<F>* owner, const char* file, int line, + const ArgumentMatcherTuple& m) : ExpectationBase(file, line), owner_(owner), matchers_(m), @@ -584,14 +740,14 @@ class Expectation : public ExpectationBase { last_clause_(kNone), action_count_checked_(false) {} - virtual ~Expectation() { + virtual ~TypedExpectation() { // Check the validity of the action count if it hasn't been done // yet (for example, if the expectation was never used). CheckActionCountIfNotDone(); } // Implements the .With() clause. - Expectation& With(const Matcher<const ArgumentTuple&>& m) { + TypedExpectation& With(const Matcher<const ArgumentTuple&>& m) { if (last_clause_ == kWith) { ExpectSpecProperty(false, ".With() cannot appear " @@ -608,7 +764,7 @@ class Expectation : public ExpectationBase { } // Implements the .Times() clause. - Expectation& Times(const Cardinality& cardinality) { + TypedExpectation& Times(const Cardinality& cardinality) { if (last_clause_ ==kTimes) { ExpectSpecProperty(false, ".Times() cannot appear " @@ -626,40 +782,70 @@ class Expectation : public ExpectationBase { } // Implements the .Times() clause. - Expectation& Times(int n) { + TypedExpectation& Times(int n) { return Times(Exactly(n)); } // Implements the .InSequence() clause. - Expectation& InSequence(const Sequence& s) { + TypedExpectation& InSequence(const Sequence& s) { ExpectSpecProperty(last_clause_ <= kInSequence, - ".InSequence() cannot appear after .WillOnce()," - " .WillRepeatedly(), or " + ".InSequence() cannot appear after .After()," + " .WillOnce(), .WillRepeatedly(), or " ".RetiresOnSaturation()."); last_clause_ = kInSequence; - s.AddExpectation(owner_->GetLinkedExpectationBase(this)); + s.AddExpectation(GetHandle()); return *this; } - Expectation& InSequence(const Sequence& s1, const Sequence& s2) { + TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2) { return InSequence(s1).InSequence(s2); } - Expectation& InSequence(const Sequence& s1, const Sequence& s2, - const Sequence& s3) { + TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2, + const Sequence& s3) { return InSequence(s1, s2).InSequence(s3); } - Expectation& InSequence(const Sequence& s1, const Sequence& s2, - const Sequence& s3, const Sequence& s4) { + TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2, + const Sequence& s3, const Sequence& s4) { return InSequence(s1, s2, s3).InSequence(s4); } - Expectation& InSequence(const Sequence& s1, const Sequence& s2, - const Sequence& s3, const Sequence& s4, - const Sequence& s5) { + TypedExpectation& InSequence(const Sequence& s1, const Sequence& s2, + const Sequence& s3, const Sequence& s4, + const Sequence& s5) { return InSequence(s1, s2, s3, s4).InSequence(s5); } + // Implements that .After() clause. + TypedExpectation& After(const ExpectationSet& s) { + ExpectSpecProperty(last_clause_ <= kAfter, + ".After() cannot appear after .WillOnce()," + " .WillRepeatedly(), or " + ".RetiresOnSaturation()."); + last_clause_ = kAfter; + + for (ExpectationSet::const_iterator it = s.begin(); it != s.end(); ++it) { + immediate_prerequisites_ += *it; + } + return *this; + } + TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2) { + return After(s1).After(s2); + } + TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2, + const ExpectationSet& s3) { + return After(s1, s2).After(s3); + } + TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2, + const ExpectationSet& s3, const ExpectationSet& s4) { + return After(s1, s2, s3).After(s4); + } + TypedExpectation& After(const ExpectationSet& s1, const ExpectationSet& s2, + const ExpectationSet& s3, const ExpectationSet& s4, + const ExpectationSet& s5) { + return After(s1, s2, s3, s4).After(s5); + } + // Implements the .WillOnce() clause. - Expectation& WillOnce(const Action<F>& action) { + TypedExpectation& WillOnce(const Action<F>& action) { ExpectSpecProperty(last_clause_ <= kWillOnce, ".WillOnce() cannot appear after " ".WillRepeatedly() or .RetiresOnSaturation()."); @@ -673,7 +859,7 @@ class Expectation : public ExpectationBase { } // Implements the .WillRepeatedly() clause. - Expectation& WillRepeatedly(const Action<F>& action) { + TypedExpectation& WillRepeatedly(const Action<F>& action) { if (last_clause_ == kWillRepeatedly) { ExpectSpecProperty(false, ".WillRepeatedly() cannot appear " @@ -698,7 +884,7 @@ class Expectation : public ExpectationBase { } // Implements the .RetiresOnSaturation() clause. - Expectation& RetiresOnSaturation() { + TypedExpectation& RetiresOnSaturation() { ExpectSpecProperty(last_clause_ < kRetiresOnSaturation, ".RetiresOnSaturation() cannot appear " "more than once."); @@ -756,6 +942,12 @@ class Expectation : public ExpectationBase { template <typename Function> friend class FunctionMockerBase; + // Returns an Expectation object that references and co-owns this + // expectation. + virtual Expectation GetHandle() { + return owner_->GetHandleOf(this); + } + // The following methods will be called only after the EXPECT_CALL() // statement finishes and when the current thread holds // g_gmock_mutex. @@ -807,12 +999,12 @@ class Expectation : public ExpectationBase { *os << " Expected: all pre-requisites are satisfied\n" << " Actual: the following immediate pre-requisites " << "are not satisfied:\n"; - ExpectationBaseSet unsatisfied_prereqs; + ExpectationSet unsatisfied_prereqs; FindUnsatisfiedPrerequisites(&unsatisfied_prereqs); int i = 0; - for (ExpectationBaseSet::const_iterator it = unsatisfied_prereqs.begin(); + for (ExpectationSet::const_iterator it = unsatisfied_prereqs.begin(); it != unsatisfied_prereqs.end(); ++it) { - (*it)->DescribeLocationTo(os); + it->expectation_base()->DescribeLocationTo(os); *os << "pre-requisite #" << i++ << "\n"; } *os << " (end of pre-requisites)\n"; @@ -957,7 +1149,7 @@ class Expectation : public ExpectationBase { Clause last_clause_; mutable bool action_count_checked_; // Under mutex_. mutable Mutex mutex_; // Protects action_count_checked_. -}; // class Expectation +}; // class TypedExpectation // A MockSpec object is used by ON_CALL() or EXPECT_CALL() for // specifying the default behavior of, or expectation on, a mock @@ -992,7 +1184,7 @@ class MockSpec { // Adds a new expectation spec to the function mocker and returns // the newly created spec. - internal::Expectation<F>& InternalExpectedAt( + internal::TypedExpectation<F>& InternalExpectedAt( const char* file, int line, const char* obj, const char* call) { LogWithLocation(internal::INFO, file, line, string("EXPECT_CALL(") + obj + ", " + call + ") invoked"); @@ -1247,18 +1439,18 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { // Adds and returns an expectation spec for this mock function. // L < g_gmock_mutex - Expectation<F>& AddNewExpectation( + TypedExpectation<F>& AddNewExpectation( const char* file, int line, const ArgumentMatcherTuple& m) { Mock::RegisterUseByOnCallOrExpectCall(MockObject(), file, line); - const linked_ptr<Expectation<F> > expectation( - new Expectation<F>(this, file, line, m)); + const linked_ptr<TypedExpectation<F> > expectation( + new TypedExpectation<F>(this, file, line, m)); expectations_.push_back(expectation); // Adds this expectation into the implicit sequence if there is one. Sequence* const implicit_sequence = g_gmock_implicit_sequence.get(); if (implicit_sequence != NULL) { - implicit_sequence->AddExpectation(expectation); + implicit_sequence->AddExpectation(Expectation(expectation)); } return *expectation; @@ -1268,22 +1460,23 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { // being described on this function mocker. MockSpec<F>& current_spec() { return current_spec_; } private: - template <typename Func> friend class Expectation; + template <typename Func> friend class TypedExpectation; - typedef std::vector<internal::linked_ptr<Expectation<F> > > Expectations; + typedef std::vector<internal::linked_ptr<TypedExpectation<F> > > + TypedExpectations; - // Gets the internal::linked_ptr<ExpectationBase> object that co-owns 'exp'. - internal::linked_ptr<ExpectationBase> GetLinkedExpectationBase( - Expectation<F>* exp) { - for (typename Expectations::const_iterator it = expectations_.begin(); + // Returns an Expectation object that references and co-owns exp, + // which must be an expectation on this mock function. + Expectation GetHandleOf(TypedExpectation<F>* exp) { + for (typename TypedExpectations::const_iterator it = expectations_.begin(); it != expectations_.end(); ++it) { if (it->get() == exp) { - return *it; + return Expectation(*it); } } Assert(false, __FILE__, __LINE__, "Cannot find expectation."); - return internal::linked_ptr<ExpectationBase>(NULL); + return Expectation(); // The above statement is just to make the code compile, and will // never be executed. } @@ -1330,7 +1523,7 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { // mock function) and excessive locking could cause a dead lock. // L < g_gmock_mutex bool FindMatchingExpectationAndAction( - const ArgumentTuple& args, Expectation<F>** exp, Action<F>* action, + const ArgumentTuple& args, TypedExpectation<F>** exp, Action<F>* action, bool* is_excessive, ::std::ostream* what, ::std::ostream* why) { MutexLock l(&g_gmock_mutex); *exp = this->FindMatchingExpectationLocked(args); @@ -1351,13 +1544,13 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { // Returns the expectation that matches the arguments, or NULL if no // expectation matches them. // L >= g_gmock_mutex - Expectation<F>* FindMatchingExpectationLocked( + TypedExpectation<F>* FindMatchingExpectationLocked( const ArgumentTuple& args) const { g_gmock_mutex.AssertHeld(); - for (typename Expectations::const_reverse_iterator it = + for (typename TypedExpectations::const_reverse_iterator it = expectations_.rbegin(); it != expectations_.rend(); ++it) { - Expectation<F>* const exp = it->get(); + TypedExpectation<F>* const exp = it->get(); if (exp->ShouldHandleArguments(args)) { return exp; } @@ -1415,7 +1608,7 @@ class FunctionMockerBase : public UntypedFunctionMockerBase { // All default action specs for this function mocker. std::vector<DefaultActionSpec<F> > default_actions_; // All expectations for this function mocker. - Expectations expectations_; + TypedExpectations expectations_; // There is no generally useful and implementable semantics of // copying a mock object, so copying a mock is usually a user error. @@ -1446,9 +1639,9 @@ template <typename F> bool FunctionMockerBase<F>::VerifyAndClearExpectationsLocked() { g_gmock_mutex.AssertHeld(); bool expectations_met = true; - for (typename Expectations::const_iterator it = expectations_.begin(); + for (typename TypedExpectations::const_iterator it = expectations_.begin(); it != expectations_.end(); ++it) { - Expectation<F>* const exp = it->get(); + TypedExpectation<F>* const exp = it->get(); if (exp->IsOverSaturated()) { // There was an upper-bound violation. Since the error was @@ -1532,7 +1725,7 @@ typename Function<F>::Result FunctionMockerBase<F>::InvokeWith( ::std::stringstream why; ::std::stringstream loc; Action<F> action; - Expectation<F>* exp; + TypedExpectation<F>* exp; // The FindMatchingExpectationAndAction() function acquires and // releases g_gmock_mutex. @@ -1605,6 +1798,10 @@ using internal::MockSpec; template <typename T> inline const T& Const(const T& x) { return x; } +// Constructs an Expectation object that references and co-owns exp. +inline Expectation::Expectation(internal::ExpectationBase& exp) // NOLINT + : expectation_base_(exp.GetHandle().expectation_base()) {} + } // namespace testing // A separate macro is required to avoid compile errors when the name diff --git a/testing/gmock/include/gmock/gmock.h b/testing/gmock/include/gmock/gmock.h index 29d9727..daf5288 100644 --- a/testing/gmock/include/gmock/gmock.h +++ b/testing/gmock/include/gmock/gmock.h @@ -60,6 +60,7 @@ #include <gmock/gmock-generated-actions.h> #include <gmock/gmock-generated-function-mockers.h> #include <gmock/gmock-generated-matchers.h> +#include <gmock/gmock-more-actions.h> #include <gmock/gmock-generated-nice-strict.h> #include <gmock/gmock-matchers.h> #include <gmock/gmock-printers.h> diff --git a/testing/gmock/include/gmock/internal/gmock-internal-utils.h b/testing/gmock/include/gmock/internal/gmock-internal-utils.h index ee6aa1e..7b17335 100644 --- a/testing/gmock/include/gmock/internal/gmock-internal-utils.h +++ b/testing/gmock/include/gmock/internal/gmock-internal-utils.h @@ -221,6 +221,34 @@ class ImplicitlyConvertible { template <typename From, typename To> const bool ImplicitlyConvertible<From, To>::value; +// Symbian compilation can be done with wchar_t being either a native +// type or a typedef. Using Google Mock with OpenC without wchar_t +// should require the definition of _STLP_NO_WCHAR_T. +// +// MSVC treats wchar_t as a native type usually, but treats it as the +// same as unsigned short when the compiler option /Zc:wchar_t- is +// specified. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t +// is a native type. +#if (GTEST_OS_SYMBIAN && defined(_STLP_NO_WCHAR_T)) || \ + (defined(_MSC_VER) && !defined(_NATIVE_WCHAR_T_DEFINED)) +// wchar_t is a typedef. +#else +#define GMOCK_WCHAR_T_IS_NATIVE_ 1 +#endif + +// signed wchar_t and unsigned wchar_t are NOT in the C++ standard. +// Using them is a bad practice and not portable. So DON'T use them. +// +// Still, Google Mock is designed to work even if the user uses signed +// wchar_t or unsigned wchar_t (obviously, assuming the compiler +// supports them). +// +// To gcc, +// wchar_t == signed wchar_t != unsigned wchar_t == unsigned int +#ifdef __GNUC__ +#define GMOCK_HAS_SIGNED_WCHAR_T_ 1 // signed/unsigned wchar_t are valid types. +#endif + // In what follows, we use the term "kind" to indicate whether a type // is bool, an integer type (excluding bool), a floating-point type, // or none of them. This categorization is useful for determining @@ -252,10 +280,7 @@ GMOCK_DECLARE_KIND_(unsigned int, kInteger); GMOCK_DECLARE_KIND_(long, kInteger); // NOLINT GMOCK_DECLARE_KIND_(unsigned long, kInteger); // NOLINT -// MSVC can be configured to define wchar_t as a typedef of unsigned -// short. It defines _NATIVE_WCHAR_T_DEFINED symbol when wchar_t is a -// native type. -#if !defined(_MSC_VER) || defined(_NATIVE_WCHAR_T_DEFINED) +#if GMOCK_WCHAR_T_IS_NATIVE_ GMOCK_DECLARE_KIND_(wchar_t, kInteger); #endif @@ -581,21 +606,9 @@ class NativeArray { typedef Element value_type; typedef const Element* const_iterator; - // Constructs from a native array passed by reference. - template <size_t N> - NativeArray(const Element (&array)[N], RelationToSource relation) { - Init(array, N, relation); - } - - // Constructs from a native array passed by a pointer and a size. - // For generality we don't artificially restrict the types of the - // pointer and the size. - template <typename Pointer, typename Size> - NativeArray(const ::std::tr1::tuple<Pointer, Size>& array, - RelationToSource relation) { - Init(internal::GetRawPointer(::std::tr1::get<0>(array)), - ::std::tr1::get<1>(array), - relation); + // Constructs from a native array. + NativeArray(const Element* array, size_t count, RelationToSource relation) { + Init(array, count, relation); } // Copy constructor. @@ -691,10 +704,31 @@ class StlContainerView<Element[N]> { static const_reference ConstReference(const Element (&array)[N]) { // Ensures that Element is not a const type. testing::StaticAssertTypeEq<Element, RawElement>(); - return type(array, kReference); +#if GTEST_OS_SYMBIAN + // The Nokia Symbian compiler confuses itself in template instantiation + // for this call without the cast to Element*: + // function call '[testing::internal::NativeArray<char *>].NativeArray( + // {lval} const char *[4], long, testing::internal::RelationToSource)' + // does not match + // 'testing::internal::NativeArray<char *>::NativeArray( + // char *const *, unsigned int, testing::internal::RelationToSource)' + // (instantiating: 'testing::internal::ContainsMatcherImpl + // <const char * (&)[4]>::Matches(const char * (&)[4]) const') + // (instantiating: 'testing::internal::StlContainerView<char *[4]>:: + // ConstReference(const char * (&)[4])') + // (and though the N parameter type is mismatched in the above explicit + // conversion of it doesn't help - only the conversion of the array). + return type(const_cast<Element*>(&array[0]), N, kReference); +#else + return type(array, N, kReference); +#endif // GTEST_OS_SYMBIAN } static type Copy(const Element (&array)[N]) { - return type(array, kCopy); +#if GTEST_OS_SYMBIAN + return type(const_cast<Element*>(&array[0]), N, kCopy); +#else + return type(array, N, kCopy); +#endif // GTEST_OS_SYMBIAN } }; @@ -710,10 +744,12 @@ class StlContainerView< ::std::tr1::tuple<ElementPointer, Size> > { static const_reference ConstReference( const ::std::tr1::tuple<ElementPointer, Size>& array) { - return type(array, kReference); + using ::std::tr1::get; + return type(get<0>(array), get<1>(array), kReference); } static type Copy(const ::std::tr1::tuple<ElementPointer, Size>& array) { - return type(array, kCopy); + using ::std::tr1::get; + return type(get<0>(array), get<1>(array), kCopy); } }; diff --git a/testing/gmock/include/gmock/internal/gmock-port.h b/testing/gmock/include/gmock/internal/gmock-port.h index 9ee8f72..0263491 100644 --- a/testing/gmock/include/gmock/internal/gmock-port.h +++ b/testing/gmock/include/gmock/internal/gmock-port.h @@ -75,33 +75,11 @@ namespace testing { namespace internal { -// For Windows, check the compiler version. At least VS 2005 SP1 is +// For MS Visual C++, check the compiler version. At least VS 2003 is // required to compile Google Mock. -#if GTEST_OS_WINDOWS - -#if _MSC_VER < 1400 -#error "At least Visual Studio 2005 SP1 is required to compile Google Mock." -#elif _MSC_VER == 1400 - -// Unfortunately there is no unique _MSC_VER number for SP1. So for VS 2005 -// we have to check if it has SP1 by checking whether a bug fixed in SP1 -// is present. The bug in question is -// http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101702 -// where the compiler incorrectly reports sizeof(poiter to an array). - -class TestForSP1 { - private: // GCC complains if x_ is used by sizeof before defining it. - static char x_[100]; - // VS 2005 RTM incorrectly reports sizeof(&x) as 100, and that value - // is used to trigger 'invalid negative array size' error. If you - // see this error, upgrade to VS 2005 SP1 since Google Mock will not - // compile in VS 2005 RTM. - static char Google_Mock_requires_Visual_Studio_2005_SP1_or_later_to_compile_[ - sizeof(&x_) != 100 ? 1 : -1]; -}; - -#endif // _MSC_VER -#endif // GTEST_OS_WINDOWS +#if defined(_MSC_VER) && _MSC_VER < 1310 +#error "At least Visual C++ 2003 (7.1) is required to compile Google Mock." +#endif // Use implicit_cast as a safe version of static_cast or const_cast // for upcasting in the type hierarchy (i.e. casting a pointer to Foo @@ -236,56 +214,6 @@ typedef ::wstring wstring; typedef ::std::wstring wstring; #endif // GTEST_HAS_GLOBAL_WSTRING -// Prints the file location in the format native to the compiler. -inline void FormatFileLocation(const char* file, int line, ::std::ostream* os) { - if (file == NULL) - file = "unknown file"; - if (line < 0) { - *os << file << ":"; - } else { -#if _MSC_VER - *os << file << "(" << line << "):"; -#else - *os << file << ":" << line << ":"; -#endif - } -} - -// INTERNAL IMPLEMENTATION - DO NOT USE. -// -// GMOCK_CHECK_ is an all mode assert. It aborts the program if the condition -// is not satisfied. -// Synopsys: -// GMOCK_CHECK_(boolean_condition); -// or -// GMOCK_CHECK_(boolean_condition) << "Additional message"; -// -// This checks the condition and if the condition is not satisfied -// it prints message about the condition violation, including the -// condition itself, plus additional message streamed into it, if any, -// and then it aborts the program. It aborts the program irrespective of -// whether it is built in the debug mode or not. - -class GMockCheckProvider { - public: - GMockCheckProvider(const char* condition, const char* file, int line) { - FormatFileLocation(file, line, &::std::cerr); - ::std::cerr << " ERROR: Condition " << condition << " failed. "; - } - ~GMockCheckProvider() { - ::std::cerr << ::std::endl; - abort(); - } - ::std::ostream& GetStream() { return ::std::cerr; } -}; -#define GMOCK_CHECK_(condition) \ - GTEST_AMBIGUOUS_ELSE_BLOCKER_ \ - if (condition) \ - ; \ - else \ - ::testing::internal::GMockCheckProvider(\ - #condition, __FILE__, __LINE__).GetStream() - } // namespace internal } // namespace testing diff --git a/testing/gmock/msvc/gmock-spec-builders_test.vcproj b/testing/gmock/msvc/gmock-spec-builders_test.vcproj index 8440742..8440742 100755..100644 --- a/testing/gmock/msvc/gmock-spec-builders_test.vcproj +++ b/testing/gmock/msvc/gmock-spec-builders_test.vcproj diff --git a/testing/gmock/scripts/fuse_gmock_files.py b/testing/gmock/scripts/fuse_gmock_files.py index 4e892e9..4e892e9 100755..100644 --- a/testing/gmock/scripts/fuse_gmock_files.py +++ b/testing/gmock/scripts/fuse_gmock_files.py diff --git a/testing/gmock/scripts/generator/cpp/__init__.py b/testing/gmock/scripts/generator/cpp/__init__.py index e69de29..e69de29 100755..100644 --- a/testing/gmock/scripts/generator/cpp/__init__.py +++ b/testing/gmock/scripts/generator/cpp/__init__.py diff --git a/testing/gmock/scripts/generator/cpp/ast.py b/testing/gmock/scripts/generator/cpp/ast.py index 47dc9a0..47dc9a0 100755..100644 --- a/testing/gmock/scripts/generator/cpp/ast.py +++ b/testing/gmock/scripts/generator/cpp/ast.py diff --git a/testing/gmock/scripts/generator/cpp/gmock_class.py b/testing/gmock/scripts/generator/cpp/gmock_class.py index 3ad0bcd..3ad0bcd 100755..100644 --- a/testing/gmock/scripts/generator/cpp/gmock_class.py +++ b/testing/gmock/scripts/generator/cpp/gmock_class.py diff --git a/testing/gmock/scripts/generator/cpp/gmock_class_test.py b/testing/gmock/scripts/generator/cpp/gmock_class_test.py index ae00800..ae00800 100755..100644 --- a/testing/gmock/scripts/generator/cpp/gmock_class_test.py +++ b/testing/gmock/scripts/generator/cpp/gmock_class_test.py diff --git a/testing/gmock/scripts/generator/cpp/keywords.py b/testing/gmock/scripts/generator/cpp/keywords.py index f694450..f694450 100755..100644 --- a/testing/gmock/scripts/generator/cpp/keywords.py +++ b/testing/gmock/scripts/generator/cpp/keywords.py diff --git a/testing/gmock/scripts/generator/cpp/tokenize.py b/testing/gmock/scripts/generator/cpp/tokenize.py index 28c3345..28c3345 100755..100644 --- a/testing/gmock/scripts/generator/cpp/tokenize.py +++ b/testing/gmock/scripts/generator/cpp/tokenize.py diff --git a/testing/gmock/scripts/generator/cpp/utils.py b/testing/gmock/scripts/generator/cpp/utils.py index eab36ee..eab36ee 100755..100644 --- a/testing/gmock/scripts/generator/cpp/utils.py +++ b/testing/gmock/scripts/generator/cpp/utils.py diff --git a/testing/gmock/scripts/generator/gmock_gen.py b/testing/gmock/scripts/generator/gmock_gen.py index 8cc0d13..8cc0d13 100755..100644 --- a/testing/gmock/scripts/generator/gmock_gen.py +++ b/testing/gmock/scripts/generator/gmock_gen.py diff --git a/testing/gmock/scripts/gmock-config.in b/testing/gmock/scripts/gmock-config.in index 9ce17a2..9ce17a2 100755..100644 --- a/testing/gmock/scripts/gmock-config.in +++ b/testing/gmock/scripts/gmock-config.in diff --git a/testing/gmock/scripts/gmock_doctor.py b/testing/gmock/scripts/gmock_doctor.py index 05e4258..40512fc 100755..100644 --- a/testing/gmock/scripts/gmock_doctor.py +++ b/testing/gmock/scripts/gmock_doctor.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2.4 +#!/usr/bin/env python # # Copyright 2008, Google Inc. # All rights reserved. @@ -71,7 +71,6 @@ _COMMON_GMOCK_SYMBOLS = [ 'Not', 'NotNull', 'Pointee', - 'PointeeIsInitializedProto', 'Property', 'Ref', 'ResultOf', diff --git a/testing/gmock/scripts/upload.py b/testing/gmock/scripts/upload.py index 6e6f9a1..6e6f9a1 100755..100644 --- a/testing/gmock/scripts/upload.py +++ b/testing/gmock/scripts/upload.py diff --git a/testing/gmock/scripts/upload_gmock.py b/testing/gmock/scripts/upload_gmock.py index 5dc484b..5dc484b 100755..100644 --- a/testing/gmock/scripts/upload_gmock.py +++ b/testing/gmock/scripts/upload_gmock.py diff --git a/testing/gmock/src/gmock-internal-utils.cc b/testing/gmock/src/gmock-internal-utils.cc index 0e693c7..196ec74 100644 --- a/testing/gmock/src/gmock-internal-utils.cc +++ b/testing/gmock/src/gmock-internal-utils.cc @@ -77,10 +77,14 @@ class GoogleTestFailureReporter : public FailureReporterInterface { public: virtual void ReportFailure(FailureType type, const char* file, int line, const string& message) { - AssertHelper(type == FATAL ? TPRT_FATAL_FAILURE : TPRT_NONFATAL_FAILURE, - file, line, message.c_str()) = Message(); + AssertHelper(type == FATAL ? + TestPartResult::kFatalFailure : + TestPartResult::kNonFatalFailure, + file, + line, + message.c_str()) = Message(); if (type == FATAL) { - abort(); + posix::Abort(); } } }; diff --git a/testing/gmock/src/gmock-printers.cc b/testing/gmock/src/gmock-printers.cc index 922a7b2..8efba78 100644 --- a/testing/gmock/src/gmock-printers.cc +++ b/testing/gmock/src/gmock-printers.cc @@ -55,11 +55,13 @@ namespace { using ::std::ostream; -#ifdef _WIN32_WCE +#if GTEST_OS_WINDOWS_MOBILE // Windows CE does not define _snprintf_s. #define snprintf _snprintf -#elif GTEST_OS_WINDOWS +#elif _MSC_VER >= 1400 // VC 8.0 and later deprecate snprintf and _snprintf. #define snprintf _snprintf_s -#endif +#elif _MSC_VER +#define snprintf _snprintf +#endif // GTEST_OS_WINDOWS_MOBILE // Prints a segment of bytes in the given object. void PrintByteSegmentInObjectTo(const unsigned char* obj_bytes, size_t start, diff --git a/testing/gmock/src/gmock-spec-builders.cc b/testing/gmock/src/gmock-spec-builders.cc index 465e4d6..6cc94dd 100644 --- a/testing/gmock/src/gmock-spec-builders.cc +++ b/testing/gmock/src/gmock-spec-builders.cc @@ -82,11 +82,9 @@ void ExpectationBase::RetireAllPreRequisites() { return; } - for (ExpectationBaseSet::const_iterator it = - immediate_prerequisites_.begin(); - it != immediate_prerequisites_.end(); - ++it) { - ExpectationBase* const prerequisite = (*it).get(); + for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); + it != immediate_prerequisites_.end(); ++it) { + ExpectationBase* const prerequisite = it->expectation_base().get(); if (!prerequisite->is_retired()) { prerequisite->RetireAllPreRequisites(); prerequisite->Retire(); @@ -99,10 +97,10 @@ void ExpectationBase::RetireAllPreRequisites() { // L >= g_gmock_mutex bool ExpectationBase::AllPrerequisitesAreSatisfied() const { g_gmock_mutex.AssertHeld(); - for (ExpectationBaseSet::const_iterator it = immediate_prerequisites_.begin(); + for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); it != immediate_prerequisites_.end(); ++it) { - if (!(*it)->IsSatisfied() || - !(*it)->AllPrerequisitesAreSatisfied()) + if (!(it->expectation_base()->IsSatisfied()) || + !(it->expectation_base()->AllPrerequisitesAreSatisfied())) return false; } return true; @@ -111,21 +109,21 @@ bool ExpectationBase::AllPrerequisitesAreSatisfied() const { // Adds unsatisfied pre-requisites of this expectation to 'result'. // L >= g_gmock_mutex void ExpectationBase::FindUnsatisfiedPrerequisites( - ExpectationBaseSet* result) const { + ExpectationSet* result) const { g_gmock_mutex.AssertHeld(); - for (ExpectationBaseSet::const_iterator it = immediate_prerequisites_.begin(); + for (ExpectationSet::const_iterator it = immediate_prerequisites_.begin(); it != immediate_prerequisites_.end(); ++it) { - if ((*it)->IsSatisfied()) { + if (it->expectation_base()->IsSatisfied()) { // If *it is satisfied and has a call count of 0, some of its // pre-requisites may not be satisfied yet. - if ((*it)->call_count_ == 0) { - (*it)->FindUnsatisfiedPrerequisites(result); + if (it->expectation_base()->call_count_ == 0) { + it->expectation_base()->FindUnsatisfiedPrerequisites(result); } } else { // Now that we know *it is unsatisfied, we are not so interested // in whether its pre-requisites are satisfied. Therefore we // don't recursively call FindUnsatisfiedPrerequisites() here. - result->insert(*it); + *result += *it; } } } @@ -188,7 +186,9 @@ class MockObjectRegistry { // object alive. Therefore we report any living object as test // failure, unless the user explicitly asked us to ignore it. ~MockObjectRegistry() { - using ::std::cout; + + // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is + // a macro. if (!GMOCK_FLAG(catch_leaked_mocks)) return; @@ -201,24 +201,24 @@ class MockObjectRegistry { // TODO(wan@google.com): Print the type of the leaked object. // This can help the user identify the leaked object. - cout << "\n"; + std::cout << "\n"; const MockObjectState& state = it->second; - internal::FormatFileLocation( - state.first_used_file, state.first_used_line, &cout); - cout << " ERROR: this mock object"; + std::cout << internal::FormatFileLocation(state.first_used_file, + state.first_used_line); + std::cout << " ERROR: this mock object"; if (state.first_used_test != "") { - cout << " (used in test " << state.first_used_test_case << "." + std::cout << " (used in test " << state.first_used_test_case << "." << state.first_used_test << ")"; } - cout << " should be deleted but never is. Its address is @" + std::cout << " should be deleted but never is. Its address is @" << it->first << "."; leaked_count++; } if (leaked_count > 0) { - cout << "\nERROR: " << leaked_count + std::cout << "\nERROR: " << leaked_count << " leaked mock " << (leaked_count == 1 ? "object" : "objects") << " found at program exit.\n"; - cout.flush(); + std::cout.flush(); ::std::cerr.flush(); // RUN_ALL_TESTS() has already returned when this destructor is // called. Therefore we cannot use the normal Google Test @@ -420,12 +420,20 @@ void Mock::ClearDefaultActionsLocked(void* mock_obj) { // needed by VerifyAndClearExpectationsLocked(). } +Expectation::Expectation() {} + +Expectation::Expectation( + const internal::linked_ptr<internal::ExpectationBase>& expectation_base) + : expectation_base_(expectation_base) {} + +Expectation::~Expectation() {} + // Adds an expectation to a sequence. -void Sequence::AddExpectation( - const internal::linked_ptr<internal::ExpectationBase>& expectation) const { +void Sequence::AddExpectation(const Expectation& expectation) const { if (*last_expectation_ != expectation) { - if (*last_expectation_ != NULL) { - expectation->immediate_prerequisites_.insert(*last_expectation_); + if (last_expectation_->expectation_base() != NULL) { + expectation.expectation_base()->immediate_prerequisites_ + += *last_expectation_; } *last_expectation_ = expectation; } diff --git a/testing/gmock/src/gmock.cc b/testing/gmock/src/gmock.cc index caafb01..f487265 100644 --- a/testing/gmock/src/gmock.cc +++ b/testing/gmock/src/gmock.cc @@ -63,7 +63,7 @@ static const char* ParseGoogleMockFlagValue(const char* str, // The flag must start with "--gmock_". const String flag_str = String::Format("--gmock_%s", flag); - const size_t flag_len = flag_str.GetLength(); + const size_t flag_len = flag_str.length(); if (strncmp(str, flag_str.c_str(), flag_len) != 0) return NULL; // Skips the flag name. diff --git a/testing/gmock/src/gmock_main.cc b/testing/gmock/src/gmock_main.cc index 85689d5..0a3071b 100644 --- a/testing/gmock/src/gmock_main.cc +++ b/testing/gmock/src/gmock_main.cc @@ -38,13 +38,13 @@ // is enabled. For this reason instead of _tmain, main function is used on // Windows. See the following link to track the current status of this bug: // http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=394464 // NOLINT -#ifdef _WIN32_WCE +#if GTEST_OS_WINDOWS_MOBILE #include <tchar.h> // NOLINT int _tmain(int argc, TCHAR** argv) { #else int main(int argc, char** argv) { -#endif // _WIN32_WCE +#endif // GTEST_OS_WINDOWS_MOBILE std::cout << "Running main() from gmock_main.cc\n"; // Since Google Mock depends on Google Test, InitGoogleMock() is // also responsible for initializing Google Test. Therefore there's diff --git a/testing/gmock/test/gmock-actions_test.cc b/testing/gmock/test/gmock-actions_test.cc index 2446260..d3d96c6 100644 --- a/testing/gmock/test/gmock-actions_test.cc +++ b/testing/gmock/test/gmock-actions_test.cc @@ -56,6 +56,7 @@ using testing::_; using testing::Action; using testing::ActionInterface; using testing::Assign; +using testing::ByRef; using testing::DefaultValue; using testing::DoDefault; using testing::IgnoreResult; @@ -68,11 +69,10 @@ using testing::Return; using testing::ReturnNull; using testing::ReturnRef; using testing::SetArgumentPointee; -using testing::SetArrayArgument; -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE using testing::SetErrnoAndReturn; -#endif // _WIN32_WCE +#endif #if GMOCK_HAS_PROTOBUF_ using testing::internal::TestMessage; @@ -98,11 +98,13 @@ TEST(BuiltInDefaultValueTest, IsZeroForNumericTypes) { EXPECT_EQ(0, BuiltInDefaultValue<unsigned char>::Get()); EXPECT_EQ(0, BuiltInDefaultValue<signed char>::Get()); EXPECT_EQ(0, BuiltInDefaultValue<char>::Get()); -#if !GTEST_OS_WINDOWS +#if GMOCK_HAS_SIGNED_WCHAR_T_ EXPECT_EQ(0, BuiltInDefaultValue<unsigned wchar_t>::Get()); EXPECT_EQ(0, BuiltInDefaultValue<signed wchar_t>::Get()); -#endif // !GTEST_OS_WINDOWS +#endif +#if GMOCK_WCHAR_T_IS_NATIVE_ EXPECT_EQ(0, BuiltInDefaultValue<wchar_t>::Get()); +#endif EXPECT_EQ(0, BuiltInDefaultValue<unsigned short>::Get()); // NOLINT EXPECT_EQ(0, BuiltInDefaultValue<signed short>::Get()); // NOLINT EXPECT_EQ(0, BuiltInDefaultValue<short>::Get()); // NOLINT @@ -124,11 +126,13 @@ TEST(BuiltInDefaultValueTest, ExistsForNumericTypes) { EXPECT_TRUE(BuiltInDefaultValue<unsigned char>::Exists()); EXPECT_TRUE(BuiltInDefaultValue<signed char>::Exists()); EXPECT_TRUE(BuiltInDefaultValue<char>::Exists()); -#if !GTEST_OS_WINDOWS +#if GMOCK_HAS_SIGNED_WCHAR_T_ EXPECT_TRUE(BuiltInDefaultValue<unsigned wchar_t>::Exists()); EXPECT_TRUE(BuiltInDefaultValue<signed wchar_t>::Exists()); -#endif // !GTEST_OS_WINDOWS +#endif +#if GMOCK_WCHAR_T_IS_NATIVE_ EXPECT_TRUE(BuiltInDefaultValue<wchar_t>::Exists()); +#endif EXPECT_TRUE(BuiltInDefaultValue<unsigned short>::Exists()); // NOLINT EXPECT_TRUE(BuiltInDefaultValue<signed short>::Exists()); // NOLINT EXPECT_TRUE(BuiltInDefaultValue<short>::Exists()); // NOLINT @@ -199,26 +203,22 @@ TEST(BuiltInDefaultValueTest, UserTypeHasNoDefault) { EXPECT_FALSE(BuiltInDefaultValue<UserType>::Exists()); } -#if GTEST_HAS_DEATH_TEST - // Tests that BuiltInDefaultValue<T&>::Get() aborts the program. TEST(BuiltInDefaultValueDeathTest, IsUndefinedForReferences) { - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ BuiltInDefaultValue<int&>::Get(); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ BuiltInDefaultValue<const char&>::Get(); }, ""); } TEST(BuiltInDefaultValueDeathTest, IsUndefinedForUserTypes) { - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ BuiltInDefaultValue<UserType>::Get(); }, ""); } -#endif // GTEST_HAS_DEATH_TEST - // Tests that DefaultValue<T>::IsSet() is false initially. TEST(DefaultValueTest, IsInitiallyUnset) { EXPECT_FALSE(DefaultValue<int>::IsSet()); @@ -260,11 +260,9 @@ TEST(DefaultValueDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { EXPECT_EQ(0, DefaultValue<int>::Get()); -#if GTEST_HAS_DEATH_TEST - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<UserType>::Get(); }, ""); -#endif // GTEST_HAS_DEATH_TEST } // Tests that DefaultValue<void>::Get() returns void. @@ -316,14 +314,12 @@ TEST(DefaultValueOfReferenceDeathTest, GetReturnsBuiltInDefaultValueWhenUnset) { EXPECT_FALSE(DefaultValue<int&>::IsSet()); EXPECT_FALSE(DefaultValue<UserType&>::IsSet()); -#if GTEST_HAS_DEATH_TEST - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<int&>::Get(); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ DefaultValue<UserType>::Get(); }, ""); -#endif // GTEST_HAS_DEATH_TEST } // Tests that ActionInterface can be implemented by defining the @@ -403,12 +399,19 @@ class IsNotZero : public ActionInterface<bool(int)> { // NOLINT } }; +#if !GTEST_OS_SYMBIAN +// Compiling this test on Nokia's Symbian compiler fails with: +// 'Result' is not a member of class 'testing::internal::Function<int>' +// (point of instantiation: '@unnamed@gmock_actions_test_cc@:: +// ActionTest_CanBeConvertedToOtherActionType_Test::TestBody()') +// with no obvious fix. TEST(ActionTest, CanBeConvertedToOtherActionType) { const Action<bool(int)> a1(new IsNotZero); // NOLINT const Action<int(char)> a2 = Action<int(char)>(a1); // NOLINT EXPECT_EQ(1, a2.Perform(make_tuple('a'))); EXPECT_EQ(0, a2.Perform(make_tuple('\0'))); } +#endif // !GTEST_OS_SYMBIAN // The following two classes are for testing MakePolymorphicAction(). @@ -559,15 +562,13 @@ TEST(DoDefaultTest, ReturnsBuiltInDefaultValueByDefault) { EXPECT_EQ(0, mock.IntFunc(true)); } -#if GTEST_HAS_DEATH_TEST - // Tests that DoDefault() aborts the process when there is no built-in // default value for the return type. TEST(DoDefaultDeathTest, DiesForUnknowType) { MockClass mock; EXPECT_CALL(mock, Foo()) .WillRepeatedly(DoDefault()); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ mock.Foo(); }, ""); } @@ -587,13 +588,11 @@ TEST(DoDefaultDeathTest, DiesIfUsedInCompositeAction) { // EXPECT_DEATH() can only capture stderr, while Google Mock's // errors are printed on stdout. Therefore we have to settle for // not verifying the message. - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ mock.IntFunc(true); }, ""); } -#endif // GTEST_HAS_DEATH_TEST - // Tests that DoDefault() returns the default value set by // DefaultValue<T>::Set() when it's not overriden by an ON_CALL(). TEST(DoDefaultTest, ReturnsUserSpecifiedPerTypeDefaultValueWhenThereIsOne) { @@ -743,85 +742,6 @@ TEST(SetArgumentPointeeTest, SetsTheNthPointeeOfProto2BufferBaseType) { #endif // GMOCK_HAS_PROTOBUF_ -// Tests that SetArrayArgument<N>(first, last) sets the elements of the array -// pointed to by the N-th (0-based) argument to values in range [first, last). -TEST(SetArrayArgumentTest, SetsTheNthArray) { - typedef void MyFunction(bool, int*, char*); - int numbers[] = { 1, 2, 3 }; - Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3); - - int n[4] = {}; - int* pn = n; - char ch[4] = {}; - char* pch = ch; - a.Perform(make_tuple(true, pn, pch)); - EXPECT_EQ(1, n[0]); - EXPECT_EQ(2, n[1]); - EXPECT_EQ(3, n[2]); - EXPECT_EQ(0, n[3]); - EXPECT_EQ('\0', ch[0]); - EXPECT_EQ('\0', ch[1]); - EXPECT_EQ('\0', ch[2]); - EXPECT_EQ('\0', ch[3]); - - // Tests first and last are iterators. - std::string letters = "abc"; - a = SetArrayArgument<2>(letters.begin(), letters.end()); - std::fill_n(n, 4, 0); - std::fill_n(ch, 4, '\0'); - a.Perform(make_tuple(true, pn, pch)); - EXPECT_EQ(0, n[0]); - EXPECT_EQ(0, n[1]); - EXPECT_EQ(0, n[2]); - EXPECT_EQ(0, n[3]); - EXPECT_EQ('a', ch[0]); - EXPECT_EQ('b', ch[1]); - EXPECT_EQ('c', ch[2]); - EXPECT_EQ('\0', ch[3]); -} - -// Tests SetArrayArgument<N>(first, last) where first == last. -TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) { - typedef void MyFunction(bool, int*); - int numbers[] = { 1, 2, 3 }; - Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers); - - int n[4] = {}; - int* pn = n; - a.Perform(make_tuple(true, pn)); - EXPECT_EQ(0, n[0]); - EXPECT_EQ(0, n[1]); - EXPECT_EQ(0, n[2]); - EXPECT_EQ(0, n[3]); -} - -// Tests SetArrayArgument<N>(first, last) where *first is convertible -// (but not equal) to the argument type. -TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) { - typedef void MyFunction(bool, char*); - int codes[] = { 97, 98, 99 }; - Action<MyFunction> a = SetArrayArgument<1>(codes, codes + 3); - - char ch[4] = {}; - char* pch = ch; - a.Perform(make_tuple(true, pch)); - EXPECT_EQ('a', ch[0]); - EXPECT_EQ('b', ch[1]); - EXPECT_EQ('c', ch[2]); - EXPECT_EQ('\0', ch[3]); -} - -// Test SetArrayArgument<N>(first, last) with iterator as argument. -TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) { - typedef void MyFunction(bool, std::back_insert_iterator<std::string>); - std::string letters = "abc"; - Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end()); - - std::string s; - a.Perform(make_tuple(true, back_inserter(s))); - EXPECT_EQ(letters, s); -} - // Sample functions and functors for testing Invoke() and etc. int Nullary() { return 1; } @@ -1002,7 +922,7 @@ TEST(AssignTest, CompatibleTypes) { EXPECT_DOUBLE_EQ(5, x); } -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE class SetErrnoAndReturnTest : public testing::Test { protected: @@ -1029,6 +949,89 @@ TEST_F(SetErrnoAndReturnTest, CompatibleTypes) { EXPECT_EQ(EINVAL, errno); } -#endif // _WIN32_WCE +#endif // !GTEST_OS_WINDOWS_MOBILE + +// Tests ByRef(). + +// Tests that ReferenceWrapper<T> is copyable. +TEST(ByRefTest, IsCopyable) { + const std::string s1 = "Hi"; + const std::string s2 = "Hello"; + + ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper = ByRef(s1); + const std::string& r1 = ref_wrapper; + EXPECT_EQ(&s1, &r1); + + // Assigns a new value to ref_wrapper. + ref_wrapper = ByRef(s2); + const std::string& r2 = ref_wrapper; + EXPECT_EQ(&s2, &r2); + + ::testing::internal::ReferenceWrapper<const std::string> ref_wrapper1 = ByRef(s1); + // Copies ref_wrapper1 to ref_wrapper. + ref_wrapper = ref_wrapper1; + const std::string& r3 = ref_wrapper; + EXPECT_EQ(&s1, &r3); +} + +// Tests using ByRef() on a const value. +TEST(ByRefTest, ConstValue) { + const int n = 0; + // int& ref = ByRef(n); // This shouldn't compile - we have a + // negative compilation test to catch it. + const int& const_ref = ByRef(n); + EXPECT_EQ(&n, &const_ref); +} + +// Tests using ByRef() on a non-const value. +TEST(ByRefTest, NonConstValue) { + int n = 0; + + // ByRef(n) can be used as either an int&, + int& ref = ByRef(n); + EXPECT_EQ(&n, &ref); + + // or a const int&. + const int& const_ref = ByRef(n); + EXPECT_EQ(&n, &const_ref); +} + +// Tests explicitly specifying the type when using ByRef(). +TEST(ByRefTest, ExplicitType) { + int n = 0; + const int& r1 = ByRef<const int>(n); + EXPECT_EQ(&n, &r1); + + // ByRef<char>(n); // This shouldn't compile - we have a negative + // compilation test to catch it. + + Derived d; + Derived& r2 = ByRef<Derived>(d); + EXPECT_EQ(&d, &r2); + + const Derived& r3 = ByRef<const Derived>(d); + EXPECT_EQ(&d, &r3); + + Base& r4 = ByRef<Base>(d); + EXPECT_EQ(&d, &r4); + + const Base& r5 = ByRef<const Base>(d); + EXPECT_EQ(&d, &r5); + + // The following shouldn't compile - we have a negative compilation + // test for it. + // + // Base b; + // ByRef<Derived>(b); +} + +// Tests that Google Mock prints expression ByRef(x) as a reference to x. +TEST(ByRefTest, PrintsCorrectly) { + int n = 42; + ::std::stringstream expected, actual; + testing::internal::UniversalPrinter<const int&>::Print(n, &expected); + testing::internal::UniversalPrint(ByRef(n), &actual); + EXPECT_EQ(expected.str(), actual.str()); +} } // Unnamed namespace diff --git a/testing/gmock/test/gmock-generated-actions_test.cc b/testing/gmock/test/gmock-generated-actions_test.cc index d0b2ddc..2e6fa0b 100644 --- a/testing/gmock/test/gmock-generated-actions_test.cc +++ b/testing/gmock/test/gmock-generated-actions_test.cc @@ -36,6 +36,7 @@ #include <gmock/gmock-generated-actions.h> #include <functional> +#include <sstream> #include <string> #include <gmock/gmock.h> #include <gtest/gtest.h> @@ -53,22 +54,16 @@ using testing::_; using testing::Action; using testing::ActionInterface; using testing::ByRef; -using testing::DeleteArg; using testing::DoAll; using testing::Invoke; -using testing::InvokeArgument; using testing::Return; using testing::ReturnNew; -using testing::SaveArg; -using testing::SetArgReferee; using testing::SetArgumentPointee; using testing::StaticAssertTypeEq; using testing::Unused; -using testing::WithArg; using testing::WithArgs; -using testing::WithoutArgs; -// Sample functions and functors for testing Invoke() and etc. +// Sample functions and functors for testing various actions. int Nullary() { return 1; } class NullaryFunctor { @@ -77,19 +72,11 @@ class NullaryFunctor { }; bool g_done = false; -void VoidNullary() { g_done = true; } - -class VoidNullaryFunctor { - public: - void operator()() { g_done = true; } -}; bool Unary(int x) { return x < 0; } const char* Plus1(const char* s) { return s + 1; } -void VoidUnary(int n) { g_done = true; } - bool ByConstRef(const string& s) { return s == "Hi"; } const double g_double = 0; @@ -111,10 +98,6 @@ void VoidTernary(int, char, bool) { g_done = true; } int SumOf4(int a, int b, int c, int d) { return a + b + c + d; } -int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; } - -void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; } - string Concat4(const char* s1, const char* s2, const char* s3, const char* s4) { return string(s1) + s2 + s3 + s4; @@ -173,379 +156,10 @@ string Concat10(const char* s1, const char* s2, const char* s3, return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; } -class Foo { - public: - Foo() : value_(123) {} - - int Nullary() const { return value_; } - - short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT - - string Binary(const string& str, char c) const { return str + c; } - - int Ternary(int x, bool y, char z) { return value_ + x + y*z; } - - int SumOf4(int a, int b, int c, int d) const { - return a + b + c + d + value_; - } - - int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; } - - int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } - - int SumOf6(int a, int b, int c, int d, int e, int f) { - return a + b + c + d + e + f; - } - - string Concat7(const char* s1, const char* s2, const char* s3, - const char* s4, const char* s5, const char* s6, - const char* s7) { - return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; - } - - string Concat8(const char* s1, const char* s2, const char* s3, - const char* s4, const char* s5, const char* s6, - const char* s7, const char* s8) { - return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; - } - - string Concat9(const char* s1, const char* s2, const char* s3, - const char* s4, const char* s5, const char* s6, - const char* s7, const char* s8, const char* s9) { - return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; - } - - string Concat10(const char* s1, const char* s2, const char* s3, - const char* s4, const char* s5, const char* s6, - const char* s7, const char* s8, const char* s9, - const char* s10) { - return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; - } - private: - int value_; -}; - -// Tests using Invoke() with a nullary function. -TEST(InvokeTest, Nullary) { - Action<int()> a = Invoke(Nullary); // NOLINT - EXPECT_EQ(1, a.Perform(make_tuple())); -} - -// Tests using Invoke() with a unary function. -TEST(InvokeTest, Unary) { - Action<bool(int)> a = Invoke(Unary); // NOLINT - EXPECT_FALSE(a.Perform(make_tuple(1))); - EXPECT_TRUE(a.Perform(make_tuple(-1))); -} - -// Tests using Invoke() with a binary function. -TEST(InvokeTest, Binary) { - Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT - const char* p = "Hello"; - EXPECT_EQ(p + 2, a.Perform(make_tuple(p, 2))); -} - -// Tests using Invoke() with a ternary function. -TEST(InvokeTest, Ternary) { - Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT - EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', 3))); -} - -// Tests using Invoke() with a 4-argument function. -TEST(InvokeTest, FunctionThatTakes4Arguments) { - Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT - EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4))); -} - -// Tests using Invoke() with a 5-argument function. -TEST(InvokeTest, FunctionThatTakes5Arguments) { - Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT - EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); -} - -// Tests using Invoke() with a 6-argument function. -TEST(InvokeTest, FunctionThatTakes6Arguments) { - Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT - EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); -} - // A helper that turns the type of a C-string literal from const // char[N] to const char*. inline const char* CharPtr(const char* s) { return s; } -// Tests using Invoke() with a 7-argument function. -TEST(InvokeTest, FunctionThatTakes7Arguments) { - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*)> a = - Invoke(Concat7); - EXPECT_EQ("1234567", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7")))); -} - -// Tests using Invoke() with a 8-argument function. -TEST(InvokeTest, FunctionThatTakes8Arguments) { - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*)> a = - Invoke(Concat8); - EXPECT_EQ("12345678", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8")))); -} - -// Tests using Invoke() with a 9-argument function. -TEST(InvokeTest, FunctionThatTakes9Arguments) { - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*, - const char*)> a = Invoke(Concat9); - EXPECT_EQ("123456789", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8"), CharPtr("9")))); -} - -// Tests using Invoke() with a 10-argument function. -TEST(InvokeTest, FunctionThatTakes10Arguments) { - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*, - const char*, const char*)> a = Invoke(Concat10); - EXPECT_EQ("1234567890", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8"), CharPtr("9"), - CharPtr("0")))); -} - -// Tests using Invoke() with functions with parameters declared as Unused. -TEST(InvokeTest, FunctionWithUnusedParameters) { - Action<int(int, int, double, const string&)> a1 = - Invoke(SumOfFirst2); - EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, CharPtr("hi")))); - - Action<int(int, int, bool, int*)> a2 = - Invoke(SumOfFirst2); - EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL)))); -} - -// Tests using Invoke() with methods with parameters declared as Unused. -TEST(InvokeTest, MethodWithUnusedParameters) { - Foo foo; - Action<int(string, bool, int, int)> a1 = - Invoke(&foo, &Foo::SumOfLast2); - EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2))); - - Action<int(char, double, int, int)> a2 = - Invoke(&foo, &Foo::SumOfLast2); - EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3))); -} - -// Tests using Invoke() with a functor. -TEST(InvokeTest, Functor) { - Action<int(short, char)> a = Invoke(plus<short>()); // NOLINT - EXPECT_EQ(3, a.Perform(make_tuple(1, 2))); -} - -// Tests using Invoke(f) as an action of a compatible type. -TEST(InvokeTest, FunctionWithCompatibleType) { - Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT - EXPECT_EQ(4321, a.Perform(make_tuple(4000, 300, 20, true))); -} - -// Tests using Invoke() with an object pointer and a method pointer. - -// Tests using Invoke() with a nullary method. -TEST(InvokeMethodTest, Nullary) { - Foo foo; - Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT - EXPECT_EQ(123, a.Perform(make_tuple())); -} - -// Tests using Invoke() with a unary method. -TEST(InvokeMethodTest, Unary) { - Foo foo; - Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT - EXPECT_EQ(4123, a.Perform(make_tuple(4000))); -} - -// Tests using Invoke() with a binary method. -TEST(InvokeMethodTest, Binary) { - Foo foo; - Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary); - string s("Hell"); - EXPECT_EQ("Hello", a.Perform(make_tuple(s, 'o'))); -} - -// Tests using Invoke() with a ternary method. -TEST(InvokeMethodTest, Ternary) { - Foo foo; - Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT - EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, 1))); -} - -// Tests using Invoke() with a 4-argument method. -TEST(InvokeMethodTest, MethodThatTakes4Arguments) { - Foo foo; - Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT - EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4))); -} - -// Tests using Invoke() with a 5-argument method. -TEST(InvokeMethodTest, MethodThatTakes5Arguments) { - Foo foo; - Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLINT - EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); -} - -// Tests using Invoke() with a 6-argument method. -TEST(InvokeMethodTest, MethodThatTakes6Arguments) { - Foo foo; - Action<int(int, int, int, int, int, int)> a = // NOLINT - Invoke(&foo, &Foo::SumOf6); - EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); -} - -// Tests using Invoke() with a 7-argument method. -TEST(InvokeMethodTest, MethodThatTakes7Arguments) { - Foo foo; - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*)> a = - Invoke(&foo, &Foo::Concat7); - EXPECT_EQ("1234567", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7")))); -} - -// Tests using Invoke() with a 8-argument method. -TEST(InvokeMethodTest, MethodThatTakes8Arguments) { - Foo foo; - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*)> a = - Invoke(&foo, &Foo::Concat8); - EXPECT_EQ("12345678", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8")))); -} - -// Tests using Invoke() with a 9-argument method. -TEST(InvokeMethodTest, MethodThatTakes9Arguments) { - Foo foo; - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*, - const char*)> a = Invoke(&foo, &Foo::Concat9); - EXPECT_EQ("123456789", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8"), CharPtr("9")))); -} - -// Tests using Invoke() with a 10-argument method. -TEST(InvokeMethodTest, MethodThatTakes10Arguments) { - Foo foo; - Action<string(const char*, const char*, const char*, const char*, - const char*, const char*, const char*, const char*, - const char*, const char*)> a = Invoke(&foo, &Foo::Concat10); - EXPECT_EQ("1234567890", - a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), - CharPtr("4"), CharPtr("5"), CharPtr("6"), - CharPtr("7"), CharPtr("8"), CharPtr("9"), - CharPtr("0")))); -} - -// Tests using Invoke(f) as an action of a compatible type. -TEST(InvokeMethodTest, MethodWithCompatibleType) { - Foo foo; - Action<long(int, short, char, bool)> a = // NOLINT - Invoke(&foo, &Foo::SumOf4); - EXPECT_EQ(4444, a.Perform(make_tuple(4000, 300, 20, true))); -} - -// Tests ByRef(). - -// Tests that ReferenceWrapper<T> is copyable. -TEST(ByRefTest, IsCopyable) { - const string s1 = "Hi"; - const string s2 = "Hello"; - - ::testing::internal::ReferenceWrapper<const string> ref_wrapper = ByRef(s1); - const string& r1 = ref_wrapper; - EXPECT_EQ(&s1, &r1); - - // Assigns a new value to ref_wrapper. - ref_wrapper = ByRef(s2); - const string& r2 = ref_wrapper; - EXPECT_EQ(&s2, &r2); - - ::testing::internal::ReferenceWrapper<const string> ref_wrapper1 = ByRef(s1); - // Copies ref_wrapper1 to ref_wrapper. - ref_wrapper = ref_wrapper1; - const string& r3 = ref_wrapper; - EXPECT_EQ(&s1, &r3); -} - -// Tests using ByRef() on a const value. -TEST(ByRefTest, ConstValue) { - const int n = 0; - // int& ref = ByRef(n); // This shouldn't compile - we have a - // negative compilation test to catch it. - const int& const_ref = ByRef(n); - EXPECT_EQ(&n, &const_ref); -} - -// Tests using ByRef() on a non-const value. -TEST(ByRefTest, NonConstValue) { - int n = 0; - - // ByRef(n) can be used as either an int&, - int& ref = ByRef(n); - EXPECT_EQ(&n, &ref); - - // or a const int&. - const int& const_ref = ByRef(n); - EXPECT_EQ(&n, &const_ref); -} - -struct Base { - bool operator==(const Base&) { return true; } -}; - -struct Derived : public Base { - bool operator==(const Derived&) { return true; } -}; - -// Tests explicitly specifying the type when using ByRef(). -TEST(ByRefTest, ExplicitType) { - int n = 0; - const int& r1 = ByRef<const int>(n); - EXPECT_EQ(&n, &r1); - - // ByRef<char>(n); // This shouldn't compile - we have a negative - // compilation test to catch it. - - - Derived d; - Derived& r2 = ByRef<Derived>(d); - EXPECT_EQ(&d, &r2); - - const Derived& r3 = ByRef<const Derived>(d); - EXPECT_EQ(&d, &r3); - - Base& r4 = ByRef<Base>(d); - EXPECT_EQ(&d, &r4); - - const Base& r5 = ByRef<const Base>(d); - EXPECT_EQ(&d, &r5); - - // The following shouldn't compile - we have a negative compilation - // test for it. - // - // Base b; - // ByRef<Derived>(b); -} - // Tests InvokeArgument<N>(...). // Tests using InvokeArgument with a nullary function. @@ -663,23 +277,11 @@ TEST(InvokeArgumentTest, ByExplicitConstReferenceFunction) { EXPECT_FALSE(a.Perform(make_tuple(&ReferencesGlobalDouble))); } -// Tests using WithoutArgs with an action that takes no argument. -TEST(WithoutArgsTest, NoArg) { - Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT - EXPECT_EQ(1, a.Perform(make_tuple(2))); -} - -// Tests using WithArgs and WithArg with an action that takes 1 argument. +// Tests using WithArgs and with an action that takes 1 argument. TEST(WithArgsTest, OneArg) { Action<bool(double x, int n)> a = WithArgs<1>(Invoke(Unary)); // NOLINT EXPECT_TRUE(a.Perform(make_tuple(1.5, -1))); EXPECT_FALSE(a.Perform(make_tuple(1.5, 1))); - - // Also tests the synonym WithArg. - Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT - EXPECT_TRUE(a.Perform(make_tuple(1.5, -1))); - EXPECT_FALSE(a.Perform(make_tuple(1.5, 1))); - } // Tests using WithArgs with an action that takes 2 arguments. @@ -1372,41 +974,6 @@ TEST(ActionPnMacroTest, CanExplicitlyInstantiateWithReferenceTypes) { EXPECT_EQ(55, a.Perform(empty)); } -TEST(SaveArgActionTest, WorksForSameType) { - int result = 0; - const Action<void(int n)> a1 = SaveArg<0>(&result); - a1.Perform(make_tuple(5)); - EXPECT_EQ(5, result); -} - -TEST(SaveArgActionTest, WorksForCompatibleType) { - int result = 0; - const Action<void(bool, char)> a1 = SaveArg<1>(&result); - a1.Perform(make_tuple(true, 'a')); - EXPECT_EQ('a', result); -} - -TEST(SetArgRefereeActionTest, WorksForSameType) { - int value = 0; - const Action<void(int&)> a1 = SetArgReferee<0>(1); - a1.Perform(tuple<int&>(value)); - EXPECT_EQ(1, value); -} - -TEST(SetArgRefereeActionTest, WorksForCompatibleType) { - int value = 0; - const Action<void(int, int&)> a1 = SetArgReferee<1>('a'); - a1.Perform(tuple<int, int&>(0, value)); - EXPECT_EQ('a', value); -} - -TEST(SetArgRefereeActionTest, WorksWithExtraArguments) { - int value = 0; - const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a'); - a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi")); - EXPECT_EQ('a', value); -} - class NullaryConstructorClass { public: NullaryConstructorClass() : value_(123) {} @@ -1471,64 +1038,6 @@ TEST(ReturnNewTest, ConstructorThatTakes10Arguments) { delete c; } -// A class that can be used to verify that its destructor is called: it will set -// the bool provided to the constructor to true when destroyed. -class DeletionTester { - public: - explicit DeletionTester(bool* is_deleted) - : is_deleted_(is_deleted) { - // Make sure the bit is set to false. - *is_deleted_ = false; - } - - ~DeletionTester() { - *is_deleted_ = true; - } - - private: - bool* is_deleted_; -}; - -TEST(DeleteArgActionTest, OneArg) { - bool is_deleted = false; - DeletionTester* t = new DeletionTester(&is_deleted); - const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT - EXPECT_FALSE(is_deleted); - a1.Perform(make_tuple(t)); - EXPECT_TRUE(is_deleted); -} - -TEST(DeleteArgActionTest, TenArgs) { - bool is_deleted = false; - DeletionTester* t = new DeletionTester(&is_deleted); - const Action<void(bool, int, int, const char*, bool, - int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>(); - EXPECT_FALSE(is_deleted); - a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t)); - EXPECT_TRUE(is_deleted); -} - -#if GTEST_HAS_EXCEPTIONS - -TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) { - const Action<void(int n)> a = Throw('a'); - EXPECT_THROW(a.Perform(make_tuple(0)), char); -} - -class MyException {}; - -TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) { - const Action<double(char ch)> a = Throw(MyException()); - EXPECT_THROW(a.Perform(make_tuple('0')), MyException); -} - -TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) { - const Action<double()> a = Throw(MyException()); - EXPECT_THROW(a.Perform(make_tuple()), MyException); -} - -#endif // GTEST_HAS_EXCEPTIONS - // Tests that ACTION_TEMPLATE works when there is no value parameter. ACTION_TEMPLATE(CreateNew, HAS_1_TEMPLATE_PARAMS(typename, T), diff --git a/testing/gmock/test/gmock-generated-function-mockers_test.cc b/testing/gmock/test/gmock-generated-function-mockers_test.cc index 7267c10..1ce8c45 100644 --- a/testing/gmock/test/gmock-generated-function-mockers_test.cc +++ b/testing/gmock/test/gmock-generated-function-mockers_test.cc @@ -66,6 +66,7 @@ using testing::Const; using testing::DoDefault; using testing::Eq; using testing::Lt; +using testing::MockFunction; using testing::Ref; using testing::Return; using testing::ReturnRef; @@ -462,5 +463,48 @@ TEST(OverloadedMockMethodTest, CanOverloadOnConstnessInMacroBody) { EXPECT_EQ(3, const_mock->Overloaded(1)); } +TEST(MockFunctionTest, WorksForVoidNullary) { + MockFunction<void()> foo; + EXPECT_CALL(foo, Call()); + foo.Call(); +} + +TEST(MockFunctionTest, WorksForNonVoidNullary) { + MockFunction<int()> foo; + EXPECT_CALL(foo, Call()) + .WillOnce(Return(1)) + .WillOnce(Return(2)); + EXPECT_EQ(1, foo.Call()); + EXPECT_EQ(2, foo.Call()); +} + +TEST(MockFunctionTest, WorksForVoidUnary) { + MockFunction<void(int)> foo; + EXPECT_CALL(foo, Call(1)); + foo.Call(1); +} + +TEST(MockFunctionTest, WorksForNonVoidBinary) { + MockFunction<int(bool, int)> foo; + EXPECT_CALL(foo, Call(false, 42)) + .WillOnce(Return(1)) + .WillOnce(Return(2)); + EXPECT_CALL(foo, Call(true, Ge(100))) + .WillOnce(Return(3)); + EXPECT_EQ(1, foo.Call(false, 42)); + EXPECT_EQ(2, foo.Call(false, 42)); + EXPECT_EQ(3, foo.Call(true, 120)); +} + +TEST(MockFunctionTest, WorksFor10Arguments) { + MockFunction<int(bool a0, char a1, int a2, int a3, int a4, + int a5, int a6, char a7, int a8, bool a9)> foo; + EXPECT_CALL(foo, Call(_, 'a', _, _, _, _, _, _, _, _)) + .WillOnce(Return(1)) + .WillOnce(Return(2)); + EXPECT_EQ(1, foo.Call(false, 'a', 0, 0, 0, 0, 0, 'b', 0, true)); + EXPECT_EQ(2, foo.Call(true, 'a', 0, 0, 0, 0, 0, 'b', 1, false)); +} + } // namespace gmock_generated_function_mockers_test } // namespace testing diff --git a/testing/gmock/test/gmock-internal-utils_test.cc b/testing/gmock/test/gmock-internal-utils_test.cc index 9ab15af..ac3b2dd 100644 --- a/testing/gmock/test/gmock-internal-utils_test.cc +++ b/testing/gmock/test/gmock-internal-utils_test.cc @@ -48,6 +48,12 @@ #include <sys/types.h> // For ssize_t. NOLINT #endif +class ProtocolMessage; + +namespace proto2 { +class Message; +} // namespace proto2 + namespace testing { namespace internal { @@ -205,7 +211,8 @@ TEST(GetRawPointerTest, WorksForSmartPointers) { TEST(GetRawPointerTest, WorksForRawPointers) { int* p = NULL; - EXPECT_EQ(NULL, GetRawPointer(p)); + // Don't use EXPECT_EQ as no NULL-testing magic on Symbian. + EXPECT_TRUE(NULL == GetRawPointer(p)); int n = 1; EXPECT_EQ(&n, GetRawPointer(&n)); } @@ -384,6 +391,7 @@ TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) { // Tests that IsAProtocolMessage<T>::value is true when T is // ProtocolMessage or a sub-class of it. TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) { + EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value); EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value); #if GMOCK_HAS_PROTOBUF_ EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value); @@ -465,21 +473,17 @@ TEST(AssertTest, SucceedsOnTrue) { Assert(true, __FILE__, __LINE__); // This should succeed too. } -#if GTEST_HAS_DEATH_TEST - // Tests that Assert(false, ...) generates a fatal failure. TEST(AssertTest, FailsFatallyOnFalse) { - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ Assert(false, __FILE__, __LINE__, "This should fail."); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ Assert(false, __FILE__, __LINE__); }, ""); } -#endif // GTEST_HAS_DEATH_TEST - // Tests that Expect(true, ...) succeeds. TEST(ExpectTest, SucceedsOnTrue) { Expect(true, __FILE__, __LINE__, "This should succeed."); @@ -501,7 +505,16 @@ TEST(ExpectTest, FailsNonfatallyOnFalse) { class LogIsVisibleTest : public ::testing::Test { protected: - virtual void SetUp() { original_verbose_ = GMOCK_FLAG(verbose); } + virtual void SetUp() { + // The code needs to work when both ::string and ::std::string are + // defined and the flag is implemented as a + // testing::internal::String. In this case, without the call to + // c_str(), the compiler will complain that it cannot figure out + // whether the String flag should be converted to a ::string or an + // ::std::string before being assigned to original_verbose_. + original_verbose_ = GMOCK_FLAG(verbose).c_str(); + } + virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } string original_verbose_; @@ -803,34 +816,19 @@ TEST(CopyArrayTest, WorksForTwoDimensionalArrays) { // Tests NativeArray. -TEST(NativeArrayTest, ConstructorFromArrayReferenceWorks) { +TEST(NativeArrayTest, ConstructorFromArrayWorks) { const int a[3] = { 0, 1, 2 }; - NativeArray<int> na(a, kReference); + NativeArray<int> na(a, 3, kReference); EXPECT_EQ(3, na.size()); EXPECT_EQ(a, na.begin()); } -TEST(NativeArrayTest, ConstructorFromTupleWorks) { - int a[3] = { 0, 1, 2 }; - int* const p = a; - // Tests with a plain pointer. - NativeArray<int> na(make_tuple(p, 3U), kReference); - EXPECT_EQ(a, na.begin()); - - const linked_ptr<char> b(new char); - *b = 'a'; - // Tests with a smart pointer. - NativeArray<char> nb(make_tuple(b, 1), kCopy); - EXPECT_NE(b.get(), nb.begin()); - EXPECT_EQ('a', nb.begin()[0]); -} - TEST(NativeArrayTest, CreatesAndDeletesCopyOfArrayWhenAskedTo) { typedef int Array[2]; Array* a = new Array[1]; (*a)[0] = 0; (*a)[1] = 1; - NativeArray<int> na(*a, kCopy); + NativeArray<int> na(*a, 2, kCopy); EXPECT_NE(*a, na.begin()); delete[] a; EXPECT_EQ(0, na.begin()[0]); @@ -849,8 +847,8 @@ TEST(NativeArrayTest, TypeMembersAreCorrect) { } TEST(NativeArrayTest, MethodsWork) { - const int a[] = { 0, 1, 2 }; - NativeArray<int> na(a, kCopy); + const int a[3] = { 0, 1, 2 }; + NativeArray<int> na(a, 3, kCopy); ASSERT_EQ(3, na.size()); EXPECT_EQ(3, na.end() - na.begin()); @@ -865,18 +863,18 @@ TEST(NativeArrayTest, MethodsWork) { EXPECT_THAT(na, Eq(na)); - NativeArray<int> na2(a, kReference); + NativeArray<int> na2(a, 3, kReference); EXPECT_THAT(na, Eq(na2)); - const int b1[] = { 0, 1, 1 }; - const int b2[] = { 0, 1, 2, 3 }; - EXPECT_THAT(na, Not(Eq(NativeArray<int>(b1, kReference)))); - EXPECT_THAT(na, Not(Eq(NativeArray<int>(b2, kCopy)))); + const int b1[3] = { 0, 1, 1 }; + const int b2[4] = { 0, 1, 2, 3 }; + EXPECT_THAT(na, Not(Eq(NativeArray<int>(b1, 3, kReference)))); + EXPECT_THAT(na, Not(Eq(NativeArray<int>(b2, 4, kCopy)))); } TEST(NativeArrayTest, WorksForTwoDimensionalArray) { const char a[2][3] = { "hi", "lo" }; - NativeArray<char[3]> na(a, kReference); + NativeArray<char[3]> na(a, 2, kReference); ASSERT_EQ(2, na.size()); EXPECT_EQ(a, na.begin()); } diff --git a/testing/gmock/test/gmock-matchers_test.cc b/testing/gmock/test/gmock-matchers_test.cc index 3541eef..20b9387 100644 --- a/testing/gmock/test/gmock-matchers_test.cc +++ b/testing/gmock/test/gmock-matchers_test.cc @@ -42,6 +42,7 @@ #include <set> #include <sstream> #include <string> +#include <utility> #include <vector> #include <gmock/gmock.h> #include <gtest/gtest.h> @@ -59,6 +60,8 @@ bool SkipPrefix(const char* prefix, const char** pstr); namespace gmock_matchers_test { +using std::map; +using std::multimap; using std::stringstream; using std::tr1::make_tuple; using testing::A; @@ -75,6 +78,8 @@ using testing::FloatEq; using testing::Ge; using testing::Gt; using testing::HasSubstr; +using testing::IsNull; +using testing::Key; using testing::Le; using testing::Lt; using testing::MakeMatcher; @@ -88,6 +93,7 @@ using testing::NanSensitiveFloatEq; using testing::Ne; using testing::Not; using testing::NotNull; +using testing::Pair; using testing::Pointee; using testing::PolymorphicMatcher; using testing::Property; @@ -123,6 +129,35 @@ using testing::MatchesRegex; using testing::internal::RE; #endif // GMOCK_HAS_REGEX +// For testing ExplainMatchResultTo(). +class GreaterThanMatcher : public MatcherInterface<int> { + public: + explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} + + virtual bool Matches(int lhs) const { return lhs > rhs_; } + + virtual void DescribeTo(::std::ostream* os) const { + *os << "is greater than " << rhs_; + } + + virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { + const int diff = lhs - rhs_; + if (diff > 0) { + *os << "is " << diff << " more than " << rhs_; + } else if (diff == 0) { + *os << "is the same as " << rhs_; + } else { + *os << "is " << -diff << " less than " << rhs_; + } + } + private: + const int rhs_; +}; + +Matcher<int> GreaterThan(int n) { + return MakeMatcher(new GreaterThanMatcher(n)); +} + // Returns the description of the given matcher. template <typename T> string Describe(const Matcher<T>& m) { @@ -651,6 +686,42 @@ TEST(NeTest, CanDescribeSelf) { EXPECT_EQ("is not equal to 5", Describe(m)); } +// Tests that IsNull() matches any NULL pointer of any type. +TEST(IsNullTest, MatchesNullPointer) { + Matcher<int*> m1 = IsNull(); + int* p1 = NULL; + int n = 0; + EXPECT_TRUE(m1.Matches(p1)); + EXPECT_FALSE(m1.Matches(&n)); + + Matcher<const char*> m2 = IsNull(); + const char* p2 = NULL; + EXPECT_TRUE(m2.Matches(p2)); + EXPECT_FALSE(m2.Matches("hi")); + +#if !GTEST_OS_SYMBIAN + // Nokia's Symbian compiler generates: + // gmock-matchers.h: ambiguous access to overloaded function + // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(void *)' + // gmock-matchers.h: 'testing::Matcher<void *>::Matcher(const testing:: + // MatcherInterface<void *> *)' + // gmock-matchers.h: (point of instantiation: 'testing:: + // gmock_matchers_test::IsNullTest_MatchesNullPointer_Test::TestBody()') + // gmock-matchers.h: (instantiating: 'testing::PolymorphicMatc + Matcher<void*> m3 = IsNull(); + void* p3 = NULL; + EXPECT_TRUE(m3.Matches(p3)); + EXPECT_FALSE(m3.Matches(reinterpret_cast<void*>(0xbeef))); +#endif +} + +// Tests that IsNull() describes itself properly. +TEST(IsNullTest, CanDescribeSelf) { + Matcher<int*> m = IsNull(); + EXPECT_EQ("is NULL", Describe(m)); + EXPECT_EQ("is not NULL", DescribeNegation(m)); +} + // Tests that NotNull() matches any non-NULL pointer of any type. TEST(NotNullTest, MatchesNonNullPointer) { Matcher<int*> m1 = NotNull(); @@ -850,6 +921,136 @@ TEST(HasSubstrTest, CanDescribeSelf) { EXPECT_EQ("has substring \"foo\\n\\\"\"", Describe(m)); } +TEST(KeyTest, CanDescribeSelf) { + Matcher<const std::pair<std::string, int>&> m = Key("foo"); + EXPECT_EQ("has a key that is equal to \"foo\"", Describe(m)); +} + +TEST(KeyTest, MatchesCorrectly) { + std::pair<int, std::string> p(25, "foo"); + EXPECT_THAT(p, Key(25)); + EXPECT_THAT(p, Not(Key(42))); + EXPECT_THAT(p, Key(Ge(20))); + EXPECT_THAT(p, Not(Key(Lt(25)))); +} + +TEST(KeyTest, SafelyCastsInnerMatcher) { + Matcher<int> is_positive = Gt(0); + Matcher<int> is_negative = Lt(0); + std::pair<char, bool> p('a', true); + EXPECT_THAT(p, Key(is_positive)); + EXPECT_THAT(p, Not(Key(is_negative))); +} + +TEST(KeyTest, InsideContainsUsingMap) { + std::map<int, char> container; + container.insert(std::make_pair(1, 'a')); + container.insert(std::make_pair(2, 'b')); + container.insert(std::make_pair(4, 'c')); + EXPECT_THAT(container, Contains(Key(1))); + EXPECT_THAT(container, Not(Contains(Key(3)))); +} + +TEST(KeyTest, InsideContainsUsingMultimap) { + std::multimap<int, char> container; + container.insert(std::make_pair(1, 'a')); + container.insert(std::make_pair(2, 'b')); + container.insert(std::make_pair(4, 'c')); + + EXPECT_THAT(container, Not(Contains(Key(25)))); + container.insert(std::make_pair(25, 'd')); + EXPECT_THAT(container, Contains(Key(25))); + container.insert(std::make_pair(25, 'e')); + EXPECT_THAT(container, Contains(Key(25))); + + EXPECT_THAT(container, Contains(Key(1))); + EXPECT_THAT(container, Not(Contains(Key(3)))); +} + +TEST(PairTest, Typing) { + // Test verifies the following type conversions can be compiled. + Matcher<const std::pair<const char*, int>&> m1 = Pair("foo", 42); + Matcher<const std::pair<const char*, int> > m2 = Pair("foo", 42); + Matcher<std::pair<const char*, int> > m3 = Pair("foo", 42); + + Matcher<std::pair<int, const std::string> > m4 = Pair(25, "42"); + Matcher<std::pair<const std::string, int> > m5 = Pair("25", 42); +} + +TEST(PairTest, CanDescribeSelf) { + Matcher<const std::pair<std::string, int>&> m1 = Pair("foo", 42); + EXPECT_EQ("has a first field that is equal to \"foo\"" + ", and has a second field that is equal to 42", + Describe(m1)); + EXPECT_EQ("has a first field that is not equal to \"foo\"" + ", or has a second field that is not equal to 42", + DescribeNegation(m1)); + // Double and triple negation (1 or 2 times not and description of negation). + Matcher<const std::pair<int, int>&> m2 = Not(Pair(Not(13), 42)); + EXPECT_EQ("has a first field that is not equal to 13" + ", and has a second field that is equal to 42", + DescribeNegation(m2)); +} + +TEST(PairTest, CanExplainMatchResultTo) { + const Matcher<std::pair<int, int> > m0 = Pair(0, 0); + EXPECT_EQ("", Explain(m0, std::make_pair(25, 42))); + + const Matcher<std::pair<int, int> > m1 = Pair(GreaterThan(0), 0); + EXPECT_EQ("the first field is 25 more than 0", + Explain(m1, std::make_pair(25, 42))); + + const Matcher<std::pair<int, int> > m2 = Pair(0, GreaterThan(0)); + EXPECT_EQ("the second field is 42 more than 0", + Explain(m2, std::make_pair(25, 42))); + + const Matcher<std::pair<int, int> > m3 = Pair(GreaterThan(0), GreaterThan(0)); + EXPECT_EQ("the first field is 25 more than 0" + ", and the second field is 42 more than 0", + Explain(m3, std::make_pair(25, 42))); +} + +TEST(PairTest, MatchesCorrectly) { + std::pair<int, std::string> p(25, "foo"); + + // Both fields match. + EXPECT_THAT(p, Pair(25, "foo")); + EXPECT_THAT(p, Pair(Ge(20), HasSubstr("o"))); + + // 'first' doesnt' match, but 'second' matches. + EXPECT_THAT(p, Not(Pair(42, "foo"))); + EXPECT_THAT(p, Not(Pair(Lt(25), "foo"))); + + // 'first' matches, but 'second' doesn't match. + EXPECT_THAT(p, Not(Pair(25, "bar"))); + EXPECT_THAT(p, Not(Pair(25, Not("foo")))); + + // Neither field matches. + EXPECT_THAT(p, Not(Pair(13, "bar"))); + EXPECT_THAT(p, Not(Pair(Lt(13), HasSubstr("a")))); +} + +TEST(PairTest, SafelyCastsInnerMatchers) { + Matcher<int> is_positive = Gt(0); + Matcher<int> is_negative = Lt(0); + std::pair<char, bool> p('a', true); + EXPECT_THAT(p, Pair(is_positive, _)); + EXPECT_THAT(p, Not(Pair(is_negative, _))); + EXPECT_THAT(p, Pair(_, is_positive)); + EXPECT_THAT(p, Not(Pair(_, is_negative))); +} + +TEST(PairTest, InsideContainsUsingMap) { + std::map<int, char> container; + container.insert(std::make_pair(1, 'a')); + container.insert(std::make_pair(2, 'b')); + container.insert(std::make_pair(4, 'c')); + EXPECT_THAT(container, Contains(Pair(1, 'a'))); + EXPECT_THAT(container, Contains(Pair(1, _))); + EXPECT_THAT(container, Contains(Pair(_, 'a'))); + EXPECT_THAT(container, Not(Contains(Pair(3, _)))); +} + // Tests StartsWith(s). TEST(StartsWithTest, MatchesStringWithGivenPrefix) { @@ -1735,17 +1936,23 @@ TEST(MatcherAssertionTest, WorksWhenMatcherIsNotSatisfied) { // which cannot reference auto variables. static int n; n = 5; - EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Gt(10)) << "This should fail.", + + // VC++ prior to version 8.0 SP1 has a bug where it will not see any + // functions declared in the namespace scope from within nested classes. + // EXPECT/ASSERT_(NON)FATAL_FAILURE macros use nested classes so that all + // namespace-level functions invoked inside them need to be explicitly + // resolved. + EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Gt(10)), "Value of: n\n" "Expected: is greater than 10\n" - " Actual: 5\n" - "This should fail."); + " Actual: 5"); n = 0; - EXPECT_NONFATAL_FAILURE(EXPECT_THAT(n, AllOf(Le(7), Ge(5))), - "Value of: n\n" - "Expected: (is less than or equal to 7) and " - "(is greater than or equal to 5)\n" - " Actual: 0"); + EXPECT_NONFATAL_FAILURE( + EXPECT_THAT(n, ::testing::AllOf(::testing::Le(7), ::testing::Ge(5))), + "Value of: n\n" + "Expected: (is less than or equal to 7) and " + "(is greater than or equal to 5)\n" + " Actual: 0"); } // Tests that ASSERT_THAT() and EXPECT_THAT() work when the argument @@ -1756,16 +1963,28 @@ TEST(MatcherAssertionTest, WorksForByRefArguments) { static int n; n = 0; EXPECT_THAT(n, AllOf(Le(7), Ref(n))); - EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))), + EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), "Value of: n\n" "Expected: does not reference the variable @"); // Tests the "Actual" part. - EXPECT_FATAL_FAILURE(ASSERT_THAT(n, Not(Ref(n))), + EXPECT_FATAL_FAILURE(ASSERT_THAT(n, ::testing::Not(::testing::Ref(n))), "Actual: 0 (is located @"); } +#if !GTEST_OS_SYMBIAN // Tests that ASSERT_THAT() and EXPECT_THAT() work when the matcher is // monomorphic. + +// ASSERT_THAT("hello", starts_with_he) fails to compile with Nokia's +// Symbian compiler: it tries to compile +// template<T, U> class MatcherCastImpl { ... +// virtual bool Matches(T x) const { +// return source_matcher_.Matches(static_cast<U>(x)); +// with U == string and T == const char* +// With ASSERT_THAT("hello"...) changed to ASSERT_THAT(string("hello") ... ) +// the compiler silently crashes with no output. +// If MatcherCastImpl is changed to use U(x) instead of static_cast<U>(x) +// the code compiles but the converted string is bogus. TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { Matcher<const char*> starts_with_he = StartsWith("he"); ASSERT_THAT("hello", starts_with_he); @@ -1779,6 +1998,7 @@ TEST(MatcherAssertionTest, WorksForMonomorphicMatcher) { "Expected: is greater than 5\n" " Actual: 5"); } +#endif // !GTEST_OS_SYMBIAN // Tests floating-point matchers. template <typename RawType> @@ -2095,35 +2315,6 @@ TEST(PointeeTest, CanDescribeSelf) { DescribeNegation(m)); } -// For testing ExplainMatchResultTo(). -class GreaterThanMatcher : public MatcherInterface<int> { - public: - explicit GreaterThanMatcher(int rhs) : rhs_(rhs) {} - - virtual bool Matches(int lhs) const { return lhs > rhs_; } - - virtual void DescribeTo(::std::ostream* os) const { - *os << "is greater than " << rhs_; - } - - virtual void ExplainMatchResultTo(int lhs, ::std::ostream* os) const { - const int diff = lhs - rhs_; - if (diff > 0) { - *os << "is " << diff << " more than " << rhs_; - } else if (diff == 0) { - *os << "is the same as " << rhs_; - } else { - *os << "is " << -diff << " less than " << rhs_; - } - } - private: - const int rhs_; -}; - -Matcher<int> GreaterThan(int n) { - return MakeMatcher(new GreaterThanMatcher(n)); -} - TEST(PointeeTest, CanExplainMatchResult) { const Matcher<const string*> m = Pointee(StartsWith("Hi")); @@ -2623,15 +2814,13 @@ TEST(ResultOfTest, WorksForCompatibleMatcherTypes) { EXPECT_FALSE(matcher.Matches(42)); } -#if GTEST_HAS_DEATH_TEST // Tests that the program aborts when ResultOf is passed // a NULL function pointer. TEST(ResultOfDeathTest, DiesOnNullFunctionPointers) { - EXPECT_DEATH( + EXPECT_DEATH_IF_SUPPORTED( ResultOf(static_cast<string(*)(int)>(NULL), Eq(string("foo"))), "NULL function pointer is passed into ResultOf\\(\\)\\."); } -#endif // GTEST_HAS_DEATH_TEST // Tests that ResultOf(f, ...) compiles and works as expected when f is a // function reference. @@ -2696,7 +2885,6 @@ TEST(ResultOfTest, WorksForReferencingCallables) { EXPECT_FALSE(matcher3.Matches(n2)); } - class DivisibleByImpl { public: explicit DivisibleByImpl(int divider) : divider_(divider) {} @@ -2714,9 +2902,11 @@ class DivisibleByImpl { *os << "is not divisible by " << divider_; } + void set_divider(int divider) { divider_ = divider; } int divider() const { return divider_; } + private: - const int divider_; + int divider_; }; // For testing using ExplainMatchResultTo() with polymorphic matchers. @@ -2810,6 +3000,7 @@ TEST(ByRefTest, AllowsNotCopyableValueInMatchers) { EXPECT_TRUE(m.Matches(n2)); } +#if GTEST_HAS_TYPED_TEST // Tests ContainerEq with different container types, and // different element types. @@ -2878,6 +3069,7 @@ TYPED_TEST(ContainerEqTest, DuplicateDifference) { // But in any case there should be no explanation. EXPECT_EQ("", Explain(m, test_set)); } +#endif // GTEST_HAS_TYPED_TEST // Tests that mutliple missing values are reported. // Using just vector here, so order is predicatble. @@ -3296,5 +3488,22 @@ TEST(FormatMatcherDescriptionTest, Strings(params, params + 1))); } +// Tests PolymorphicMatcher::mutable_impl(). +TEST(PolymorphicMatcherTest, CanAccessMutableImpl) { + PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); + DivisibleByImpl& impl = m.mutable_impl(); + EXPECT_EQ(42, impl.divider()); + + impl.set_divider(0); + EXPECT_EQ(0, m.mutable_impl().divider()); +} + +// Tests PolymorphicMatcher::impl(). +TEST(PolymorphicMatcherTest, CanAccessImpl) { + const PolymorphicMatcher<DivisibleByImpl> m(DivisibleByImpl(42)); + const DivisibleByImpl& impl = m.impl(); + EXPECT_EQ(42, impl.divider()); +} + } // namespace gmock_matchers_test } // namespace testing diff --git a/testing/gmock/test/gmock-more-actions_test.cc b/testing/gmock/test/gmock-more-actions_test.cc new file mode 100644 index 0000000..f5dab5b --- /dev/null +++ b/testing/gmock/test/gmock-more-actions_test.cc @@ -0,0 +1,664 @@ +// Copyright 2007, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +// Author: wan@google.com (Zhanyong Wan) + +// Google Mock - a framework for writing C++ mock classes. +// +// This file tests the built-in actions in gmock-more-actions.h. + +#include <gmock/gmock-more-actions.h> + +#include <functional> +#include <sstream> +#include <string> +#include <gmock/gmock.h> +#include <gtest/gtest.h> + +namespace testing { +namespace gmock_more_actions_test { + +using ::std::plus; +using ::std::string; +using ::std::tr1::get; +using ::std::tr1::make_tuple; +using ::std::tr1::tuple; +using ::std::tr1::tuple_element; +using testing::_; +using testing::Action; +using testing::ActionInterface; +using testing::DeleteArg; +using testing::Invoke; +using testing::Return; +using testing::ReturnArg; +using testing::SaveArg; +using testing::SetArgReferee; +using testing::SetArgumentPointee; +using testing::StaticAssertTypeEq; +using testing::Unused; +using testing::WithArg; +using testing::WithoutArgs; + +// Sample functions and functors for testing Invoke() and etc. +int Nullary() { return 1; } + +class NullaryFunctor { + public: + int operator()() { return 2; } +}; + +bool g_done = false; +void VoidNullary() { g_done = true; } + +class VoidNullaryFunctor { + public: + void operator()() { g_done = true; } +}; + +bool Unary(int x) { return x < 0; } + +const char* Plus1(const char* s) { return s + 1; } + +void VoidUnary(int n) { g_done = true; } + +bool ByConstRef(const string& s) { return s == "Hi"; } + +const double g_double = 0; +bool ReferencesGlobalDouble(const double& x) { return &x == &g_double; } + +string ByNonConstRef(string& s) { return s += "+"; } // NOLINT + +struct UnaryFunctor { + int operator()(bool x) { return x ? 1 : -1; } +}; + +const char* Binary(const char* input, short n) { return input + n; } // NOLINT + +void VoidBinary(int, char) { g_done = true; } + +int Ternary(int x, char y, short z) { return x + y + z; } // NOLINT + +void VoidTernary(int, char, bool) { g_done = true; } + +int SumOf4(int a, int b, int c, int d) { return a + b + c + d; } + +int SumOfFirst2(int a, int b, Unused, Unused) { return a + b; } + +void VoidFunctionWithFourArguments(char, int, float, double) { g_done = true; } + +string Concat4(const char* s1, const char* s2, const char* s3, + const char* s4) { + return string(s1) + s2 + s3 + s4; +} + +int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } + +struct SumOf5Functor { + int operator()(int a, int b, int c, int d, int e) { + return a + b + c + d + e; + } +}; + +string Concat5(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5) { + return string(s1) + s2 + s3 + s4 + s5; +} + +int SumOf6(int a, int b, int c, int d, int e, int f) { + return a + b + c + d + e + f; +} + +struct SumOf6Functor { + int operator()(int a, int b, int c, int d, int e, int f) { + return a + b + c + d + e + f; + } +}; + +string Concat6(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6) { + return string(s1) + s2 + s3 + s4 + s5 + s6; +} + +string Concat7(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; +} + +string Concat8(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; +} + +string Concat9(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8, const char* s9) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; +} + +string Concat10(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8, const char* s9, + const char* s10) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; +} + +class Foo { + public: + Foo() : value_(123) {} + + int Nullary() const { return value_; } + + short Unary(long x) { return static_cast<short>(value_ + x); } // NOLINT + + string Binary(const string& str, char c) const { return str + c; } + + int Ternary(int x, bool y, char z) { return value_ + x + y*z; } + + int SumOf4(int a, int b, int c, int d) const { + return a + b + c + d + value_; + } + + int SumOfLast2(Unused, Unused, int a, int b) const { return a + b; } + + int SumOf5(int a, int b, int c, int d, int e) { return a + b + c + d + e; } + + int SumOf6(int a, int b, int c, int d, int e, int f) { + return a + b + c + d + e + f; + } + + string Concat7(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7; + } + + string Concat8(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8; + } + + string Concat9(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8, const char* s9) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9; + } + + string Concat10(const char* s1, const char* s2, const char* s3, + const char* s4, const char* s5, const char* s6, + const char* s7, const char* s8, const char* s9, + const char* s10) { + return string(s1) + s2 + s3 + s4 + s5 + s6 + s7 + s8 + s9 + s10; + } + private: + int value_; +}; + +// Tests using Invoke() with a nullary function. +TEST(InvokeTest, Nullary) { + Action<int()> a = Invoke(Nullary); // NOLINT + EXPECT_EQ(1, a.Perform(make_tuple())); +} + +// Tests using Invoke() with a unary function. +TEST(InvokeTest, Unary) { + Action<bool(int)> a = Invoke(Unary); // NOLINT + EXPECT_FALSE(a.Perform(make_tuple(1))); + EXPECT_TRUE(a.Perform(make_tuple(-1))); +} + +// Tests using Invoke() with a binary function. +TEST(InvokeTest, Binary) { + Action<const char*(const char*, short)> a = Invoke(Binary); // NOLINT + const char* p = "Hello"; + EXPECT_EQ(p + 2, a.Perform(make_tuple(p, 2))); +} + +// Tests using Invoke() with a ternary function. +TEST(InvokeTest, Ternary) { + Action<int(int, char, short)> a = Invoke(Ternary); // NOLINT + EXPECT_EQ(6, a.Perform(make_tuple(1, '\2', 3))); +} + +// Tests using Invoke() with a 4-argument function. +TEST(InvokeTest, FunctionThatTakes4Arguments) { + Action<int(int, int, int, int)> a = Invoke(SumOf4); // NOLINT + EXPECT_EQ(1234, a.Perform(make_tuple(1000, 200, 30, 4))); +} + +// Tests using Invoke() with a 5-argument function. +TEST(InvokeTest, FunctionThatTakes5Arguments) { + Action<int(int, int, int, int, int)> a = Invoke(SumOf5); // NOLINT + EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); +} + +// Tests using Invoke() with a 6-argument function. +TEST(InvokeTest, FunctionThatTakes6Arguments) { + Action<int(int, int, int, int, int, int)> a = Invoke(SumOf6); // NOLINT + EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); +} + +// A helper that turns the type of a C-string literal from const +// char[N] to const char*. +inline const char* CharPtr(const char* s) { return s; } + +// Tests using Invoke() with a 7-argument function. +TEST(InvokeTest, FunctionThatTakes7Arguments) { + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*)> a = + Invoke(Concat7); + EXPECT_EQ("1234567", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7")))); +} + +// Tests using Invoke() with a 8-argument function. +TEST(InvokeTest, FunctionThatTakes8Arguments) { + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*)> a = + Invoke(Concat8); + EXPECT_EQ("12345678", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8")))); +} + +// Tests using Invoke() with a 9-argument function. +TEST(InvokeTest, FunctionThatTakes9Arguments) { + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*, + const char*)> a = Invoke(Concat9); + EXPECT_EQ("123456789", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9")))); +} + +// Tests using Invoke() with a 10-argument function. +TEST(InvokeTest, FunctionThatTakes10Arguments) { + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*, + const char*, const char*)> a = Invoke(Concat10); + EXPECT_EQ("1234567890", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9"), + CharPtr("0")))); +} + +// Tests using Invoke() with functions with parameters declared as Unused. +TEST(InvokeTest, FunctionWithUnusedParameters) { + Action<int(int, int, double, const string&)> a1 = + Invoke(SumOfFirst2); + EXPECT_EQ(12, a1.Perform(make_tuple(10, 2, 5.6, CharPtr("hi")))); + + Action<int(int, int, bool, int*)> a2 = + Invoke(SumOfFirst2); + EXPECT_EQ(23, a2.Perform(make_tuple(20, 3, true, static_cast<int*>(NULL)))); +} + +// Tests using Invoke() with methods with parameters declared as Unused. +TEST(InvokeTest, MethodWithUnusedParameters) { + Foo foo; + Action<int(string, bool, int, int)> a1 = + Invoke(&foo, &Foo::SumOfLast2); + EXPECT_EQ(12, a1.Perform(make_tuple(CharPtr("hi"), true, 10, 2))); + + Action<int(char, double, int, int)> a2 = + Invoke(&foo, &Foo::SumOfLast2); + EXPECT_EQ(23, a2.Perform(make_tuple('a', 2.5, 20, 3))); +} + +// Tests using Invoke() with a functor. +TEST(InvokeTest, Functor) { + Action<int(short, char)> a = Invoke(plus<short>()); // NOLINT + EXPECT_EQ(3, a.Perform(make_tuple(1, 2))); +} + +// Tests using Invoke(f) as an action of a compatible type. +TEST(InvokeTest, FunctionWithCompatibleType) { + Action<long(int, short, char, bool)> a = Invoke(SumOf4); // NOLINT + EXPECT_EQ(4321, a.Perform(make_tuple(4000, 300, 20, true))); +} + +// Tests using Invoke() with an object pointer and a method pointer. + +// Tests using Invoke() with a nullary method. +TEST(InvokeMethodTest, Nullary) { + Foo foo; + Action<int()> a = Invoke(&foo, &Foo::Nullary); // NOLINT + EXPECT_EQ(123, a.Perform(make_tuple())); +} + +// Tests using Invoke() with a unary method. +TEST(InvokeMethodTest, Unary) { + Foo foo; + Action<short(long)> a = Invoke(&foo, &Foo::Unary); // NOLINT + EXPECT_EQ(4123, a.Perform(make_tuple(4000))); +} + +// Tests using Invoke() with a binary method. +TEST(InvokeMethodTest, Binary) { + Foo foo; + Action<string(const string&, char)> a = Invoke(&foo, &Foo::Binary); + string s("Hell"); + EXPECT_EQ("Hello", a.Perform(make_tuple(s, 'o'))); +} + +// Tests using Invoke() with a ternary method. +TEST(InvokeMethodTest, Ternary) { + Foo foo; + Action<int(int, bool, char)> a = Invoke(&foo, &Foo::Ternary); // NOLINT + EXPECT_EQ(1124, a.Perform(make_tuple(1000, true, 1))); +} + +// Tests using Invoke() with a 4-argument method. +TEST(InvokeMethodTest, MethodThatTakes4Arguments) { + Foo foo; + Action<int(int, int, int, int)> a = Invoke(&foo, &Foo::SumOf4); // NOLINT + EXPECT_EQ(1357, a.Perform(make_tuple(1000, 200, 30, 4))); +} + +// Tests using Invoke() with a 5-argument method. +TEST(InvokeMethodTest, MethodThatTakes5Arguments) { + Foo foo; + Action<int(int, int, int, int, int)> a = Invoke(&foo, &Foo::SumOf5); // NOLINT + EXPECT_EQ(12345, a.Perform(make_tuple(10000, 2000, 300, 40, 5))); +} + +// Tests using Invoke() with a 6-argument method. +TEST(InvokeMethodTest, MethodThatTakes6Arguments) { + Foo foo; + Action<int(int, int, int, int, int, int)> a = // NOLINT + Invoke(&foo, &Foo::SumOf6); + EXPECT_EQ(123456, a.Perform(make_tuple(100000, 20000, 3000, 400, 50, 6))); +} + +// Tests using Invoke() with a 7-argument method. +TEST(InvokeMethodTest, MethodThatTakes7Arguments) { + Foo foo; + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*)> a = + Invoke(&foo, &Foo::Concat7); + EXPECT_EQ("1234567", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7")))); +} + +// Tests using Invoke() with a 8-argument method. +TEST(InvokeMethodTest, MethodThatTakes8Arguments) { + Foo foo; + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*)> a = + Invoke(&foo, &Foo::Concat8); + EXPECT_EQ("12345678", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8")))); +} + +// Tests using Invoke() with a 9-argument method. +TEST(InvokeMethodTest, MethodThatTakes9Arguments) { + Foo foo; + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*, + const char*)> a = Invoke(&foo, &Foo::Concat9); + EXPECT_EQ("123456789", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9")))); +} + +// Tests using Invoke() with a 10-argument method. +TEST(InvokeMethodTest, MethodThatTakes10Arguments) { + Foo foo; + Action<string(const char*, const char*, const char*, const char*, + const char*, const char*, const char*, const char*, + const char*, const char*)> a = Invoke(&foo, &Foo::Concat10); + EXPECT_EQ("1234567890", + a.Perform(make_tuple(CharPtr("1"), CharPtr("2"), CharPtr("3"), + CharPtr("4"), CharPtr("5"), CharPtr("6"), + CharPtr("7"), CharPtr("8"), CharPtr("9"), + CharPtr("0")))); +} + +// Tests using Invoke(f) as an action of a compatible type. +TEST(InvokeMethodTest, MethodWithCompatibleType) { + Foo foo; + Action<long(int, short, char, bool)> a = // NOLINT + Invoke(&foo, &Foo::SumOf4); + EXPECT_EQ(4444, a.Perform(make_tuple(4000, 300, 20, true))); +} + +// Tests using WithoutArgs with an action that takes no argument. +TEST(WithoutArgsTest, NoArg) { + Action<int(int n)> a = WithoutArgs(Invoke(Nullary)); // NOLINT + EXPECT_EQ(1, a.Perform(make_tuple(2))); +} + +// Tests using WithArg with an action that takes 1 argument. +TEST(WithArgTest, OneArg) { + Action<bool(double x, int n)> b = WithArg<1>(Invoke(Unary)); // NOLINT + EXPECT_TRUE(b.Perform(make_tuple(1.5, -1))); + EXPECT_FALSE(b.Perform(make_tuple(1.5, 1))); +} + +TEST(ReturnArgActionTest, WorksForOneArgIntArg0) { + const Action<int(int)> a = ReturnArg<0>(); + EXPECT_EQ(5, a.Perform(make_tuple(5))); +} + +TEST(ReturnArgActionTest, WorksForMultiArgBoolArg0) { + const Action<bool(bool, bool, bool)> a = ReturnArg<0>(); + EXPECT_TRUE(a.Perform(make_tuple(true, false, false))); +} + +TEST(ReturnArgActionTest, WorksForMultiArgStringArg2) { + const Action<string(int, int, string, int)> a = ReturnArg<2>(); + EXPECT_EQ("seven", a.Perform(make_tuple(5, 6, string("seven"), 8))); +} + +TEST(SaveArgActionTest, WorksForSameType) { + int result = 0; + const Action<void(int n)> a1 = SaveArg<0>(&result); + a1.Perform(make_tuple(5)); + EXPECT_EQ(5, result); +} + +TEST(SaveArgActionTest, WorksForCompatibleType) { + int result = 0; + const Action<void(bool, char)> a1 = SaveArg<1>(&result); + a1.Perform(make_tuple(true, 'a')); + EXPECT_EQ('a', result); +} + +TEST(SetArgRefereeActionTest, WorksForSameType) { + int value = 0; + const Action<void(int&)> a1 = SetArgReferee<0>(1); + a1.Perform(tuple<int&>(value)); + EXPECT_EQ(1, value); +} + +TEST(SetArgRefereeActionTest, WorksForCompatibleType) { + int value = 0; + const Action<void(int, int&)> a1 = SetArgReferee<1>('a'); + a1.Perform(tuple<int, int&>(0, value)); + EXPECT_EQ('a', value); +} + +TEST(SetArgRefereeActionTest, WorksWithExtraArguments) { + int value = 0; + const Action<void(bool, int, int&, const char*)> a1 = SetArgReferee<2>('a'); + a1.Perform(tuple<bool, int, int&, const char*>(true, 0, value, "hi")); + EXPECT_EQ('a', value); +} + +// A class that can be used to verify that its destructor is called: it will set +// the bool provided to the constructor to true when destroyed. +class DeletionTester { + public: + explicit DeletionTester(bool* is_deleted) + : is_deleted_(is_deleted) { + // Make sure the bit is set to false. + *is_deleted_ = false; + } + + ~DeletionTester() { + *is_deleted_ = true; + } + + private: + bool* is_deleted_; +}; + +TEST(DeleteArgActionTest, OneArg) { + bool is_deleted = false; + DeletionTester* t = new DeletionTester(&is_deleted); + const Action<void(DeletionTester*)> a1 = DeleteArg<0>(); // NOLINT + EXPECT_FALSE(is_deleted); + a1.Perform(make_tuple(t)); + EXPECT_TRUE(is_deleted); +} + +TEST(DeleteArgActionTest, TenArgs) { + bool is_deleted = false; + DeletionTester* t = new DeletionTester(&is_deleted); + const Action<void(bool, int, int, const char*, bool, + int, int, int, int, DeletionTester*)> a1 = DeleteArg<9>(); + EXPECT_FALSE(is_deleted); + a1.Perform(make_tuple(true, 5, 6, CharPtr("hi"), false, 7, 8, 9, 10, t)); + EXPECT_TRUE(is_deleted); +} + +#if GTEST_HAS_EXCEPTIONS + +TEST(ThrowActionTest, ThrowsGivenExceptionInVoidFunction) { + const Action<void(int n)> a = Throw('a'); + EXPECT_THROW(a.Perform(make_tuple(0)), char); +} + +class MyException {}; + +TEST(ThrowActionTest, ThrowsGivenExceptionInNonVoidFunction) { + const Action<double(char ch)> a = Throw(MyException()); + EXPECT_THROW(a.Perform(make_tuple('0')), MyException); +} + +TEST(ThrowActionTest, ThrowsGivenExceptionInNullaryFunction) { + const Action<double()> a = Throw(MyException()); + EXPECT_THROW(a.Perform(make_tuple()), MyException); +} + +#endif // GTEST_HAS_EXCEPTIONS + +// Tests that SetArrayArgument<N>(first, last) sets the elements of the array +// pointed to by the N-th (0-based) argument to values in range [first, last). +TEST(SetArrayArgumentTest, SetsTheNthArray) { + typedef void MyFunction(bool, int*, char*); + int numbers[] = { 1, 2, 3 }; + Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers + 3); + + int n[4] = {}; + int* pn = n; + char ch[4] = {}; + char* pch = ch; + a.Perform(make_tuple(true, pn, pch)); + EXPECT_EQ(1, n[0]); + EXPECT_EQ(2, n[1]); + EXPECT_EQ(3, n[2]); + EXPECT_EQ(0, n[3]); + EXPECT_EQ('\0', ch[0]); + EXPECT_EQ('\0', ch[1]); + EXPECT_EQ('\0', ch[2]); + EXPECT_EQ('\0', ch[3]); + + // Tests first and last are iterators. + std::string letters = "abc"; + a = SetArrayArgument<2>(letters.begin(), letters.end()); + std::fill_n(n, 4, 0); + std::fill_n(ch, 4, '\0'); + a.Perform(make_tuple(true, pn, pch)); + EXPECT_EQ(0, n[0]); + EXPECT_EQ(0, n[1]); + EXPECT_EQ(0, n[2]); + EXPECT_EQ(0, n[3]); + EXPECT_EQ('a', ch[0]); + EXPECT_EQ('b', ch[1]); + EXPECT_EQ('c', ch[2]); + EXPECT_EQ('\0', ch[3]); +} + +// Tests SetArrayArgument<N>(first, last) where first == last. +TEST(SetArrayArgumentTest, SetsTheNthArrayWithEmptyRange) { + typedef void MyFunction(bool, int*); + int numbers[] = { 1, 2, 3 }; + Action<MyFunction> a = SetArrayArgument<1>(numbers, numbers); + + int n[4] = {}; + int* pn = n; + a.Perform(make_tuple(true, pn)); + EXPECT_EQ(0, n[0]); + EXPECT_EQ(0, n[1]); + EXPECT_EQ(0, n[2]); + EXPECT_EQ(0, n[3]); +} + +// Tests SetArrayArgument<N>(first, last) where *first is convertible +// (but not equal) to the argument type. +TEST(SetArrayArgumentTest, SetsTheNthArrayWithConvertibleType) { + typedef void MyFunction(bool, char*); + int codes[] = { 97, 98, 99 }; + Action<MyFunction> a = SetArrayArgument<1>(codes, codes + 3); + + char ch[4] = {}; + char* pch = ch; + a.Perform(make_tuple(true, pch)); + EXPECT_EQ('a', ch[0]); + EXPECT_EQ('b', ch[1]); + EXPECT_EQ('c', ch[2]); + EXPECT_EQ('\0', ch[3]); +} + +// Test SetArrayArgument<N>(first, last) with iterator as argument. +TEST(SetArrayArgumentTest, SetsTheNthArrayWithIteratorArgument) { + typedef void MyFunction(bool, std::back_insert_iterator<std::string>); + std::string letters = "abc"; + Action<MyFunction> a = SetArrayArgument<1>(letters.begin(), letters.end()); + + std::string s; + a.Perform(make_tuple(true, back_inserter(s))); + EXPECT_EQ(letters, s); +} + +} // namespace gmock_generated_actions_test +} // namespace testing diff --git a/testing/gmock/test/gmock-nice-strict_test.cc b/testing/gmock/test/gmock-nice-strict_test.cc index 955961c..0dc7106 100644 --- a/testing/gmock/test/gmock-nice-strict_test.cc +++ b/testing/gmock/test/gmock-nice-strict_test.cc @@ -36,6 +36,13 @@ #include <gtest/gtest.h> #include <gtest/gtest-spi.h> +// This must not be defined inside the ::testing namespace, or it will +// clash with ::testing::Mock. +class Mock { + public: + MOCK_METHOD0(DoThis, void()); +}; + namespace testing { namespace gmock_nice_strict_test { @@ -166,6 +173,22 @@ TEST(NiceMockTest, NonDefaultConstructor10) { nice_bar.That(5, true); } +#if !GTEST_OS_SYMBIAN +// Tests that NiceMock<Mock> compiles where Mock is a user-defined +// class (as opposed to ::testing::Mock). We had to workaround an +// MSVC 8.0 bug that caused the symbol Mock used in the definition of +// NiceMock to be looked up in the wrong context, and this test +// ensures that our fix works. +// +// We have to skip this test on Symbian, as it causes the program to +// crash there, for reasons unclear to us yet. +TEST(NiceMockTest, AcceptsClassNamedMock) { + NiceMock< ::Mock> nice; + EXPECT_CALL(nice, DoThis()); + nice.DoThis(); +} +#endif // !GTEST_OS_SYMBIAN + // Tests that a strict mock allows expected calls. TEST(StrictMockTest, AllowsExpectedCall) { StrictMock<MockFoo> strict_foo; @@ -224,5 +247,21 @@ TEST(StrictMockTest, NonDefaultConstructor10) { "Uninteresting mock function call"); } +#if !GTEST_OS_SYMBIAN +// Tests that StrictMock<Mock> compiles where Mock is a user-defined +// class (as opposed to ::testing::Mock). We had to workaround an +// MSVC 8.0 bug that caused the symbol Mock used in the definition of +// StrictMock to be looked up in the wrong context, and this test +// ensures that our fix works. +// +// We have to skip this test on Symbian, as it causes the program to +// crash there, for reasons unclear to us yet. +TEST(StrictMockTest, AcceptsClassNamedMock) { + StrictMock< ::Mock> strict; + EXPECT_CALL(strict, DoThis()); + strict.DoThis(); +} +#endif // !GTEST_OS_SYMBIAN + } // namespace gmock_nice_strict_test } // namespace testing diff --git a/testing/gmock/test/gmock-port_test.cc b/testing/gmock/test/gmock-port_test.cc index 2e85bcc..9a64ec33e 100644 --- a/testing/gmock/test/gmock-port_test.cc +++ b/testing/gmock/test/gmock-port_test.cc @@ -36,60 +36,4 @@ #include <gmock/internal/gmock-port.h> #include <gtest/gtest.h> -TEST(GmockCheckSyntaxTest, BehavesLikeASingleStatement) { - if (false) - GMOCK_CHECK_(false) << "This should never be executed; " - "It's a compilation test only."; - - if (true) - GMOCK_CHECK_(true); - else - ; - - if (false) - ; - else - GMOCK_CHECK_(true) << ""; -} - -TEST(GmockCheckSyntaxTest, WorksWithSwitch) { - switch (0) { - case 1: - break; - default: - GMOCK_CHECK_(true); - } - - switch(0) - case 0: - GMOCK_CHECK_(true) << "Check failed in switch case"; -} - -#if GTEST_HAS_DEATH_TEST - -TEST(GmockCheckDeathTest, DiesWithCorrectOutputOnFailure) { - const bool a_false_condition = false; - // MSVC and gcc use different formats to print source file locations. - // Google Mock's failure messages use the same format as used by the - // compiler, in order for the IDE to recognize them. Therefore we look - // for different patterns here depending on the compiler. - const char regex[] = -#ifdef _MSC_VER - "gmock-port_test\\.cc\\(\\d+\\):" -#else - "gmock-port_test\\.cc:[0-9]+" -#endif // _MSC_VER - ".*a_false_condition.*Extra info"; - - EXPECT_DEATH(GMOCK_CHECK_(a_false_condition) << "Extra info", regex); -} - -TEST(GmockCheckDeathTest, LivesSilentlyOnSuccess) { - EXPECT_EXIT({ - GMOCK_CHECK_(true) << "Extra info"; - ::std::cerr << "Success\n"; - exit(0); }, - ::testing::ExitedWithCode(0), "Success"); -} - -#endif // GTEST_HAS_DEATH_TEST +// This file intentionally contains no test at this moment. diff --git a/testing/gmock/test/gmock-printers_test.cc b/testing/gmock/test/gmock-printers_test.cc index 8c03ec4..c72e3d3 100644 --- a/testing/gmock/test/gmock-printers_test.cc +++ b/testing/gmock/test/gmock-printers_test.cc @@ -705,7 +705,7 @@ TEST(PrintStlContainerTest, HashMultiSet) { std::vector<int> numbers; for (size_t i = 0; i != result.length(); i++) { if (expected_pattern[i] == 'd') { - ASSERT_TRUE(isdigit(result[i])); + ASSERT_TRUE(isdigit(result[i]) != 0); numbers.push_back(result[i] - '0'); } else { EXPECT_EQ(expected_pattern[i], result[i]) << " where result is " @@ -790,14 +790,14 @@ TEST(PrintStlContainerTest, NestedContainer) { } TEST(PrintStlContainerTest, OneDimensionalNativeArray) { - const int a[] = { 1, 2, 3 }; - NativeArray<int> b(a, kReference); + const int a[3] = { 1, 2, 3 }; + NativeArray<int> b(a, 3, kReference); EXPECT_EQ("{ 1, 2, 3 }", Print(b)); } TEST(PrintStlContainerTest, TwoDimensionalNativeArray) { - const int a[][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; - NativeArray<int[3]> b(a, kReference); + const int a[2][3] = { { 1, 2, 3 }, { 4, 5, 6 } }; + NativeArray<int[3]> b(a, 2, kReference); EXPECT_EQ("{ { 1, 2, 3 }, { 4, 5, 6 } }", Print(b)); } @@ -840,16 +840,16 @@ TEST(PrintTupleTest, VariousSizes) { const char* const str = "8"; tuple<bool, char, short, testing::internal::Int32, // NOLINT testing::internal::Int64, float, double, const char*, void*, string> - t10(false, 'a', 3, 4, 5, 6.5F, 7.5, str, NULL, "10"); - EXPECT_EQ("(false, 'a' (97), 3, 4, 5, 6.5, 7.5, " + PrintPointer(str) + + t10(false, 'a', 3, 4, 5, 1.5F, -2.5, str, NULL, "10"); + EXPECT_EQ("(false, 'a' (97), 3, 4, 5, 1.5, -2.5, " + PrintPointer(str) + " pointing to \"8\", NULL, \"10\")", Print(t10)); } // Nested tuples. TEST(PrintTupleTest, NestedTuple) { - tuple<tuple<int, double>, char> nested(make_tuple(5, 9.5), 'a'); - EXPECT_EQ("((5, 9.5), 'a' (97))", Print(nested)); + tuple<tuple<int, bool>, char> nested(make_tuple(5, true), 'a'); + EXPECT_EQ("((5, true), 'a' (97))", Print(nested)); } // Tests printing user-defined unprintable types. @@ -919,12 +919,31 @@ TEST(PrintProtocolMessageTest, PrintsShortDebugString) { EXPECT_EQ("<member:\"yes\">", Print(msg)); } -// Tests printing a proto2 message. -TEST(PrintProto2MessageTest, PrintsShortDebugString) { +// Tests printing a short proto2 message. +TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) { testing::internal::FooMessage msg; msg.set_int_field(2); + msg.set_string_field("hello"); EXPECT_PRED2(RE::FullMatch, Print(msg), - "<int_field:\\s*2\\s*>"); + "<int_field:\\s*2\\s+string_field:\\s*\"hello\">"); +} + +// Tests printing a long proto2 message. +TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) { + testing::internal::FooMessage msg; + msg.set_int_field(2); + msg.set_string_field("hello"); + msg.add_names("peter"); + msg.add_names("paul"); + msg.add_names("mary"); + EXPECT_PRED2(RE::FullMatch, Print(msg), + "<\n" + "int_field:\\s*2\n" + "string_field:\\s*\"hello\"\n" + "names:\\s*\"peter\"\n" + "names:\\s*\"paul\"\n" + "names:\\s*\"mary\"\n" + ">"); } #endif // GMOCK_HAS_PROTOBUF_ diff --git a/testing/gmock/test/gmock-spec-builders_test.cc b/testing/gmock/test/gmock-spec-builders_test.cc index 058d343c..707d896 100644 --- a/testing/gmock/test/gmock-spec-builders_test.cc +++ b/testing/gmock/test/gmock-spec-builders_test.cc @@ -71,6 +71,9 @@ using testing::CardinalityInterface; using testing::Const; using testing::DoAll; using testing::DoDefault; +using testing::Eq; +using testing::Expectation; +using testing::ExpectationSet; using testing::GMOCK_FLAG(verbose); using testing::Gt; using testing::InSequence; @@ -80,13 +83,13 @@ using testing::IsSubstring; using testing::Lt; using testing::Message; using testing::Mock; +using testing::Ne; using testing::Return; using testing::Sequence; using testing::internal::g_gmock_mutex; using testing::internal::kErrorVerbosity; using testing::internal::kInfoVerbosity; using testing::internal::kWarningVerbosity; -using testing::internal::Expectation; using testing::internal::ExpectationTester; using testing::internal::string; @@ -194,19 +197,15 @@ TEST(OnCallSyntaxTest, WithCanAppearAtMostOnce) { }, ".With() cannot appear more than once in an ON_CALL()"); } -#if GTEST_HAS_DEATH_TEST - TEST(OnCallSyntaxTest, WillByDefaultIsMandatory) { MockA a; - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ ON_CALL(a, DoA(5)); a.DoA(5); }, ""); } -#endif // GTEST_HAS_DEATH_TEST - TEST(OnCallSyntaxTest, WillByDefaultCanAppearAtMostOnce) { MockA a; @@ -345,7 +344,22 @@ TEST(ExpectCallSyntaxTest, InSequenceCanAppearMultipleTimes) { a.DoA(1); } -TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWill) { +TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeAfter) { + MockA a; + Sequence s; + + Expectation e = EXPECT_CALL(a, DoA(1)) + .Times(AnyNumber()); + EXPECT_NONFATAL_FAILURE({ // NOLINT + EXPECT_CALL(a, DoA(2)) + .After(e) + .InSequence(s); + }, ".InSequence() cannot appear after "); + + a.DoA(2); +} + +TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWillOnce) { MockA a; Sequence s; @@ -358,6 +372,20 @@ TEST(ExpectCallSyntaxTest, InSequenceMustBeBeforeWill) { a.DoA(1); } +TEST(ExpectCallSyntaxTest, AfterMustBeBeforeWillOnce) { + MockA a; + + Expectation e = EXPECT_CALL(a, DoA(1)); + EXPECT_NONFATAL_FAILURE({ + EXPECT_CALL(a, DoA(2)) + .WillOnce(Return()) + .After(e); + }, ".After() cannot appear after "); + + a.DoA(1); + a.DoA(2); +} + TEST(ExpectCallSyntaxTest, WillIsOptional) { MockA a; @@ -957,7 +985,7 @@ TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) { // There should be one non-fatal failure. ASSERT_EQ(1, failures.size()); const ::testing::TestPartResult& r = failures.GetTestPartResult(0); - EXPECT_EQ(::testing::TPRT_NONFATAL_FAILURE, r.type()); + EXPECT_EQ(::testing::TestPartResult::kNonFatalFailure, r.type()); // Verifies that the failure message contains the two unsatisfied // pre-requisites but not the satisfied one. @@ -986,18 +1014,14 @@ TEST(UnexpectedCallTest, UnsatisifiedPrerequisites) { #endif // GMOCK_HAS_REGEX -#if GTEST_HAS_DEATH_TEST - TEST(UndefinedReturnValueTest, ReturnValueIsMandatory) { MockA a; // TODO(wan@google.com): We should really verify the output message, // but we cannot yet due to that EXPECT_DEATH only captures stderr // while Google Mock logs to stdout. - EXPECT_DEATH(a.ReturnResult(1), ""); + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(1), ""); } -#endif // GTEST_HAS_DEATH_TEST - // Tests that an excessive call (one whose arguments match the // matchers but is called too many times) performs the default action. TEST(ExcessiveCallTest, DoesDefaultAction) { @@ -1142,8 +1166,6 @@ TEST(SequenceTest, AnyOrderIsOkByDefault) { } } -#if GTEST_HAS_DEATH_TEST - // Tests that the calls must be in strict order when a complete order // is specified. TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo) { @@ -1162,13 +1184,13 @@ TEST(SequenceTest, CallsMustBeInStrictOrderWhenSaidSo) { .InSequence(s) .WillOnce(Return(Result())); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(1); a.ReturnResult(3); a.ReturnResult(2); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(2); a.ReturnResult(1); a.ReturnResult(3); @@ -1201,21 +1223,21 @@ TEST(SequenceTest, CallsMustConformToSpecifiedDag) { .InSequence(x) .WillOnce(Return(Result())); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(1); b.DoB(); a.ReturnResult(2); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(2); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(3); }, ""); - EXPECT_DEATH({ // NOLINT + EXPECT_DEATH_IF_SUPPORTED({ a.ReturnResult(1); b.DoB(); b.DoB(); @@ -1229,8 +1251,6 @@ TEST(SequenceTest, CallsMustConformToSpecifiedDag) { a.ReturnResult(3); } -#endif // GTEST_HAS_DEATH_TEST - TEST(SequenceTest, Retirement) { MockA a; Sequence s; @@ -1248,6 +1268,292 @@ TEST(SequenceTest, Retirement) { a.DoA(1); } +// Tests Expectation. + +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. + + EXPECT_THAT(e1, Ne(e2)); + EXPECT_THAT(e2, Eq(e3)); + a.DoA(1); +} + +TEST(ExpectationTest, AssignmentWorks) { + MockA a; + Expectation e1; + Expectation e2 = EXPECT_CALL(a, DoA(1)); + + EXPECT_THAT(e1, Ne(e2)); + + e1 = e2; + EXPECT_THAT(e1, Eq(e2)); + + a.DoA(1); +} + +// Tests ExpectationSet. + +TEST(ExpectationSetTest, MemberTypesAreCorrect) { + ::testing::StaticAssertTypeEq<Expectation, ExpectationSet::value_type>(); +} + +TEST(ExpectationSetTest, ConstructorsWork) { + MockA a; + + Expectation e1; + const Expectation e2; + ExpectationSet es1; // Default ctor. + ExpectationSet es2 = EXPECT_CALL(a, DoA(1)); // Ctor from EXPECT_CALL. + ExpectationSet es3 = e1; // Ctor from Expectation. + ExpectationSet es4(e1); // Ctor from Expectation; alternative syntax. + ExpectationSet es5 = e2; // Ctor from const Expectation. + ExpectationSet es6(e2); // Ctor from const Expectation; alternative syntax. + ExpectationSet es7 = es2; // Copy ctor. + + EXPECT_EQ(0, es1.size()); + EXPECT_EQ(1, es2.size()); + EXPECT_EQ(1, es3.size()); + EXPECT_EQ(1, es4.size()); + EXPECT_EQ(1, es5.size()); + EXPECT_EQ(1, es6.size()); + EXPECT_EQ(1, es7.size()); + + EXPECT_THAT(es3, Ne(es2)); + EXPECT_THAT(es4, Eq(es3)); + EXPECT_THAT(es5, Eq(es4)); + EXPECT_THAT(es6, Eq(es5)); + EXPECT_THAT(es7, Eq(es2)); + a.DoA(1); +} + +TEST(ExpectationSetTest, AssignmentWorks) { + ExpectationSet es1; + ExpectationSet es2 = Expectation(); + + es1 = es2; + EXPECT_EQ(1, es1.size()); + EXPECT_THAT(*(es1.begin()), Eq(Expectation())); + EXPECT_THAT(es1, Eq(es2)); +} + +TEST(ExpectationSetTest, InsertionWorks) { + ExpectationSet es1; + Expectation e1; + es1 += e1; + EXPECT_EQ(1, es1.size()); + EXPECT_THAT(*(es1.begin()), Eq(e1)); + + MockA a; + Expectation e2 = EXPECT_CALL(a, DoA(1)); + es1 += e2; + EXPECT_EQ(2, es1.size()); + + ExpectationSet::const_iterator it1 = es1.begin(); + ExpectationSet::const_iterator it2 = it1; + ++it2; + EXPECT_TRUE(*it1 == e1 || *it2 == e1); // e1 must be in the set. + EXPECT_TRUE(*it1 == e2 || *it2 == e2); // e2 must be in the set too. + a.DoA(1); +} + +TEST(ExpectationSetTest, SizeWorks) { + ExpectationSet es; + EXPECT_EQ(0, es.size()); + + es += Expectation(); + EXPECT_EQ(1, es.size()); + + MockA a; + es += EXPECT_CALL(a, DoA(1)); + EXPECT_EQ(2, es.size()); + + a.DoA(1); +} + +TEST(ExpectationSetTest, IsEnumerable) { + ExpectationSet es; + EXPECT_THAT(es.begin(), Eq(es.end())); + + es += Expectation(); + ExpectationSet::const_iterator it = es.begin(); + EXPECT_THAT(it, Ne(es.end())); + EXPECT_THAT(*it, Eq(Expectation())); + ++it; + EXPECT_THAT(it, Eq(es.end())); +} + +// Tests the .After() clause. + +TEST(AfterTest, SucceedsWhenPartialOrderIsSatisfied) { + MockA a; + ExpectationSet es; + es += EXPECT_CALL(a, DoA(1)); + es += EXPECT_CALL(a, DoA(2)); + EXPECT_CALL(a, DoA(3)) + .After(es); + + a.DoA(1); + a.DoA(2); + a.DoA(3); +} + +TEST(AfterTest, SucceedsWhenTotalOrderIsSatisfied) { + MockA a; + MockB b; + // The following also verifies that const Expectation objects work + // too. Do not remove the const modifiers. + const Expectation e1 = EXPECT_CALL(a, DoA(1)); + const Expectation e2 = EXPECT_CALL(b, DoB()) + .Times(2) + .After(e1); + EXPECT_CALL(a, DoA(2)).After(e2); + + a.DoA(1); + b.DoB(); + b.DoB(); + a.DoA(2); +} + +// Calls must be in strict order when specified so. +TEST(AfterDeathTest, CallsMustBeInStrictOrderWhenSpecifiedSo) { + MockA a; + MockB b; + Expectation e1 = EXPECT_CALL(a, DoA(1)); + Expectation e2 = EXPECT_CALL(b, DoB()) + .Times(2) + .After(e1); + EXPECT_CALL(a, ReturnResult(2)) + .After(e2) + .WillOnce(Return(Result())); + + a.DoA(1); + // If a call to ReturnResult() violates the specified order, no + // matching expectation will be found, and thus the default action + // will be done. Since the return type of ReturnResult() is not a + // built-in type, gmock won't know what to return and will thus + // abort the program. Therefore a death test can tell us whether + // gmock catches the order violation correctly. + // + // gtest and gmock print messages to stdout, which isn't captured by + // death tests. Therefore we have to match with an empty regular + // expression in all the EXPECT_DEATH()s. + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(2), ""); + + b.DoB(); + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(2), ""); + + b.DoB(); + a.ReturnResult(2); +} + +// Calls must satisfy the partial order when specified so. +TEST(AfterDeathTest, CallsMustSatisfyPartialOrderWhenSpecifiedSo) { + MockA a; + Expectation e = EXPECT_CALL(a, DoA(1)); + const ExpectationSet es = EXPECT_CALL(a, DoA(2)); + EXPECT_CALL(a, ReturnResult(3)) + .After(e, es) + .WillOnce(Return(Result())); + + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); + + a.DoA(2); + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); + + a.DoA(1); + a.ReturnResult(3); +} + +// .After() can be combined with .InSequence(). +TEST(AfterDeathTest, CanBeUsedWithInSequence) { + MockA a; + Sequence s; + Expectation e = EXPECT_CALL(a, DoA(1)); + EXPECT_CALL(a, DoA(2)).InSequence(s); + EXPECT_CALL(a, ReturnResult(3)) + .InSequence(s).After(e) + .WillOnce(Return(Result())); + + a.DoA(1); + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); + + a.DoA(2); + a.ReturnResult(3); +} + +// .After() can be called multiple times. +TEST(AfterTest, CanBeCalledManyTimes) { + MockA a; + Expectation e1 = EXPECT_CALL(a, DoA(1)); + Expectation e2 = EXPECT_CALL(a, DoA(2)); + Expectation e3 = EXPECT_CALL(a, DoA(3)); + EXPECT_CALL(a, DoA(4)) + .After(e1) + .After(e2) + .After(e3); + + a.DoA(3); + a.DoA(1); + a.DoA(2); + a.DoA(4); +} + +// .After() accepts up to 5 arguments. +TEST(AfterTest, AcceptsUpToFiveArguments) { + MockA a; + Expectation e1 = EXPECT_CALL(a, DoA(1)); + Expectation e2 = EXPECT_CALL(a, DoA(2)); + Expectation e3 = EXPECT_CALL(a, DoA(3)); + ExpectationSet es1 = EXPECT_CALL(a, DoA(4)); + ExpectationSet es2 = EXPECT_CALL(a, DoA(5)); + EXPECT_CALL(a, DoA(6)) + .After(e1, e2, e3, es1, es2); + + a.DoA(5); + a.DoA(2); + a.DoA(4); + a.DoA(1); + a.DoA(3); + a.DoA(6); +} + +// .After() allows input to contain duplicated Expectations. +TEST(AfterTest, AcceptsDuplicatedInput) { + MockA a; + Expectation e1 = EXPECT_CALL(a, DoA(1)); + Expectation e2 = EXPECT_CALL(a, DoA(2)); + ExpectationSet es; + es += e1; + es += e2; + EXPECT_CALL(a, ReturnResult(3)) + .After(e1, e2, es, e1) + .WillOnce(Return(Result())); + + a.DoA(1); + EXPECT_DEATH_IF_SUPPORTED(a.ReturnResult(3), ""); + + a.DoA(2); + a.ReturnResult(3); +} + +// An Expectation added to an ExpectationSet after it has been used in +// an .After() has no effect. +TEST(AfterTest, ChangesToExpectationSetHaveNoEffectAfterwards) { + MockA a; + ExpectationSet es1 = EXPECT_CALL(a, DoA(1)); + Expectation e2 = EXPECT_CALL(a, DoA(2)); + EXPECT_CALL(a, DoA(3)) + .After(es1); + es1 += e2; + + a.DoA(1); + a.DoA(3); + a.DoA(2); +} + // Tests that Google Mock correctly handles calls to mock functions // after a mock object owning one of their pre-requisites has died. @@ -1628,7 +1934,16 @@ class LogTestHelper { class GMockLogTest : public ::testing::Test { protected: - virtual void SetUp() { original_verbose_ = GMOCK_FLAG(verbose); } + virtual void SetUp() { + // The code needs to work when both ::string and ::std::string are + // defined and the flag is implemented as a + // testing::internal::String. In this case, without the call to + // c_str(), the compiler will complain that it cannot figure out + // whether the String flag should be converted to a ::string or an + // ::std::string before being assigned to original_verbose_. + original_verbose_ = GMOCK_FLAG(verbose).c_str(); + } + virtual void TearDown() { GMOCK_FLAG(verbose) = original_verbose_; } LogTestHelper helper_; @@ -2000,7 +2315,14 @@ void Helper(MockC* c) { } // namespace +// Allows the user to define his own main and then invoke gmock_main +// from it. This might be necessary on some platforms which require +// specific setup and teardown. +#if GMOCK_RENAME_MAIN +int gmock_main(int argc, char **argv) { +#else int main(int argc, char **argv) { +#endif // GMOCK_RENAME_MAIN testing::InitGoogleMock(&argc, argv); // Ensures that the tests pass no matter what value of diff --git a/testing/gmock/test/gmock_leak_test.py b/testing/gmock/test/gmock_leak_test.py index 1337e0b..1337e0b 100755..100644 --- a/testing/gmock/test/gmock_leak_test.py +++ b/testing/gmock/test/gmock_leak_test.py diff --git a/testing/gmock/test/gmock_link_test.h b/testing/gmock/test/gmock_link_test.h index b903f3c..40554bf 100644 --- a/testing/gmock/test/gmock_link_test.h +++ b/testing/gmock/test/gmock_link_test.h @@ -116,7 +116,7 @@ #include <gmock/gmock.h> -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE #include <errno.h> #endif @@ -147,6 +147,7 @@ using testing::IgnoreResult; using testing::Invoke; using testing::InvokeArgument; using testing::InvokeWithoutArgs; +using testing::IsNull; using testing::Le; using testing::Lt; using testing::Matcher; @@ -176,18 +177,18 @@ using testing::WithArg; using testing::WithArgs; using testing::WithoutArgs; -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE using testing::SetErrnoAndReturn; -#endif // _WIN32_WCE +#endif #if GTEST_HAS_EXCEPTIONS using testing::Throw; -#endif // GTEST_HAS_EXCEPTIONS +#endif #if GMOCK_HAS_REGEX using testing::ContainsRegex; using testing::MatchesRegex; -#endif // GMOCK_HAS_REGEX +#endif class Interface { public: @@ -297,7 +298,7 @@ TEST(LinkTest, TestSetArrayArgument) { mock.VoidFromString(&ch); } -#ifndef _WIN32_WCE +#if !GTEST_OS_WINDOWS_MOBILE // Tests the linkage of the SetErrnoAndReturn action. TEST(LinkTest, TestSetErrnoAndReturn) { @@ -309,7 +310,7 @@ TEST(LinkTest, TestSetErrnoAndReturn) { errno = saved_errno; } -#endif // _WIN32_WCE +#endif // !GTEST_OS_WINDOWS_MOBILE // Tests the linkage of the Invoke(function) and Invoke(object, method) actions. TEST(LinkTest, TestInvoke) { @@ -491,6 +492,13 @@ TEST(LinkTest, TestMatcherNotNull) { ON_CALL(mock, VoidFromString(NotNull())).WillByDefault(Return()); } +// Tests the linkage of the IsNull matcher. +TEST(LinkTest, TestMatcherIsNull) { + Mock mock; + + ON_CALL(mock, VoidFromString(IsNull())).WillByDefault(Return()); +} + // Tests the linkage of the Ref matcher. TEST(LinkTest, TestMatcherRef) { Mock mock; diff --git a/testing/gmock/test/gmock_output_test.py b/testing/gmock/test/gmock_output_test.py index f43f707..f43f707 100755..100644 --- a/testing/gmock/test/gmock_output_test.py +++ b/testing/gmock/test/gmock_output_test.py diff --git a/testing/gmock/test/gmock_test_utils.py b/testing/gmock/test/gmock_test_utils.py index 4c09e39..2fda138 100755..100644 --- a/testing/gmock/test/gmock_test_utils.py +++ b/testing/gmock/test/gmock_test_utils.py @@ -1,4 +1,4 @@ -#!/usr/bin/python2.4 +#!/usr/bin/env python # # Copyright 2006, Google Inc. # All rights reserved. diff --git a/testing/gtest.gyp b/testing/gtest.gyp index f89d6e5c..17546f4 100644 --- a/testing/gtest.gyp +++ b/testing/gtest.gyp @@ -87,6 +87,7 @@ 'target_conditions': [ ['_type=="executable"', {'test': 1}], ], + 'msvs_disabled_warnings': [4800], }, }, { |