xref: /aosp_15_r20/external/cronet/base/test/gmock_move_support_unittest.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/test/gmock_move_support.h"
6 
7 #include <memory>
8 
9 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 
12 namespace {
13 
14 using ::testing::DoAll;
15 using ::testing::Pointee;
16 
17 using MoveOnly = std::unique_ptr<int>;
18 
19 struct MockFoo {
20   MOCK_METHOD(void, ByRef, (MoveOnly&), ());
21   MOCK_METHOD(void, ByVal, (MoveOnly), ());
22   MOCK_METHOD(void, TwiceByRef, (MoveOnly&, MoveOnly&), ());
23   MOCK_METHOD(bool, ByValWithReturnValue, (MoveOnly), ());
24 };
25 }  // namespace
26 
TEST(GmockMoveSupportTest,MoveArgByRef)27 TEST(GmockMoveSupportTest, MoveArgByRef) {
28   MoveOnly result;
29 
30   MockFoo foo;
31   EXPECT_CALL(foo, ByRef).WillOnce(MoveArg(&result));
32   MoveOnly arg = std::make_unique<int>(123);
33   foo.ByRef(arg);
34 
35   EXPECT_THAT(result, Pointee(123));
36 }
37 
TEST(GmockMoveSupportTest,MoveArgByVal)38 TEST(GmockMoveSupportTest, MoveArgByVal) {
39   MoveOnly result;
40 
41   MockFoo foo;
42   EXPECT_CALL(foo, ByVal).WillOnce(MoveArg(&result));
43   foo.ByVal(std::make_unique<int>(456));
44 
45   EXPECT_THAT(result, Pointee(456));
46 }
47 
TEST(GmockMoveSupportTest,MoveArgsTwiceByRef)48 TEST(GmockMoveSupportTest, MoveArgsTwiceByRef) {
49   MoveOnly result1;
50   MoveOnly result2;
51 
52   MockFoo foo;
53   EXPECT_CALL(foo, TwiceByRef)
54       .WillOnce(DoAll(MoveArg<0>(&result1), MoveArg<1>(&result2)));
55   MoveOnly arg1 = std::make_unique<int>(123);
56   MoveOnly arg2 = std::make_unique<int>(456);
57   foo.TwiceByRef(arg1, arg2);
58 
59   EXPECT_THAT(result1, Pointee(123));
60   EXPECT_THAT(result2, Pointee(456));
61 }
62 
TEST(GmockMoveSupportTest,MoveArgAndReturn)63 TEST(GmockMoveSupportTest, MoveArgAndReturn) {
64   MoveOnly result;
65 
66   MockFoo foo;
67   EXPECT_CALL(foo, ByValWithReturnValue)
68       .WillOnce(MoveArgAndReturn(&result, true));
69   EXPECT_TRUE(foo.ByValWithReturnValue(std::make_unique<int>(123)));
70 
71   EXPECT_THAT(result, Pointee(123));
72 }
73