5ac7eb21c7b0b5cfef23a73ab01db42b86c59110
[buliscores] / qjson / tests / qobjecthelper / testqobjecthelper.cpp
1
2 /* This file is part of QJson
3  *
4  * Copyright (C) 2009 Flavio Castelli <flavio.castelli@gmail.com>
5  *
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.
10  *
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.
15  *
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.
20  */
21
22 #include <limits>
23
24 #include <QtTest/QtTest>
25
26 #include "parser.h"
27 #include "person.h"
28 #include "serializer.h"
29 #include "qobjecthelper.h"
30
31 #include <QtCore/QVariant>
32
33 class TestQObjectHelper: public QObject
34 {
35   Q_OBJECT
36   private slots:
37     void testQObject2QVariant();
38     void testQVariant2QObject();
39 };
40
41 using namespace QJson;
42
43 void TestQObjectHelper::testQObject2QVariant()
44 {
45   QString name = QLatin1String("Flavio Castelli");
46   int phoneNumber = 123456;
47   Person::Gender gender = Person::Male;
48   QDate dob (1982, 7, 12);
49
50   Person person;
51   person.setName(name);
52   person.setPhoneNumber(phoneNumber);
53   person.setGender(gender);
54   person.setDob(dob);
55
56   QVariantMap expected;
57   expected[QLatin1String("name")] = QVariant(name);
58   expected[QLatin1String("phoneNumber")] = QVariant(phoneNumber);
59   expected[QLatin1String("gender")] = QVariant(gender);
60   expected[QLatin1String("dob")] = QVariant(dob);
61
62   QVariantMap result = QObjectHelper::qobject2qvariant(&person);
63
64   QCOMPARE(result, expected);
65 }
66
67 void TestQObjectHelper::testQVariant2QObject()
68 {
69   bool ok;
70   QString name = QLatin1String("Flavio Castelli");
71   int phoneNumber = 123456;
72   Person::Gender gender = Person::Male;
73   QDate dob (1982, 7, 12);
74
75   Person expected_person;
76   expected_person.setName(name);
77   expected_person.setPhoneNumber(phoneNumber);
78   expected_person.setGender(gender);
79   expected_person.setDob(dob);
80
81   QVariantMap variant = QObjectHelper::qobject2qvariant(&expected_person);
82
83   Serializer serializer;
84   QByteArray json = serializer.serialize(variant);
85
86   Parser parser;
87   QVariant parsedVariant = parser.parse(json,&ok);
88   QVERIFY(ok);
89   QVERIFY(parsedVariant.canConvert(QVariant::Map));
90
91   Person person;
92   QObjectHelper::qvariant2qobject(parsedVariant.toMap(), &person);
93
94   QCOMPARE(person.name(), name);
95   QCOMPARE(person.phoneNumber(),phoneNumber);
96   QCOMPARE(person.gender(), gender);
97   QCOMPARE(person.dob(), dob);
98 }
99
100 QTEST_MAIN(TestQObjectHelper)
101 #ifdef QMAKE_BUILD
102   #include "testqobjecthelper.moc"
103 #else
104   #include "moc_testqobjecthelper.cxx"
105 #endif