Initial commit.
This commit is contained in:
5
3rdparty/qjson/tests/qobjecthelper/.gitignore
vendored
Normal file
5
3rdparty/qjson/tests/qobjecthelper/.gitignore
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
Makefile
|
||||
*.o
|
||||
*.moc
|
||||
moc_*
|
||||
qobjecthelper
|
||||
55
3rdparty/qjson/tests/qobjecthelper/CMakeLists.txt
vendored
Normal file
55
3rdparty/qjson/tests/qobjecthelper/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
##### Probably don't want to edit below this line #####
|
||||
|
||||
SET( QT_USE_QTTEST TRUE )
|
||||
|
||||
IF (NOT Qt5Core_FOUND)
|
||||
# Use it
|
||||
INCLUDE( ${QT_USE_FILE} )
|
||||
ENDIF()
|
||||
|
||||
INCLUDE(AddFileDependencies)
|
||||
|
||||
# Include the library include directories, and the current build directory (moc)
|
||||
INCLUDE_DIRECTORIES(
|
||||
../../include
|
||||
${CMAKE_CURRENT_BINARY_DIR}
|
||||
)
|
||||
|
||||
SET (qjson_test_support_SRCS person.cpp)
|
||||
IF (NOT Qt5Core_FOUND)
|
||||
QT4_WRAP_CPP(qjson_test_support_MOC_SRCS person.h)
|
||||
ENDIF()
|
||||
|
||||
ADD_LIBRARY (qjson_test_support STATIC ${qjson_test_support_SRCS}
|
||||
${qjson_test_support_MOC_SRCS})
|
||||
|
||||
SET( UNIT_TESTS
|
||||
testqobjecthelper
|
||||
)
|
||||
|
||||
# Build the tests
|
||||
FOREACH(test ${UNIT_TESTS})
|
||||
MESSAGE(STATUS "Building ${test}")
|
||||
IF (NOT Qt5Core_FOUND)
|
||||
QT4_WRAP_CPP(MOC_SOURCE ${test}.cpp)
|
||||
ENDIF()
|
||||
ADD_EXECUTABLE(
|
||||
${test}
|
||||
${test}.cpp
|
||||
)
|
||||
|
||||
ADD_FILE_DEPENDENCIES(${test}.cpp ${MOC_SOURCE})
|
||||
TARGET_LINK_LIBRARIES(
|
||||
${test}
|
||||
${QT_LIBRARIES}
|
||||
${TEST_LIBRARIES}
|
||||
qjson${QJSON_SUFFIX}
|
||||
qjson_test_support
|
||||
)
|
||||
if (QJSON_TEST_OUTPUT STREQUAL "xml")
|
||||
# produce XML output
|
||||
add_test( ${test} ${test} -xml -o ${test}.tml )
|
||||
else (QJSON_TEST_OUTPUT STREQUAL "xml")
|
||||
add_test( ${test} ${test} )
|
||||
endif (QJSON_TEST_OUTPUT STREQUAL "xml")
|
||||
ENDFOREACH()
|
||||
75
3rdparty/qjson/tests/qobjecthelper/person.cpp
vendored
Normal file
75
3rdparty/qjson/tests/qobjecthelper/person.cpp
vendored
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "person.h"
|
||||
|
||||
Person::Person(QObject* parent)
|
||||
: QObject(parent),
|
||||
m_name(),
|
||||
m_phoneNumber(0),
|
||||
m_gender(Female),
|
||||
m_luckyNumber(0)
|
||||
{
|
||||
}
|
||||
|
||||
Person::~Person()
|
||||
{
|
||||
}
|
||||
|
||||
QString Person::name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
void Person::setName(const QString& name)
|
||||
{
|
||||
m_name = name;
|
||||
}
|
||||
|
||||
int Person::phoneNumber() const
|
||||
{
|
||||
return m_phoneNumber;
|
||||
}
|
||||
|
||||
void Person::setPhoneNumber(const int phoneNumber)
|
||||
{
|
||||
m_phoneNumber = phoneNumber;
|
||||
}
|
||||
|
||||
void Person::setGender(Gender gender)
|
||||
{
|
||||
m_gender = gender;
|
||||
}
|
||||
|
||||
Person::Gender Person::gender() const
|
||||
{
|
||||
return m_gender;
|
||||
}
|
||||
|
||||
QDate Person::dob() const
|
||||
{
|
||||
return m_dob;
|
||||
}
|
||||
|
||||
void Person::setDob(const QDate& dob)
|
||||
{
|
||||
m_dob = dob;
|
||||
}
|
||||
|
||||
QVariant Person::customField() const
|
||||
{
|
||||
return m_customField;
|
||||
}
|
||||
|
||||
void Person::setCustomField(const QVariant& customField)
|
||||
{
|
||||
m_customField = customField;
|
||||
}
|
||||
|
||||
const quint16 Person::luckyNumber() const
|
||||
{
|
||||
return m_luckyNumber;
|
||||
}
|
||||
|
||||
void Person::setLuckyNumber(const quint16 luckyNumber)
|
||||
{
|
||||
m_luckyNumber = luckyNumber;
|
||||
}
|
||||
|
||||
73
3rdparty/qjson/tests/qobjecthelper/person.h
vendored
Normal file
73
3rdparty/qjson/tests/qobjecthelper/person.h
vendored
Normal file
@@ -0,0 +1,73 @@
|
||||
/* This file is part of qjson
|
||||
*
|
||||
* Copyright (C) 2009 Till Adam <adam@kde.org>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library; see the file COPYING.LIB. If not, write to
|
||||
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#ifndef PERSON_H
|
||||
#define PERSON_H
|
||||
|
||||
#include <QtCore/QDate>
|
||||
#include <QtCore/QtGlobal>
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QVariant>
|
||||
|
||||
class Person : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QString name READ name WRITE setName)
|
||||
Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber)
|
||||
Q_PROPERTY(Gender gender READ gender WRITE setGender)
|
||||
Q_PROPERTY(QDate dob READ dob WRITE setDob)
|
||||
Q_PROPERTY(QVariant customField READ customField WRITE setCustomField)
|
||||
Q_PROPERTY(quint16 luckyNumber READ luckyNumber WRITE setLuckyNumber)
|
||||
Q_ENUMS(Gender)
|
||||
|
||||
public:
|
||||
Person(QObject* parent = 0);
|
||||
~Person();
|
||||
|
||||
QString name() const;
|
||||
void setName(const QString& name);
|
||||
|
||||
int phoneNumber() const;
|
||||
void setPhoneNumber(const int phoneNumber);
|
||||
|
||||
enum Gender {Male, Female};
|
||||
void setGender(Gender gender);
|
||||
Gender gender() const;
|
||||
|
||||
QDate dob() const;
|
||||
void setDob(const QDate& dob);
|
||||
|
||||
QVariant customField() const;
|
||||
void setCustomField(const QVariant& customField);
|
||||
|
||||
const quint16 luckyNumber() const;
|
||||
void setLuckyNumber(const quint16 luckyNumber);
|
||||
|
||||
private:
|
||||
QString m_name;
|
||||
int m_phoneNumber;
|
||||
Gender m_gender;
|
||||
QDate m_dob;
|
||||
QVariant m_customField;
|
||||
quint16 m_luckyNumber;
|
||||
};
|
||||
|
||||
#endif
|
||||
126
3rdparty/qjson/tests/qobjecthelper/testqobjecthelper.cpp
vendored
Normal file
126
3rdparty/qjson/tests/qobjecthelper/testqobjecthelper.cpp
vendored
Normal file
@@ -0,0 +1,126 @@
|
||||
|
||||
/* This file is part of QJson
|
||||
*
|
||||
* Copyright (C) 2009 Flavio Castelli <flavio.castelli@gmail.com>
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License version 2.1, as published by the Free Software Foundation.
|
||||
*
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this library; see the file COPYING.LIB. If not, write to
|
||||
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
* Boston, MA 02110-1301, USA.
|
||||
*/
|
||||
|
||||
#include <limits>
|
||||
|
||||
#include <QtCore/QVariant>
|
||||
#include <QtCore/QVariantList>
|
||||
|
||||
#include <QtTest/QtTest>
|
||||
|
||||
#include <QJson/Parser>
|
||||
#include <QJson/Serializer>
|
||||
#include <QJson/QObjectHelper>
|
||||
|
||||
#include "person.h"
|
||||
|
||||
class TestQObjectHelper: public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
private slots:
|
||||
void testQObject2QVariant();
|
||||
void testQVariant2QObject();
|
||||
};
|
||||
|
||||
using namespace QJson;
|
||||
|
||||
void TestQObjectHelper::testQObject2QVariant()
|
||||
{
|
||||
QString name = QLatin1String("Flavio Castelli");
|
||||
int phoneNumber = 123456;
|
||||
Person::Gender gender = Person::Male;
|
||||
QDate dob (1982, 7, 12);
|
||||
QVariantList nicknames;
|
||||
nicknames << QLatin1String("nickname1") << QLatin1String("nickname2");
|
||||
quint16 luckyNumber = 123;
|
||||
|
||||
Person person;
|
||||
person.setName(name);
|
||||
person.setPhoneNumber(phoneNumber);
|
||||
person.setGender(gender);
|
||||
person.setDob(dob);
|
||||
person.setCustomField(nicknames);
|
||||
person.setLuckyNumber(luckyNumber);
|
||||
|
||||
QVariantMap expected;
|
||||
expected[QLatin1String("name")] = QVariant(name);
|
||||
expected[QLatin1String("phoneNumber")] = QVariant(phoneNumber);
|
||||
expected[QLatin1String("gender")] = QVariant(gender);
|
||||
expected[QLatin1String("dob")] = QVariant(dob);
|
||||
expected[QLatin1String("customField")] = nicknames;
|
||||
expected[QLatin1String("luckyNumber")] = luckyNumber;
|
||||
|
||||
QVariantMap result = QObjectHelper::qobject2qvariant(&person);
|
||||
QCOMPARE(result, expected);
|
||||
}
|
||||
|
||||
void TestQObjectHelper::testQVariant2QObject()
|
||||
{
|
||||
bool ok;
|
||||
QString name = QLatin1String("Flavio Castelli");
|
||||
int phoneNumber = 123456;
|
||||
Person::Gender gender = Person::Male;
|
||||
QDate dob (1982, 7, 12);
|
||||
QVariantList nicknames;
|
||||
nicknames << QLatin1String("nickname1") << QLatin1String("nickname2");
|
||||
quint16 luckyNumber = 123;
|
||||
|
||||
Person expected_person;
|
||||
expected_person.setName(name);
|
||||
expected_person.setPhoneNumber(phoneNumber);
|
||||
expected_person.setGender(gender);
|
||||
expected_person.setDob(dob);
|
||||
expected_person.setCustomField(nicknames);
|
||||
expected_person.setLuckyNumber(luckyNumber);
|
||||
|
||||
QVariantMap variant = QObjectHelper::qobject2qvariant(&expected_person);
|
||||
|
||||
Serializer serializer;
|
||||
QByteArray json = serializer.serialize(variant, &ok);
|
||||
qDebug() << "json is" << json;
|
||||
QVERIFY(ok);
|
||||
|
||||
Parser parser;
|
||||
QVariant parsedVariant = parser.parse(json,&ok);
|
||||
QVERIFY(ok);
|
||||
qDebug() << parsedVariant;
|
||||
QVERIFY(parsedVariant.canConvert(QVariant::Map));
|
||||
|
||||
Person person;
|
||||
QCOMPARE(Person::Female, person.gender());
|
||||
QObjectHelper::qvariant2qobject(parsedVariant.toMap(), &person);
|
||||
|
||||
QCOMPARE(person.name(), name);
|
||||
QCOMPARE(person.phoneNumber(), phoneNumber);
|
||||
QCOMPARE(person.gender(), gender);
|
||||
QCOMPARE(person.dob(), dob);
|
||||
QCOMPARE(person.customField(), QVariant(nicknames));
|
||||
QCOMPARE(person.luckyNumber(), luckyNumber);
|
||||
}
|
||||
|
||||
#if QT_VERSION < QT_VERSION_CHECK(5,0,0)
|
||||
// using Qt4 rather then Qt5
|
||||
QTEST_MAIN(TestQObjectHelper)
|
||||
#include "moc_testqobjecthelper.cxx"
|
||||
#else
|
||||
QTEST_GUILESS_MAIN(TestQObjectHelper)
|
||||
#include "testqobjecthelper.moc"
|
||||
#endif
|
||||
Reference in New Issue
Block a user