src: Yearly copyright bump
[quassel.git] / src / test / util / mockedpeer.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #pragma once
22
23 #include "test-util-export.h"
24
25 #include <boost/variant.hpp>
26
27 #include "internalpeer.h"
28 #include "testglobal.h"
29
30 namespace test {
31
32 /// Variant-based wrapper struct for supported protocol messages
33 struct ProtocolMessage;
34
35 TEST_UTIL_EXPORT void PrintTo(const ProtocolMessage&, std::ostream*);
36
37 /**
38  * Mocked peer that can be used in test cases involving SignalProxy.
39  *
40  * This mock class is intended to be used for testing on the abstraction level of Quassel protocol messages; it does not support
41  * mocking e.g. serialization or network connections, or other lower-level implementation details of specific peer types.
42  *
43  * To set expectations, use EXPECT_CALL on the mocked Dispatches() method and the provided protocol message matchers like this:
44  *
45  * @code
46  * EXPECT_CALL(*peer, Dispatches(RpcCall(QByteArray{SIGNAL(sendData(int,QString))}, ElementsAre(42, "Hello"))));
47  * @endcode
48  *
49  * The matchers take matchers themselves for their individual arguments, so you can do things like
50  *
51  * @code
52  * EXPECT_CALL(*peer, Dispatches(RpcCall(_, Contains(42))));
53  * @endcode
54  *
55  * @note For now, MockedPeer only supports SignalProxy message types. It can be extended in the future to support auth messages,
56  *       as well.
57  */
58 class TEST_UTIL_EXPORT MockedPeer : public InternalPeer
59 {
60     Q_OBJECT
61
62 public:
63     MockedPeer(QObject* parent = nullptr);
64     ~MockedPeer() override;
65
66     /// Every message dispatched goes through this mocked method, which can be used with the protocol message machters to define expectations
67     MOCK_METHOD1(Dispatches, void(const ProtocolMessage&));
68
69     using InternalPeer::dispatch;
70     void dispatch(const Protocol::SyncMessage&) override;
71     void dispatch(const Protocol::RpcCall&) override;
72     void dispatch(const Protocol::InitRequest&) override;
73     void dispatch(const Protocol::InitData&) override;
74
75 private:
76     void dispatchInternal(const ProtocolMessage&);
77
78     template<typename T>
79     void realDispatch(const T&);
80
81     friend struct DispatchVisitor;
82 };
83
84 // ---- Matchers for defining protocol message expectations for Dispatches() ---------------------------------------------------------------
85
86 TEST_UTIL_EXPORT ::testing::Matcher<const ProtocolMessage&> SyncMessage(
87         ::testing::Matcher<QByteArray> className,
88         ::testing::Matcher<QString> objectName,
89         ::testing::Matcher<QByteArray> slotName,
90         ::testing::Matcher<QVariantList> params);
91
92 TEST_UTIL_EXPORT ::testing::Matcher<const ProtocolMessage&> RpcCall(
93         ::testing::Matcher<QByteArray> signalName,
94         ::testing::Matcher<QVariantList> params);
95
96 TEST_UTIL_EXPORT ::testing::Matcher<const ProtocolMessage&> InitRequest(
97         ::testing::Matcher<QByteArray> className,
98         ::testing::Matcher<QString> objectName);
99
100 TEST_UTIL_EXPORT ::testing::Matcher<const ProtocolMessage&> InitData(
101         ::testing::Matcher<QByteArray> className,
102         ::testing::Matcher<QString> objectName,
103         ::testing::Matcher<QVariantMap> initData);
104
105 }  // namespace test