2009-02-08 16:35:56 +01:00
|
|
|
/**
|
|
|
|
* This file is part of the KDE project
|
|
|
|
* Copyright (C) 2008 Kevin Ottens <ervin@kde.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library 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 <QtCore/QObject>
|
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
2009-04-25 16:06:25 +02:00
|
|
|
#include <QtJolie/Value>
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
#include "testhelpers.h"
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
using namespace Jolie;
|
|
|
|
|
|
|
|
class TestValue : public QObject
|
2009-02-08 16:35:56 +01:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private slots:
|
|
|
|
void shouldHandleInvalids()
|
|
|
|
{
|
2009-04-25 15:50:35 +02:00
|
|
|
Value v;
|
2009-02-08 16:35:56 +01:00
|
|
|
|
|
|
|
QCOMPARE(v.toInt(), 0);
|
|
|
|
QCOMPARE(v.toDouble(), 0.0);
|
2009-05-30 10:44:34 +02:00
|
|
|
QCOMPARE(v.toByteArray(), QByteArray());
|
2009-02-08 16:35:56 +01:00
|
|
|
|
|
|
|
QVERIFY(!v.isValid());
|
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
QVERIFY(!v.isByteArray());
|
2009-02-08 16:35:56 +01:00
|
|
|
QVERIFY(!v.isInt());
|
|
|
|
QVERIFY(!v.isDouble());
|
|
|
|
}
|
|
|
|
|
|
|
|
void shouldRespectIntValues()
|
|
|
|
{
|
2009-04-25 15:50:35 +02:00
|
|
|
Value v1(42), v2;
|
2009-02-08 16:35:56 +01:00
|
|
|
|
|
|
|
QCOMPARE(v1.toInt(), 42);
|
|
|
|
QCOMPARE(v2.toInt(), 0);
|
|
|
|
|
|
|
|
QVERIFY(v1.isInt());
|
|
|
|
QVERIFY(!v2.isInt());
|
|
|
|
|
|
|
|
v2 = v1;
|
|
|
|
|
|
|
|
QCOMPARE(v2.toInt(), 42);
|
|
|
|
QVERIFY(v2.isInt());
|
|
|
|
|
|
|
|
QCOMPARE(v2.toDouble(), 0.0);
|
2009-05-30 10:44:34 +02:00
|
|
|
QCOMPARE(v2.toByteArray(), QByteArray());
|
2009-02-08 16:35:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void shouldRespectDoubleValues()
|
|
|
|
{
|
2009-04-25 15:50:35 +02:00
|
|
|
Value v1(0.42), v2;
|
2009-02-08 16:35:56 +01:00
|
|
|
|
|
|
|
QCOMPARE(v1.toDouble(), 0.42);
|
|
|
|
QCOMPARE(v2.toDouble(), 0.0);
|
|
|
|
|
|
|
|
QVERIFY(v1.isDouble());
|
|
|
|
QVERIFY(!v2.isDouble());
|
|
|
|
|
|
|
|
v2 = v1;
|
|
|
|
|
|
|
|
QCOMPARE(v2.toDouble(), 0.42);
|
|
|
|
QVERIFY(v2.isDouble());
|
|
|
|
|
|
|
|
QCOMPARE(v2.toInt(), 0);
|
2009-05-30 10:44:34 +02:00
|
|
|
QCOMPARE(v2.toByteArray(), QByteArray());
|
2009-02-08 16:35:56 +01:00
|
|
|
}
|
|
|
|
|
2009-06-29 17:29:52 +02:00
|
|
|
void shouldAutoConvertByteArrayValues()
|
2009-02-08 16:35:56 +01:00
|
|
|
{
|
2009-04-25 15:50:35 +02:00
|
|
|
Value v1("42"), v2;
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
QCOMPARE(v1.toByteArray(), QByteArray("42"));
|
|
|
|
QCOMPARE(v2.toByteArray(), QByteArray());
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
QVERIFY(v1.isByteArray());
|
|
|
|
QVERIFY(!v2.isByteArray());
|
2009-02-08 16:35:56 +01:00
|
|
|
|
|
|
|
v2 = v1;
|
|
|
|
|
2009-05-30 10:44:34 +02:00
|
|
|
QCOMPARE(v2.toByteArray(), QByteArray("42"));
|
|
|
|
QVERIFY(v2.isByteArray());
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-06-29 17:29:52 +02:00
|
|
|
QCOMPARE(v2.toInt(), 42);
|
|
|
|
QCOMPARE(v2.toDouble(), 42.0);
|
2009-02-08 16:35:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void shouldHandleChildren()
|
|
|
|
{
|
2009-04-25 15:50:35 +02:00
|
|
|
Value v;
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
v.children("first") << Value(7) << Value(8);
|
|
|
|
v.children("second") << Value(42);
|
2009-02-08 16:35:56 +01:00
|
|
|
|
|
|
|
QCOMPARE(v.children("second").size(), 1);
|
|
|
|
QCOMPARE(v.children("second")[0].toInt(), 42);
|
|
|
|
|
|
|
|
QCOMPARE(v.children("first").size(), 2);
|
|
|
|
QCOMPARE(v.children("first")[0].toInt(), 7);
|
|
|
|
QCOMPARE(v.children("first")[1].toInt(), 8);
|
|
|
|
|
|
|
|
QCOMPARE(v.children("bwaaaaah!").size(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void shouldBeSerializable_data()
|
|
|
|
{
|
2009-04-25 15:50:35 +02:00
|
|
|
QTest::addColumn<Value>("original");
|
2009-02-08 16:35:56 +01:00
|
|
|
QTest::addColumn<QByteArray>("serialized");
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
QTest::newRow("empty value") << Value()
|
2009-02-08 16:35:56 +01:00
|
|
|
<< QByteArray::fromHex("0000000000");
|
2009-04-25 15:50:35 +02:00
|
|
|
QTest::newRow("double value") << Value(0.42)
|
2009-02-08 16:35:56 +01:00
|
|
|
<< QByteArray::fromHex("033FDAE147AE147AE100000000");
|
2009-04-25 15:50:35 +02:00
|
|
|
QTest::newRow("int value") << Value(42)
|
2009-02-08 16:35:56 +01:00
|
|
|
<< QByteArray::fromHex("020000002A00000000");
|
2009-04-25 15:50:35 +02:00
|
|
|
QTest::newRow("string value") << Value("foo")
|
2009-02-08 16:35:56 +01:00
|
|
|
<< QByteArray::fromHex("0100000003")+QByteArray("foo")
|
|
|
|
+ QByteArray::fromHex("00000000");
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
Value complex("complex");
|
|
|
|
complex.children("foo") << Value(42);
|
|
|
|
complex.children("bar") << Value(0.42);
|
2009-02-08 16:35:56 +01:00
|
|
|
QTest::newRow("complex value") << complex
|
|
|
|
<< QByteArray::fromHex("0100000007")+QByteArray("complex")
|
|
|
|
+ QByteArray::fromHex("00000002") // two children
|
|
|
|
+ QByteArray::fromHex("00000003")+QByteArray("bar")
|
|
|
|
+ QByteArray::fromHex("00000001") // one value
|
|
|
|
+ QByteArray::fromHex("033FDAE147AE147AE100000000")
|
|
|
|
+ QByteArray::fromHex("00000003")+QByteArray("foo")
|
|
|
|
+ QByteArray::fromHex("00000001") // one value
|
|
|
|
+ QByteArray::fromHex("020000002A00000000");
|
|
|
|
}
|
|
|
|
|
|
|
|
void shouldBeSerializable()
|
|
|
|
{
|
|
|
|
QBuffer buffer;
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
QFETCH(Value, original);
|
2009-02-08 16:35:56 +01:00
|
|
|
QFETCH(QByteArray, serialized);
|
2009-04-25 15:50:35 +02:00
|
|
|
Value result;
|
2009-02-08 16:35:56 +01:00
|
|
|
|
|
|
|
buffer.open(QIODevice::WriteOnly);
|
|
|
|
sodepWrite(buffer, original);
|
|
|
|
buffer.close();
|
|
|
|
|
|
|
|
buffer.open(QIODevice::ReadOnly);
|
|
|
|
result = sodepReadValue(buffer);
|
|
|
|
buffer.close();
|
|
|
|
|
|
|
|
sodepCompare(result, original);
|
|
|
|
QCOMPARE(buffer.data(), serialized);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
QTEST_MAIN(TestValue)
|
2009-02-08 16:35:56 +01:00
|
|
|
|
2009-04-25 15:50:35 +02:00
|
|
|
#include "testvalue.moc"
|