shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // Copyright 2007, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
| 32 | // Google Mock - a framework for writing C++ mock classes. |
| 33 | // |
| 34 | // This file implements some commonly used actions. |
| 35 | |
| 36 | #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ |
| 37 | #define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ |
| 38 | |
| 39 | #include <algorithm> |
| 40 | #include <string> |
| 41 | #include <errno.h> |
| 42 | #include <gmock/internal/gmock-internal-utils.h> |
| 43 | #include <gmock/internal/gmock-port.h> |
| 44 | |
| 45 | namespace testing { |
| 46 | |
| 47 | // To implement an action Foo, define: |
| 48 | // 1. a class FooAction that implements the ActionInterface interface, and |
| 49 | // 2. a factory function that creates an Action object from a |
| 50 | // const FooAction*. |
| 51 | // |
| 52 | // The two-level delegation design follows that of Matcher, providing |
| 53 | // consistency for extension developers. It also eases ownership |
| 54 | // management as Action objects can now be copied like plain values. |
| 55 | |
| 56 | namespace internal { |
| 57 | |
| 58 | template <typename F> |
| 59 | class MonomorphicDoDefaultActionImpl; |
| 60 | |
| 61 | template <typename F1, typename F2> |
| 62 | class ActionAdaptor; |
| 63 | |
| 64 | // BuiltInDefaultValue<T>::Get() returns the "built-in" default |
| 65 | // value for type T, which is NULL when T is a pointer type, 0 when T |
| 66 | // is a numeric type, false when T is bool, or "" when T is string or |
| 67 | // std::string. For any other type T, this value is undefined and the |
| 68 | // function will abort the process. |
| 69 | template <typename T> |
| 70 | class BuiltInDefaultValue { |
| 71 | public: |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame^] | 72 | // This function returns true iff type T has a built-in default value. |
| 73 | static bool Exists() { return false; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 74 | static T Get() { |
| 75 | Assert(false, __FILE__, __LINE__, |
| 76 | "Default action undefined for the function return type."); |
| 77 | return internal::Invalid<T>(); |
| 78 | // The above statement will never be reached, but is required in |
| 79 | // order for this function to compile. |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | // This partial specialization says that we use the same built-in |
| 84 | // default value for T and const T. |
| 85 | template <typename T> |
| 86 | class BuiltInDefaultValue<const T> { |
| 87 | public: |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame^] | 88 | static bool Exists() { return BuiltInDefaultValue<T>::Exists(); } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 89 | static T Get() { return BuiltInDefaultValue<T>::Get(); } |
| 90 | }; |
| 91 | |
| 92 | // This partial specialization defines the default values for pointer |
| 93 | // types. |
| 94 | template <typename T> |
| 95 | class BuiltInDefaultValue<T*> { |
| 96 | public: |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame^] | 97 | static bool Exists() { return true; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 98 | static T* Get() { return NULL; } |
| 99 | }; |
| 100 | |
| 101 | // The following specializations define the default values for |
| 102 | // specific types we care about. |
| 103 | #define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(type, value) \ |
| 104 | template <> \ |
| 105 | class BuiltInDefaultValue<type> { \ |
| 106 | public: \ |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame^] | 107 | static bool Exists() { return true; } \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 108 | static type Get() { return value; } \ |
| 109 | } |
| 110 | |
| 111 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(void, ); // NOLINT |
| 112 | #if GTEST_HAS_GLOBAL_STRING |
| 113 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(::string, ""); |
| 114 | #endif // GTEST_HAS_GLOBAL_STRING |
| 115 | #if GTEST_HAS_STD_STRING |
| 116 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(::std::string, ""); |
| 117 | #endif // GTEST_HAS_STD_STRING |
| 118 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(bool, false); |
| 119 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(unsigned char, '\0'); |
| 120 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(signed char, '\0'); |
| 121 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(char, '\0'); |
| 122 | |
| 123 | // signed wchar_t and unsigned wchar_t are NOT in the C++ standard. |
| 124 | // Using them is a bad practice and not portable. So don't use them. |
| 125 | // |
| 126 | // Still, Google Mock is designed to work even if the user uses signed |
| 127 | // wchar_t or unsigned wchar_t (obviously, assuming the compiler |
| 128 | // supports them). |
| 129 | // |
| 130 | // To gcc, |
| 131 | // |
| 132 | // wchar_t == signed wchar_t != unsigned wchar_t == unsigned int |
| 133 | // |
| 134 | // MSVC does not recognize signed wchar_t or unsigned wchar_t. It |
| 135 | // treats wchar_t as a native type usually, but treats it as the same |
| 136 | // as unsigned short when the compiler option /Zc:wchar_t- is |
| 137 | // specified. |
| 138 | // |
| 139 | // Therefore we provide a default action for wchar_t when compiled |
| 140 | // with gcc or _NATIVE_WCHAR_T_DEFINED is defined. |
| 141 | // |
| 142 | // There's no need for a default action for signed wchar_t, as that |
| 143 | // type is the same as wchar_t for gcc, and invalid for MSVC. |
| 144 | // |
| 145 | // There's also no need for a default action for unsigned wchar_t, as |
| 146 | // that type is the same as unsigned int for gcc, and invalid for |
| 147 | // MSVC. |
| 148 | #if defined(__GNUC__) || defined(_NATIVE_WCHAR_T_DEFINED) |
| 149 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(wchar_t, 0U); // NOLINT |
| 150 | #endif |
| 151 | |
| 152 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(unsigned short, 0U); // NOLINT |
| 153 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(signed short, 0); // NOLINT |
| 154 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(unsigned int, 0U); |
| 155 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(signed int, 0); |
| 156 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(unsigned long, 0UL); // NOLINT |
| 157 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(signed long, 0L); // NOLINT |
| 158 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(UInt64, 0); |
| 159 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(Int64, 0); |
| 160 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(float, 0); |
| 161 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE(double, 0); |
| 162 | |
| 163 | #undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE |
| 164 | |
| 165 | } // namespace internal |
| 166 | |
| 167 | // When an unexpected function call is encountered, Google Mock will |
| 168 | // let it return a default value if the user has specified one for its |
| 169 | // return type, or if the return type has a built-in default value; |
| 170 | // otherwise Google Mock won't know what value to return and will have |
| 171 | // to abort the process. |
| 172 | // |
| 173 | // The DefaultValue<T> class allows a user to specify the |
| 174 | // default value for a type T that is both copyable and publicly |
| 175 | // destructible (i.e. anything that can be used as a function return |
| 176 | // type). The usage is: |
| 177 | // |
| 178 | // // Sets the default value for type T to be foo. |
| 179 | // DefaultValue<T>::Set(foo); |
| 180 | template <typename T> |
| 181 | class DefaultValue { |
| 182 | public: |
| 183 | // Sets the default value for type T; requires T to be |
| 184 | // copy-constructable and have a public destructor. |
| 185 | static void Set(T x) { |
| 186 | delete value_; |
| 187 | value_ = new T(x); |
| 188 | } |
| 189 | |
| 190 | // Unsets the default value for type T. |
| 191 | static void Clear() { |
| 192 | delete value_; |
| 193 | value_ = NULL; |
| 194 | } |
| 195 | |
| 196 | // Returns true iff the user has set the default value for type T. |
| 197 | static bool IsSet() { return value_ != NULL; } |
| 198 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame^] | 199 | // Returns true if T has a default return value set by the user or there |
| 200 | // exists a built-in default value. |
| 201 | static bool Exists() { |
| 202 | return IsSet() || internal::BuiltInDefaultValue<T>::Exists(); |
| 203 | } |
| 204 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 205 | // Returns the default value for type T if the user has set one; |
| 206 | // otherwise returns the built-in default value if there is one; |
| 207 | // otherwise aborts the process. |
| 208 | static T Get() { |
| 209 | return value_ == NULL ? |
| 210 | internal::BuiltInDefaultValue<T>::Get() : *value_; |
| 211 | } |
| 212 | private: |
| 213 | static const T* value_; |
| 214 | }; |
| 215 | |
| 216 | // This partial specialization allows a user to set default values for |
| 217 | // reference types. |
| 218 | template <typename T> |
| 219 | class DefaultValue<T&> { |
| 220 | public: |
| 221 | // Sets the default value for type T&. |
| 222 | static void Set(T& x) { // NOLINT |
| 223 | address_ = &x; |
| 224 | } |
| 225 | |
| 226 | // Unsets the default value for type T&. |
| 227 | static void Clear() { |
| 228 | address_ = NULL; |
| 229 | } |
| 230 | |
| 231 | // Returns true iff the user has set the default value for type T&. |
| 232 | static bool IsSet() { return address_ != NULL; } |
| 233 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame^] | 234 | // Returns true if T has a default return value set by the user or there |
| 235 | // exists a built-in default value. |
| 236 | static bool Exists() { |
| 237 | return IsSet() || internal::BuiltInDefaultValue<T&>::Exists(); |
| 238 | } |
| 239 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 240 | // Returns the default value for type T& if the user has set one; |
| 241 | // otherwise returns the built-in default value if there is one; |
| 242 | // otherwise aborts the process. |
| 243 | static T& Get() { |
| 244 | return address_ == NULL ? |
| 245 | internal::BuiltInDefaultValue<T&>::Get() : *address_; |
| 246 | } |
| 247 | private: |
| 248 | static T* address_; |
| 249 | }; |
| 250 | |
| 251 | // This specialization allows DefaultValue<void>::Get() to |
| 252 | // compile. |
| 253 | template <> |
| 254 | class DefaultValue<void> { |
| 255 | public: |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame^] | 256 | static bool Exists() { return true; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 257 | static void Get() {} |
| 258 | }; |
| 259 | |
| 260 | // Points to the user-set default value for type T. |
| 261 | template <typename T> |
| 262 | const T* DefaultValue<T>::value_ = NULL; |
| 263 | |
| 264 | // Points to the user-set default value for type T&. |
| 265 | template <typename T> |
| 266 | T* DefaultValue<T&>::address_ = NULL; |
| 267 | |
| 268 | // Implement this interface to define an action for function type F. |
| 269 | template <typename F> |
| 270 | class ActionInterface { |
| 271 | public: |
| 272 | typedef typename internal::Function<F>::Result Result; |
| 273 | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 274 | |
| 275 | ActionInterface() : is_do_default_(false) {} |
| 276 | |
| 277 | virtual ~ActionInterface() {} |
| 278 | |
| 279 | // Performs the action. This method is not const, as in general an |
| 280 | // action can have side effects and be stateful. For example, a |
| 281 | // get-the-next-element-from-the-collection action will need to |
| 282 | // remember the current element. |
| 283 | virtual Result Perform(const ArgumentTuple& args) = 0; |
| 284 | |
| 285 | // Returns true iff this is the DoDefault() action. |
| 286 | bool IsDoDefault() const { return is_do_default_; } |
| 287 | private: |
| 288 | template <typename Function> |
| 289 | friend class internal::MonomorphicDoDefaultActionImpl; |
| 290 | |
| 291 | // This private constructor is reserved for implementing |
| 292 | // DoDefault(), the default action for a given mock function. |
| 293 | explicit ActionInterface(bool is_do_default) |
| 294 | : is_do_default_(is_do_default) {} |
| 295 | |
| 296 | // True iff this action is DoDefault(). |
| 297 | const bool is_do_default_; |
| 298 | }; |
| 299 | |
| 300 | // An Action<F> is a copyable and IMMUTABLE (except by assignment) |
| 301 | // object that represents an action to be taken when a mock function |
| 302 | // of type F is called. The implementation of Action<T> is just a |
| 303 | // linked_ptr to const ActionInterface<T>, so copying is fairly cheap. |
| 304 | // Don't inherit from Action! |
| 305 | // |
| 306 | // You can view an object implementing ActionInterface<F> as a |
| 307 | // concrete action (including its current state), and an Action<F> |
| 308 | // object as a handle to it. |
| 309 | template <typename F> |
| 310 | class Action { |
| 311 | public: |
| 312 | typedef typename internal::Function<F>::Result Result; |
| 313 | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 314 | |
| 315 | // Constructs a null Action. Needed for storing Action objects in |
| 316 | // STL containers. |
| 317 | Action() : impl_(NULL) {} |
| 318 | |
| 319 | // Constructs an Action from its implementation. |
| 320 | explicit Action(ActionInterface<F>* impl) : impl_(impl) {} |
| 321 | |
| 322 | // Copy constructor. |
| 323 | Action(const Action& action) : impl_(action.impl_) {} |
| 324 | |
| 325 | // This constructor allows us to turn an Action<Func> object into an |
| 326 | // Action<F>, as long as F's arguments can be implicitly converted |
| 327 | // to Func's and Func's return type cann be implicitly converted to |
| 328 | // F's. |
| 329 | template <typename Func> |
| 330 | explicit Action(const Action<Func>& action); |
| 331 | |
| 332 | // Returns true iff this is the DoDefault() action. |
| 333 | bool IsDoDefault() const { return impl_->IsDoDefault(); } |
| 334 | |
| 335 | // Performs the action. Note that this method is const even though |
| 336 | // the corresponding method in ActionInterface is not. The reason |
| 337 | // is that a const Action<F> means that it cannot be re-bound to |
| 338 | // another concrete action, not that the concrete action it binds to |
| 339 | // cannot change state. (Think of the difference between a const |
| 340 | // pointer and a pointer to const.) |
| 341 | Result Perform(const ArgumentTuple& args) const { |
| 342 | return impl_->Perform(args); |
| 343 | } |
| 344 | private: |
| 345 | template <typename F1, typename F2> |
| 346 | friend class internal::ActionAdaptor; |
| 347 | |
| 348 | internal::linked_ptr<ActionInterface<F> > impl_; |
| 349 | }; |
| 350 | |
| 351 | // The PolymorphicAction class template makes it easy to implement a |
| 352 | // polymorphic action (i.e. an action that can be used in mock |
| 353 | // functions of than one type, e.g. Return()). |
| 354 | // |
| 355 | // To define a polymorphic action, a user first provides a COPYABLE |
| 356 | // implementation class that has a Perform() method template: |
| 357 | // |
| 358 | // class FooAction { |
| 359 | // public: |
| 360 | // template <typename Result, typename ArgumentTuple> |
| 361 | // Result Perform(const ArgumentTuple& args) const { |
| 362 | // // Processes the arguments and returns a result, using |
| 363 | // // tr1::get<N>(args) to get the N-th (0-based) argument in the tuple. |
| 364 | // } |
| 365 | // ... |
| 366 | // }; |
| 367 | // |
| 368 | // Then the user creates the polymorphic action using |
| 369 | // MakePolymorphicAction(object) where object has type FooAction. See |
| 370 | // the definition of Return(void) and SetArgumentPointee<N>(value) for |
| 371 | // complete examples. |
| 372 | template <typename Impl> |
| 373 | class PolymorphicAction { |
| 374 | public: |
| 375 | explicit PolymorphicAction(const Impl& impl) : impl_(impl) {} |
| 376 | |
| 377 | template <typename F> |
| 378 | operator Action<F>() const { |
| 379 | return Action<F>(new MonomorphicImpl<F>(impl_)); |
| 380 | } |
| 381 | private: |
| 382 | template <typename F> |
| 383 | class MonomorphicImpl : public ActionInterface<F> { |
| 384 | public: |
| 385 | typedef typename internal::Function<F>::Result Result; |
| 386 | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 387 | |
| 388 | explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} |
| 389 | |
| 390 | virtual Result Perform(const ArgumentTuple& args) { |
| 391 | return impl_.template Perform<Result>(args); |
| 392 | } |
| 393 | |
| 394 | private: |
| 395 | Impl impl_; |
| 396 | }; |
| 397 | |
| 398 | Impl impl_; |
| 399 | }; |
| 400 | |
| 401 | // Creates an Action from its implementation and returns it. The |
| 402 | // created Action object owns the implementation. |
| 403 | template <typename F> |
| 404 | Action<F> MakeAction(ActionInterface<F>* impl) { |
| 405 | return Action<F>(impl); |
| 406 | } |
| 407 | |
| 408 | // Creates a polymorphic action from its implementation. This is |
| 409 | // easier to use than the PolymorphicAction<Impl> constructor as it |
| 410 | // doesn't require you to explicitly write the template argument, e.g. |
| 411 | // |
| 412 | // MakePolymorphicAction(foo); |
| 413 | // vs |
| 414 | // PolymorphicAction<TypeOfFoo>(foo); |
| 415 | template <typename Impl> |
| 416 | inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) { |
| 417 | return PolymorphicAction<Impl>(impl); |
| 418 | } |
| 419 | |
| 420 | namespace internal { |
| 421 | |
| 422 | // Allows an Action<F2> object to pose as an Action<F1>, as long as F2 |
| 423 | // and F1 are compatible. |
| 424 | template <typename F1, typename F2> |
| 425 | class ActionAdaptor : public ActionInterface<F1> { |
| 426 | public: |
| 427 | typedef typename internal::Function<F1>::Result Result; |
| 428 | typedef typename internal::Function<F1>::ArgumentTuple ArgumentTuple; |
| 429 | |
| 430 | explicit ActionAdaptor(const Action<F2>& from) : impl_(from.impl_) {} |
| 431 | |
| 432 | virtual Result Perform(const ArgumentTuple& args) { |
| 433 | return impl_->Perform(args); |
| 434 | } |
| 435 | private: |
| 436 | const internal::linked_ptr<ActionInterface<F2> > impl_; |
| 437 | }; |
| 438 | |
| 439 | // Implements the polymorphic Return(x) action, which can be used in |
| 440 | // any function that returns the type of x, regardless of the argument |
| 441 | // types. |
| 442 | template <typename R> |
| 443 | class ReturnAction { |
| 444 | public: |
| 445 | // Constructs a ReturnAction object from the value to be returned. |
| 446 | // 'value' is passed by value instead of by const reference in order |
| 447 | // to allow Return("string literal") to compile. |
| 448 | explicit ReturnAction(R value) : value_(value) {} |
| 449 | |
| 450 | // This template type conversion operator allows Return(x) to be |
| 451 | // used in ANY function that returns x's type. |
| 452 | template <typename F> |
| 453 | operator Action<F>() const { |
| 454 | // Assert statement belongs here because this is the best place to verify |
| 455 | // conditions on F. It produces the clearest error messages |
| 456 | // in most compilers. |
| 457 | // Impl really belongs in this scope as a local class but can't |
| 458 | // because MSVC produces duplicate symbols in different translation units |
| 459 | // in this case. Until MS fixes that bug we put Impl into the class scope |
| 460 | // and put the typedef both here (for use in assert statement) and |
| 461 | // in the Impl class. But both definitions must be the same. |
| 462 | typedef typename Function<F>::Result Result; |
| 463 | GMOCK_COMPILE_ASSERT(!internal::is_reference<Result>::value, |
| 464 | use_ReturnRef_instead_of_Return_to_return_a_reference); |
| 465 | return Action<F>(new Impl<F>(value_)); |
| 466 | } |
| 467 | private: |
| 468 | // Implements the Return(x) action for a particular function type F. |
| 469 | template <typename F> |
| 470 | class Impl : public ActionInterface<F> { |
| 471 | public: |
| 472 | typedef typename Function<F>::Result Result; |
| 473 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 474 | |
| 475 | explicit Impl(R value) : value_(value) {} |
| 476 | |
| 477 | virtual Result Perform(const ArgumentTuple&) { return value_; } |
| 478 | |
| 479 | private: |
| 480 | R value_; |
| 481 | }; |
| 482 | |
| 483 | R value_; |
| 484 | }; |
| 485 | |
| 486 | // Implements the ReturnNull() action. |
| 487 | class ReturnNullAction { |
| 488 | public: |
| 489 | // Allows ReturnNull() to be used in any pointer-returning function. |
| 490 | template <typename Result, typename ArgumentTuple> |
| 491 | static Result Perform(const ArgumentTuple&) { |
| 492 | GMOCK_COMPILE_ASSERT(internal::is_pointer<Result>::value, |
| 493 | ReturnNull_can_be_used_to_return_a_pointer_only); |
| 494 | return NULL; |
| 495 | } |
| 496 | }; |
| 497 | |
| 498 | // Implements the Return() action. |
| 499 | class ReturnVoidAction { |
| 500 | public: |
| 501 | // Allows Return() to be used in any void-returning function. |
| 502 | template <typename Result, typename ArgumentTuple> |
| 503 | static void Perform(const ArgumentTuple&) { |
| 504 | CompileAssertTypesEqual<void, Result>(); |
| 505 | } |
| 506 | }; |
| 507 | |
| 508 | // Implements the polymorphic ReturnRef(x) action, which can be used |
| 509 | // in any function that returns a reference to the type of x, |
| 510 | // regardless of the argument types. |
| 511 | template <typename T> |
| 512 | class ReturnRefAction { |
| 513 | public: |
| 514 | // Constructs a ReturnRefAction object from the reference to be returned. |
| 515 | explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT |
| 516 | |
| 517 | // This template type conversion operator allows ReturnRef(x) to be |
| 518 | // used in ANY function that returns a reference to x's type. |
| 519 | template <typename F> |
| 520 | operator Action<F>() const { |
| 521 | typedef typename Function<F>::Result Result; |
| 522 | // Asserts that the function return type is a reference. This |
| 523 | // catches the user error of using ReturnRef(x) when Return(x) |
| 524 | // should be used, and generates some helpful error message. |
| 525 | GMOCK_COMPILE_ASSERT(internal::is_reference<Result>::value, |
| 526 | use_Return_instead_of_ReturnRef_to_return_a_value); |
| 527 | return Action<F>(new Impl<F>(ref_)); |
| 528 | } |
| 529 | private: |
| 530 | // Implements the ReturnRef(x) action for a particular function type F. |
| 531 | template <typename F> |
| 532 | class Impl : public ActionInterface<F> { |
| 533 | public: |
| 534 | typedef typename Function<F>::Result Result; |
| 535 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 536 | |
| 537 | explicit Impl(T& ref) : ref_(ref) {} // NOLINT |
| 538 | |
| 539 | virtual Result Perform(const ArgumentTuple&) { |
| 540 | return ref_; |
| 541 | } |
| 542 | private: |
| 543 | T& ref_; |
| 544 | }; |
| 545 | |
| 546 | T& ref_; |
| 547 | }; |
| 548 | |
| 549 | // Implements the DoDefault() action for a particular function type F. |
| 550 | template <typename F> |
| 551 | class MonomorphicDoDefaultActionImpl : public ActionInterface<F> { |
| 552 | public: |
| 553 | typedef typename Function<F>::Result Result; |
| 554 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 555 | |
| 556 | MonomorphicDoDefaultActionImpl() : ActionInterface<F>(true) {} |
| 557 | |
| 558 | // For technical reasons, DoDefault() cannot be used inside a |
| 559 | // composite action (e.g. DoAll(...)). It can only be used at the |
| 560 | // top level in an EXPECT_CALL(). If this function is called, the |
| 561 | // user must be using DoDefault() inside a composite action, and we |
| 562 | // have to generate a run-time error. |
| 563 | virtual Result Perform(const ArgumentTuple&) { |
| 564 | Assert(false, __FILE__, __LINE__, |
| 565 | "You are using DoDefault() inside a composite action like " |
| 566 | "DoAll() or WithArgs(). This is not supported for technical " |
| 567 | "reasons. Please instead spell out the default action, or " |
| 568 | "assign the default action to an Action variable and use " |
| 569 | "the variable in various places."); |
| 570 | return internal::Invalid<Result>(); |
| 571 | // The above statement will never be reached, but is required in |
| 572 | // order for this function to compile. |
| 573 | } |
| 574 | }; |
| 575 | |
| 576 | // Implements the polymorphic DoDefault() action. |
| 577 | class DoDefaultAction { |
| 578 | public: |
| 579 | // This template type conversion operator allows DoDefault() to be |
| 580 | // used in any function. |
| 581 | template <typename F> |
| 582 | operator Action<F>() const { |
| 583 | return Action<F>(new MonomorphicDoDefaultActionImpl<F>); |
| 584 | } |
| 585 | }; |
| 586 | |
| 587 | // Implements the Assign action to set a given pointer referent to a |
| 588 | // particular value. |
| 589 | template <typename T1, typename T2> |
| 590 | class AssignAction { |
| 591 | public: |
| 592 | AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {} |
| 593 | |
| 594 | template <typename Result, typename ArgumentTuple> |
| 595 | void Perform(const ArgumentTuple &args) const { |
| 596 | *ptr_ = value_; |
| 597 | } |
| 598 | private: |
| 599 | T1* const ptr_; |
| 600 | const T2 value_; |
| 601 | }; |
| 602 | |
| 603 | // Implements the SetErrnoAndReturn action to simulate return from |
| 604 | // various system calls and libc functions. |
| 605 | template <typename T> |
| 606 | class SetErrnoAndReturnAction { |
| 607 | public: |
| 608 | SetErrnoAndReturnAction(int errno_value, T result) |
| 609 | : errno_(errno_value), |
| 610 | result_(result) {} |
| 611 | template <typename Result, typename ArgumentTuple> |
| 612 | Result Perform(const ArgumentTuple &args) const { |
| 613 | errno = errno_; |
| 614 | return result_; |
| 615 | } |
| 616 | private: |
| 617 | const int errno_; |
| 618 | const T result_; |
| 619 | }; |
| 620 | |
| 621 | // Implements the SetArgumentPointee<N>(x) action for any function |
| 622 | // whose N-th argument (0-based) is a pointer to x's type. The |
| 623 | // template parameter kIsProto is true iff type A is ProtocolMessage, |
| 624 | // proto2::Message, or a sub-class of those. |
| 625 | template <size_t N, typename A, bool kIsProto> |
| 626 | class SetArgumentPointeeAction { |
| 627 | public: |
| 628 | // Constructs an action that sets the variable pointed to by the |
| 629 | // N-th function argument to 'value'. |
| 630 | explicit SetArgumentPointeeAction(const A& value) : value_(value) {} |
| 631 | |
| 632 | template <typename Result, typename ArgumentTuple> |
| 633 | void Perform(const ArgumentTuple& args) const { |
| 634 | CompileAssertTypesEqual<void, Result>(); |
| 635 | *::std::tr1::get<N>(args) = value_; |
| 636 | } |
| 637 | |
| 638 | private: |
| 639 | const A value_; |
| 640 | }; |
| 641 | |
| 642 | template <size_t N, typename Proto> |
| 643 | class SetArgumentPointeeAction<N, Proto, true> { |
| 644 | public: |
| 645 | // Constructs an action that sets the variable pointed to by the |
| 646 | // N-th function argument to 'proto'. Both ProtocolMessage and |
| 647 | // proto2::Message have the CopyFrom() method, so the same |
| 648 | // implementation works for both. |
| 649 | explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) { |
| 650 | proto_->CopyFrom(proto); |
| 651 | } |
| 652 | |
| 653 | template <typename Result, typename ArgumentTuple> |
| 654 | void Perform(const ArgumentTuple& args) const { |
| 655 | CompileAssertTypesEqual<void, Result>(); |
| 656 | ::std::tr1::get<N>(args)->CopyFrom(*proto_); |
| 657 | } |
| 658 | private: |
| 659 | const internal::linked_ptr<Proto> proto_; |
| 660 | }; |
| 661 | |
| 662 | // Implements the SetArrayArgument<N>(first, last) action for any function |
| 663 | // whose N-th argument (0-based) is a pointer or iterator to a type that can be |
| 664 | // implicitly converted from *first. |
| 665 | template <size_t N, typename InputIterator> |
| 666 | class SetArrayArgumentAction { |
| 667 | public: |
| 668 | // Constructs an action that sets the variable pointed to by the |
| 669 | // N-th function argument to 'value'. |
| 670 | explicit SetArrayArgumentAction(InputIterator first, InputIterator last) |
| 671 | : first_(first), last_(last) { |
| 672 | } |
| 673 | |
| 674 | template <typename Result, typename ArgumentTuple> |
| 675 | void Perform(const ArgumentTuple& args) const { |
| 676 | CompileAssertTypesEqual<void, Result>(); |
| 677 | |
| 678 | // Microsoft compiler deprecates ::std::copy, so we want to suppress warning |
| 679 | // 4996 (Function call with parameters that may be unsafe) there. |
| 680 | #ifdef GTEST_OS_WINDOWS |
| 681 | #pragma warning(push) // Saves the current warning state. |
| 682 | #pragma warning(disable:4996) // Temporarily disables warning 4996. |
| 683 | #endif // GTEST_OS_WINDOWS |
| 684 | ::std::copy(first_, last_, ::std::tr1::get<N>(args)); |
| 685 | #ifdef GTEST_OS_WINDOWS |
| 686 | #pragma warning(pop) // Restores the warning state. |
| 687 | #endif // GTEST_OS_WINDOWS |
| 688 | } |
| 689 | |
| 690 | private: |
| 691 | const InputIterator first_; |
| 692 | const InputIterator last_; |
| 693 | }; |
| 694 | |
| 695 | // Implements the InvokeWithoutArgs(f) action. The template argument |
| 696 | // FunctionImpl is the implementation type of f, which can be either a |
| 697 | // function pointer or a functor. InvokeWithoutArgs(f) can be used as an |
| 698 | // Action<F> as long as f's type is compatible with F (i.e. f can be |
| 699 | // assigned to a tr1::function<F>). |
| 700 | template <typename FunctionImpl> |
| 701 | class InvokeWithoutArgsAction { |
| 702 | public: |
| 703 | // The c'tor makes a copy of function_impl (either a function |
| 704 | // pointer or a functor). |
| 705 | explicit InvokeWithoutArgsAction(FunctionImpl function_impl) |
| 706 | : function_impl_(function_impl) {} |
| 707 | |
| 708 | // Allows InvokeWithoutArgs(f) to be used as any action whose type is |
| 709 | // compatible with f. |
| 710 | template <typename Result, typename ArgumentTuple> |
| 711 | Result Perform(const ArgumentTuple&) { return function_impl_(); } |
| 712 | private: |
| 713 | FunctionImpl function_impl_; |
| 714 | }; |
| 715 | |
| 716 | // Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action. |
| 717 | template <class Class, typename MethodPtr> |
| 718 | class InvokeMethodWithoutArgsAction { |
| 719 | public: |
| 720 | InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr) |
| 721 | : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {} |
| 722 | |
| 723 | template <typename Result, typename ArgumentTuple> |
| 724 | Result Perform(const ArgumentTuple&) const { |
| 725 | return (obj_ptr_->*method_ptr_)(); |
| 726 | } |
| 727 | private: |
| 728 | Class* const obj_ptr_; |
| 729 | const MethodPtr method_ptr_; |
| 730 | }; |
| 731 | |
| 732 | // Implements the IgnoreResult(action) action. |
| 733 | template <typename A> |
| 734 | class IgnoreResultAction { |
| 735 | public: |
| 736 | explicit IgnoreResultAction(const A& action) : action_(action) {} |
| 737 | |
| 738 | template <typename F> |
| 739 | operator Action<F>() const { |
| 740 | // Assert statement belongs here because this is the best place to verify |
| 741 | // conditions on F. It produces the clearest error messages |
| 742 | // in most compilers. |
| 743 | // Impl really belongs in this scope as a local class but can't |
| 744 | // because MSVC produces duplicate symbols in different translation units |
| 745 | // in this case. Until MS fixes that bug we put Impl into the class scope |
| 746 | // and put the typedef both here (for use in assert statement) and |
| 747 | // in the Impl class. But both definitions must be the same. |
| 748 | typedef typename internal::Function<F>::Result Result; |
| 749 | |
| 750 | // Asserts at compile time that F returns void. |
| 751 | CompileAssertTypesEqual<void, Result>(); |
| 752 | |
| 753 | return Action<F>(new Impl<F>(action_)); |
| 754 | } |
| 755 | private: |
| 756 | template <typename F> |
| 757 | class Impl : public ActionInterface<F> { |
| 758 | public: |
| 759 | typedef typename internal::Function<F>::Result Result; |
| 760 | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 761 | |
| 762 | explicit Impl(const A& action) : action_(action) {} |
| 763 | |
| 764 | virtual void Perform(const ArgumentTuple& args) { |
| 765 | // Performs the action and ignores its result. |
| 766 | action_.Perform(args); |
| 767 | } |
| 768 | |
| 769 | private: |
| 770 | // Type OriginalFunction is the same as F except that its return |
| 771 | // type is IgnoredValue. |
| 772 | typedef typename internal::Function<F>::MakeResultIgnoredValue |
| 773 | OriginalFunction; |
| 774 | |
| 775 | const Action<OriginalFunction> action_; |
| 776 | }; |
| 777 | |
| 778 | const A action_; |
| 779 | }; |
| 780 | |
| 781 | } // namespace internal |
| 782 | |
| 783 | // An Unused object can be implicitly constructed from ANY value. |
| 784 | // This is handy when defining actions that ignore some or all of the |
| 785 | // mock function arguments. For example, given |
| 786 | // |
| 787 | // MOCK_METHOD3(Foo, double(const string& label, double x, double y)); |
| 788 | // MOCK_METHOD3(Bar, double(int index, double x, double y)); |
| 789 | // |
| 790 | // instead of |
| 791 | // |
| 792 | // double DistanceToOriginWithLabel(const string& label, double x, double y) { |
| 793 | // return sqrt(x*x + y*y); |
| 794 | // } |
| 795 | // double DistanceToOriginWithIndex(int index, double x, double y) { |
| 796 | // return sqrt(x*x + y*y); |
| 797 | // } |
| 798 | // ... |
| 799 | // EXEPCT_CALL(mock, Foo("abc", _, _)) |
| 800 | // .WillOnce(Invoke(DistanceToOriginWithLabel)); |
| 801 | // EXEPCT_CALL(mock, Bar(5, _, _)) |
| 802 | // .WillOnce(Invoke(DistanceToOriginWithIndex)); |
| 803 | // |
| 804 | // you could write |
| 805 | // |
| 806 | // // We can declare any uninteresting argument as Unused. |
| 807 | // double DistanceToOrigin(Unused, double x, double y) { |
| 808 | // return sqrt(x*x + y*y); |
| 809 | // } |
| 810 | // ... |
| 811 | // EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin)); |
| 812 | // EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin)); |
| 813 | typedef internal::IgnoredValue Unused; |
| 814 | |
| 815 | // This constructor allows us to turn an Action<From> object into an |
| 816 | // Action<To>, as long as To's arguments can be implicitly converted |
| 817 | // to From's and From's return type cann be implicitly converted to |
| 818 | // To's. |
| 819 | template <typename To> |
| 820 | template <typename From> |
| 821 | Action<To>::Action(const Action<From>& from) |
| 822 | : impl_(new internal::ActionAdaptor<To, From>(from)) {} |
| 823 | |
| 824 | // Creates an action that returns 'value'. 'value' is passed by value |
| 825 | // instead of const reference - otherwise Return("string literal") |
| 826 | // will trigger a compiler error about using array as initializer. |
| 827 | template <typename R> |
| 828 | internal::ReturnAction<R> Return(R value) { |
| 829 | return internal::ReturnAction<R>(value); |
| 830 | } |
| 831 | |
| 832 | // Creates an action that returns NULL. |
| 833 | inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() { |
| 834 | return MakePolymorphicAction(internal::ReturnNullAction()); |
| 835 | } |
| 836 | |
| 837 | // Creates an action that returns from a void function. |
| 838 | inline PolymorphicAction<internal::ReturnVoidAction> Return() { |
| 839 | return MakePolymorphicAction(internal::ReturnVoidAction()); |
| 840 | } |
| 841 | |
| 842 | // Creates an action that returns the reference to a variable. |
| 843 | template <typename R> |
| 844 | inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT |
| 845 | return internal::ReturnRefAction<R>(x); |
| 846 | } |
| 847 | |
| 848 | // Creates an action that does the default action for the give mock function. |
| 849 | inline internal::DoDefaultAction DoDefault() { |
| 850 | return internal::DoDefaultAction(); |
| 851 | } |
| 852 | |
| 853 | // Creates an action that sets the variable pointed by the N-th |
| 854 | // (0-based) function argument to 'value'. |
| 855 | template <size_t N, typename T> |
| 856 | PolymorphicAction< |
| 857 | internal::SetArgumentPointeeAction< |
| 858 | N, T, internal::IsAProtocolMessage<T>::value> > |
| 859 | SetArgumentPointee(const T& x) { |
| 860 | return MakePolymorphicAction(internal::SetArgumentPointeeAction< |
| 861 | N, T, internal::IsAProtocolMessage<T>::value>(x)); |
| 862 | } |
| 863 | |
| 864 | // Creates an action that sets the elements of the array pointed to by the N-th |
| 865 | // (0-based) function argument, which can be either a pointer or an iterator, |
| 866 | // to the values of the elements in the source range [first, last). |
| 867 | template <size_t N, typename InputIterator> |
| 868 | PolymorphicAction<internal::SetArrayArgumentAction<N, InputIterator> > |
| 869 | SetArrayArgument(InputIterator first, InputIterator last) { |
| 870 | return MakePolymorphicAction(internal::SetArrayArgumentAction< |
| 871 | N, InputIterator>(first, last)); |
| 872 | } |
| 873 | |
| 874 | // Creates an action that sets a pointer referent to a given value. |
| 875 | template <typename T1, typename T2> |
| 876 | PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) { |
| 877 | return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val)); |
| 878 | } |
| 879 | |
| 880 | // Creates an action that sets errno and returns the appropriate error. |
| 881 | template <typename T> |
| 882 | PolymorphicAction<internal::SetErrnoAndReturnAction<T> > |
| 883 | SetErrnoAndReturn(int errval, T result) { |
| 884 | return MakePolymorphicAction( |
| 885 | internal::SetErrnoAndReturnAction<T>(errval, result)); |
| 886 | } |
| 887 | |
| 888 | // Various overloads for InvokeWithoutArgs(). |
| 889 | |
| 890 | // Creates an action that invokes 'function_impl' with no argument. |
| 891 | template <typename FunctionImpl> |
| 892 | PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> > |
| 893 | InvokeWithoutArgs(FunctionImpl function_impl) { |
| 894 | return MakePolymorphicAction( |
| 895 | internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl)); |
| 896 | } |
| 897 | |
| 898 | // Creates an action that invokes the given method on the given object |
| 899 | // with no argument. |
| 900 | template <class Class, typename MethodPtr> |
| 901 | PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> > |
| 902 | InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) { |
| 903 | return MakePolymorphicAction( |
| 904 | internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>( |
| 905 | obj_ptr, method_ptr)); |
| 906 | } |
| 907 | |
| 908 | // Creates an action that performs an_action and throws away its |
| 909 | // result. In other words, it changes the return type of an_action to |
| 910 | // void. an_action MUST NOT return void, or the code won't compile. |
| 911 | template <typename A> |
| 912 | inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) { |
| 913 | return internal::IgnoreResultAction<A>(an_action); |
| 914 | } |
| 915 | |
| 916 | } // namespace testing |
| 917 | |
| 918 | #endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ |