initial commit

This commit is contained in:
mikhail "synzr" 2025-12-25 01:37:49 +05:00
commit 9d20827c46
2469 changed files with 470994 additions and 0 deletions

44
third-party/JsonQt/tests/CMakeLists.txt vendored Normal file
View file

@ -0,0 +1,44 @@
SET(
TESTS
JsonRpc
JsonRpcAdaptor
JsonToProperties
JsonToVariant
VariantToJson
)
##### Probably don't want to edit below this line #####
# Find Qt4
FIND_PACKAGE( Qt4 REQUIRED )
# Pick modules
SET( QT_DONT_USE_QTGUI TRUE )
SET( QT_USE_QTTEST TRUE )
# Use it
INCLUDE( ${QT_USE_FILE} )
# Include the library include directories, and the current build directory (moc)
INCLUDE_DIRECTORIES(
../lib
${CMAKE_CURRENT_BINARY_DIR}
)
# Build the tests
INCLUDE(AddFileDependencies)
FOREACH(test ${TESTS})
MESSAGE(STATUS "Building ${test}")
QT4_WRAP_CPP(MOC_SOURCE ${test}.cpp)
ADD_EXECUTABLE(
${test}
${test}.cpp
)
ADD_FILE_DEPENDENCIES(${test}.cpp ${MOC_SOURCE})
TARGET_LINK_LIBRARIES(
${test}
${QT_LIBRARIES}
JsonQt
)
ENDFOREACH()

300
third-party/JsonQt/tests/JsonRpc.cpp vendored Normal file
View file

@ -0,0 +1,300 @@
#include "JsonRpc.h"
#include "JsonToVariant.h"
#include <QtTest/QtTest>
#include <QSignalSpy>
/// Check that JSON a gets error code b
#define ERROR_TEST(a, b) \
JsonQt::JsonRpc t; \
QSignalSpy jsonSpy(&t, SIGNAL(sendJson(const QString&))); \
t.processJson(a); \
\
QCOMPARE(jsonSpy.count(), 1); \
\
QString json(jsonSpy.first().first().toString()); \
QVariantMap response = JsonQt::JsonToVariant::parse(json).toMap(); \
\
QVERIFY(response.contains("error")); \
QCOMPARE(response.value("error").type(), QVariant::Map); \
\
QVariantMap error = response.value("error").toMap(); \
QVERIFY(error.contains("code")); \
QVERIFY(error.contains("message")); \
\
QCOMPARE(error.value("code").type(), QVariant::Int); \
QCOMPARE(error.value("code").toInt(), static_cast<int>(b));
/// Get a QVariantMap from spy
#define GET_DATA() \
QCOMPARE(spy.count(), 1); \
QCOMPARE(spy.first().first().type(), QVariant::String); \
QVariantMap data(JsonQt::JsonToVariant::parse(spy.first().first().toString()).toMap());
/// Setup client and server JsonRpc instances
#define SETUP_CLIENT_SERVER() \
JsonQt::JsonRpc client; \
JsonQt::JsonRpc server; \
\
connect( \
&client, SIGNAL(sendJson(const QString&)), \
&server, SLOT(processJson(const QString&)) \
);
Q_DECLARE_METATYPE(QVariant);
class JsonRpc : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
qRegisterMetaType<QVariant>("QVariant");
}
void testInvalidJson()
{
ERROR_TEST("{", JsonQt::JsonRpc::InvalidJson);
}
void testEmptyJsonObject()
{
ERROR_TEST("{}", JsonQt::JsonRpc::InvalidJsonRpc);
}
void testSimpleNotificationReceived()
{
JsonQt::JsonRpc t;
QSignalSpy spy(&t, SIGNAL(notificationReceived(const QString&, const QVariant&)));
t.processJson(
"{"
"\"jsonrpc\": \"2.0\","
"\"method\": \"ping\""
"}"
);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.first().first().toString(), QString("ping"));
}
void testSimpleRequestReceived()
{
JsonQt::JsonRpc t;
QSignalSpy requestSpy(&t, SIGNAL(requestReceived(const QVariant&, const QString&, const QVariant&)));
t.processJson(
"{"
"\"jsonrpc\": \"2.0\","
"\"method\": \"ping\","
"\"id\": 9001"
"}"
);
QCOMPARE(requestSpy.count(), 1);
// method
QCOMPARE(requestSpy.first().value(1).toString(), QString("ping"));
// id
QVariant id = requestSpy.first().value(0).value<QVariant>();
QCOMPARE(id.type(), QVariant::Int);
QCOMPARE(id.toInt(), 9001);
}
void testErrorReceived()
{
JsonQt::JsonRpc t;
QSignalSpy spy(&t, SIGNAL(errorReceived(const QVariant&, int, const QString&, const QVariant&)));
t.processJson(
"{"
"\"jsonrpc\": \"2.0\","
"\"error\":"
"{"
"\"code\": 1234,"
"\"message\": \"foo\","
"\"data\": \"bar\""
"},"
"\"id\": 9001"
"}"
);
QCOMPARE(spy.count(), 1);
// id
QVariant id = spy.first().first().value<QVariant>();
QCOMPARE(id.type(), QVariant::Int);
QCOMPARE(id.toInt(), 9001);
// code
QCOMPARE(spy.first().value(1).type(), QVariant::Int);
QCOMPARE(spy.first().value(1).toInt(), 1234);
// message
QCOMPARE(spy.first().value(2).type(), QVariant::String);
QCOMPARE(spy.first().value(2).toString(), QString("foo"));
// data
QVariant data = spy.first().value(3).value<QVariant>();
QCOMPARE(data.type(), QVariant::String);
QCOMPARE(data.toString(), QString("bar"));
}
void testJsonRpcVersion()
{
QString testString =
"{"
"\"jsonrpc\": \"9001\","
"\"method\": \"ping\""
"}";
ERROR_TEST(testString, JsonQt::JsonRpc::InvalidJsonRpc);
}
void testComplexNotificationReceived()
{
JsonQt::JsonRpc t;
QSignalSpy spy(&t, SIGNAL(notificationReceived(const QString&, const QVariant&)));
t.processJson(
"{"
"\"jsonrpc\": \"2.0\","
"\"method\": \"ping\","
"\"params\":"
"{"
"\"foo\": \"bar\","
"\"123\": 456"
"}"
"}"
);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.first().first().toString(), QString("ping"));
QVariantMap parameters = spy.first().value(1).value<QVariant>().toMap();
QVERIFY(parameters.contains("foo"));
QCOMPARE(parameters.value("foo").type(), QVariant::String);
QCOMPARE(parameters.value("foo").toString(), QString("bar"));
QVERIFY(parameters.contains("123"));
QCOMPARE(parameters.value("123").type(), QVariant::Int);
QCOMPARE(parameters.value("123").toInt(), 456);
}
void testSendingSimpleNotification()
{
JsonQt::JsonRpc t;
QSignalSpy spy(&t, SIGNAL(sendJson(const QString&)));
t.sendNotification("foo");
GET_DATA();
QCOMPARE(data.value("method").toString(), QString("foo"));
QVERIFY(!data.contains("id"));
QVERIFY(!data.contains("params"));
}
void testSendingSimpleRequest()
{
JsonQt::JsonRpc t;
QSignalSpy spy(&t, SIGNAL(sendJson(const QString&)));
t.sendRequest(QVariant(123), "foo");
GET_DATA();
QCOMPARE(data.value("method").toString(), QString("foo"));
QCOMPARE(data.value("id").toInt(), 123);
QVERIFY(!data.contains("params"));
}
void testSendingResponse()
{
JsonQt::JsonRpc t;
QSignalSpy spy(&t, SIGNAL(sendJson(const QString&)));
t.sendResponse(QVariant(123), "foo");
GET_DATA();
QCOMPARE(data.value("result").toString(), QString("foo"));
QCOMPARE(data.value("id").toInt(), 123);
}
void testSendingError()
{
JsonQt::JsonRpc t;
QSignalSpy spy(&t, SIGNAL(sendJson(const QString&)));
t.sendError(QVariant(123), 456, "foo", QVariant(789));
GET_DATA();
QCOMPARE(data.value("id").toInt(), 123);
QCOMPARE(data.value("error").type(), QVariant::Map);
QVariantMap error = data.value("error").toMap();
QCOMPARE(error.value("code").toInt(), 456);
QCOMPARE(error.value("message").toString(), QString("foo"));
QCOMPARE(error.value("data").toInt(), 789);
}
void testSendingComplexNotification()
{
JsonQt::JsonRpc t;
QSignalSpy spy(&t, SIGNAL(sendJson(const QString&)));
QVariantMap params;
params["bar"] = "baz";
t.sendNotification("foo", params);
GET_DATA();
QCOMPARE(data.value("method").toString(), QString("foo"));
QVERIFY(!data.contains("id"));
QCOMPARE(data.value("params").toMap(), params);
}
void testNotificationBothEnds()
{
SETUP_CLIENT_SERVER();
QSignalSpy spy(&server, SIGNAL(notificationReceived(const QString&, const QVariant&)));
QVariantMap params;
params["bar"] = "baz";
client.sendNotification("foo", params);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.first().value(0).toString(), QString("foo"));
QCOMPARE(spy.first().value(1).value<QVariant>().toMap(), params);
}
void testRequestBothEnds()
{
SETUP_CLIENT_SERVER();
QSignalSpy spy(&server, SIGNAL(requestReceived(const QVariant&, const QString&, const QVariant&)));
QVariantMap params;
params["bar"] = "baz";
client.sendRequest(123, "foo", params);
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.first().value(0).value<QVariant>(), QVariant::fromValue<QVariant>(123));
QCOMPARE(spy.first().value(1).toString(), QString("foo"));
QCOMPARE(spy.first().value(2).value<QVariant>().toMap(), params);
}
void testResponseBothEnds()
{
SETUP_CLIENT_SERVER();
QSignalSpy spy(&server, SIGNAL(responseReceived(const QVariant&, const QVariant&)));
client.sendResponse(123, "foo");
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.first().value(0).value<QVariant>(), QVariant::fromValue<QVariant>(123));
QCOMPARE(spy.first().value(1).value<QVariant>().toString(), QString("foo"));
}
void testErrorBothEnds()
{
SETUP_CLIENT_SERVER();
QSignalSpy spy(&server, SIGNAL(errorReceived(const QVariant&, int, const QString&, const QVariant&)));
client.sendError(123, 456, "789", "foo");
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.first().value(0).value<QVariant>(), QVariant::fromValue<QVariant>(123));
QCOMPARE(spy.first().value(1).toInt(), 456);
QCOMPARE(spy.first().value(2).toString(), QString("789"));
QCOMPARE(spy.first().value(3).value<QVariant>().toString(), QString("foo"));
}
void testJsonErrorBothEnds()
{
SETUP_CLIENT_SERVER();
QSignalSpy spy(&server, SIGNAL(errorReceived(const QVariant&, int, const QString&, const QVariant&)));
client.processJson("{");
QCOMPARE(spy.count(), 1);
QCOMPARE(spy.first().value(1).toInt(), static_cast<int>(JsonQt::JsonRpc::InvalidJson));
}
};
QTEST_MAIN(JsonRpc);
#include "moc_JsonRpc.cxx"

