diff options
Diffstat (limited to 'tests/client')
-rw-r--r-- | tests/client/convertertest.cpp | 104 |
1 files changed, 95 insertions, 9 deletions
diff --git a/tests/client/convertertest.cpp b/tests/client/convertertest.cpp index cd791c7..43127a7 100644 --- a/tests/client/convertertest.cpp +++ b/tests/client/convertertest.cpp @@ -28,13 +28,11 @@ extern "C" { #include <QtTest> #include <QDebug> -class QGVariantType : public QObject +class QGVariantType { - Q_OBJECT public: QGVariantType() : type(NULL) {} QGVariantType(const GVariantType *gvtype) : type(gvtype) {} - QGVariantType(const QGVariantType &other) : type(other.type) {} const GVariantType *getType() const { return type; } operator const GVariantType*() const { return type; } @@ -43,14 +41,13 @@ private: }; Q_DECLARE_METATYPE(QGVariantType); -class QGVariant : public QObject +class QGVariant { - Q_OBJECT public: QGVariant() : variant(NULL) {} ~QGVariant() { if (variant) g_variant_unref(variant); } QGVariant(GVariant *gv) : variant(g_variant_ref_sink(gv)) {} - QGVariant(const QGVariant &other) : variant(g_variant_ref_sink(other.variant)) {} + QGVariant(const QGVariant &other) : QGVariant(other.variant) {} GVariant *gvariant() const { return variant; } operator GVariant*() const { return variant; } @@ -104,6 +101,7 @@ private: << "Expected:"<< g_variant_type_peek_string(expected_type); } g_variant_unref(gv); + g_variant_type_free(expected_type); return result; } @@ -130,9 +128,16 @@ private Q_SLOTS: QTest::newRow("UInt64") << QVariant::fromValue<qulonglong>(42) << QGVariantType(G_VARIANT_TYPE_UINT64); QTest::newRow("Double") << QVariant((double)42.42) << QGVariantType(G_VARIANT_TYPE_DOUBLE); QTest::newRow("String") << QVariant(QString("42")) << QGVariantType(G_VARIANT_TYPE_STRING); + QTest::newRow("String List") << QVariant(QStringList({"42", "42"})) << QGVariantType(G_VARIANT_TYPE_STRING_ARRAY); QTest::newRow("ByteArray") << QVariant(QByteArray("42")) << QGVariantType(G_VARIANT_TYPE_BYTESTRING); QTest::newRow("Map") << QVariant(QVariantMap()) << QGVariantType(G_VARIANT_TYPE_VARDICT); + QTest::newRow("Map Filled") << QVariant(QVariantMap({{"fooBar", 0xdeadbeef}})) << QGVariantType(G_VARIANT_TYPE_VARDICT); QTest::newRow("List") << QVariant(QVariantList()) << QGVariantType(G_VARIANT_TYPE_UNIT); + QTest::newRow("List Filled") << QVariant(QVariantList({"fooBar", 0xdeadbeef})) << QGVariantType(G_VARIANT_TYPE("(su)")); + + QVariant result; + result.setValue(QByteArrayList({"42", "53"})); + QTest::newRow("ByteArrayList") << result << QGVariantType(G_VARIANT_TYPE_BYTESTRING_ARRAY); } void testConvertToGVariant() @@ -143,6 +148,23 @@ private Q_SLOTS: QVERIFY(compare(value, expectedType)); } + void testConvertToGVariantAndBack_data() + { + testConvertToGVariant_data(); + } + + void testConvertToGVariantAndBack() + { + QFETCH(QVariant, value); + + GVariant *gv = Converter::toGVariant(value); + QVERIFY(gv != NULL); + + QCOMPARE(Converter::toQVariant(gv), value); + + g_variant_unref(gv); + } + void testTupleConversion() { QVariantList qTuple; @@ -175,9 +197,22 @@ private Q_SLOTS: QTest::addColumn<QVariant>("value"); QTest::addColumn<QString>("schema"); + // convert to byte + QTest::newRow("byte") << QVariant::fromValue<int>(1) << "y"; + // convert to integer QTest::newRow("integer") << QVariant::fromValue<int>(1) << "i"; QTest::newRow("integer from double") << QVariant::fromValue<double>(1.1) << "i"; + QTest::newRow("int16") << QVariant::fromValue<int>(-1) << "n"; + QTest::newRow("uint16") << QVariant::fromValue<int>(1) << "q"; + QTest::newRow("uint32") << QVariant::fromValue<int>(1) << "u"; + QTest::newRow("int64") << QVariant::fromValue<int>(1) << "x"; + QTest::newRow("uint64") << QVariant::fromValue<int>(1) << "t"; + + // convert to variant + QTest::newRow("variant from int") << QVariant::fromValue<int>(1) << "v"; + QTest::newRow("variant from double") << QVariant::fromValue<double>(1.1) << "v"; + QTest::newRow("variant from string") << QVariant::fromValue<QString>("string") << "v"; // convert to bool QTest::newRow("bool") << QVariant::fromValue<bool>(true) << "b"; @@ -242,17 +277,24 @@ private Q_SLOTS: QTest::newRow("UInt64") << QGVariant(g_variant_new_uint64(53)) << (unsigned) QVariant::ULongLong; QTest::newRow("Double") << QGVariant(g_variant_new_double(53.3)) << (unsigned) QVariant::Double; QTest::newRow("String") << QGVariant(g_variant_new_string("53")) << (unsigned) QVariant::String; + QTest::newRow("Byte string") << QGVariant(g_variant_new_bytestring("53")) << (unsigned) QVariant::ByteArray; QTest::newRow("Tuple") << QGVariant(g_variant_new("(si)", "foo", 53)) << (unsigned) QVariant::List; - GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}")); - g_variant_builder_add(builder, "{sv}", "fooo", g_variant_new_variant(g_variant_new_int64(53))); - QTest::newRow("Map") << QGVariant(g_variant_new("a{sv}", builder)) << (unsigned) QVariant::Map; + GVariantBuilder* builder = g_variant_builder_new(G_VARIANT_TYPE_VARDICT); + g_variant_builder_add(builder, "{sv}", "fooo", g_variant_new_int64(53)); + QTest::newRow("Map") << QGVariant(g_variant_builder_end(builder)) << (unsigned) QVariant::Map; g_variant_builder_unref(builder); builder = g_variant_builder_new(G_VARIANT_TYPE("ai")); g_variant_builder_add(builder, "i", g_variant_new_int32(53)); QTest::newRow("List") << QGVariant(g_variant_new("ai", builder)) << (unsigned) QVariant::List; g_variant_builder_unref(builder); + + QTest::newRow("Tuple") << QGVariant(g_variant_new("(i)", 53)) << (unsigned) QVariant::List; + + const gchar *byteArray[] = {"42", "53", NULL}; + QTest::newRow("ByteArrayList") << QGVariant(g_variant_new_bytestring_array(byteArray, -1)) << (unsigned) QMetaType::QByteArrayList; + QTest::newRow("String List") << QGVariant(g_variant_new_strv(byteArray, -1)) << (unsigned) QVariant::StringList; } void testConvertToQVariant() @@ -263,6 +305,47 @@ private Q_SLOTS: QVERIFY(compare(value, (QVariant::Type) expectedType)); } + void testConvertToQVariantAndBack_data() + { + testConvertToQVariant_data(); + } + + void testConvertToQVariantAndBack() + { + QFETCH(QGVariant, value); + + QVariant qv = Converter::toQVariant(value); + QVERIFY(qv.isValid()); + + GVariant *gv = Converter::toGVariant(qv); + gboolean equals = g_variant_equal(value, gv); + + if (!equals && qv.type() == QVariant::List) { + QVERIFY(g_variant_type_is_array(g_variant_get_type(value))); + QVERIFY(g_variant_type_is_tuple(g_variant_get_type(gv))); + + gsize vsize = g_variant_n_children(value); + QCOMPARE(vsize, g_variant_n_children(gv)); + + for (gsize i = 0; i < vsize; ++i) { + equals = g_variant_equal(g_variant_get_child_value(value, i), g_variant_get_child_value(gv, i)); + if (!equals) + break; + } + } + + if (!equals) { + gchar *vs = g_variant_print(value, TRUE); + gchar *gvs = g_variant_print(gv, TRUE); + qWarning() << "Values do not match. Old" << vs << "converted" << gvs; + g_free(vs); + g_free(gvs); + } + + g_variant_unref(gv); + QVERIFY(equals != FALSE); + } + void testConvertToQVariantFromString_data() { QTest::addColumn<QString>("value"); @@ -279,9 +362,12 @@ private Q_SLOTS: QTest::newRow("Double") << "double 65" << (unsigned) QVariant::Double; QTest::newRow("String") << "string '65'" << (unsigned) QVariant::String; QTest::newRow("String simple") << "\"65\"" << (unsigned) QVariant::String; + QTest::newRow("String List") << "['foo', 'bar']" << (unsigned) QVariant::StringList; + QTest::newRow("Byte string") << "b'fooo'" << (unsigned) QVariant::ByteArray; QTest::newRow("Map") << "{'foo': <65>}" << (unsigned) QVariant::Map; QTest::newRow("List") << "[65, 66]" << (unsigned) QVariant::List; QTest::newRow("Tuple") << "('foo', 65)" << (unsigned) QVariant::List; + QTest::newRow("ByteArrayList") << "[b'foo', b'bar']" << (unsigned) QMetaType::QByteArrayList; } void testConvertToQVariantFromString() |