Added unit tests for n tests for networkpackage serialization
This commit is contained in:
parent
b1e4e2d836
commit
17d0fd4902
5 changed files with 67 additions and 3 deletions
|
@ -33,6 +33,7 @@ NetworkPackage::NetworkPackage(QString type)
|
||||||
{
|
{
|
||||||
mId = time(NULL);
|
mId = time(NULL);
|
||||||
mType = type;
|
mType = type;
|
||||||
|
mVersion = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
QByteArray NetworkPackage::serialize() const
|
QByteArray NetworkPackage::serialize() const
|
||||||
|
@ -48,7 +49,7 @@ QByteArray NetworkPackage::serialize() const
|
||||||
bool ok;
|
bool ok;
|
||||||
QJson::Serializer serializer;
|
QJson::Serializer serializer;
|
||||||
QByteArray json = serializer.serialize(variant,&ok);
|
QByteArray json = serializer.serialize(variant,&ok);
|
||||||
if (!ok) qDebug() << "D'oh!";
|
if (!ok) qDebug() << "Serialization error:" << serializer.errorMessage();
|
||||||
|
|
||||||
return json;
|
return json;
|
||||||
}
|
}
|
||||||
|
@ -57,7 +58,12 @@ void NetworkPackage::unserialize(QByteArray a, NetworkPackage* np)
|
||||||
{
|
{
|
||||||
//Json -> QVariant
|
//Json -> QVariant
|
||||||
QJson::Parser parser;
|
QJson::Parser parser;
|
||||||
QVariantMap variant = parser.parse(a).toMap();
|
bool ok;
|
||||||
|
QVariantMap variant = parser.parse(a, &ok).toMap();
|
||||||
|
if (!ok) {
|
||||||
|
qDebug() << "Unserialization error:" << parser.errorLine() << parser.errorString();
|
||||||
|
np->setVersion(-1);
|
||||||
|
}
|
||||||
|
|
||||||
//QVariant -> Object
|
//QVariant -> Object
|
||||||
//NetworkPackage np;
|
//NetworkPackage np;
|
||||||
|
|
|
@ -52,7 +52,7 @@ public:
|
||||||
template<typename T> T get(const QString& property, const T& defaultValue = default_arg<T>::get()) const {
|
template<typename T> T get(const QString& property, const T& defaultValue = default_arg<T>::get()) const {
|
||||||
return mBody.value(property,defaultValue).template value<T>(); //Important note: Awesome template syntax is awesome
|
return mBody.value(property,defaultValue).template value<T>(); //Important note: Awesome template syntax is awesome
|
||||||
}
|
}
|
||||||
template<typename T> void set(const QString& property, const T& value) const { return mBody[property].setValue(value); }
|
template<typename T> void set(const QString& property, const T& value) { mBody[property] = value; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void setId(long id) { mId = id; }
|
void setId(long id) { mId = id; }
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
set(kded_kdeconnect_tests_SRCS
|
set(kded_kdeconnect_tests_SRCS
|
||||||
|
../daemon/networkpackage.cpp
|
||||||
backendtests.cpp
|
backendtests.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -8,6 +9,7 @@ target_link_libraries(kded_kdeconnect_tests
|
||||||
${KDE4_KDECORE_LIBS}
|
${KDE4_KDECORE_LIBS}
|
||||||
${KDE4_KDEUI_LIBS}
|
${KDE4_KDEUI_LIBS}
|
||||||
kdnssd
|
kdnssd
|
||||||
|
qjson
|
||||||
${QT_QTTEST_LIBRARY}
|
${QT_QTTEST_LIBRARY}
|
||||||
${QT_QTNETWORK_LIBRARY}
|
${QT_QTNETWORK_LIBRARY}
|
||||||
)
|
)
|
||||||
|
|
|
@ -17,7 +17,10 @@
|
||||||
|
|
||||||
#include "backendtests.h"
|
#include "backendtests.h"
|
||||||
|
|
||||||
|
#include "../daemon/networkpackage.h"
|
||||||
|
|
||||||
#include <qtest_kde.h>
|
#include <qtest_kde.h>
|
||||||
|
#include <QtTest>
|
||||||
|
|
||||||
QTEST_KDEMAIN(BackendTests, NoGUI);
|
QTEST_KDEMAIN(BackendTests, NoGUI);
|
||||||
|
|
||||||
|
@ -26,6 +29,55 @@ void BackendTests::initTestCase()
|
||||||
// Called before the first testfunction is executed
|
// Called before the first testfunction is executed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BackendTests::dummyTest()
|
||||||
|
{
|
||||||
|
QDate date;
|
||||||
|
date.setYMD( 1967, 3, 11 );
|
||||||
|
QVERIFY( date.isValid() );
|
||||||
|
QCOMPARE( date.month(), 3 );
|
||||||
|
QCOMPARE( QDate::longMonthName(date.month()), QString("March") );
|
||||||
|
}
|
||||||
|
|
||||||
|
void BackendTests::networkPackageTest()
|
||||||
|
{
|
||||||
|
NetworkPackage np("com.test");
|
||||||
|
|
||||||
|
np.set("hello","hola");
|
||||||
|
QCOMPARE( (np.get<QString>("hello","bye")) , QString("hola") );
|
||||||
|
|
||||||
|
np.set("hello","");
|
||||||
|
QCOMPARE( (np.get<QString>("hello","bye")) , QString("") );
|
||||||
|
|
||||||
|
np.body().remove("hello");
|
||||||
|
QCOMPARE( (np.get<QString>("hello","bye")) , QString("bye") );
|
||||||
|
|
||||||
|
QByteArray ba = np.serialize();
|
||||||
|
//qDebug() << "Serialized package:" << ba;
|
||||||
|
NetworkPackage np2;
|
||||||
|
NetworkPackage::unserialize(ba,&np2);
|
||||||
|
|
||||||
|
QCOMPARE( np.id(), np2.id() );
|
||||||
|
QCOMPARE( np.type(), np2.type() );
|
||||||
|
QCOMPARE( np.version(), np2.version() );
|
||||||
|
QCOMPARE( np.body(), np2.body() );
|
||||||
|
|
||||||
|
QByteArray json("{ \"id\": 123, \"type\": \"test\", \"body\": { \"testing\": true }, \"version\": 3 }");
|
||||||
|
//qDebug() << json;
|
||||||
|
NetworkPackage::unserialize(json,&np2);
|
||||||
|
QCOMPARE( np2.id(), long(123) );
|
||||||
|
QCOMPARE( np2.version(), 3 );
|
||||||
|
QCOMPARE( (np2.get<bool>("testing")), true );
|
||||||
|
QCOMPARE( (np2.get<bool>("not_testing")), false );
|
||||||
|
QCOMPARE( (np2.get<bool>("not_testing",true)), true );
|
||||||
|
|
||||||
|
//NetworkPackage::unserialize("this is not json",&np2);
|
||||||
|
//QtTest::ignoreMessage(QtSystemMsg, "json_parser - syntax error found, forcing abort, Line 1 Column 0");
|
||||||
|
//QtTest::ignoreMessage(QtDebugMsg, "Unserialization error: 1 \"syntax error, unexpected string\"");
|
||||||
|
//QCOMPARE( np2.version(), -1 );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void BackendTests::cleanupTestCase()
|
void BackendTests::cleanupTestCase()
|
||||||
{
|
{
|
||||||
// Called after the last testfunction was executed
|
// Called after the last testfunction was executed
|
||||||
|
|
|
@ -26,6 +26,10 @@ class BackendTests : public QObject
|
||||||
|
|
||||||
private Q_SLOTS:
|
private Q_SLOTS:
|
||||||
void initTestCase();
|
void initTestCase();
|
||||||
|
|
||||||
|
void dummyTest();
|
||||||
|
void networkPackageTest();
|
||||||
|
|
||||||
void cleanupTestCase();
|
void cleanupTestCase();
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
|
Loading…
Reference in a new issue