View file

@ -0,0 +1,258 @@
#include "JsonRpcAdaptor.h"
#include "JsonRpc.h"
#include <QtTest/QtTest>
Q_DECLARE_METATYPE(QVariant);
class TestObject : public QObject
{
Q_OBJECT;
Q_CLASSINFO("JsonQt-RPC-name", "TestName");
Q_CLASSINFO("JsonQt-RPC-id", "urn:data:test");
Q_CLASSINFO("JsonQt-RPC-version", "9000.001");
Q_CLASSINFO("JsonQt-RPC-summary", "Ponies");
public:
TestObject(QObject* parent) : QObject(parent) {}
public slots:
void functionOne(const QString& foo)
{
emit functionOneCalled(foo);
}
QString functionTwo()
{
emit functionTwoCalled();
return "Mary had a little lamb.";
}
void functionThree(int one, bool two, QVariantList three, QVariantMap four)
{
emit functionThreeCalled(one, two, three, four);
}
void functionFour(const QString& foo, const QString& bar)
{
emit functionFourCalled(foo, bar);
}
signals:
void functionOneCalled(const QString& foo);
void functionTwoCalled();
void functionThreeCalled(int, bool, QVariantList, QVariantMap);
void functionFourCalled(QString, QString);
};
class JsonRpcAdaptor : public QObject
{
Q_OBJECT
private slots:
void initTestCase()
{
qRegisterMetaType<QVariant>("QVariant");
m_testObject = new TestObject(this);
m_adaptor = new JsonQt::JsonRpcAdaptor(m_testObject, this);
m_rpc = new JsonQt::JsonRpc(this);
connect(
m_rpc, SIGNAL(sendJson(const QString&)),
m_adaptor, SLOT(processJson(const QString&))
);
connect(
m_adaptor, SIGNAL(sendJson(const QString&)),
m_rpc, SLOT(processJson(const QString&))
);
}
void testFunctionOne()
{
QSignalSpy testObjectSpy(m_testObject, SIGNAL(functionOneCalled(const QString&)));
QSignalSpy rpcSpy(m_rpc, SIGNAL(responseReceived(const QVariant&, const QVariant&)));
QVariantMap parameters;
parameters["foo"] = "bar";
m_rpc->sendRequest(42, "functionOne", parameters);
QCOMPARE(testObjectSpy.count(), 1);
QCOMPARE(testObjectSpy.first().first().toString(), QString("bar"));
QCOMPARE(rpcSpy.count(), 1);
QCOMPARE(rpcSpy.first().value(0).value<QVariant>(), QVariant(42));
QCOMPARE(rpcSpy.first().value(1).value<QVariant>(), QVariant());
}
void testFunctionFour()
{
QSignalSpy testObjectSpy(m_testObject, SIGNAL(functionFourCalled(QString, QString)));
QVariantMap parameters;
parameters["foo"] = "bar";
parameters["bar"] = "baz";
m_rpc->sendRequest(123, "functionFour", parameters);
QCOMPARE(testObjectSpy.count(), 1);
QCOMPARE(testObjectSpy.first().value(0).toString(), QString("bar"));
QCOMPARE(testObjectSpy.first().value(1).toString(), QString("baz"));
}
void testFunctionTwo()
{
QSignalSpy testObjectSpy(m_testObject, SIGNAL(functionTwoCalled()));
QSignalSpy rpcSpy(m_rpc, SIGNAL(responseReceived(const QVariant&, const QVariant&)));
m_rpc->sendRequest(1337, "functionTwo");
QCOMPARE(testObjectSpy.count(), 1);
QCOMPARE(rpcSpy.count(), 1);
QCOMPARE(rpcSpy.first().value(0).value<QVariant>(), QVariant(1337));
QCOMPARE(rpcSpy.first().value(1).value<QVariant>().toString(), QString("Mary had a little lamb."));
}
void testFunctionThree()
{
QSignalSpy testObjectSpy(m_testObject, SIGNAL(functionThreeCalled(int, bool, QVariantList, QVariantMap)));
QSignalSpy rpcSpy(m_rpc, SIGNAL(responseReceived(const QVariant&, const QVariant&)));
QVariantList listParameter;
QVariantMap nestedMap;
nestedMap["foo"] = "bar";
listParameter.append(nestedMap);
listParameter.append("baz");
QVariantList parameters;
parameters.append(123);
parameters.append(true);
parameters.append(listParameter);
parameters.append(nestedMap);
m_rpc->sendRequest(42, "functionThree", parameters);
QCOMPARE(testObjectSpy.count(), 1);
QVariantList result = testObjectSpy.first();
QCOMPARE(result.value(0), QVariant(123));
QCOMPARE(result.value(1), QVariant(true));
QCOMPARE(result.value(2), QVariant(listParameter));
QCOMPARE(result.value(3), QVariant(nestedMap));
QCOMPARE(rpcSpy.count(), 1);
QCOMPARE(rpcSpy.first().value(0).value<QVariant>(), QVariant(42));
QCOMPARE(rpcSpy.first().value(1).value<QVariant>(), QVariant());
}
void testInvalidFunction()
{
QSignalSpy rpcSpy(m_rpc, SIGNAL(errorReceived(QVariant, int, QString, QVariant)));
m_rpc->sendRequest("123", "DOES NOT EXIST");
QCOMPARE(rpcSpy.count(), 1);
QCOMPARE(rpcSpy.first().value(0).value<QVariant>(), QVariant("123"));
QCOMPARE(rpcSpy.first().value(1), QVariant(JsonQt::JsonRpc::MethodNotFound));
}
void testBadArgumentCount()
{
QSignalSpy rpcSpy(m_rpc, SIGNAL(errorReceived(QVariant, int, QString, QVariant)));
m_rpc->sendRequest("123", "functionOne");
QCOMPARE(rpcSpy.count(), 1);
QCOMPARE(rpcSpy.first().value(0).value<QVariant>(), QVariant("123"));
QCOMPARE(rpcSpy.first().value(1), QVariant(JsonQt::JsonRpc::BadParameters));
}
void testWrongArgumentTypes()
{
QSignalSpy rpcSpy(m_rpc, SIGNAL(errorReceived(QVariant, int, QString, QVariant)));
QVariantMap parameters;
parameters["foo"] = 123;
m_rpc->sendRequest("123", "functionOne", parameters);
QCOMPARE(rpcSpy.count(), 1);
QCOMPARE(rpcSpy.first().value(0).value<QVariant>(), QVariant("123"));
QCOMPARE(rpcSpy.first().value(1), QVariant(JsonQt::JsonRpc::BadParameters));
}
void testIntropection()
{
QSignalSpy rpcSpy(m_rpc, SIGNAL(responseReceived(const QVariant&, const QVariant&)));
m_rpc->sendRequest(QVariant(), "system.describe");
QCOMPARE(rpcSpy.count(), 1);
QVariantMap introspectionData = rpcSpy.first().value(1).value<QVariant>().toMap();
QVERIFY(!introspectionData.isEmpty());
// Service data
QCOMPARE(introspectionData.value("sdversion").toString(), QString("1.0")); // Service Description version
QCOMPARE(introspectionData.value("name").toString(), QString("TestName"));
QCOMPARE(introspectionData.value("id").toString(), QString("urn:data:test"));
QCOMPARE(introspectionData.value("version").toString(), QString("9000.001"));
QCOMPARE(introspectionData.value("summary").toString(), QString("Ponies"));
QVERIFY(!introspectionData.contains("help"));
QVERIFY(introspectionData.contains("procs"));
// Method data
QVariantList methods = introspectionData.value("procs").toList();
QCOMPARE(methods.count(), 4);
QVariantMap functionOne = methods.takeFirst().toMap();
QVariantMap functionTwo = methods.takeFirst().toMap();
QVariantMap functionThree = methods.takeFirst().toMap();
QVariantMap functionFour = methods.takeFirst().toMap();
QVariantList params;
QVariantMap param;
// functionOne
QCOMPARE(functionOne.value("name").toString(), QString("functionOne"));
QCOMPARE(functionOne.value("return").toString(), QString("nil"));
params = functionOne.value("params").toList();
QCOMPARE(params.count(), 1);
param = params.first().toMap();
QCOMPARE(param.value("name").toString(), QString("foo"));
QCOMPARE(param.value("type").toString(), QString("str"));
// functionTwo
QCOMPARE(functionTwo.value("name").toString(), QString("functionTwo"));
QCOMPARE(functionTwo.value("return").toString(), QString("str"));
params = functionTwo.value("params").toList();
QCOMPARE(params.count(), 0);
// functionThree
QCOMPARE(functionThree.value("name").toString(), QString("functionThree"));
QCOMPARE(functionThree.value("return").toString(), QString("nil"));
params = functionThree.value("params").toList();
QCOMPARE(params.count(), 4);
param = params.takeFirst().toMap();
QCOMPARE(param.value("name").toString(), QString("one"));
QCOMPARE(param.value("type").toString(), QString("num"));
param = params.takeFirst().toMap();
QCOMPARE(param.value("name").toString(), QString("two"));
QCOMPARE(param.value("type").toString(), QString("bit"));
param = params.takeFirst().toMap();
QCOMPARE(param.value("name").toString(), QString("three"));
QCOMPARE(param.value("type").toString(), QString("arr"));
param = params.takeFirst().toMap();
QCOMPARE(param.value("name").toString(), QString("four"));
QCOMPARE(param.value("type").toString(), QString("obj"));
// functionFour
QCOMPARE(functionFour.value("name").toString(), QString("functionFour"));
QCOMPARE(functionFour.value("return").toString(), QString("nil"));
params = functionFour.value("params").toList();
QCOMPARE(params.count(), 2);
param = params.takeFirst().toMap();
QCOMPARE(param.value("name").toString(), QString("foo"));
QCOMPARE(param.value("type").toString(), QString("str"));
param = params.takeFirst().toMap();
QCOMPARE(param.value("name").toString(), QString("bar"));
QCOMPARE(param.value("type").toString(), QString("str"));
}
private:
JsonQt::JsonRpc* m_rpc;
JsonQt::JsonRpcAdaptor* m_adaptor;
TestObject* m_testObject;
};
QTEST_MAIN(JsonRpcAdaptor);
#include "moc_JsonRpcAdaptor.cxx"

