aboutsummaryrefslogtreecommitdiffstats
path: root/googlemock/docs
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2019-08-07 03:19:07 -0400
committerGennadiy Civil <misterg@google.com>2019-08-07 11:57:13 -0400
commitdd5402d9d4f3477471abb89a95a60f09edc0cccd (patch)
tree601a603fa7859562fcfa5e2498d4bb5900e7036b /googlemock/docs
parent79690c53756314a92cd8f465273d4fd3cd8d4915 (diff)
downloadgoogletest-dd5402d9d4f3477471abb89a95a60f09edc0cccd.tar.gz
googletest-dd5402d9d4f3477471abb89a95a60f09edc0cccd.tar.bz2
googletest-dd5402d9d4f3477471abb89a95a60f09edc0cccd.zip
Googletest export
Add general explanation of MOCK_METHOD, including list of supported qualifiers. PiperOrigin-RevId: 262077180
Diffstat (limited to 'googlemock/docs')
-rw-r--r--googlemock/docs/cook_book.md30
1 files changed, 27 insertions, 3 deletions
diff --git a/googlemock/docs/cook_book.md b/googlemock/docs/cook_book.md
index 0352ef65..f2b7d300 100644
--- a/googlemock/docs/cook_book.md
+++ b/googlemock/docs/cook_book.md
@@ -12,12 +12,36 @@ brevity, but you should do it in your own code.
### Creating Mock Classes
+Mock classes are defined as normal classes, using the `MOCK_METHOD` macro to
+generate mocked methods. The macro gets 3 or 4 parameters:
+
+```cpp
+class MyMock {
+ public:
+ MOCK_METHOD(ReturnType, MethodName, (Args...));
+ MOCK_METHOD(ReturnType, MethodName, (Args...), (Specs...));
+};
+```
+
+The first 3 parameters are simply the method declaration, split into 3 parts.
+The 4th parameter accepts a closed list of qualifiers, which affect the
+generated method:
+
+* **`const`** - Makes the mocked method a `const` method. Required if
+ overriding a `const` method.
+* **`override`** - Marks the method with `override`. Recommended if overriding
+ a `virtual` method.
+* **`noexcept`** - Marks the method with `noexcept`. Required if overriding a
+ `noexcept` method.
+* **`Calltype(...)`** - Sets the call type for the method (e.g. to
+ `STDMETHODCALLTYPE`), useful in Windows.
+
#### Dealing with unprotected commas
Unprotected commas, i.e. commas which are not surrounded by parentheses, prevent
`MOCK_METHOD` from parsing its arguments correctly:
-```cpp
+```cpp {.bad}
class MockFoo {
public:
MOCK_METHOD(std::pair<bool, int>, GetPair, ()); // Won't compile!
@@ -27,7 +51,7 @@ class MockFoo {
Solution 1 - wrap with parentheses:
-```cpp
+```cpp {.good}
class MockFoo {
public:
MOCK_METHOD((std::pair<bool, int>), GetPair, ());
@@ -40,7 +64,7 @@ invalid C++. `MOCK_METHOD` removes the parentheses.
Solution 2 - define an alias:
-```cpp
+```cpp {.good}
class MockFoo {
public:
using BoolAndInt = std::pair<bool, int>;