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