/home/arjun/llvm-project/llvm/utils/unittest/googlemock/include/gmock/gmock-actions.h
Line | Count | Source (jump to first uncovered line) |
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 | | // IWYU pragma: private, include "gmock/gmock.h" |
37 | | |
38 | | #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ |
39 | | #define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ |
40 | | |
41 | | #ifndef _WIN32_WCE |
42 | | # include <errno.h> |
43 | | #endif |
44 | | |
45 | | #include <algorithm> |
46 | | #include <string> |
47 | | |
48 | | #include "gmock/internal/gmock-internal-utils.h" |
49 | | #include "gmock/internal/gmock-port.h" |
50 | | |
51 | | #if GTEST_HAS_STD_TYPE_TRAITS_ // Defined by gtest-port.h via gmock-port.h. |
52 | | #include <type_traits> |
53 | | #endif |
54 | | |
55 | | namespace testing { |
56 | | |
57 | | // To implement an action Foo, define: |
58 | | // 1. a class FooAction that implements the ActionInterface interface, and |
59 | | // 2. a factory function that creates an Action object from a |
60 | | // const FooAction*. |
61 | | // |
62 | | // The two-level delegation design follows that of Matcher, providing |
63 | | // consistency for extension developers. It also eases ownership |
64 | | // management as Action objects can now be copied like plain values. |
65 | | |
66 | | namespace internal { |
67 | | |
68 | | template <typename F1, typename F2> |
69 | | class ActionAdaptor; |
70 | | |
71 | | // BuiltInDefaultValueGetter<T, true>::Get() returns a |
72 | | // default-constructed T value. BuiltInDefaultValueGetter<T, |
73 | | // false>::Get() crashes with an error. |
74 | | // |
75 | | // This primary template is used when kDefaultConstructible is true. |
76 | | template <typename T, bool kDefaultConstructible> |
77 | | struct BuiltInDefaultValueGetter { |
78 | | static T Get() { return T(); } |
79 | | }; |
80 | | template <typename T> |
81 | | struct BuiltInDefaultValueGetter<T, false> { |
82 | | static T Get() { |
83 | | Assert(false, __FILE__, __LINE__, |
84 | | "Default action undefined for the function return type."); |
85 | | return internal::Invalid<T>(); |
86 | | // The above statement will never be reached, but is required in |
87 | | // order for this function to compile. |
88 | | } |
89 | | }; |
90 | | |
91 | | // BuiltInDefaultValue<T>::Get() returns the "built-in" default value |
92 | | // for type T, which is NULL when T is a raw pointer type, 0 when T is |
93 | | // a numeric type, false when T is bool, or "" when T is string or |
94 | | // std::string. In addition, in C++11 and above, it turns a |
95 | | // default-constructed T value if T is default constructible. For any |
96 | | // other type T, the built-in default T value is undefined, and the |
97 | | // function will abort the process. |
98 | | template <typename T> |
99 | | class BuiltInDefaultValue { |
100 | | public: |
101 | | #if GTEST_HAS_STD_TYPE_TRAITS_ |
102 | | // This function returns true iff type T has a built-in default value. |
103 | | static bool Exists() { |
104 | | return ::std::is_default_constructible<T>::value; |
105 | | } |
106 | | |
107 | | static T Get() { |
108 | | return BuiltInDefaultValueGetter< |
109 | | T, ::std::is_default_constructible<T>::value>::Get(); |
110 | | } |
111 | | |
112 | | #else // GTEST_HAS_STD_TYPE_TRAITS_ |
113 | | // This function returns true iff type T has a built-in default value. |
114 | | static bool Exists() { |
115 | | return false; |
116 | | } |
117 | | |
118 | | static T Get() { |
119 | | return BuiltInDefaultValueGetter<T, false>::Get(); |
120 | | } |
121 | | |
122 | | #endif // GTEST_HAS_STD_TYPE_TRAITS_ |
123 | | }; |
124 | | |
125 | | // This partial specialization says that we use the same built-in |
126 | | // default value for T and const T. |
127 | | template <typename T> |
128 | | class BuiltInDefaultValue<const T> { |
129 | | public: |
130 | | static bool Exists() { return BuiltInDefaultValue<T>::Exists(); } |
131 | | static T Get() { return BuiltInDefaultValue<T>::Get(); } |
132 | | }; |
133 | | |
134 | | // This partial specialization defines the default values for pointer |
135 | | // types. |
136 | | template <typename T> |
137 | | class BuiltInDefaultValue<T*> { |
138 | | public: |
139 | | static bool Exists() { return true; } |
140 | | static T* Get() { return NULL; } |
141 | | }; |
142 | | |
143 | | // The following specializations define the default values for |
144 | | // specific types we care about. |
145 | | #define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \ |
146 | | template <> \ |
147 | | class BuiltInDefaultValue<type> { \ |
148 | | public: \ |
149 | 0 | static bool Exists() { return true; } \ Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIvE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIbE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIhE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIaE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIcE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIwE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueItE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIsE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIjE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIiE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueImE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIlE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIyE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIxE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIfE6ExistsEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIdE6ExistsEv |
150 | 0 | static type Get() { return value; } \ Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIvE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIbE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIhE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIaE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIcE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIwE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueItE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIsE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIjE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIiE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueImE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIlE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIyE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIxE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIfE3GetEv Unexecuted instantiation: _ZN7testing8internal19BuiltInDefaultValueIdE3GetEv |
151 | | } |
152 | | |
153 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, ); // NOLINT |
154 | | #if GTEST_HAS_GLOBAL_STRING |
155 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::string, ""); |
156 | | #endif // GTEST_HAS_GLOBAL_STRING |
157 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, ""); |
158 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false); |
159 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0'); |
160 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0'); |
161 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0'); |
162 | | |
163 | | // There's no need for a default action for signed wchar_t, as that |
164 | | // type is the same as wchar_t for gcc, and invalid for MSVC. |
165 | | // |
166 | | // There's also no need for a default action for unsigned wchar_t, as |
167 | | // that type is the same as unsigned int for gcc, and invalid for |
168 | | // MSVC. |
169 | | #if GMOCK_WCHAR_T_IS_NATIVE_ |
170 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); // NOLINT |
171 | | #endif |
172 | | |
173 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U); // NOLINT |
174 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0); // NOLINT |
175 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U); |
176 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0); |
177 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL); // NOLINT |
178 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L); // NOLINT |
179 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(UInt64, 0); |
180 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(Int64, 0); |
181 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0); |
182 | | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0); |
183 | | |
184 | | #undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_ |
185 | | |
186 | | } // namespace internal |
187 | | |
188 | | // When an unexpected function call is encountered, Google Mock will |
189 | | // let it return a default value if the user has specified one for its |
190 | | // return type, or if the return type has a built-in default value; |
191 | | // otherwise Google Mock won't know what value to return and will have |
192 | | // to abort the process. |
193 | | // |
194 | | // The DefaultValue<T> class allows a user to specify the |
195 | | // default value for a type T that is both copyable and publicly |
196 | | // destructible (i.e. anything that can be used as a function return |
197 | | // type). The usage is: |
198 | | // |
199 | | // // Sets the default value for type T to be foo. |
200 | | // DefaultValue<T>::Set(foo); |
201 | | template <typename T> |
202 | | class DefaultValue { |
203 | | public: |
204 | | // Sets the default value for type T; requires T to be |
205 | | // copy-constructable and have a public destructor. |
206 | | static void Set(T x) { |
207 | | delete producer_; |
208 | | producer_ = new FixedValueProducer(x); |
209 | | } |
210 | | |
211 | | // Provides a factory function to be called to generate the default value. |
212 | | // This method can be used even if T is only move-constructible, but it is not |
213 | | // limited to that case. |
214 | | typedef T (*FactoryFunction)(); |
215 | | static void SetFactory(FactoryFunction factory) { |
216 | | delete producer_; |
217 | | producer_ = new FactoryValueProducer(factory); |
218 | | } |
219 | | |
220 | | // Unsets the default value for type T. |
221 | | static void Clear() { |
222 | | delete producer_; |
223 | | producer_ = NULL; |
224 | | } |
225 | | |
226 | | // Returns true iff the user has set the default value for type T. |
227 | | static bool IsSet() { return producer_ != NULL; } |
228 | | |
229 | | // Returns true if T has a default return value set by the user or there |
230 | | // exists a built-in default value. |
231 | | static bool Exists() { |
232 | | return IsSet() || internal::BuiltInDefaultValue<T>::Exists(); |
233 | | } |
234 | | |
235 | | // Returns the default value for type T if the user has set one; |
236 | | // otherwise returns the built-in default value. Requires that Exists() |
237 | | // is true, which ensures that the return value is well-defined. |
238 | | static T Get() { |
239 | | return producer_ == NULL ? |
240 | | internal::BuiltInDefaultValue<T>::Get() : producer_->Produce(); |
241 | | } |
242 | | |
243 | | private: |
244 | | class ValueProducer { |
245 | | public: |
246 | | virtual ~ValueProducer() {} |
247 | | virtual T Produce() = 0; |
248 | | }; |
249 | | |
250 | | class FixedValueProducer : public ValueProducer { |
251 | | public: |
252 | | explicit FixedValueProducer(T value) : value_(value) {} |
253 | | virtual T Produce() { return value_; } |
254 | | |
255 | | private: |
256 | | const T value_; |
257 | | GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer); |
258 | | }; |
259 | | |
260 | | class FactoryValueProducer : public ValueProducer { |
261 | | public: |
262 | | explicit FactoryValueProducer(FactoryFunction factory) |
263 | | : factory_(factory) {} |
264 | | virtual T Produce() { return factory_(); } |
265 | | |
266 | | private: |
267 | | const FactoryFunction factory_; |
268 | | GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer); |
269 | | }; |
270 | | |
271 | | static ValueProducer* producer_; |
272 | | }; |
273 | | |
274 | | // This partial specialization allows a user to set default values for |
275 | | // reference types. |
276 | | template <typename T> |
277 | | class DefaultValue<T&> { |
278 | | public: |
279 | | // Sets the default value for type T&. |
280 | | static void Set(T& x) { // NOLINT |
281 | | address_ = &x; |
282 | | } |
283 | | |
284 | | // Unsets the default value for type T&. |
285 | | static void Clear() { |
286 | | address_ = NULL; |
287 | | } |
288 | | |
289 | | // Returns true iff the user has set the default value for type T&. |
290 | | static bool IsSet() { return address_ != NULL; } |
291 | | |
292 | | // Returns true if T has a default return value set by the user or there |
293 | | // exists a built-in default value. |
294 | | static bool Exists() { |
295 | | return IsSet() || internal::BuiltInDefaultValue<T&>::Exists(); |
296 | | } |
297 | | |
298 | | // Returns the default value for type T& if the user has set one; |
299 | | // otherwise returns the built-in default value if there is one; |
300 | | // otherwise aborts the process. |
301 | | static T& Get() { |
302 | | return address_ == NULL ? |
303 | | internal::BuiltInDefaultValue<T&>::Get() : *address_; |
304 | | } |
305 | | |
306 | | private: |
307 | | static T* address_; |
308 | | }; |
309 | | |
310 | | // This specialization allows DefaultValue<void>::Get() to |
311 | | // compile. |
312 | | template <> |
313 | | class DefaultValue<void> { |
314 | | public: |
315 | 0 | static bool Exists() { return true; } |
316 | 0 | static void Get() {} |
317 | | }; |
318 | | |
319 | | // Points to the user-set default value for type T. |
320 | | template <typename T> |
321 | | typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = NULL; |
322 | | |
323 | | // Points to the user-set default value for type T&. |
324 | | template <typename T> |
325 | | T* DefaultValue<T&>::address_ = NULL; |
326 | | |
327 | | // Implement this interface to define an action for function type F. |
328 | | template <typename F> |
329 | | class ActionInterface { |
330 | | public: |
331 | | typedef typename internal::Function<F>::Result Result; |
332 | | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
333 | | |
334 | | ActionInterface() {} |
335 | | virtual ~ActionInterface() {} |
336 | | |
337 | | // Performs the action. This method is not const, as in general an |
338 | | // action can have side effects and be stateful. For example, a |
339 | | // get-the-next-element-from-the-collection action will need to |
340 | | // remember the current element. |
341 | | virtual Result Perform(const ArgumentTuple& args) = 0; |
342 | | |
343 | | private: |
344 | | GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface); |
345 | | }; |
346 | | |
347 | | // An Action<F> is a copyable and IMMUTABLE (except by assignment) |
348 | | // object that represents an action to be taken when a mock function |
349 | | // of type F is called. The implementation of Action<T> is just a |
350 | | // linked_ptr to const ActionInterface<T>, so copying is fairly cheap. |
351 | | // Don't inherit from Action! |
352 | | // |
353 | | // You can view an object implementing ActionInterface<F> as a |
354 | | // concrete action (including its current state), and an Action<F> |
355 | | // object as a handle to it. |
356 | | template <typename F> |
357 | | class Action { |
358 | | public: |
359 | | typedef typename internal::Function<F>::Result Result; |
360 | | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
361 | | |
362 | | // Constructs a null Action. Needed for storing Action objects in |
363 | | // STL containers. |
364 | | Action() : impl_(NULL) {} |
365 | | |
366 | | // Constructs an Action from its implementation. A NULL impl is |
367 | | // used to represent the "do-default" action. |
368 | | explicit Action(ActionInterface<F>* impl) : impl_(impl) {} |
369 | | |
370 | | // Copy constructor. |
371 | | Action(const Action &action) = default; |
372 | | Action &operator=(const Action &action) = default; |
373 | | |
374 | | // This constructor allows us to turn an Action<Func> object into an |
375 | | // Action<F>, as long as F's arguments can be implicitly converted |
376 | | // to Func's and Func's return type can be implicitly converted to |
377 | | // F's. |
378 | | template <typename Func> |
379 | | explicit Action(const Action<Func>& action); |
380 | | |
381 | | // Returns true iff this is the DoDefault() action. |
382 | | bool IsDoDefault() const { return impl_.get() == NULL; } |
383 | | |
384 | | // Performs the action. Note that this method is const even though |
385 | | // the corresponding method in ActionInterface is not. The reason |
386 | | // is that a const Action<F> means that it cannot be re-bound to |
387 | | // another concrete action, not that the concrete action it binds to |
388 | | // cannot change state. (Think of the difference between a const |
389 | | // pointer and a pointer to const.) |
390 | | Result Perform(const ArgumentTuple& args) const { |
391 | | internal::Assert( |
392 | | !IsDoDefault(), __FILE__, __LINE__, |
393 | | "You are using DoDefault() inside a composite action like " |
394 | | "DoAll() or WithArgs(). This is not supported for technical " |
395 | | "reasons. Please instead spell out the default action, or " |
396 | | "assign the default action to an Action variable and use " |
397 | | "the variable in various places."); |
398 | | return impl_->Perform(args); |
399 | | } |
400 | | |
401 | | private: |
402 | | template <typename F1, typename F2> |
403 | | friend class internal::ActionAdaptor; |
404 | | |
405 | | internal::linked_ptr<ActionInterface<F> > impl_; |
406 | | }; |
407 | | |
408 | | // The PolymorphicAction class template makes it easy to implement a |
409 | | // polymorphic action (i.e. an action that can be used in mock |
410 | | // functions of than one type, e.g. Return()). |
411 | | // |
412 | | // To define a polymorphic action, a user first provides a COPYABLE |
413 | | // implementation class that has a Perform() method template: |
414 | | // |
415 | | // class FooAction { |
416 | | // public: |
417 | | // template <typename Result, typename ArgumentTuple> |
418 | | // Result Perform(const ArgumentTuple& args) const { |
419 | | // // Processes the arguments and returns a result, using |
420 | | // // tr1::get<N>(args) to get the N-th (0-based) argument in the tuple. |
421 | | // } |
422 | | // ... |
423 | | // }; |
424 | | // |
425 | | // Then the user creates the polymorphic action using |
426 | | // MakePolymorphicAction(object) where object has type FooAction. See |
427 | | // the definition of Return(void) and SetArgumentPointee<N>(value) for |
428 | | // complete examples. |
429 | | template <typename Impl> |
430 | | class PolymorphicAction { |
431 | | public: |
432 | | explicit PolymorphicAction(const Impl& impl) : impl_(impl) {} |
433 | | |
434 | | template <typename F> |
435 | | operator Action<F>() const { |
436 | | return Action<F>(new MonomorphicImpl<F>(impl_)); |
437 | | } |
438 | | |
439 | | private: |
440 | | template <typename F> |
441 | | class MonomorphicImpl : public ActionInterface<F> { |
442 | | public: |
443 | | typedef typename internal::Function<F>::Result Result; |
444 | | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
445 | | |
446 | | explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} |
447 | | |
448 | | virtual Result Perform(const ArgumentTuple& args) { |
449 | | return impl_.template Perform<Result>(args); |
450 | | } |
451 | | |
452 | | private: |
453 | | Impl impl_; |
454 | | |
455 | | GTEST_DISALLOW_ASSIGN_(MonomorphicImpl); |
456 | | }; |
457 | | |
458 | | Impl impl_; |
459 | | |
460 | | GTEST_DISALLOW_ASSIGN_(PolymorphicAction); |
461 | | }; |
462 | | |
463 | | // Creates an Action from its implementation and returns it. The |
464 | | // created Action object owns the implementation. |
465 | | template <typename F> |
466 | | Action<F> MakeAction(ActionInterface<F>* impl) { |
467 | | return Action<F>(impl); |
468 | | } |
469 | | |
470 | | // Creates a polymorphic action from its implementation. This is |
471 | | // easier to use than the PolymorphicAction<Impl> constructor as it |
472 | | // doesn't require you to explicitly write the template argument, e.g. |
473 | | // |
474 | | // MakePolymorphicAction(foo); |
475 | | // vs |
476 | | // PolymorphicAction<TypeOfFoo>(foo); |
477 | | template <typename Impl> |
478 | 0 | inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) { |
479 | 0 | return PolymorphicAction<Impl>(impl); |
480 | 0 | } Unexecuted instantiation: _ZN7testing21MakePolymorphicActionINS_8internal16ReturnNullActionEEENS_17PolymorphicActionIT_EERKS4_ Unexecuted instantiation: _ZN7testing21MakePolymorphicActionINS_8internal16ReturnVoidActionEEENS_17PolymorphicActionIT_EERKS4_ |
481 | | |
482 | | namespace internal { |
483 | | |
484 | | // Allows an Action<F2> object to pose as an Action<F1>, as long as F2 |
485 | | // and F1 are compatible. |
486 | | template <typename F1, typename F2> |
487 | | class ActionAdaptor : public ActionInterface<F1> { |
488 | | public: |
489 | | typedef typename internal::Function<F1>::Result Result; |
490 | | typedef typename internal::Function<F1>::ArgumentTuple ArgumentTuple; |
491 | | |
492 | | explicit ActionAdaptor(const Action<F2>& from) : impl_(from.impl_) {} |
493 | | |
494 | | virtual Result Perform(const ArgumentTuple& args) { |
495 | | return impl_->Perform(args); |
496 | | } |
497 | | |
498 | | private: |
499 | | const internal::linked_ptr<ActionInterface<F2> > impl_; |
500 | | |
501 | | GTEST_DISALLOW_ASSIGN_(ActionAdaptor); |
502 | | }; |
503 | | |
504 | | // Helper struct to specialize ReturnAction to execute a move instead of a copy |
505 | | // on return. Useful for move-only types, but could be used on any type. |
506 | | template <typename T> |
507 | | struct ByMoveWrapper { |
508 | | explicit ByMoveWrapper(T value) : payload(internal::move(value)) {} |
509 | | T payload; |
510 | | }; |
511 | | |
512 | | // Implements the polymorphic Return(x) action, which can be used in |
513 | | // any function that returns the type of x, regardless of the argument |
514 | | // types. |
515 | | // |
516 | | // Note: The value passed into Return must be converted into |
517 | | // Function<F>::Result when this action is cast to Action<F> rather than |
518 | | // when that action is performed. This is important in scenarios like |
519 | | // |
520 | | // MOCK_METHOD1(Method, T(U)); |
521 | | // ... |
522 | | // { |
523 | | // Foo foo; |
524 | | // X x(&foo); |
525 | | // EXPECT_CALL(mock, Method(_)).WillOnce(Return(x)); |
526 | | // } |
527 | | // |
528 | | // In the example above the variable x holds reference to foo which leaves |
529 | | // scope and gets destroyed. If copying X just copies a reference to foo, |
530 | | // that copy will be left with a hanging reference. If conversion to T |
531 | | // makes a copy of foo, the above code is safe. To support that scenario, we |
532 | | // need to make sure that the type conversion happens inside the EXPECT_CALL |
533 | | // statement, and conversion of the result of Return to Action<T(U)> is a |
534 | | // good place for that. |
535 | | // |
536 | | template <typename R> |
537 | | class ReturnAction { |
538 | | public: |
539 | | // Constructs a ReturnAction object from the value to be returned. |
540 | | // 'value' is passed by value instead of by const reference in order |
541 | | // to allow Return("string literal") to compile. |
542 | | explicit ReturnAction(R value) : value_(new R(internal::move(value))) {} |
543 | | |
544 | | // This template type conversion operator allows Return(x) to be |
545 | | // used in ANY function that returns x's type. |
546 | | template <typename F> |
547 | | operator Action<F>() const { |
548 | | // Assert statement belongs here because this is the best place to verify |
549 | | // conditions on F. It produces the clearest error messages |
550 | | // in most compilers. |
551 | | // Impl really belongs in this scope as a local class but can't |
552 | | // because MSVC produces duplicate symbols in different translation units |
553 | | // in this case. Until MS fixes that bug we put Impl into the class scope |
554 | | // and put the typedef both here (for use in assert statement) and |
555 | | // in the Impl class. But both definitions must be the same. |
556 | | typedef typename Function<F>::Result Result; |
557 | | GTEST_COMPILE_ASSERT_( |
558 | | !is_reference<Result>::value, |
559 | | use_ReturnRef_instead_of_Return_to_return_a_reference); |
560 | | return Action<F>(new Impl<R, F>(value_)); |
561 | | } |
562 | | |
563 | | private: |
564 | | // Implements the Return(x) action for a particular function type F. |
565 | | template <typename R_, typename F> |
566 | | class Impl : public ActionInterface<F> { |
567 | | public: |
568 | | typedef typename Function<F>::Result Result; |
569 | | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
570 | | |
571 | | // The implicit cast is necessary when Result has more than one |
572 | | // single-argument constructor (e.g. Result is std::vector<int>) and R |
573 | | // has a type conversion operator template. In that case, value_(value) |
574 | | // won't compile as the compiler doesn't known which constructor of |
575 | | // Result to call. ImplicitCast_ forces the compiler to convert R to |
576 | | // Result without considering explicit constructors, thus resolving the |
577 | | // ambiguity. value_ is then initialized using its copy constructor. |
578 | | explicit Impl(const linked_ptr<R>& value) |
579 | | : value_before_cast_(*value), |
580 | | value_(ImplicitCast_<Result>(value_before_cast_)) {} |
581 | | |
582 | | virtual Result Perform(const ArgumentTuple&) { return value_; } |
583 | | |
584 | | private: |
585 | | GTEST_COMPILE_ASSERT_(!is_reference<Result>::value, |
586 | | Result_cannot_be_a_reference_type); |
587 | | // We save the value before casting just in case it is being cast to a |
588 | | // wrapper type. |
589 | | R value_before_cast_; |
590 | | Result value_; |
591 | | |
592 | | GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl); |
593 | | }; |
594 | | |
595 | | // Partially specialize for ByMoveWrapper. This version of ReturnAction will |
596 | | // move its contents instead. |
597 | | template <typename R_, typename F> |
598 | | class Impl<ByMoveWrapper<R_>, F> : public ActionInterface<F> { |
599 | | public: |
600 | | typedef typename Function<F>::Result Result; |
601 | | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
602 | | |
603 | | explicit Impl(const linked_ptr<R>& wrapper) |
604 | | : performed_(false), wrapper_(wrapper) {} |
605 | | |
606 | | virtual Result Perform(const ArgumentTuple&) { |
607 | | GTEST_CHECK_(!performed_) |
608 | | << "A ByMove() action should only be performed once."; |
609 | | performed_ = true; |
610 | | return internal::move(wrapper_->payload); |
611 | | } |
612 | | |
613 | | private: |
614 | | bool performed_; |
615 | | const linked_ptr<R> wrapper_; |
616 | | |
617 | | GTEST_DISALLOW_ASSIGN_(Impl); |
618 | | }; |
619 | | |
620 | | const linked_ptr<R> value_; |
621 | | |
622 | | GTEST_DISALLOW_ASSIGN_(ReturnAction); |
623 | | }; |
624 | | |
625 | | // Implements the ReturnNull() action. |
626 | | class ReturnNullAction { |
627 | | public: |
628 | | // Allows ReturnNull() to be used in any pointer-returning function. In C++11 |
629 | | // this is enforced by returning nullptr, and in non-C++11 by asserting a |
630 | | // pointer type on compile time. |
631 | | template <typename Result, typename ArgumentTuple> |
632 | | static Result Perform(const ArgumentTuple&) { |
633 | | #if GTEST_LANG_CXX11 |
634 | | return nullptr; |
635 | | #else |
636 | | GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value, |
637 | | ReturnNull_can_be_used_to_return_a_pointer_only); |
638 | | return NULL; |
639 | | #endif // GTEST_LANG_CXX11 |
640 | | } |
641 | | }; |
642 | | |
643 | | // Implements the Return() action. |
644 | | class ReturnVoidAction { |
645 | | public: |
646 | | // Allows Return() to be used in any void-returning function. |
647 | | template <typename Result, typename ArgumentTuple> |
648 | | static void Perform(const ArgumentTuple&) { |
649 | | CompileAssertTypesEqual<void, Result>(); |
650 | | } |
651 | | }; |
652 | | |
653 | | // Implements the polymorphic ReturnRef(x) action, which can be used |
654 | | // in any function that returns a reference to the type of x, |
655 | | // regardless of the argument types. |
656 | | template <typename T> |
657 | | class ReturnRefAction { |
658 | | public: |
659 | | // Constructs a ReturnRefAction object from the reference to be returned. |
660 | | explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT |
661 | | |
662 | | // This template type conversion operator allows ReturnRef(x) to be |
663 | | // used in ANY function that returns a reference to x's type. |
664 | | template <typename F> |
665 | | operator Action<F>() const { |
666 | | typedef typename Function<F>::Result Result; |
667 | | // Asserts that the function return type is a reference. This |
668 | | // catches the user error of using ReturnRef(x) when Return(x) |
669 | | // should be used, and generates some helpful error message. |
670 | | GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value, |
671 | | use_Return_instead_of_ReturnRef_to_return_a_value); |
672 | | return Action<F>(new Impl<F>(ref_)); |
673 | | } |
674 | | |
675 | | private: |
676 | | // Implements the ReturnRef(x) action for a particular function type F. |
677 | | template <typename F> |
678 | | class Impl : public ActionInterface<F> { |
679 | | public: |
680 | | typedef typename Function<F>::Result Result; |
681 | | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
682 | | |
683 | | explicit Impl(T& ref) : ref_(ref) {} // NOLINT |
684 | | |
685 | | virtual Result Perform(const ArgumentTuple&) { |
686 | | return ref_; |
687 | | } |
688 | | |
689 | | private: |
690 | | T& ref_; |
691 | | |
692 | | GTEST_DISALLOW_ASSIGN_(Impl); |
693 | | }; |
694 | | |
695 | | T& ref_; |
696 | | |
697 | | GTEST_DISALLOW_ASSIGN_(ReturnRefAction); |
698 | | }; |
699 | | |
700 | | // Implements the polymorphic ReturnRefOfCopy(x) action, which can be |
701 | | // used in any function that returns a reference to the type of x, |
702 | | // regardless of the argument types. |
703 | | template <typename T> |
704 | | class ReturnRefOfCopyAction { |
705 | | public: |
706 | | // Constructs a ReturnRefOfCopyAction object from the reference to |
707 | | // be returned. |
708 | | explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT |
709 | | |
710 | | // This template type conversion operator allows ReturnRefOfCopy(x) to be |
711 | | // used in ANY function that returns a reference to x's type. |
712 | | template <typename F> |
713 | | operator Action<F>() const { |
714 | | typedef typename Function<F>::Result Result; |
715 | | // Asserts that the function return type is a reference. This |
716 | | // catches the user error of using ReturnRefOfCopy(x) when Return(x) |
717 | | // should be used, and generates some helpful error message. |
718 | | GTEST_COMPILE_ASSERT_( |
719 | | internal::is_reference<Result>::value, |
720 | | use_Return_instead_of_ReturnRefOfCopy_to_return_a_value); |
721 | | return Action<F>(new Impl<F>(value_)); |
722 | | } |
723 | | |
724 | | private: |
725 | | // Implements the ReturnRefOfCopy(x) action for a particular function type F. |
726 | | template <typename F> |
727 | | class Impl : public ActionInterface<F> { |
728 | | public: |
729 | | typedef typename Function<F>::Result Result; |
730 | | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
731 | | |
732 | | explicit Impl(const T& value) : value_(value) {} // NOLINT |
733 | | |
734 | | virtual Result Perform(const ArgumentTuple&) { |
735 | | return value_; |
736 | | } |
737 | | |
738 | | private: |
739 | | T value_; |
740 | | |
741 | | GTEST_DISALLOW_ASSIGN_(Impl); |
742 | | }; |
743 | | |
744 | | const T value_; |
745 | | |
746 | | GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction); |
747 | | }; |
748 | | |
749 | | // Implements the polymorphic DoDefault() action. |
750 | | class DoDefaultAction { |
751 | | public: |
752 | | // This template type conversion operator allows DoDefault() to be |
753 | | // used in any function. |
754 | | template <typename F> |
755 | | operator Action<F>() const { return Action<F>(NULL); } |
756 | | }; |
757 | | |
758 | | // Implements the Assign action to set a given pointer referent to a |
759 | | // particular value. |
760 | | template <typename T1, typename T2> |
761 | | class AssignAction { |
762 | | public: |
763 | | AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {} |
764 | | |
765 | | template <typename Result, typename ArgumentTuple> |
766 | | void Perform(const ArgumentTuple& /* args */) const { |
767 | | *ptr_ = value_; |
768 | | } |
769 | | |
770 | | private: |
771 | | T1* const ptr_; |
772 | | const T2 value_; |
773 | | |
774 | | GTEST_DISALLOW_ASSIGN_(AssignAction); |
775 | | }; |
776 | | |
777 | | #if !GTEST_OS_WINDOWS_MOBILE |
778 | | |
779 | | // Implements the SetErrnoAndReturn action to simulate return from |
780 | | // various system calls and libc functions. |
781 | | template <typename T> |
782 | | class SetErrnoAndReturnAction { |
783 | | public: |
784 | | SetErrnoAndReturnAction(int errno_value, T result) |
785 | | : errno_(errno_value), |
786 | | result_(result) {} |
787 | | template <typename Result, typename ArgumentTuple> |
788 | | Result Perform(const ArgumentTuple& /* args */) const { |
789 | | errno = errno_; |
790 | | return result_; |
791 | | } |
792 | | |
793 | | private: |
794 | | const int errno_; |
795 | | const T result_; |
796 | | |
797 | | GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction); |
798 | | }; |
799 | | |
800 | | #endif // !GTEST_OS_WINDOWS_MOBILE |
801 | | |
802 | | // Implements the SetArgumentPointee<N>(x) action for any function |
803 | | // whose N-th argument (0-based) is a pointer to x's type. The |
804 | | // template parameter kIsProto is true iff type A is ProtocolMessage, |
805 | | // proto2::Message, or a sub-class of those. |
806 | | template <size_t N, typename A, bool kIsProto> |
807 | | class SetArgumentPointeeAction { |
808 | | public: |
809 | | // Constructs an action that sets the variable pointed to by the |
810 | | // N-th function argument to 'value'. |
811 | | explicit SetArgumentPointeeAction(const A& value) : value_(value) {} |
812 | | |
813 | | template <typename Result, typename ArgumentTuple> |
814 | | void Perform(const ArgumentTuple& args) const { |
815 | | CompileAssertTypesEqual<void, Result>(); |
816 | | *::testing::get<N>(args) = value_; |
817 | | } |
818 | | |
819 | | private: |
820 | | const A value_; |
821 | | |
822 | | GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction); |
823 | | }; |
824 | | |
825 | | template <size_t N, typename Proto> |
826 | | class SetArgumentPointeeAction<N, Proto, true> { |
827 | | public: |
828 | | // Constructs an action that sets the variable pointed to by the |
829 | | // N-th function argument to 'proto'. Both ProtocolMessage and |
830 | | // proto2::Message have the CopyFrom() method, so the same |
831 | | // implementation works for both. |
832 | | explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) { |
833 | | proto_->CopyFrom(proto); |
834 | | } |
835 | | |
836 | | template <typename Result, typename ArgumentTuple> |
837 | | void Perform(const ArgumentTuple& args) const { |
838 | | CompileAssertTypesEqual<void, Result>(); |
839 | | ::testing::get<N>(args)->CopyFrom(*proto_); |
840 | | } |
841 | | |
842 | | private: |
843 | | const internal::linked_ptr<Proto> proto_; |
844 | | |
845 | | GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction); |
846 | | }; |
847 | | |
848 | | // Implements the InvokeWithoutArgs(f) action. The template argument |
849 | | // FunctionImpl is the implementation type of f, which can be either a |
850 | | // function pointer or a functor. InvokeWithoutArgs(f) can be used as an |
851 | | // Action<F> as long as f's type is compatible with F (i.e. f can be |
852 | | // assigned to a tr1::function<F>). |
853 | | template <typename FunctionImpl> |
854 | | class InvokeWithoutArgsAction { |
855 | | public: |
856 | | // The c'tor makes a copy of function_impl (either a function |
857 | | // pointer or a functor). |
858 | | explicit InvokeWithoutArgsAction(FunctionImpl function_impl) |
859 | | : function_impl_(function_impl) {} |
860 | | |
861 | | // Allows InvokeWithoutArgs(f) to be used as any action whose type is |
862 | | // compatible with f. |
863 | | template <typename Result, typename ArgumentTuple> |
864 | | Result Perform(const ArgumentTuple&) { return function_impl_(); } |
865 | | |
866 | | private: |
867 | | FunctionImpl function_impl_; |
868 | | |
869 | | GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction); |
870 | | }; |
871 | | |
872 | | // Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action. |
873 | | template <class Class, typename MethodPtr> |
874 | | class InvokeMethodWithoutArgsAction { |
875 | | public: |
876 | | InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr) |
877 | | : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {} |
878 | | |
879 | | template <typename Result, typename ArgumentTuple> |
880 | | Result Perform(const ArgumentTuple&) const { |
881 | | return (obj_ptr_->*method_ptr_)(); |
882 | | } |
883 | | |
884 | | private: |
885 | | Class* const obj_ptr_; |
886 | | const MethodPtr method_ptr_; |
887 | | |
888 | | GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction); |
889 | | }; |
890 | | |
891 | | // Implements the IgnoreResult(action) action. |
892 | | template <typename A> |
893 | | class IgnoreResultAction { |
894 | | public: |
895 | | explicit IgnoreResultAction(const A& action) : action_(action) {} |
896 | | |
897 | | template <typename F> |
898 | | operator Action<F>() const { |
899 | | // Assert statement belongs here because this is the best place to verify |
900 | | // conditions on F. It produces the clearest error messages |
901 | | // in most compilers. |
902 | | // Impl really belongs in this scope as a local class but can't |
903 | | // because MSVC produces duplicate symbols in different translation units |
904 | | // in this case. Until MS fixes that bug we put Impl into the class scope |
905 | | // and put the typedef both here (for use in assert statement) and |
906 | | // in the Impl class. But both definitions must be the same. |
907 | | typedef typename internal::Function<F>::Result Result; |
908 | | |
909 | | // Asserts at compile time that F returns void. |
910 | | CompileAssertTypesEqual<void, Result>(); |
911 | | |
912 | | return Action<F>(new Impl<F>(action_)); |
913 | | } |
914 | | |
915 | | private: |
916 | | template <typename F> |
917 | | class Impl : public ActionInterface<F> { |
918 | | public: |
919 | | typedef typename internal::Function<F>::Result Result; |
920 | | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
921 | | |
922 | | explicit Impl(const A& action) : action_(action) {} |
923 | | |
924 | | virtual void Perform(const ArgumentTuple& args) { |
925 | | // Performs the action and ignores its result. |
926 | | action_.Perform(args); |
927 | | } |
928 | | |
929 | | private: |
930 | | // Type OriginalFunction is the same as F except that its return |
931 | | // type is IgnoredValue. |
932 | | typedef typename internal::Function<F>::MakeResultIgnoredValue |
933 | | OriginalFunction; |
934 | | |
935 | | const Action<OriginalFunction> action_; |
936 | | |
937 | | GTEST_DISALLOW_ASSIGN_(Impl); |
938 | | }; |
939 | | |
940 | | const A action_; |
941 | | |
942 | | GTEST_DISALLOW_ASSIGN_(IgnoreResultAction); |
943 | | }; |
944 | | |
945 | | // A ReferenceWrapper<T> object represents a reference to type T, |
946 | | // which can be either const or not. It can be explicitly converted |
947 | | // from, and implicitly converted to, a T&. Unlike a reference, |
948 | | // ReferenceWrapper<T> can be copied and can survive template type |
949 | | // inference. This is used to support by-reference arguments in the |
950 | | // InvokeArgument<N>(...) action. The idea was from "reference |
951 | | // wrappers" in tr1, which we don't have in our source tree yet. |
952 | | template <typename T> |
953 | | class ReferenceWrapper { |
954 | | public: |
955 | | // Constructs a ReferenceWrapper<T> object from a T&. |
956 | | explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT |
957 | | |
958 | | // Allows a ReferenceWrapper<T> object to be implicitly converted to |
959 | | // a T&. |
960 | | operator T&() const { return *pointer_; } |
961 | | private: |
962 | | T* pointer_; |
963 | | }; |
964 | | |
965 | | // Allows the expression ByRef(x) to be printed as a reference to x. |
966 | | template <typename T> |
967 | | void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) { |
968 | | T& value = ref; |
969 | | UniversalPrinter<T&>::Print(value, os); |
970 | | } |
971 | | |
972 | | // Does two actions sequentially. Used for implementing the DoAll(a1, |
973 | | // a2, ...) action. |
974 | | template <typename Action1, typename Action2> |
975 | | class DoBothAction { |
976 | | public: |
977 | | DoBothAction(Action1 action1, Action2 action2) |
978 | | : action1_(action1), action2_(action2) {} |
979 | | |
980 | | // This template type conversion operator allows DoAll(a1, ..., a_n) |
981 | | // to be used in ANY function of compatible type. |
982 | | template <typename F> |
983 | | operator Action<F>() const { |
984 | | return Action<F>(new Impl<F>(action1_, action2_)); |
985 | | } |
986 | | |
987 | | private: |
988 | | // Implements the DoAll(...) action for a particular function type F. |
989 | | template <typename F> |
990 | | class Impl : public ActionInterface<F> { |
991 | | public: |
992 | | typedef typename Function<F>::Result Result; |
993 | | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
994 | | typedef typename Function<F>::MakeResultVoid VoidResult; |
995 | | |
996 | | Impl(const Action<VoidResult>& action1, const Action<F>& action2) |
997 | | : action1_(action1), action2_(action2) {} |
998 | | |
999 | | virtual Result Perform(const ArgumentTuple& args) { |
1000 | | action1_.Perform(args); |
1001 | | return action2_.Perform(args); |
1002 | | } |
1003 | | |
1004 | | private: |
1005 | | const Action<VoidResult> action1_; |
1006 | | const Action<F> action2_; |
1007 | | |
1008 | | GTEST_DISALLOW_ASSIGN_(Impl); |
1009 | | }; |
1010 | | |
1011 | | Action1 action1_; |
1012 | | Action2 action2_; |
1013 | | |
1014 | | GTEST_DISALLOW_ASSIGN_(DoBothAction); |
1015 | | }; |
1016 | | |
1017 | | } // namespace internal |
1018 | | |
1019 | | // An Unused object can be implicitly constructed from ANY value. |
1020 | | // This is handy when defining actions that ignore some or all of the |
1021 | | // mock function arguments. For example, given |
1022 | | // |
1023 | | // MOCK_METHOD3(Foo, double(const string& label, double x, double y)); |
1024 | | // MOCK_METHOD3(Bar, double(int index, double x, double y)); |
1025 | | // |
1026 | | // instead of |
1027 | | // |
1028 | | // double DistanceToOriginWithLabel(const string& label, double x, double y) { |
1029 | | // return sqrt(x*x + y*y); |
1030 | | // } |
1031 | | // double DistanceToOriginWithIndex(int index, double x, double y) { |
1032 | | // return sqrt(x*x + y*y); |
1033 | | // } |
1034 | | // ... |
1035 | | // EXEPCT_CALL(mock, Foo("abc", _, _)) |
1036 | | // .WillOnce(Invoke(DistanceToOriginWithLabel)); |
1037 | | // EXEPCT_CALL(mock, Bar(5, _, _)) |
1038 | | // .WillOnce(Invoke(DistanceToOriginWithIndex)); |
1039 | | // |
1040 | | // you could write |
1041 | | // |
1042 | | // // We can declare any uninteresting argument as Unused. |
1043 | | // double DistanceToOrigin(Unused, double x, double y) { |
1044 | | // return sqrt(x*x + y*y); |
1045 | | // } |
1046 | | // ... |
1047 | | // EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin)); |
1048 | | // EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin)); |
1049 | | typedef internal::IgnoredValue Unused; |
1050 | | |
1051 | | // This constructor allows us to turn an Action<From> object into an |
1052 | | // Action<To>, as long as To's arguments can be implicitly converted |
1053 | | // to From's and From's return type cann be implicitly converted to |
1054 | | // To's. |
1055 | | template <typename To> |
1056 | | template <typename From> |
1057 | | Action<To>::Action(const Action<From>& from) |
1058 | | : impl_(new internal::ActionAdaptor<To, From>(from)) {} |
1059 | | |
1060 | | // Creates an action that returns 'value'. 'value' is passed by value |
1061 | | // instead of const reference - otherwise Return("string literal") |
1062 | | // will trigger a compiler error about using array as initializer. |
1063 | | template <typename R> |
1064 | | internal::ReturnAction<R> Return(R value) { |
1065 | | return internal::ReturnAction<R>(internal::move(value)); |
1066 | | } |
1067 | | |
1068 | | // Creates an action that returns NULL. |
1069 | 0 | inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() { |
1070 | 0 | return MakePolymorphicAction(internal::ReturnNullAction()); |
1071 | 0 | } |
1072 | | |
1073 | | // Creates an action that returns from a void function. |
1074 | 0 | inline PolymorphicAction<internal::ReturnVoidAction> Return() { |
1075 | 0 | return MakePolymorphicAction(internal::ReturnVoidAction()); |
1076 | 0 | } |
1077 | | |
1078 | | // Creates an action that returns the reference to a variable. |
1079 | | template <typename R> |
1080 | | inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT |
1081 | | return internal::ReturnRefAction<R>(x); |
1082 | | } |
1083 | | |
1084 | | // Creates an action that returns the reference to a copy of the |
1085 | | // argument. The copy is created when the action is constructed and |
1086 | | // lives as long as the action. |
1087 | | template <typename R> |
1088 | | inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) { |
1089 | | return internal::ReturnRefOfCopyAction<R>(x); |
1090 | | } |
1091 | | |
1092 | | // Modifies the parent action (a Return() action) to perform a move of the |
1093 | | // argument instead of a copy. |
1094 | | // Return(ByMove()) actions can only be executed once and will assert this |
1095 | | // invariant. |
1096 | | template <typename R> |
1097 | | internal::ByMoveWrapper<R> ByMove(R x) { |
1098 | | return internal::ByMoveWrapper<R>(internal::move(x)); |
1099 | | } |
1100 | | |
1101 | | // Creates an action that does the default action for the give mock function. |
1102 | 0 | inline internal::DoDefaultAction DoDefault() { |
1103 | 0 | return internal::DoDefaultAction(); |
1104 | 0 | } |
1105 | | |
1106 | | // Creates an action that sets the variable pointed by the N-th |
1107 | | // (0-based) function argument to 'value'. |
1108 | | template <size_t N, typename T> |
1109 | | PolymorphicAction< |
1110 | | internal::SetArgumentPointeeAction< |
1111 | | N, T, internal::IsAProtocolMessage<T>::value> > |
1112 | | SetArgPointee(const T& x) { |
1113 | | return MakePolymorphicAction(internal::SetArgumentPointeeAction< |
1114 | | N, T, internal::IsAProtocolMessage<T>::value>(x)); |
1115 | | } |
1116 | | |
1117 | | #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN) |
1118 | | // This overload allows SetArgPointee() to accept a string literal. |
1119 | | // GCC prior to the version 4.0 and Symbian C++ compiler cannot distinguish |
1120 | | // this overload from the templated version and emit a compile error. |
1121 | | template <size_t N> |
1122 | | PolymorphicAction< |
1123 | | internal::SetArgumentPointeeAction<N, const char*, false> > |
1124 | | SetArgPointee(const char* p) { |
1125 | | return MakePolymorphicAction(internal::SetArgumentPointeeAction< |
1126 | | N, const char*, false>(p)); |
1127 | | } |
1128 | | |
1129 | | template <size_t N> |
1130 | | PolymorphicAction< |
1131 | | internal::SetArgumentPointeeAction<N, const wchar_t*, false> > |
1132 | | SetArgPointee(const wchar_t* p) { |
1133 | | return MakePolymorphicAction(internal::SetArgumentPointeeAction< |
1134 | | N, const wchar_t*, false>(p)); |
1135 | | } |
1136 | | #endif |
1137 | | |
1138 | | // The following version is DEPRECATED. |
1139 | | template <size_t N, typename T> |
1140 | | PolymorphicAction< |
1141 | | internal::SetArgumentPointeeAction< |
1142 | | N, T, internal::IsAProtocolMessage<T>::value> > |
1143 | | SetArgumentPointee(const T& x) { |
1144 | | return MakePolymorphicAction(internal::SetArgumentPointeeAction< |
1145 | | N, T, internal::IsAProtocolMessage<T>::value>(x)); |
1146 | | } |
1147 | | |
1148 | | // Creates an action that sets a pointer referent to a given value. |
1149 | | template <typename T1, typename T2> |
1150 | | PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) { |
1151 | | return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val)); |
1152 | | } |
1153 | | |
1154 | | #if !GTEST_OS_WINDOWS_MOBILE |
1155 | | |
1156 | | // Creates an action that sets errno and returns the appropriate error. |
1157 | | template <typename T> |
1158 | | PolymorphicAction<internal::SetErrnoAndReturnAction<T> > |
1159 | | SetErrnoAndReturn(int errval, T result) { |
1160 | | return MakePolymorphicAction( |
1161 | | internal::SetErrnoAndReturnAction<T>(errval, result)); |
1162 | | } |
1163 | | |
1164 | | #endif // !GTEST_OS_WINDOWS_MOBILE |
1165 | | |
1166 | | // Various overloads for InvokeWithoutArgs(). |
1167 | | |
1168 | | // Creates an action that invokes 'function_impl' with no argument. |
1169 | | template <typename FunctionImpl> |
1170 | | PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> > |
1171 | | InvokeWithoutArgs(FunctionImpl function_impl) { |
1172 | | return MakePolymorphicAction( |
1173 | | internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl)); |
1174 | | } |
1175 | | |
1176 | | // Creates an action that invokes the given method on the given object |
1177 | | // with no argument. |
1178 | | template <class Class, typename MethodPtr> |
1179 | | PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> > |
1180 | | InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) { |
1181 | | return MakePolymorphicAction( |
1182 | | internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>( |
1183 | | obj_ptr, method_ptr)); |
1184 | | } |
1185 | | |
1186 | | // Creates an action that performs an_action and throws away its |
1187 | | // result. In other words, it changes the return type of an_action to |
1188 | | // void. an_action MUST NOT return void, or the code won't compile. |
1189 | | template <typename A> |
1190 | | inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) { |
1191 | | return internal::IgnoreResultAction<A>(an_action); |
1192 | | } |
1193 | | |
1194 | | // Creates a reference wrapper for the given L-value. If necessary, |
1195 | | // you can explicitly specify the type of the reference. For example, |
1196 | | // suppose 'derived' is an object of type Derived, ByRef(derived) |
1197 | | // would wrap a Derived&. If you want to wrap a const Base& instead, |
1198 | | // where Base is a base class of Derived, just write: |
1199 | | // |
1200 | | // ByRef<const Base>(derived) |
1201 | | template <typename T> |
1202 | | inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT |
1203 | | return internal::ReferenceWrapper<T>(l_value); |
1204 | | } |
1205 | | |
1206 | | } // namespace testing |
1207 | | |
1208 | | #endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ |