forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_callback.h.pump
85 lines (68 loc) · 2.21 KB
/
mock_callback.h.pump
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
$$ This is a pump file for generating file templates. Pump is a python
$$ script that is part of the Google Test suite of utilities. Description
$$ can be found here:
$$
$$ https://github.com/google/googletest/blob/master/googletest/docs/PumpManual.md
$$
$$ MAX_ARITY controls the number of arguments that MockCallback supports.
$$ It is choosen to match the number GMock supports.
$var MAX_ARITY = 10
$$
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Analogous to GMock's built-in MockFunction, but for base::Callback instead of
// std::function. It takes the full callback type as a parameter, so that it can
// support both OnceCallback and RepeatingCallback.
//
// Use:
// using FooCallback = base::Callback<int(std::string)>;
//
// TEST(FooTest, RunsCallbackWithBarArgument) {
// base::MockCallback<FooCallback> callback;
// EXPECT_CALL(callback, Run("bar")).WillOnce(Return(1));
// Foo(callback.Get());
// }
//
// Can be used with StrictMock and NiceMock. Caller must ensure that it outlives
// any base::Callback obtained from it.
#ifndef BASE_TEST_MOCK_CALLBACK_H_
#define BASE_TEST_MOCK_CALLBACK_H_
#include "base/bind.h"
#include "base/callback.h"
#include "base/macros.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace base {
// clang-format off
template <typename F>
class MockCallback;
$range i 0..MAX_ARITY
$for i [[
$range j 1..i
$var run_type = [[R($for j, [[A$j]])]]
template <typename R$for j [[, typename A$j]]>
class MockCallback<Callback<$run_type>> {
public:
MockCallback() = default;
MOCK_METHOD$(i)_T(Run, $run_type);
Callback<$run_type> Get() {
return Bind(&MockCallback::Run, Unretained(this));
}
private:
DISALLOW_COPY_AND_ASSIGN(MockCallback);
};
template <typename R$for j [[, typename A$j]]>
class MockCallback<OnceCallback<$run_type>> {
public:
MockCallback() = default;
MOCK_METHOD$(i)_T(Run, $run_type);
OnceCallback<$run_type> Get() {
return BindOnce(&MockCallback::Run, Unretained(this));
}
private:
DISALLOW_COPY_AND_ASSIGN(MockCallback);
};
]]
// clang-format on
} // namespace base
#endif // BASE_TEST_MOCK_CALLBACK_H_