Skip to content

Commit ec4fed9

Browse files
Abseil Teamcopybara-github
authored andcommitted
Update code examples in the gMock Cookbook following C++ best practices.
PiperOrigin-RevId: 542564354 Change-Id: Ia3307f13f845c662c88fb7303112f41ef8c56b28
1 parent af39146 commit ec4fed9

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

docs/gmock_cook_book.md

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,9 @@ TEST(AbcTest, Xyz) {
697697
EXPECT_CALL(foo, DoThat(_, _));
698698

699699
int n = 0;
700-
EXPECT_EQ('+', foo.DoThis(5)); // FakeFoo::DoThis() is invoked.
700+
EXPECT_EQ(foo.DoThis(5), '+'); // FakeFoo::DoThis() is invoked.
701701
foo.DoThat("Hi", &n); // FakeFoo::DoThat() is invoked.
702-
EXPECT_EQ(2, n);
702+
EXPECT_EQ(n, 2);
703703
}
704704
```
705705

@@ -1129,11 +1129,11 @@ using STL's `<functional>` header is just painful). For example, here's a
11291129
predicate that's satisfied by any number that is >= 0, <= 100, and != 50:
11301130

11311131
```cpp
1132-
using testing::AllOf;
1133-
using testing::Ge;
1134-
using testing::Le;
1135-
using testing::Matches;
1136-
using testing::Ne;
1132+
using ::testing::AllOf;
1133+
using ::testing::Ge;
1134+
using ::testing::Le;
1135+
using ::testing::Matches;
1136+
using ::testing::Ne;
11371137
...
11381138
Matches(AllOf(Ge(0), Le(100), Ne(50)))
11391139
```
@@ -1861,7 +1861,7 @@ error. So, what shall you do?
18611861
Though you may be tempted, DO NOT use `std::ref()`:
18621862

18631863
```cpp
1864-
using testing::Return;
1864+
using ::testing::Return;
18651865

18661866
class MockFoo : public Foo {
18671867
public:
@@ -1873,7 +1873,7 @@ class MockFoo : public Foo {
18731873
EXPECT_CALL(foo, GetValue())
18741874
.WillRepeatedly(Return(std::ref(x))); // Wrong!
18751875
x = 42;
1876-
EXPECT_EQ(42, foo.GetValue());
1876+
EXPECT_EQ(foo.GetValue(), 42);
18771877
```
18781878

18791879
Unfortunately, it doesn't work here. The above code will fail with error:
@@ -1895,14 +1895,14 @@ the expectation is set, and `Return(std::ref(x))` will always return 0.
18951895
returns the value pointed to by `pointer` at the time the action is *executed*:
18961896

18971897
```cpp
1898-
using testing::ReturnPointee;
1898+
using ::testing::ReturnPointee;
18991899
...
19001900
int x = 0;
19011901
MockFoo foo;
19021902
EXPECT_CALL(foo, GetValue())
19031903
.WillRepeatedly(ReturnPointee(&x)); // Note the & here.
19041904
x = 42;
1905-
EXPECT_EQ(42, foo.GetValue()); // This will succeed now.
1905+
EXPECT_EQ(foo.GetValue(), 42); // This will succeed now.
19061906
```
19071907

19081908
### Combining Actions
@@ -2264,7 +2264,7 @@ TEST_F(FooTest, Test) {
22642264

22652265
EXPECT_CALL(foo, DoThis(2))
22662266
.WillOnce(Invoke(NewPermanentCallback(SignOfSum, 5)));
2267-
EXPECT_EQ('+', foo.DoThis(2)); // Invokes SignOfSum(5, 2).
2267+
EXPECT_EQ(foo.DoThis(2), '+'); // Invokes SignOfSum(5, 2).
22682268
}
22692269
```
22702270

@@ -2771,11 +2771,13 @@ returns a null `unique_ptr`, that’s what you’ll get if you don’t specify a
27712771
action:
27722772

27732773
```cpp
2774+
using ::testing::IsNull;
2775+
...
27742776
// Use the default action.
27752777
EXPECT_CALL(mock_buzzer_, MakeBuzz("hello"));
27762778

27772779
// Triggers the previous EXPECT_CALL.
2778-
EXPECT_EQ(nullptr, mock_buzzer_.MakeBuzz("hello"));
2780+
EXPECT_THAT(mock_buzzer_.MakeBuzz("hello"), IsNull());
27792781
```
27802782

27812783
If you are not happy with the default action, you can tweak it as usual; see
@@ -3194,9 +3196,9 @@ flag. For example, given the test program:
31943196
```cpp
31953197
#include "gmock/gmock.h"
31963198

3197-
using testing::_;
3198-
using testing::HasSubstr;
3199-
using testing::Return;
3199+
using ::testing::_;
3200+
using ::testing::HasSubstr;
3201+
using ::testing::Return;
32003202

32013203
class MockFoo {
32023204
public:
@@ -3817,15 +3819,15 @@ If the built-in actions don't work for you, you can easily define your own one.
38173819
All you need is a call operator with a signature compatible with the mocked
38183820
function. So you can use a lambda:
38193821

3820-
```
3822+
```cpp
38213823
MockFunction<int(int)> mock;
38223824
EXPECT_CALL(mock, Call).WillOnce([](const int input) { return input * 7; });
3823-
EXPECT_EQ(14, mock.AsStdFunction()(2));
3825+
EXPECT_EQ(mock.AsStdFunction()(2), 14);
38243826
```
38253827

38263828
Or a struct with a call operator (even a templated one):
38273829

3828-
```
3830+
```cpp
38293831
struct MultiplyBy {
38303832
template <typename T>
38313833
T operator()(T arg) { return arg * multiplier; }
@@ -3840,16 +3842,16 @@ struct MultiplyBy {
38403842
It's also fine for the callable to take no arguments, ignoring the arguments
38413843
supplied to the mock function:
38423844

3843-
```
3845+
```cpp
38443846
MockFunction<int(int)> mock;
38453847
EXPECT_CALL(mock, Call).WillOnce([] { return 17; });
3846-
EXPECT_EQ(17, mock.AsStdFunction()(0));
3848+
EXPECT_EQ(mock.AsStdFunction()(0), 17);
38473849
```
38483850

38493851
When used with `WillOnce`, the callable can assume it will be called at most
38503852
once and is allowed to be a move-only type:
38513853

3852-
```
3854+
```cpp
38533855
// An action that contains move-only types and has an &&-qualified operator,
38543856
// demanding in the type system that it be called at most once. This can be
38553857
// used with WillOnce, but the compiler will reject it if handed to

0 commit comments

Comments
 (0)