smf/smfservermodule/util/qjson/tests/testqobjecthelper.cpp
changeset 7 be09cf1f39dd
equal deleted inserted replaced
6:c39a6cfd1fb9 7:be09cf1f39dd
       
     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 "person.h"
       
    27 #include "qobjecthelper.h"
       
    28 
       
    29 #include <QtCore/QVariant>
       
    30 
       
    31 class TestQObjectHelper: public QObject
       
    32 {
       
    33   Q_OBJECT
       
    34   private slots:
       
    35     void testQObject2QVariant();
       
    36     void testQVariant2QObject();
       
    37 };
       
    38 
       
    39 Q_DECLARE_METATYPE(QVariant)
       
    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   QString name = QLatin1String("Flavio Castelli");
       
    70   int phoneNumber = 123456;
       
    71   Person::Gender gender = Person::Male;
       
    72   QDate dob (1982, 7, 12);
       
    73 
       
    74   Person person;
       
    75 
       
    76   QVariantMap variant;
       
    77   variant[QLatin1String("name")] = QVariant(name);
       
    78   variant[QLatin1String("phoneNumber")] = QVariant(phoneNumber);
       
    79   variant[QLatin1String("gender")] = QVariant(gender);
       
    80   variant[QLatin1String("dob")] = QVariant(dob);
       
    81 
       
    82   QObjectHelper::qvariant2qobject(variant, &person);
       
    83 
       
    84   QCOMPARE(person.name(), name);
       
    85   QCOMPARE(person.phoneNumber(),phoneNumber);
       
    86   QCOMPARE(person.gender(), gender);
       
    87   QCOMPARE(person.dob(), dob);
       
    88 }
       
    89 
       
    90 QTEST_MAIN(TestQObjectHelper)
       
    91 #include "moc_testqobjecthelper.cxx"