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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// Copyright (c) 2013 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.
#ifndef TESTING_GMOCK_MUTANT_H_
#define TESTING_GMOCK_MUTANT_H_
// The intention of this file is to make possible using GMock actions in
// all of its syntactic beauty.
//
//
// Sample usage with gMock:
//
// struct Mock : public ObjectDelegate {
// MOCK_METHOD2(string, OnRequest(int n, const string& request));
// MOCK_METHOD1(void, OnQuit(int exit_code));
// MOCK_METHOD2(void, LogMessage(int level, const string& message));
//
// string HandleFlowers(const string& reply, int n, const string& request) {
// string result = SStringPrintf("In request of %d %s ", n, request);
// for (int i = 0; i < n; ++i) result.append(reply)
// return result;
// }
//
// void DoLogMessage(int level, const string& message) {
// }
//
// void QuitMessageLoop(int seconds) {
// base::MessageLoop* loop = base::MessageLoop::current();
// loop->PostDelayedTask(FROM_HERE,
// base::MessageLoop::QuitWhenIdleClosure(),
// 1000 * seconds);
// }
// };
//
// Mock mock;
// // Will invoke mock.HandleFlowers("orchids", n, request)
// // "orchids" is a pre-bound argument, and <n> and <request> are call-time
// // arguments - they are not known until the OnRequest mock is invoked.
// EXPECT_CALL(mock, OnRequest(Ge(5), base::StartsWith("flower"))
// .Times(1)
// .WillOnce(Invoke(CreateFunctor(
// &Mock::HandleFlowers, base::Unretained(&mock), string("orchids"))));
//
//
// // No pre-bound arguments, two call-time arguments passed
// // directly to DoLogMessage
// EXPECT_CALL(mock, OnLogMessage(_, _))
// .Times(AnyNumber())
// .WillAlways(Invoke(CreateFunctor(
// &Mock::DoLogMessage, base::Unretained(&mock))));
//
//
// // In this case we have a single pre-bound argument - 3. We ignore
// // all of the arguments of OnQuit.
// EXCEPT_CALL(mock, OnQuit(_))
// .Times(1)
// .WillOnce(InvokeWithoutArgs(CreateFunctor(
// &Mock::QuitMessageLoop, base::Unretained(&mock), 3)));
//
// MessageLoop loop;
// loop.Run();
//
//
// // Here is another example of how we can set an action that invokes
// // method of an object that is not yet created.
// struct Mock : public ObjectDelegate {
// MOCK_METHOD1(void, DemiurgeCreated(Demiurge*));
// MOCK_METHOD2(void, OnRequest(int count, const string&));
//
// void StoreDemiurge(Demiurge* w) {
// demiurge_ = w;
// }
//
// Demiurge* demiurge;
// }
//
// EXPECT_CALL(mock, DemiurgeCreated(_)).Times(1)
// .WillOnce(Invoke(CreateFunctor(
// &Mock::StoreDemiurge, base::Unretained(&mock))));
//
// EXPECT_CALL(mock, OnRequest(_, StrEq("Moby Dick")))
// .Times(AnyNumber())
// .WillAlways(WithArgs<0>(Invoke(CreateFunctor(
// &Demiurge::DecreaseMonsters, base::Unretained(&mock->demiurge_)))));
//
#include "base/bind.h"
namespace testing {
template <typename Signature>
class CallbackToFunctorHelper;
template <typename R, typename... Args>
class CallbackToFunctorHelper<R(Args...)> {
public:
explicit CallbackToFunctorHelper(const base::Callback<R(Args...)>& cb)
: cb_(cb) {}
template <typename... RunArgs>
R operator()(RunArgs&&... args) {
return cb_.Run(std::forward<RunArgs>(args)...);
}
private:
base::Callback<R(Args...)> cb_;
};
template <typename Signature>
CallbackToFunctorHelper<Signature>
CallbackToFunctor(const base::Callback<Signature>& cb) {
return CallbackToFunctorHelper<Signature>(cb);
}
template <typename Functor>
CallbackToFunctorHelper<typename Functor::RunType> CreateFunctor(
Functor functor) {
return CallbackToFunctor(functor);
}
template <typename Functor, typename... BoundArgs>
CallbackToFunctorHelper<base::MakeUnboundRunType<Functor, BoundArgs...>>
CreateFunctor(Functor functor, const BoundArgs&... args) {
return CallbackToFunctor(base::Bind(functor, args...));
}
} // namespace testing
#endif // TESTING_GMOCK_MUTANT_H_
|