Warning, /JETSCAPE/external_packages/googletest/googlemock/docs/DesignDoc.md is written in an unsupported language. File is not indexed.
0001 This page discusses the design of new Google Mock features.
0002
0003
0004
0005 # Macros for Defining Actions #
0006
0007 ## Problem ##
0008
0009 Due to the lack of closures in C++, it currently requires some
0010 non-trivial effort to define a custom action in Google Mock. For
0011 example, suppose you want to "increment the value pointed to by the
0012 second argument of the mock function and return it", you could write:
0013
0014 ```
0015 int IncrementArg1(Unused, int* p, Unused) {
0016 return ++(*p);
0017 }
0018
0019 ... WillOnce(Invoke(IncrementArg1));
0020 ```
0021
0022 There are several things unsatisfactory about this approach:
0023
0024 * Even though the action only cares about the second argument of the mock function, its definition needs to list other arguments as dummies. This is tedious.
0025 * The defined action is usable only in mock functions that takes exactly 3 arguments - an unnecessary restriction.
0026 * To use the action, one has to say `Invoke(IncrementArg1)`, which isn't as nice as `IncrementArg1()`.
0027
0028 The latter two problems can be overcome using `MakePolymorphicAction()`,
0029 but it requires much more boilerplate code:
0030
0031 ```
0032 class IncrementArg1Action {
0033 public:
0034 template <typename Result, typename ArgumentTuple>
0035 Result Perform(const ArgumentTuple& args) const {
0036 return ++(*tr1::get<1>(args));
0037 }
0038 };
0039
0040 PolymorphicAction<IncrementArg1Action> IncrementArg1() {
0041 return MakePolymorphicAction(IncrementArg1Action());
0042 }
0043
0044 ... WillOnce(IncrementArg1());
0045 ```
0046
0047 Our goal is to allow defining custom actions with the least amount of
0048 boiler-plate C++ requires.
0049
0050 ## Solution ##
0051
0052 We propose to introduce a new macro:
0053 ```
0054 ACTION(name) { statements; }
0055 ```
0056
0057 Using this in a namespace scope will define an action with the given
0058 name that executes the statements. Inside the statements, you can
0059 refer to the K-th (0-based) argument of the mock function as `argK`.
0060 For example:
0061 ```
0062 ACTION(IncrementArg1) { return ++(*arg1); }
0063 ```
0064 allows you to write
0065 ```
0066 ... WillOnce(IncrementArg1());
0067 ```
0068
0069 Note that you don't need to specify the types of the mock function
0070 arguments, as brevity is a top design goal here. Rest assured that
0071 your code is still type-safe though: you'll get a compiler error if
0072 `*arg1` doesn't support the `++` operator, or if the type of
0073 `++(*arg1)` isn't compatible with the mock function's return type.
0074
0075 Another example:
0076 ```
0077 ACTION(Foo) {
0078 (*arg2)(5);
0079 Blah();
0080 *arg1 = 0;
0081 return arg0;
0082 }
0083 ```
0084 defines an action `Foo()` that invokes argument #2 (a function pointer)
0085 with 5, calls function `Blah()`, sets the value pointed to by argument
0086 #1 to 0, and returns argument #0.
0087
0088 For more convenience and flexibility, you can also use the following
0089 pre-defined symbols in the body of `ACTION`:
0090
0091 | `argK_type` | The type of the K-th (0-based) argument of the mock function |
0092 |:------------|:-------------------------------------------------------------|
0093 | `args` | All arguments of the mock function as a tuple |
0094 | `args_type` | The type of all arguments of the mock function as a tuple |
0095 | `return_type` | The return type of the mock function |
0096 | `function_type` | The type of the mock function |
0097
0098 For example, when using an `ACTION` as a stub action for mock function:
0099 ```
0100 int DoSomething(bool flag, int* ptr);
0101 ```
0102 we have:
0103 | **Pre-defined Symbol** | **Is Bound To** |
0104 |:-----------------------|:----------------|
0105 | `arg0` | the value of `flag` |
0106 | `arg0_type` | the type `bool` |
0107 | `arg1` | the value of `ptr` |
0108 | `arg1_type` | the type `int*` |
0109 | `args` | the tuple `(flag, ptr)` |
0110 | `args_type` | the type `std::tr1::tuple<bool, int*>` |
0111 | `return_type` | the type `int` |
0112 | `function_type` | the type `int(bool, int*)` |
0113
0114 ## Parameterized actions ##
0115
0116 Sometimes you'll want to parameterize the action. For that we propose
0117 another macro
0118 ```
0119 ACTION_P(name, param) { statements; }
0120 ```
0121
0122 For example,
0123 ```
0124 ACTION_P(Add, n) { return arg0 + n; }
0125 ```
0126 will allow you to write
0127 ```
0128 // Returns argument #0 + 5.
0129 ... WillOnce(Add(5));
0130 ```
0131
0132 For convenience, we use the term _arguments_ for the values used to
0133 invoke the mock function, and the term _parameters_ for the values
0134 used to instantiate an action.
0135
0136 Note that you don't need to provide the type of the parameter either.
0137 Suppose the parameter is named `param`, you can also use the
0138 Google-Mock-defined symbol `param_type` to refer to the type of the
0139 parameter as inferred by the compiler.
0140
0141 We will also provide `ACTION_P2`, `ACTION_P3`, and etc to support
0142 multi-parameter actions. For example,
0143 ```
0144 ACTION_P2(ReturnDistanceTo, x, y) {
0145 double dx = arg0 - x;
0146 double dy = arg1 - y;
0147 return sqrt(dx*dx + dy*dy);
0148 }
0149 ```
0150 lets you write
0151 ```
0152 ... WillOnce(ReturnDistanceTo(5.0, 26.5));
0153 ```
0154
0155 You can view `ACTION` as a degenerated parameterized action where the
0156 number of parameters is 0.
0157
0158 ## Advanced Usages ##
0159
0160 ### Overloading Actions ###
0161
0162 You can easily define actions overloaded on the number of parameters:
0163 ```
0164 ACTION_P(Plus, a) { ... }
0165 ACTION_P2(Plus, a, b) { ... }
0166 ```
0167
0168 ### Restricting the Type of an Argument or Parameter ###
0169
0170 For maximum brevity and reusability, the `ACTION*` macros don't let
0171 you specify the types of the mock function arguments and the action
0172 parameters. Instead, we let the compiler infer the types for us.
0173
0174 Sometimes, however, we may want to be more explicit about the types.
0175 There are several tricks to do that. For example:
0176 ```
0177 ACTION(Foo) {
0178 // Makes sure arg0 can be converted to int.
0179 int n = arg0;
0180 ... use n instead of arg0 here ...
0181 }
0182
0183 ACTION_P(Bar, param) {
0184 // Makes sure the type of arg1 is const char*.
0185 ::testing::StaticAssertTypeEq<const char*, arg1_type>();
0186
0187 // Makes sure param can be converted to bool.
0188 bool flag = param;
0189 }
0190 ```
0191 where `StaticAssertTypeEq` is a compile-time assertion we plan to add to
0192 Google Test (the name is chosen to match `static_assert` in C++0x).
0193
0194 ### Using the ACTION Object's Type ###
0195
0196 If you are writing a function that returns an `ACTION` object, you'll
0197 need to know its type. The type depends on the macro used to define
0198 the action and the parameter types. The rule is relatively simple:
0199 | **Given Definition** | **Expression** | **Has Type** |
0200 |:---------------------|:---------------|:-------------|
0201 | `ACTION(Foo)` | `Foo()` | `FooAction` |
0202 | `ACTION_P(Bar, param)` | `Bar(int_value)` | `BarActionP<int>` |
0203 | `ACTION_P2(Baz, p1, p2)` | `Baz(bool_value, int_value)` | `BazActionP2<bool, int>` |
0204 | ... | ... | ... |
0205
0206 Note that we have to pick different suffixes (`Action`, `ActionP`,
0207 `ActionP2`, and etc) for actions with different numbers of parameters,
0208 or the action definitions cannot be overloaded on the number of
0209 parameters.
0210
0211 ## When to Use ##
0212
0213 While the new macros are very convenient, please also consider other
0214 means of implementing actions (e.g. via `ActionInterface` or
0215 `MakePolymorphicAction()`), especially if you need to use the defined
0216 action a lot. While the other approaches require more work, they give
0217 you more control on the types of the mock function arguments and the
0218 action parameters, which in general leads to better compiler error
0219 messages that pay off in the long run. They also allow overloading
0220 actions based on parameter types, as opposed to just the number of
0221 parameters.
0222
0223 ## Related Work ##
0224
0225 As you may have realized, the `ACTION*` macros resemble closures (also
0226 known as lambda expressions or anonymous functions). Indeed, both of
0227 them seek to lower the syntactic overhead for defining a function.
0228
0229 C++0x will support lambdas, but they are not part of C++ right now.
0230 Some non-standard libraries (most notably BLL or Boost Lambda Library)
0231 try to alleviate this problem. However, they are not a good choice
0232 for defining actions as:
0233
0234 * They are non-standard and not widely installed. Google Mock only depends on standard libraries and `tr1::tuple`, which is part of the new C++ standard and comes with gcc 4+. We want to keep it that way.
0235 * They are not trivial to learn.
0236 * They will become obsolete when C++0x's lambda feature is widely supported. We don't want to make our users use a dying library.
0237 * Since they are based on operators, they are rather ad hoc: you cannot use statements, and you cannot pass the lambda arguments to a function, for example.
0238 * They have subtle semantics that easily confuses new users. For example, in expression `_1++ + foo++`, `foo` will be incremented only once where the expression is evaluated, while `_1` will be incremented every time the unnamed function is invoked. This is far from intuitive.
0239
0240 `ACTION*` avoid all these problems.
0241
0242 ## Future Improvements ##
0243
0244 There may be a need for composing `ACTION*` definitions (i.e. invoking
0245 another `ACTION` inside the definition of one `ACTION*`). We are not
0246 sure we want it yet, as one can get a similar effect by putting
0247 `ACTION` definitions in function templates and composing the function
0248 templates. We'll revisit this based on user feedback.
0249
0250 The reason we don't allow `ACTION*()` inside a function body is that
0251 the current C++ standard doesn't allow function-local types to be used
0252 to instantiate templates. The upcoming C++0x standard will lift this
0253 restriction. Once this feature is widely supported by compilers, we
0254 can revisit the implementation and add support for using `ACTION*()`
0255 inside a function.
0256
0257 C++0x will also support lambda expressions. When they become
0258 available, we may want to support using lambdas as actions.
0259
0260 # Macros for Defining Matchers #
0261
0262 Once the macros for defining actions are implemented, we plan to do
0263 the same for matchers:
0264
0265 ```
0266 MATCHER(name) { statements; }
0267 ```
0268
0269 where you can refer to the value being matched as `arg`. For example,
0270 given:
0271
0272 ```
0273 MATCHER(IsPositive) { return arg > 0; }
0274 ```
0275
0276 you can use `IsPositive()` as a matcher that matches a value iff it is
0277 greater than 0.
0278
0279 We will also add `MATCHER_P`, `MATCHER_P2`, and etc for parameterized
0280 matchers.