1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "mockedpeer.h"
23 #include <boost/any.hpp>
25 using namespace ::testing;
29 // ---- Protocol message wrapper -----------------------------------------------------------------------------------------------------------
31 struct ProtocolMessage
33 // Supported protocol message types. If extended, the various visitors in this file need to be extended too.
35 Protocol::SyncMessage,
37 Protocol::InitRequest,
42 void PrintTo(const ProtocolMessage& msg, std::ostream* os)
44 struct PrintToVisitor : public boost::static_visitor<void>
46 PrintToVisitor(std::ostream* os)
50 void operator()(const Protocol::SyncMessage& syncMessage) const
52 *_os << "SyncMessage{className = " << PrintToString(syncMessage.className)
53 << ", objectName = " << PrintToString(syncMessage.objectName)
54 << ", slotName = " << PrintToString(syncMessage.slotName)
55 << ", params = " << PrintToString(syncMessage.params)
59 void operator()(const Protocol::RpcCall& rpcCall) const
61 *_os << "RpcCall{signalName = " << PrintToString(rpcCall.signalName)
62 << ", params = " << PrintToString(rpcCall.params)
66 void operator()(const Protocol::InitRequest& initRequest) const
68 *_os << "InitRequest{className = " << PrintToString(initRequest.className)
69 << ", objectName = " << PrintToString(initRequest.objectName)
73 void operator()(const Protocol::InitData& initData) const
75 *_os << "InitData{className = " << PrintToString(initData.className)
76 << ", objectName = " << PrintToString(initData.objectName)
77 << ", initData = " << PrintToString(initData.initData)
85 boost::apply_visitor(PrintToVisitor{os}, msg.message);
88 // ---- MockedPeer -------------------------------------------------------------------------------------------------------------------------
90 MockedPeer::MockedPeer(QObject* parent)
91 : InternalPeer(parent)
93 // Default behavior will just delegate to InternalPeer (through the mocked Dispatches() method)
94 ON_CALL(*this, Dispatches(_)).WillByDefault(Invoke(this, &MockedPeer::dispatchInternal));
97 MockedPeer::~MockedPeer() = default;
99 void MockedPeer::dispatch(const Protocol::SyncMessage& msg)
104 void MockedPeer::dispatch(const Protocol::RpcCall& msg)
109 void MockedPeer::dispatch(const Protocol::InitRequest& msg)
114 void MockedPeer::dispatch(const Protocol::InitData& msg)
119 // Unwraps the type before calling the correct overload of realDispatch()
120 struct DispatchVisitor : public boost::static_visitor<void>
122 DispatchVisitor(MockedPeer* mock)
127 void operator()(const T& message) const
129 _mock->realDispatch(message);
135 void MockedPeer::dispatchInternal(const ProtocolMessage& message)
137 boost::apply_visitor(DispatchVisitor{this}, message.message);
141 void MockedPeer::realDispatch(const T& message)
143 InternalPeer::dispatch(message);
146 // ---- Expectations and matchers ----------------------------------------------------------------------------------------------------------
150 struct SyncMessageExpectation
152 Matcher<QByteArray> className;
153 Matcher<QString> objectName;
154 Matcher<QByteArray> slotName;
155 Matcher<QVariantList> params;
158 struct RpcCallExpectation
160 Matcher<QByteArray> signalName;
161 Matcher<QVariantList> params;
164 struct InitRequestExpectation
166 Matcher<QByteArray> className;
167 Matcher<QString> objectName;
170 struct InitDataExpectation
172 Matcher<QByteArray> className;
173 Matcher<QString> objectName;
174 Matcher<QVariantMap> initData;
178 * Generic matcher for protocol messages.
180 * @note The matcher, maybe somewhat surprisingly, always matches; it does, however, add test failures if expectations fail.
181 * This makes for a much better readable output when used in EXPECT_CALL chains, while still letting test cases fail
184 class ProtocolMessageMatcher : public MatcherInterface<const ProtocolMessage&>
188 ProtocolMessageMatcher(T expectation)
189 : _expectation{std::move(expectation)}
193 * Visitor used for matching a particular type of protocol message.
195 * Each supported type requires a corresponding overload for the call operator.
197 struct MatchVisitor : public boost::static_visitor<bool>
199 MatchVisitor(const boost::any& expectation)
200 : _expectation{expectation}
203 bool operator()(const Protocol::SyncMessage& syncMessage) const
205 auto e = boost::any_cast<SyncMessageExpectation>(&_expectation);
207 ADD_FAILURE() << "Did not expect a SyncMessage!";
210 EXPECT_THAT(syncMessage.className, e->className);
211 EXPECT_THAT(syncMessage.objectName, e->objectName);
212 EXPECT_THAT(syncMessage.slotName, e->slotName);
213 EXPECT_THAT(syncMessage.params, e->params);
217 bool operator()(const Protocol::RpcCall& rpcCall) const
219 auto e = boost::any_cast<RpcCallExpectation>(&_expectation);
221 ADD_FAILURE() << "Did not expect an RpcCall!";
224 EXPECT_THAT(rpcCall.signalName, e->signalName);
225 EXPECT_THAT(rpcCall.params, e->params);
229 bool operator()(const Protocol::InitRequest& initRequest) const
231 auto e = boost::any_cast<InitRequestExpectation>(&_expectation);
233 ADD_FAILURE() << "Did not expect an InitRequest!";
236 EXPECT_THAT(initRequest.className, e->className);
237 EXPECT_THAT(initRequest.objectName, e->objectName);
241 bool operator()(const Protocol::InitData& initData) const
243 auto e = boost::any_cast<InitDataExpectation>(&_expectation);
245 ADD_FAILURE() << "Did not expect InitData!";
248 EXPECT_THAT(initData.className, e->className);
249 EXPECT_THAT(initData.objectName, e->objectName);
250 EXPECT_THAT(initData.initData, e->initData);
255 const boost::any& _expectation;
258 bool MatchAndExplain(const ProtocolMessage& protoMsg, MatchResultListener*) const override
260 return boost::apply_visitor(MatchVisitor{_expectation}, protoMsg.message);
263 void DescribeTo(std::ostream* os) const override
265 // This should never be actually called because we always match (but fail sub-expectations if appropriate)
266 *os << "Matcher for protocol messages";
270 boost::any _expectation;
275 // Create matcher instances
277 Matcher<const ProtocolMessage&> SyncMessage(Matcher<QByteArray> className, Matcher<QString> objectName, Matcher<QByteArray> slotName, Matcher<QVariantList> params)
279 return MakeMatcher(new ProtocolMessageMatcher{SyncMessageExpectation{std::move(className), std::move(objectName), std::move(slotName), std::move(params)}});
282 Matcher<const ProtocolMessage&> RpcCall(Matcher<QByteArray> signalName, Matcher<QVariantList> params)
284 return MakeMatcher(new ProtocolMessageMatcher{RpcCallExpectation{std::move(signalName), std::move(params)}});
287 Matcher<const ProtocolMessage&> InitRequest(Matcher<QByteArray> className, Matcher<QString> objectName)
289 return MakeMatcher(new ProtocolMessageMatcher{InitRequestExpectation{std::move(className), std::move(objectName)}});
292 Matcher<const ProtocolMessage&> InitData(Matcher<QByteArray> className, Matcher<QString> objectName, Matcher<QVariantMap> initData)
294 return MakeMatcher(new ProtocolMessageMatcher{InitDataExpectation{std::move(className), std::move(objectName), std::move(initData)}});