2 /* This file is part of QJson
4 * Copyright (C) 2009 Flavio Castelli <flavio.castelli@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include <QtTest/QtTest>
28 #include "serializer.h"
29 #include "qobjecthelper.h"
31 #include <QtCore/QVariant>
33 class TestQObjectHelper: public QObject
37 void testQObject2QVariant();
38 void testQVariant2QObject();
41 using namespace QJson;
43 void TestQObjectHelper::testQObject2QVariant()
45 QString name = QLatin1String("Flavio Castelli");
46 int phoneNumber = 123456;
47 Person::Gender gender = Person::Male;
48 QDate dob (1982, 7, 12);
52 person.setPhoneNumber(phoneNumber);
53 person.setGender(gender);
57 expected[QLatin1String("name")] = QVariant(name);
58 expected[QLatin1String("phoneNumber")] = QVariant(phoneNumber);
59 expected[QLatin1String("gender")] = QVariant(gender);
60 expected[QLatin1String("dob")] = QVariant(dob);
62 QVariantMap result = QObjectHelper::qobject2qvariant(&person);
64 QCOMPARE(result, expected);
67 void TestQObjectHelper::testQVariant2QObject()
70 QString name = QLatin1String("Flavio Castelli");
71 int phoneNumber = 123456;
72 Person::Gender gender = Person::Male;
73 QDate dob (1982, 7, 12);
75 Person expected_person;
76 expected_person.setName(name);
77 expected_person.setPhoneNumber(phoneNumber);
78 expected_person.setGender(gender);
79 expected_person.setDob(dob);
81 QVariantMap variant = QObjectHelper::qobject2qvariant(&expected_person);
83 Serializer serializer;
84 QByteArray json = serializer.serialize(variant);
87 QVariant parsedVariant = parser.parse(json,&ok);
89 QVERIFY(parsedVariant.canConvert(QVariant::Map));
92 QObjectHelper::qvariant2qobject(parsedVariant.toMap(), &person);
94 QCOMPARE(person.name(), name);
95 QCOMPARE(person.phoneNumber(),phoneNumber);
96 QCOMPARE(person.gender(), gender);
97 QCOMPARE(person.dob(), dob);
100 QTEST_MAIN(TestQObjectHelper)
102 #include "testqobjecthelper.moc"
104 #include "moc_testqobjecthelper.cxx"