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<int>. 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 <macadder1@gmail.com>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@digia.com>
This commit is contained in:
David Faure 2013-01-19 11:52:03 +01:00 committed by The Qt Project
parent c73dc44606
commit e01b163404
7 changed files with 103 additions and 30 deletions

View File

@ -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 <typename T>
inline bool qCompare(QList<T> const &t1, QList<T> 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<QString>(t1, t2, actual, expected, file, line);
}
template <typename T>
inline bool qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected,
const char *file, int line)

View File

@ -2684,6 +2684,10 @@ bool QTest::compare_string_helper(const char *t1, const char *t2, const char *ac
\internal
*/
/*! \fn bool QTest::qCompare(QList<T> const &t1, QList<T> const &t2, const char *actual, const char *expected, const char *file, int line)
\internal
*/
/*! \fn bool QTest::qCompare(QFlags<T> const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line)
\internal
*/

View File

@ -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<int> int1; int1 << 1 << 2 << 3;
QList<int> int2; int2 << 1 << 2 << 4;
QCOMPARE(int1, int2);
}
void tst_Cmptest::compareQListDouble()
{
QList<double> double1; double1 << 1.5 << 2 << 3;
QList<double> double2; double2 << 1 << 2 << 4;
QCOMPARE(double1, double2);
}
void tst_Cmptest::compareQPixmaps_data()
{
QTest::addColumn<QPixmap>("opA");

View File

@ -49,35 +49,49 @@
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp" line="308">
<DataTag><![CDATA[last item different]]></DataTag>
<Description><![CDATA[Compared QStringLists differ at index 2.
<Description><![CDATA[Compared lists differ at index 2.
Actual (opA): 'string3'
Expected (opB): 'DIFFERS']]></Description>
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp" line="308">
<DataTag><![CDATA[second-last item different]]></DataTag>
<Description><![CDATA[Compared QStringLists differ at index 2.
<Description><![CDATA[Compared lists differ at index 2.
Actual (opA): 'string3'
Expected (opB): 'DIFFERS']]></Description>
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp" line="308">
<DataTag><![CDATA[prefix]]></DataTag>
<Description><![CDATA[Compared QStringLists have different sizes.
<Description><![CDATA[Compared lists have different sizes.
Actual (opA) size: '2'
Expected (opB) size: '1']]></Description>
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp" line="308">
<DataTag><![CDATA[short list second]]></DataTag>
<Description><![CDATA[Compared QStringLists have different sizes.
<Description><![CDATA[Compared lists have different sizes.
Actual (opA) size: '12'
Expected (opB) size: '1']]></Description>
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp" line="308">
<DataTag><![CDATA[short list first]]></DataTag>
<Description><![CDATA[Compared QStringLists have different sizes.
<Description><![CDATA[Compared lists have different sizes.
Actual (opA) size: '1'
Expected (opB) size: '12']]></Description>
</Incident>
</TestFunction>
<TestFunction name="compareQListInt">
<Incident type="fail" file="tst_cmptest.cpp" line="316">
<Description><![CDATA[Compared lists differ at index 2.
Actual (int1): '3'
Expected (int2): '4']]></Description>
</Incident>
</TestFunction>
<TestFunction name="compareQListDouble">
<Incident type="fail" file="tst_cmptest.cpp" line="323">
<Description><![CDATA[Compared lists differ at index 0.
Actual (double1): '1.5'
Expected (double2): '1']]></Description>
</Incident>
</TestFunction>
<TestFunction name="compareQPixmaps">
<Incident type="pass" file="" line="0">
<DataTag><![CDATA[both null]]></DataTag>

View File

@ -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 *********

View File

@ -51,35 +51,49 @@
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp" line="308">
<DataTag><![CDATA[last item different]]></DataTag>
<Description><![CDATA[Compared QStringLists differ at index 2.
<Description><![CDATA[Compared lists differ at index 2.
Actual (opA): 'string3'
Expected (opB): 'DIFFERS']]></Description>
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp" line="308">
<DataTag><![CDATA[second-last item different]]></DataTag>
<Description><![CDATA[Compared QStringLists differ at index 2.
<Description><![CDATA[Compared lists differ at index 2.
Actual (opA): 'string3'
Expected (opB): 'DIFFERS']]></Description>
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp" line="308">
<DataTag><![CDATA[prefix]]></DataTag>
<Description><![CDATA[Compared QStringLists have different sizes.
<Description><![CDATA[Compared lists have different sizes.
Actual (opA) size: '2'
Expected (opB) size: '1']]></Description>
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp" line="308">
<DataTag><![CDATA[short list second]]></DataTag>
<Description><![CDATA[Compared QStringLists have different sizes.
<Description><![CDATA[Compared lists have different sizes.
Actual (opA) size: '12'
Expected (opB) size: '1']]></Description>
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp" line="308">
<DataTag><![CDATA[short list first]]></DataTag>
<Description><![CDATA[Compared QStringLists have different sizes.
<Description><![CDATA[Compared lists have different sizes.
Actual (opA) size: '1'
Expected (opB) size: '12']]></Description>
</Incident>
</TestFunction>
<TestFunction name="compareQListInt">
<Incident type="fail" file="tst_cmptest.cpp" line="316">
<Description><![CDATA[Compared lists differ at index 2.
Actual (int1): '3'
Expected (int2): '4']]></Description>
</Incident>
</TestFunction>
<TestFunction name="compareQListDouble">
<Incident type="fail" file="tst_cmptest.cpp" line="323">
<Description><![CDATA[Compared lists differ at index 0.
Actual (double1): '1.5'
Expected (double2): '1']]></Description>
</Incident>
</TestFunction>
<TestFunction name="compareQPixmaps">
<Incident type="pass" file="" line="0">
<DataTag><![CDATA[both null]]></DataTag>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?>
<testsuite errors="0" failures="18" tests="8" name="tst_Cmptest">
<testsuite errors="0" failures="20" tests="10" name="tst_Cmptest">
<properties>
<property value="@INSERT_QT_VERSION_HERE@" name="QTestVersion"/>
<property value="@INSERT_QT_VERSION_HERE@" name="QtVersion"/>
@ -22,22 +22,32 @@
Expected (expected): QVariant(PhonyClass,&lt;value not representable as string&gt;)" result="fail"/>
</testcase>
<testcase result="fail" name="compareQStringLists">
<failure tag="last item different" message="Compared QStringLists differ at index 2.
<failure tag="last item different" message="Compared lists differ at index 2.
Actual (opA): &apos;string3&apos;
Expected (opB): &apos;DIFFERS&apos;" result="fail"/>
<failure tag="second&#x002D;last item different" message="Compared QStringLists differ at index 2.
<failure tag="second&#x002D;last item different" message="Compared lists differ at index 2.
Actual (opA): &apos;string3&apos;
Expected (opB): &apos;DIFFERS&apos;" result="fail"/>
<failure tag="prefix" message="Compared QStringLists have different sizes.
<failure tag="prefix" message="Compared lists have different sizes.
Actual (opA) size: &apos;2&apos;
Expected (opB) size: &apos;1&apos;" result="fail"/>
<failure tag="short list second" message="Compared QStringLists have different sizes.
<failure tag="short list second" message="Compared lists have different sizes.
Actual (opA) size: &apos;12&apos;
Expected (opB) size: &apos;1&apos;" result="fail"/>
<failure tag="short list first" message="Compared QStringLists have different sizes.
<failure tag="short list first" message="Compared lists have different sizes.
Actual (opA) size: &apos;1&apos;
Expected (opB) size: &apos;12&apos;" result="fail"/>
</testcase>
<testcase result="fail" name="compareQListInt">
<failure message="Compared lists differ at index 2.
Actual (int1): &apos;3&apos;
Expected (int2): &apos;4&apos;" result="fail"/>
</testcase>
<testcase result="fail" name="compareQListDouble">
<failure message="Compared lists differ at index 0.
Actual (double1): &apos;1.5&apos;
Expected (double2): &apos;1&apos;" result="fail"/>
</testcase>
<testcase result="fail" name="compareQPixmaps">
<failure tag="one null" message="Compared QPixmaps differ.
Actual (opA).isNull(): 1