From 62d81d9955f39bac327affd703b9df006ee8f6f7 Mon Sep 17 00:00:00 2001 From: Rohan McGovern Date: Tue, 28 Jul 2009 16:52:13 +1000 Subject: Added a QVariant testlib toString specialization. If comparing two variants fails, the failure message will now output the type and value of the variants (rather than "Compared values are not the same"). Reviewed-by: Thiago --- src/testlib/qtest.h | 25 +++++++++++++++ tests/auto/selftests/cmptest/tst_cmptest.cpp | 46 ++++++++++++++++++++++++++++ tests/auto/selftests/expected_cmptest.txt | 20 ++++++++++-- 3 files changed, 89 insertions(+), 2 deletions(-) diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index 9d6f68d..8cdfab0 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -53,6 +53,7 @@ #include #include #include +#include #include #include @@ -146,6 +147,30 @@ template<> inline char *toString(const QUrl &uri) return qstrdup(uri.toEncoded().constData()); } +template<> inline char *toString(const QVariant &v) +{ + QByteArray vstring("QVariant("); + if (v.isValid()) { + QByteArray type(v.typeName()); + if (type.isEmpty()) { + type = QByteArray::number(v.userType()); + } + vstring.append(type); + if (!v.isNull()) { + vstring.append(','); + if (v.canConvert(QVariant::String)) { + vstring.append(qVariantValue(v).toLatin1()); + } + else { + vstring.append(""); + } + } + } + vstring.append(')'); + + return qstrdup(vstring.constData()); +} + #ifndef QTEST_NO_SPECIALIZATIONS template<> #endif diff --git a/tests/auto/selftests/cmptest/tst_cmptest.cpp b/tests/auto/selftests/cmptest/tst_cmptest.cpp index 59dd678..7395210 100644 --- a/tests/auto/selftests/cmptest/tst_cmptest.cpp +++ b/tests/auto/selftests/cmptest/tst_cmptest.cpp @@ -50,6 +50,8 @@ class tst_Cmptest: public QObject private slots: void compare_boolfuncs(); void compare_pointerfuncs(); + void compare_tostring(); + void compare_tostring_data(); }; static bool boolfunc() { return true; } @@ -76,6 +78,50 @@ void tst_Cmptest::compare_pointerfuncs() QCOMPARE(&i, intptr()); } +Q_DECLARE_METATYPE(QVariant) + +class PhonyClass +{}; + +void tst_Cmptest::compare_tostring_data() +{ + QTest::addColumn("actual"); + QTest::addColumn("expected"); + + QTest::newRow("int, string") + << QVariant::fromValue(123) + << QVariant::fromValue(QString("hi")) + ; + + QTest::newRow("both invalid") + << QVariant() + << QVariant() + ; + + QTest::newRow("null hash, invalid") + << QVariant(QVariant::Hash) + << QVariant() + ; + + QTest::newRow("string, null user type") + << QVariant::fromValue(QString::fromLatin1("A simple string")) + << QVariant(QVariant::Type(qRegisterMetaType("PhonyClass"))) + ; + + QTest::newRow("both non-null user type") + << QVariant(qRegisterMetaType("PhonyClass"), (const void*)0) + << QVariant(qRegisterMetaType("PhonyClass"), (const void*)0) + ; +} + +void tst_Cmptest::compare_tostring() +{ + QFETCH(QVariant, actual); + QFETCH(QVariant, expected); + + QCOMPARE(actual, expected); +} + QTEST_MAIN(tst_Cmptest) #include "tst_cmptest.moc" diff --git a/tests/auto/selftests/expected_cmptest.txt b/tests/auto/selftests/expected_cmptest.txt index dc89d9d..f70eba5 100644 --- a/tests/auto/selftests/expected_cmptest.txt +++ b/tests/auto/selftests/expected_cmptest.txt @@ -1,8 +1,24 @@ ********* Start testing of tst_Cmptest ********* -Config: Using QTest library 4.1.0, Qt 4.1.0 +Config: Using QTest library 4.6.0, Qt 4.6.0 PASS : tst_Cmptest::initTestCase() PASS : tst_Cmptest::compare_boolfuncs() PASS : tst_Cmptest::compare_pointerfuncs() +FAIL! : tst_Cmptest::compare_tostring(int, string) Compared values are not the same + Actual (actual): QVariant(int,123) + Expected (expected): QVariant(QString,hi) + Loc: [/home/rmcgover/depot/qt/master/tests/auto/selftests/cmptest/tst_cmptest.cpp(122)] +FAIL! : tst_Cmptest::compare_tostring(null hash, invalid) Compared values are not the same + Actual (actual): QVariant(QVariantHash) + Expected (expected): QVariant() + Loc: [/home/rmcgover/depot/qt/master/tests/auto/selftests/cmptest/tst_cmptest.cpp(122)] +FAIL! : tst_Cmptest::compare_tostring(string, null user type) Compared values are not the same + Actual (actual): QVariant(QString,A simple string) + Expected (expected): QVariant(PhonyClass) + Loc: [/home/rmcgover/depot/qt/master/tests/auto/selftests/cmptest/tst_cmptest.cpp(122)] +FAIL! : tst_Cmptest::compare_tostring(both non-null user type) Compared values are not the same + Actual (actual): QVariant(PhonyClass,) + Expected (expected): QVariant(PhonyClass,) + Loc: [/home/rmcgover/depot/qt/master/tests/auto/selftests/cmptest/tst_cmptest.cpp(122)] PASS : tst_Cmptest::cleanupTestCase() -Totals: 4 passed, 0 failed, 0 skipped +Totals: 4 passed, 4 failed, 0 skipped ********* Finished testing of tst_Cmptest ********* -- cgit v0.12