From e01b1634048ebf5858b77f2a6541a960babe232c Mon Sep 17 00:00:00 2001 From: David Faure Date: Sat, 19 Jan 2013 11:52:03 +0100 Subject: [PATCH] QTestLib: improve output when comparing lists fails. Only QStringList was handled before, now any QList is handled. A specialization for QStringList is still needed though, due to the way template matching works. Example with QList. Before: FAIL! : tst_QTextCodec::threadSafety() Compared values are not the same Loc: [../tst_qtextcodec.cpp(2057)] After: FAIL! : tst_QTextCodec::threadSafety() Compared lists differ at index 0. Actual (res2.toList()): '0' Expected (mibList): '3' Loc: [../tst_qtextcodec.cpp(2057)] Change-Id: If0fdec3236ddb78a679ee549aba569ef5571c395 Reviewed-by: Jason McDonald Reviewed-by: Friedemann Kleint --- src/testlib/qtest.h | 23 +++++++++++------- src/testlib/qtestcase.cpp | 4 ++++ .../testlib/selftests/cmptest/tst_cmptest.cpp | 16 +++++++++++++ .../selftests/expected_cmptest.lightxml | 24 +++++++++++++++---- .../testlib/selftests/expected_cmptest.txt | 20 +++++++++++----- .../testlib/selftests/expected_cmptest.xml | 24 +++++++++++++++---- .../selftests/expected_cmptest.xunitxml | 22 ++++++++++++----- 7 files changed, 103 insertions(+), 30 deletions(-) diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index a38b65671b..4b4ca191fc 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -186,9 +186,9 @@ inline bool qCompare(QLatin1String const &t1, QString const &t2, const char *act return qCompare(QString(t1), t2, actual, expected, file, line); } -template<> -inline bool qCompare(QStringList const &t1, QStringList const &t2, - const char *actual, const char *expected, const char *file, int line) +template +inline bool qCompare(QList const &t1, QList const &t2, const char *actual, const char *expected, + const char *file, int line) { char msg[1024]; msg[0] = '\0'; @@ -196,23 +196,30 @@ inline bool qCompare(QStringList const &t1, QStringList const &t2, const int actualSize = t1.count(); const int expectedSize = t2.count(); if (actualSize != expectedSize) { - qsnprintf(msg, sizeof(msg), "Compared QStringLists have different sizes.\n" + qsnprintf(msg, sizeof(msg), "Compared lists have different sizes.\n" " Actual (%s) size: '%d'\n" " Expected (%s) size: '%d'", actual, actualSize, expected, expectedSize); isOk = false; } for (int i = 0; isOk && i < actualSize; ++i) { - if (t1.at(i) != t2.at(i)) { - qsnprintf(msg, sizeof(msg), "Compared QStringLists differ at index %d.\n" + if (!(t1.at(i) == t2.at(i))) { + qsnprintf(msg, sizeof(msg), "Compared lists differ at index %d.\n" " Actual (%s): '%s'\n" - " Expected (%s): '%s'", i, actual, t1.at(i).toLatin1().constData(), - expected, t2.at(i).toLatin1().constData()); + " Expected (%s): '%s'", i, actual, toString(t1.at(i)), + expected, toString(t2.at(i))); isOk = false; } } return compare_helper(isOk, msg, 0, 0, actual, expected, file, line); } +template <> +inline bool qCompare(QStringList const &t1, QStringList const &t2, const char *actual, const char *expected, + const char *file, int line) +{ + return qCompare(t1, t2, actual, expected, file, line); +} + template inline bool qCompare(QFlags const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line) diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 2bfd3412c7..0b73fabfe1 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2684,6 +2684,10 @@ bool QTest::compare_string_helper(const char *t1, const char *t2, const char *ac \internal */ +/*! \fn bool QTest::qCompare(QList const &t1, QList const &t2, const char *actual, const char *expected, const char *file, int line) + \internal +*/ + /*! \fn bool QTest::qCompare(QFlags const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line) \internal */ diff --git a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp index 572388df6c..7213d9a82d 100644 --- a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp +++ b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp @@ -136,6 +136,8 @@ private slots: void compare_tostring_data(); void compareQStringLists(); void compareQStringLists_data(); + void compareQListInt(); + void compareQListDouble(); void compareQPixmaps(); void compareQPixmaps_data(); void compareQImages(); @@ -307,6 +309,20 @@ void tst_Cmptest::compareQStringLists() QCOMPARE(opA, opB); } +void tst_Cmptest::compareQListInt() +{ + QList int1; int1 << 1 << 2 << 3; + QList int2; int2 << 1 << 2 << 4; + QCOMPARE(int1, int2); +} + +void tst_Cmptest::compareQListDouble() +{ + QList double1; double1 << 1.5 << 2 << 3; + QList double2; double2 << 1 << 2 << 4; + QCOMPARE(double1, double2); +} + void tst_Cmptest::compareQPixmaps_data() { QTest::addColumn("opA"); diff --git a/tests/auto/testlib/selftests/expected_cmptest.lightxml b/tests/auto/testlib/selftests/expected_cmptest.lightxml index 83b2e6e137..54c5bb85f0 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.lightxml +++ b/tests/auto/testlib/selftests/expected_cmptest.lightxml @@ -49,35 +49,49 @@ - - - - - + + + + + + + + + + diff --git a/tests/auto/testlib/selftests/expected_cmptest.txt b/tests/auto/testlib/selftests/expected_cmptest.txt index de666ed8b1..d41da53b06 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.txt +++ b/tests/auto/testlib/selftests/expected_cmptest.txt @@ -22,26 +22,34 @@ FAIL! : tst_Cmptest::compare_tostring(both non-null user type) Compared values Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(214)] PASS : tst_Cmptest::compareQStringLists(empty lists) PASS : tst_Cmptest::compareQStringLists(equal lists) -FAIL! : tst_Cmptest::compareQStringLists(last item different) Compared QStringLists differ at index 2. +FAIL! : tst_Cmptest::compareQStringLists(last item different) Compared lists differ at index 2. Actual (opA): 'string3' Expected (opB): 'DIFFERS' Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(308)] -FAIL! : tst_Cmptest::compareQStringLists(second-last item different) Compared QStringLists differ at index 2. +FAIL! : tst_Cmptest::compareQStringLists(second-last item different) Compared lists differ at index 2. Actual (opA): 'string3' Expected (opB): 'DIFFERS' Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(308)] -FAIL! : tst_Cmptest::compareQStringLists(prefix) Compared QStringLists have different sizes. +FAIL! : tst_Cmptest::compareQStringLists(prefix) Compared lists have different sizes. Actual (opA) size: '2' Expected (opB) size: '1' Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(308)] -FAIL! : tst_Cmptest::compareQStringLists(short list second) Compared QStringLists have different sizes. +FAIL! : tst_Cmptest::compareQStringLists(short list second) Compared lists have different sizes. Actual (opA) size: '12' Expected (opB) size: '1' Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(308)] -FAIL! : tst_Cmptest::compareQStringLists(short list first) Compared QStringLists have different sizes. +FAIL! : tst_Cmptest::compareQStringLists(short list first) Compared lists have different sizes. Actual (opA) size: '1' Expected (opB) size: '12' Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(308)] +FAIL! : tst_Cmptest::compareQListInt() Compared lists differ at index 2. + Actual (int1): '3' + Expected (int2): '4' + Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(316)] +FAIL! : tst_Cmptest::compareQListDouble() Compared lists differ at index 0. + Actual (double1): '1.5' + Expected (double2): '1' + Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(323)] PASS : tst_Cmptest::compareQPixmaps(both null) FAIL! : tst_Cmptest::compareQPixmaps(one null) Compared QPixmaps differ. Actual (opA).isNull(): 1 @@ -79,5 +87,5 @@ FAIL! : tst_Cmptest::compareQImages(different format) Compared QImages differ i FAIL! : tst_Cmptest::compareQImages(different pixels) Compared values are not the same Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp(360)] PASS : tst_Cmptest::cleanupTestCase() -Totals: 11 passed, 18 failed, 0 skipped +Totals: 11 passed, 20 failed, 0 skipped ********* Finished testing of tst_Cmptest ********* diff --git a/tests/auto/testlib/selftests/expected_cmptest.xml b/tests/auto/testlib/selftests/expected_cmptest.xml index 0e5b6de335..ccab93d7c0 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.xml +++ b/tests/auto/testlib/selftests/expected_cmptest.xml @@ -51,35 +51,49 @@ - - - - - + + + + + + + + + + diff --git a/tests/auto/testlib/selftests/expected_cmptest.xunitxml b/tests/auto/testlib/selftests/expected_cmptest.xunitxml index 33e78a5290..0cb88147b0 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.xunitxml +++ b/tests/auto/testlib/selftests/expected_cmptest.xunitxml @@ -1,5 +1,5 @@ - + @@ -22,22 +22,32 @@ Expected (expected): QVariant(PhonyClass,<value not representable as string>)" result="fail"/> - - - - - + + + + + +