Apply tidy

This commit is contained in:
Martchus 2017-07-02 22:08:30 +02:00
parent 0949ebae68
commit da8f6f7353
3 changed files with 55 additions and 60 deletions

View File

@ -1629,7 +1629,8 @@ void SyncthingConnection::readDevPauseResume()
emit devicePauseTriggered(devIds);
}
break;
} default:
}
default:
emit error(tr("Unable to request device pause/resume: ") + reply->errorString(), SyncthingErrorCategory::SpecificRequest, reply->error());
}
}
@ -1649,7 +1650,8 @@ void SyncthingConnection::readDirPauseResume()
emit directoryPauseTriggered(dirIds);
}
break;
} default:
}
default:
emit error(tr("Unable to request directory pause/resume: ") + reply->errorString(), SyncthingErrorCategory::SpecificRequest, reply->error());
}
}

View File

@ -9,8 +9,8 @@
#include <QDir>
#include <QJsonArray>
#include <QStringBuilder>
#include <QJsonDocument>
#include <QStringBuilder>
using namespace std;
using namespace Data;
@ -46,10 +46,10 @@ public:
void tearDown();
private:
template <typename Action, typename... SignalInfos>
void waitForConnection(Action action, int timeout, const SignalInfos &... signalInfos);
template<typename Signal, typename Handler = function<void(void)>>
SignalInfo<Signal, Handler> connectionSignal(Signal signal, const Handler &handler = function<void(void)>(), bool *correctSignalEmitted = nullptr);
template <typename Action, typename... SignalInfos> void waitForConnection(Action action, int timeout, const SignalInfos &... signalInfos);
template <typename Signal, typename Handler = function<void(void)>>
SignalInfo<Signal, Handler> connectionSignal(
Signal signal, const Handler &handler = function<void(void)>(), bool *correctSignalEmitted = nullptr);
static void (SyncthingConnection::*defaultConnect())(void);
static void (SyncthingConnection::*defaultReconnect())(void);
static void (SyncthingConnection::*defaultDisconnect())(void);
@ -87,7 +87,7 @@ void ConnectionTests::setUp()
// log configuration change
QObject::connect(&m_connection, &SyncthingConnection::newConfig,
[] (const QJsonObject &config) { cerr << " - New config: " << QJsonDocument(config).toJson(QJsonDocument::Compact).data() << endl; });
[](const QJsonObject &config) { cerr << " - New config: " << QJsonDocument(config).toJson(QJsonDocument::Compact).data() << endl; });
}
/*!
@ -106,12 +106,12 @@ void ConnectionTests::tearDown()
* \brief Variant of waitForSignal() where sender is the connection and the action is a method of the connection.
*/
template <typename Action, typename... SignalInfos>
void ConnectionTests::waitForConnection(Action action, int timeout, const SignalInfos &...signalInfos)
void ConnectionTests::waitForConnection(Action action, int timeout, const SignalInfos &... signalInfos)
{
waitForSignals(bind(action, &m_connection), timeout, signalInfos...);
}
template<typename Signal, typename Handler>
template <typename Signal, typename Handler>
SignalInfo<Signal, Handler> ConnectionTests::connectionSignal(Signal signal, const Handler &handler, bool *correctSignalEmitted)
{
return SignalInfo<Signal, Handler>(&m_connection, signal, handler, correctSignalEmitted);
@ -119,17 +119,17 @@ SignalInfo<Signal, Handler> ConnectionTests::connectionSignal(Signal signal, con
void (SyncthingConnection::*ConnectionTests::defaultConnect())(void)
{
return static_cast<void(SyncthingConnection::*)(void)>(&SyncthingConnection::connect);
return static_cast<void (SyncthingConnection::*)(void)>(&SyncthingConnection::connect);
}
void (SyncthingConnection::*ConnectionTests::defaultReconnect())(void)
{
return static_cast<void(SyncthingConnection::*)(void)>(&SyncthingConnection::reconnect);
return static_cast<void (SyncthingConnection::*)(void)>(&SyncthingConnection::reconnect);
}
void (SyncthingConnection::*ConnectionTests::defaultDisconnect())(void)
{
return static_cast<void(SyncthingConnection::*)(void)>(&SyncthingConnection::disconnect);
return static_cast<void (SyncthingConnection::*)(void)>(&SyncthingConnection::disconnect);
}
/*!
@ -179,9 +179,8 @@ void ConnectionTests::testConnection()
void ConnectionTests::testErrorCases()
{
cerr << "\n - Error handling in case of insufficient conficuration ..." << endl;
waitForConnection(defaultConnect(), 1000,
connectionSignal(&SyncthingConnection::error, [](const QString &errorMessage) {
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Connection configuration is insufficient."), errorMessage);
waitForConnection(defaultConnect(), 1000, connectionSignal(&SyncthingConnection::error, [](const QString &errorMessage) {
CPPUNIT_ASSERT_EQUAL(QStringLiteral("Connection configuration is insufficient."), errorMessage);
}));
cerr << "\n - Error handling in case of inavailability and wrong API key ..." << endl;
@ -291,13 +290,12 @@ void ConnectionTests::testResumingAllDevices()
devResumed = true;
}
};
const function<void(const QStringList &)> devResumedTriggeredHandler = [this](const QStringList &devIds) {
CPPUNIT_ASSERT_EQUAL(m_connection.deviceIds(), devIds);
};
const function<void(const QStringList &)> devResumedTriggeredHandler
= [this](const QStringList &devIds) { CPPUNIT_ASSERT_EQUAL(m_connection.deviceIds(), devIds); };
const auto newDevsConnection = handleNewDevices(devResumedHandler, &devResumed);
waitForConnection(&SyncthingConnection::resumeAllDevs, 1000,
connectionSignal(&SyncthingConnection::devStatusChanged, devResumedHandler, &devResumed),
connectionSignal(&SyncthingConnection::deviceResumeTriggered, devResumedTriggeredHandler));
connectionSignal(&SyncthingConnection::devStatusChanged, devResumedHandler, &devResumed),
connectionSignal(&SyncthingConnection::deviceResumeTriggered, devResumedTriggeredHandler));
CPPUNIT_ASSERT(devResumed);
CPPUNIT_ASSERT_EQUAL_MESSAGE("not paused anymore", QStringLiteral("connected"), m_connection.statusText());
for (const QJsonValue &devValue : m_connection.m_rawConfig.value(QStringLiteral("devices")).toArray()) {
@ -317,7 +315,8 @@ void ConnectionTests::testResumingDirectory()
}
};
const auto newDirsConnection = handleNewDirs(dirResumedHandler, &dirResumed);
waitForConnection(&SyncthingConnection::resumeAllDirs, 1000, connectionSignal(&SyncthingConnection::dirStatusChanged, dirResumedHandler, &dirResumed));
waitForConnection(
&SyncthingConnection::resumeAllDirs, 1000, connectionSignal(&SyncthingConnection::dirStatusChanged, dirResumedHandler, &dirResumed));
CPPUNIT_ASSERT(dirResumed);
CPPUNIT_ASSERT_EQUAL_MESSAGE("still 2 dirs present", 2_st, m_connection.dirInfo().size());
CPPUNIT_ASSERT_EQUAL_MESSAGE("still not paused anymore", QStringLiteral("connected"), m_connection.statusText());
@ -334,7 +333,7 @@ void ConnectionTests::testPausingDirectory()
};
const auto newDirsConnection = handleNewDirs(dirPausedHandler, &dirPaused);
waitForSignals(bind(&SyncthingConnection::pauseDirectories, &m_connection, QStringList({ QStringLiteral("test1") })), 1000,
connectionSignal(&SyncthingConnection::dirStatusChanged, dirPausedHandler, &dirPaused));
connectionSignal(&SyncthingConnection::dirStatusChanged, dirPausedHandler, &dirPaused));
CPPUNIT_ASSERT(dirPaused);
CPPUNIT_ASSERT_EQUAL_MESSAGE("still 2 dirs present", 2_st, m_connection.dirInfo().size());
CPPUNIT_ASSERT_EQUAL_MESSAGE("still not paused anymore", QStringLiteral("connected"), m_connection.statusText());

View File

@ -63,11 +63,10 @@ inline void noop()
/*!
* \brief The TemporaryConnection class disconnects a QMetaObject::Connection when being destroyed.
*/
class TemporaryConnection
{
class TemporaryConnection {
public:
TemporaryConnection(QMetaObject::Connection connection) :
m_connection(connection)
TemporaryConnection(QMetaObject::Connection connection)
: m_connection(connection)
{
}
@ -86,9 +85,7 @@ private:
* SignalInfo objects are meant to be passed to waitForSignals() so the function can keep track
* of emitted signals.
*/
template<typename Signal, typename Handler>
class SignalInfo
{
template <typename Signal, typename Handler> class SignalInfo {
public:
/*!
* \brief Constructs a SignalInfo with handler and automatically connects the handler to the signal.
@ -98,23 +95,22 @@ public:
* \param correctSignalEmitted Specifies whether the correct signal has been emitted. Should be set in \a handler to indicate that the emitted signal is actually the one
* the test is waiting for (and not just one which has been emitted as side-effect).
*/
SignalInfo(typename QtPrivate::FunctionPointer<Signal>::Object *sender, Signal signal, const Handler &handler, bool *correctSignalEmitted = nullptr) :
sender(sender),
signal(signal),
correctSignalEmitted(correctSignalEmitted),
signalEmitted(false)
SignalInfo(
typename QtPrivate::FunctionPointer<Signal>::Object *sender, Signal signal, const Handler &handler, bool *correctSignalEmitted = nullptr)
: sender(sender)
, signal(signal)
, correctSignalEmitted(correctSignalEmitted)
, signalEmitted(false)
{
// register handler if specified
if(handler) {
if (handler) {
handlerConnection = QObject::connect(sender, signal, sender, handler, Qt::DirectConnection);
if (!handlerConnection) {
CPPUNIT_FAIL(argsToString("Unable to connect signal ", signalName().data(), " to handler"));
}
}
// register own handler to detect whether signal has been emitted
emittedConnection = QObject::connect(sender, signal, sender, [this] {
signalEmitted = true;
}, Qt::DirectConnection);
emittedConnection = QObject::connect(sender, signal, sender, [this] { signalEmitted = true; }, Qt::DirectConnection);
if (!emittedConnection) {
CPPUNIT_FAIL(argsToString("Unable to connect signal ", signalName().data(), " to check for signal emmitation"));
}
@ -122,14 +118,14 @@ public:
SignalInfo(const SignalInfo &other) = delete;
SignalInfo(SignalInfo &&other) :
sender(other.sender),
signal(other.signal),
handlerConnection(other.handlerConnection),
emittedConnection(other.emittedConnection),
loopConnection(other.loopConnection),
correctSignalEmitted(other.correctSignalEmitted),
signalEmitted(other.signalEmitted)
SignalInfo(SignalInfo &&other)
: sender(other.sender)
, signal(other.signal)
, handlerConnection(other.handlerConnection)
, emittedConnection(other.emittedConnection)
, loopConnection(other.loopConnection)
, correctSignalEmitted(other.correctSignalEmitted)
, signalEmitted(other.signalEmitted)
{
other.handlerConnection = other.emittedConnection = other.loopConnection = QMetaObject::Connection();
}
@ -186,8 +182,9 @@ private:
/*!
* \brief Constructs a new SignalInfo.
*/
template<typename Signal, typename Handler>
inline SignalInfo<Signal, Handler> signalInfo(typename QtPrivate::FunctionPointer<Signal>::Object *sender, Signal signal, const Handler &handler = Handler(), bool *correctSignalEmitted = nullptr)
template <typename Signal, typename Handler>
inline SignalInfo<Signal, Handler> signalInfo(typename QtPrivate::FunctionPointer<Signal>::Object *sender, Signal signal,
const Handler &handler = Handler(), bool *correctSignalEmitted = nullptr)
{
return SignalInfo<Signal, Handler>(sender, signal, handler, correctSignalEmitted);
}
@ -195,8 +192,7 @@ inline SignalInfo<Signal, Handler> signalInfo(typename QtPrivate::FunctionPointe
/*!
* \brief Connects the specified signal infos the \a loop via SignalInfo::connectToLoop().
*/
template <typename SignalInfo>
inline void connectSignalInfosToLoop(QEventLoop *loop, const SignalInfo &signalInfo)
template <typename SignalInfo> inline void connectSignalInfosToLoop(QEventLoop *loop, const SignalInfo &signalInfo)
{
signalInfo.connectToLoop(loop);
}
@ -214,8 +210,7 @@ inline void connectSignalInfosToLoop(QEventLoop *loop, const SignalInfo &firstSi
/*!
* \brief Checks whether all specified signals have been emitted.
*/
template <typename SignalInfo>
inline bool checkWhetherAllSignalsEmitted(const SignalInfo &signalInfo)
template <typename SignalInfo> inline bool checkWhetherAllSignalsEmitted(const SignalInfo &signalInfo)
{
return signalInfo;
}
@ -232,8 +227,7 @@ inline bool checkWhetherAllSignalsEmitted(const SignalInfo &firstSignalInfo, con
/*!
* \brief Returns the names of all specified signal infos which haven't been emitted yet as comma-separated string.
*/
template <typename SignalInfo>
inline QByteArray failedSignalNames(const SignalInfo &signalInfo)
template <typename SignalInfo> inline QByteArray failedSignalNames(const SignalInfo &signalInfo)
{
return !signalInfo ? signalInfo.signalName() : QByteArray();
}
@ -245,7 +239,7 @@ template <typename SignalInfo, typename... SignalInfos>
inline QByteArray failedSignalNames(const SignalInfo &firstSignalInfo, const SignalInfos &... remainingSignalInfos)
{
const QByteArray firstSignalName = failedSignalNames(firstSignalInfo);
if(!firstSignalName.isEmpty()) {
if (!firstSignalName.isEmpty()) {
return firstSignalName + ", " + failedSignalNames(remainingSignalInfos...);
} else {
return failedSignalNames(remainingSignalInfos...);
@ -260,8 +254,7 @@ inline QByteArray failedSignalNames(const SignalInfo &firstSignalInfo, const Sig
* \throws Fails if not all signals have been emitted in at least \a timeout milliseconds or when at least one of the
* required connections can not be established.
*/
template <typename Action, typename... SignalInfos>
void waitForSignals(Action action, int timeout, const SignalInfos &... signalInfos)
template <typename Action, typename... SignalInfos> void waitForSignals(Action action, int timeout, const SignalInfos &... signalInfos)
{
// use loop for waiting
QEventLoop loop;
@ -273,7 +266,7 @@ void waitForSignals(Action action, int timeout, const SignalInfos &... signalInf
action();
// no reason to enter event loop when all signals have been emitted directly
if(checkWhetherAllSignalsEmitted(signalInfos...)) {
if (checkWhetherAllSignalsEmitted(signalInfos...)) {
return;
}
@ -294,7 +287,8 @@ void waitForSignals(Action action, int timeout, const SignalInfos &... signalInf
// check whether a timeout occured
if (!allSignalsEmitted && timeout && !timer.isActive()) {
CPPUNIT_FAIL(argsToString("Signal(s) ", failedSignalNames(signalInfos...).data(), " has/have not emmitted within at least ", timeout, " ms."));
CPPUNIT_FAIL(
argsToString("Signal(s) ", failedSignalNames(signalInfos...).data(), " has/have not emmitted within at least ", timeout, " ms."));
}
}