View file

@ -0,0 +1,46 @@
/* LICENSE NOTICE
Copyright (c) 2008, Frederick Emmott <mail@fredemmott.co.uk>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "JsonToProperties.h"
#include <QtTest/QtTest>
// This test assumes that JsonToVariant works correctly.
class JsonToProperties : public QObject
{
Q_OBJECT;
private slots:
void initTestCase()
{
JsonQt::JsonToProperties::parse(
"{"
"\"objectName\": \"My JSON Properties Test Object\","
"\"END\" : [\"OF\", \"TEST\"]"
"}",
this
);
}
void testObjectName()
{
QCOMPARE(
this->objectName(),
QString("My JSON Properties Test Object")
);
}
};
QTEST_MAIN(JsonToProperties);
#include "moc_JsonToProperties.cxx"

View file

@ -0,0 +1,166 @@
/* LICENSE NOTICE
Copyright (c) 2008, Frederick Emmott <mail@fredemmott.co.uk>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "JsonToVariant.h"
#include <QtTest/QtTest>
class JsonToVariant : public QObject
{
Q_OBJECT;
private slots:
void initTestCase()
{
// Variable use of whitespace intentional
QVariant data = JsonQt::JsonToVariant::parse(
"{"
"\"MyString\": \"foo\","
"\"MyInt\" :123 ,"
"\"MyBigNum\" : 42e10 , "
"\"MyLongNum\" : 8589934592,"
"\"MyFraction\" : 2.718,"
"\"MyArray\" : [1, \"2\", {\"3\": 4}],"
"\"MyObject\" : {\"a\" : 1, \"b\" : 2 },"
"\"MyEmptyObject\" : { },"
"\"MyBool\": true,"
"\"END\" : [\"OF\", \"TEST\"]"
"}"
);
QCOMPARE(data.type(), QVariant::Map);
m_parsed = data.toMap();
}
void testStringWithEscapedForwardSlash()
{
QVariant data = JsonQt::JsonToVariant::parse("\"\\/\"");
QCOMPARE(data.type(), QVariant::String);
QCOMPARE(data.toString(), QString("/"));
}
void testMulti()
{
QList<QVariantMap> parsed = JsonQt::JsonToVariant::multiParse(
"{"
"\"foo\": \"bar\""
"}"
"{"
"\"x\": \"y\""
"}"
);
QCOMPARE(parsed.count(), 2);
QCOMPARE(parsed.at(0).value("foo"), QVariant("bar"));
QCOMPARE(parsed.at(1).value("x"), QVariant("y"));
}
void testEmpty()
{
bool thrown = false;
try
{
JsonQt::JsonToVariant::parse("");
}
catch(JsonQt::ParseException e)
{
thrown = true;
}
QVERIFY(thrown);
}
void testString()
{
QVariant data = m_parsed["MyString"];
QCOMPARE(data.type(), QVariant::String);
QCOMPARE(data.toString(), QString("foo"));
QCOMPARE(data, JsonQt::JsonToVariant::parse("\"foo\""));
}
void testInt()
{
QVariant data = m_parsed["MyInt"];
QCOMPARE(data.type(), QVariant::Int);
QCOMPARE(data.toInt(), 123);
QCOMPARE(data, JsonQt::JsonToVariant::parse("123"));
}
void testBigNum()
{
QVariant data = m_parsed["MyBigNum"];
QCOMPARE(data.type(), QVariant::Double);
QCOMPARE(data.toDouble(), 42e10);
QCOMPARE(data, JsonQt::JsonToVariant::parse("42e10"));
}
void testLongNum()
{
QVariant data = m_parsed["MyLongNum"];
QCOMPARE(data.type(), QVariant::LongLong);
QCOMPARE(data.value<qint64>(), Q_INT64_C(8589934592));
QCOMPARE(data, JsonQt::JsonToVariant::parse("8589934592"));
}
void testFraction()
{
QVariant data = m_parsed["MyFraction"];
QCOMPARE(data.type(), QVariant::Double);
QCOMPARE(data.toDouble(), double(2.718));
QCOMPARE(data, JsonQt::JsonToVariant::parse("2.718"));
}
void testArray()
{
QVariant root = m_parsed["MyArray"];
QCOMPARE(root.type(), QVariant::List);
QVariantList rootData = root.toList();
QCOMPARE(rootData[0].type(), QVariant::Int);
QCOMPARE(rootData[0].toInt(), 1);
QCOMPARE(rootData[1].type(), QVariant::String);
QCOMPARE(rootData[1].toString(), QString("2"));
QCOMPARE(rootData[2].type(), QVariant::Map);
QVariantMap childData = rootData[2].toMap();
QCOMPARE(childData["3"].type(), QVariant::Int);
QCOMPARE(childData["3"].toInt(), 4);
QCOMPARE(root, JsonQt::JsonToVariant::parse("[1, \"2\", {\"3\": 4}]"));
}
void testObject()
{
QVariant root = m_parsed["MyObject"];
QCOMPARE(root.type(), QVariant::Map);
QVariantMap rootData = root.toMap();
QCOMPARE(rootData["a"].toInt(), 1);
QCOMPARE(rootData["b"].toInt(), 2);
QCOMPARE(root, JsonQt::JsonToVariant::parse("{\"a\" : 1, \"b\" : 2 }"));
}
void testEmptyObject()
{
QVariant root = m_parsed["MyEmptyObject"];
QCOMPARE(root.type(), QVariant::Map);
QCOMPARE(root.value<QVariantMap>().size(), 0);
QCOMPARE(root, JsonQt::JsonToVariant::parse("{}"));
}
private:
QVariantMap m_parsed;
};
QTEST_MAIN(JsonToVariant);
#include "moc_JsonToVariant.cxx"

View file

@ -0,0 +1,46 @@
#include "VariantToJson.h"
#include "JsonToVariant.h"
#include <QtTest/QtTest>
class VariantToJson : public QObject
{
Q_OBJECT
private slots:
void testFlat()
{
QVariantMap map;
map["string_foo"] = "foo";
map["bool_true"] = true;
map["int_42"] = 42;
map["double_pi"] = 3.14159;
QString json(JsonQt::VariantToJson::parse(map));
QCOMPARE(map, JsonQt::JsonToVariant::parse(json).toMap());
}
void testComplex()
{
QVariantMap map;
map["string_foo"] = "foo";
map["bool_true"] = true;
map["int_42"] = 42;
map["double_pi"] = 3.14159;
map["recurse"] = map;
QVariantList list;
list.append("zero");
list.append("one");
map["list"] = list;
QStringList list2;
for(int i = 0; i < 25; i++)
list2.append(QString("element").append(QString::number(i)));
map["list2"] = list2;
QString json(JsonQt::VariantToJson::parse(map));
QCOMPARE(map, JsonQt::JsonToVariant::parse(json).toMap());
}
};
QTEST_MAIN(VariantToJson);
#include "moc_VariantToJson.cxx"