From 579526cfec082679241548a0fca1ff9ba2c350a7 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Mon, 20 Jan 2014 16:03:30 -0800 Subject: Make the printing of complex Unicode in a QString prettier This also has the advantage of not requiring the use of the locale codec. Quite an advantage if you're debugging the locale codec. But it's mostly so that we don't get question marks that hide the difference we were trying to locate. [ChangeLog][QtTest] QtTest now prints an escaped version of QStrings that failed to compare with QCOMPARE. That is, instead of converting non-printable characters to question marks, QtTest will print the Unicode representation of the character in question. Change-Id: I44c1ef3246b188c913dacd3ca4df02581356ea41 Reviewed-by: Lars Knoll --- src/testlib/qtest.h | 16 +++--- src/testlib/qtestcase.cpp | 66 +++++++++++++++++++++- src/testlib/qtestcase.h | 1 + .../testlib/selftests/expected_cmptest.lightxml | 28 ++++----- tests/auto/testlib/selftests/expected_cmptest.txt | 28 ++++----- tests/auto/testlib/selftests/expected_cmptest.xml | 28 ++++----- .../testlib/selftests/expected_cmptest.xunitxml | 28 ++++----- .../testlib/selftests/expected_subtest.lightxml | 8 +-- tests/auto/testlib/selftests/expected_subtest.txt | 8 +-- tests/auto/testlib/selftests/expected_subtest.xml | 8 +-- .../testlib/selftests/expected_subtest.xunitxml | 8 +-- 11 files changed, 144 insertions(+), 83 deletions(-) diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index ac1d6cc9ef..7c9a7b2b3f 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -66,14 +66,14 @@ QT_BEGIN_NAMESPACE namespace QTest { -template<> inline char *toString(const QLatin1String &str) +template<> inline char *toString(const QString &str) { - return qstrdup(qPrintable(QString(str))); + return QTest::toPrettyUnicode(reinterpret_cast(str.constData()), str.length()); } -template<> inline char *toString(const QString &str) +template<> inline char *toString(const QLatin1String &str) { - return qstrdup(qPrintable(str)); + return toString(QString(str)); } template<> inline char *toString(const QByteArray &ba) @@ -195,15 +195,15 @@ inline bool qCompare(QList const &t1, QList const &t2, const char *actual, const int expectedSize = t2.count(); if (actualSize != expectedSize) { qsnprintf(msg, sizeof(msg), "Compared lists have different sizes.\n" - " Actual (%s) size: '%d'\n" - " Expected (%s) size: '%d'", actual, actualSize, expected, expectedSize); + " 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 lists differ at index %d.\n" - " Actual (%s): '%s'\n" - " Expected (%s): '%s'", i, actual, toString(t1.at(i)), + " Actual (%s): %s\n" + " Expected (%s): %s", i, actual, toString(t1.at(i)), expected, toString(t2.at(i))); isOk = false; } diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 0d7a017f89..6c1df8815c 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -1894,6 +1894,12 @@ void *fetchData(QTestData *data, const char *tagName, int typeId) return data->data(idx); } +static char toHex(ushort value) +{ + static const char hexdigits[] = "0123456789ABCDEF"; + return hexdigits[value & 0xF]; +} + /*! \fn char* QTest::toHexRepresentation(const char *ba, int length) @@ -1937,16 +1943,15 @@ char *toHexRepresentation(const char *ba, int length) result[size - 1] = '\0'; } - const char toHex[] = "0123456789ABCDEF"; int i = 0; int o = 0; while (true) { const char at = ba[i]; - result[o] = toHex[(at >> 4) & 0x0F]; + result[o] = toHex(at >> 4); ++o; - result[o] = toHex[at & 0x0F]; + result[o] = toHex(at); ++i; ++o; @@ -1961,6 +1966,61 @@ char *toHexRepresentation(const char *ba, int length) return result; } +/*! + \internal + Returns the same QString but with only the ASCII characters still shown; + everything else is replaced with \c {\uXXXX}. +*/ +char *toPrettyUnicode(const ushort *p, int length) +{ + // keep it simple for the vast majority of cases + QScopedArrayPointer buffer(new char[length * 6 + 3]); + const ushort *end = p + length; + char *dst = buffer.data(); + + *dst++ = '"'; + for ( ; p != end; ++p) { + if (*p < 0x7f && *p >= 0x20 && *p != '\\') { + *dst++ = *p; + continue; + } + + // write as an escape sequence + *dst++ = '\\'; + switch (*p) { + case 0x22: + case 0x5c: + *dst++ = uchar(*p); + break; + case 0x8: + *dst++ = 'b'; + break; + case 0xc: + *dst++ = 'f'; + break; + case 0xa: + *dst++ = 'n'; + break; + case 0xd: + *dst++ = 'r'; + break; + case 0x9: + *dst++ = 't'; + break; + default: + *dst++ = 'u'; + *dst++ = toHex(*p >> 12); + *dst++ = toHex(*p >> 8); + *dst++ = toHex(*p >> 4); + *dst++ = toHex(*p); + } + } + + *dst++ = '"'; + *dst++ = '\0'; + return buffer.take(); +} + static void qInvokeTestMethods(QObject *testObject) { const QMetaObject *metaObject = testObject->metaObject(); diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 05da33c400..b715d83383 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -233,6 +233,7 @@ namespace QTest Q_TESTLIB_EXPORT char *toHexRepresentation(const char *ba, int length); + Q_TESTLIB_EXPORT char *toPrettyUnicode(const ushort *unicode, int length); Q_TESTLIB_EXPORT char *toString(const char *); Q_TESTLIB_EXPORT char *toString(const void *); diff --git a/tests/auto/testlib/selftests/expected_cmptest.lightxml b/tests/auto/testlib/selftests/expected_cmptest.lightxml index a83a971d8a..3a776ea7cb 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.lightxml +++ b/tests/auto/testlib/selftests/expected_cmptest.lightxml @@ -54,48 +54,48 @@ + Actual (opA): "string3" + Expected (opB): "DIFFERS"]]> + Actual (opA): "string3" + Expected (opB): "DIFFERS"]]> + Actual (opA) size: 2 + Expected (opB) size: 1]]> + Actual (opA) size: 12 + Expected (opB) size: 1]]> + Actual (opA) size: 1 + Expected (opB) size: 12]]> + Actual (int1): 3 + Expected (int2): 4]]> + Actual (double1): 1.5 + Expected (double2): 1]]> diff --git a/tests/auto/testlib/selftests/expected_cmptest.txt b/tests/auto/testlib/selftests/expected_cmptest.txt index b3d34d49de..8473b4528e 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.txt +++ b/tests/auto/testlib/selftests/expected_cmptest.txt @@ -23,32 +23,32 @@ FAIL! : tst_Cmptest::compare_tostring(both non-null user type) Compared values PASS : tst_Cmptest::compareQStringLists(empty lists) PASS : tst_Cmptest::compareQStringLists(equal lists) FAIL! : tst_Cmptest::compareQStringLists(last item different) Compared lists differ at index 2. - Actual (opA): 'string3' - Expected (opB): 'DIFFERS' + Actual (opA): "string3" + Expected (opB): "DIFFERS" Loc: [tst_cmptest.cpp(313)] FAIL! : tst_Cmptest::compareQStringLists(second-last item different) Compared lists differ at index 2. - Actual (opA): 'string3' - Expected (opB): 'DIFFERS' + Actual (opA): "string3" + Expected (opB): "DIFFERS" Loc: [tst_cmptest.cpp(313)] FAIL! : tst_Cmptest::compareQStringLists(prefix) Compared lists have different sizes. - Actual (opA) size: '2' - Expected (opB) size: '1' + Actual (opA) size: 2 + Expected (opB) size: 1 Loc: [tst_cmptest.cpp(313)] FAIL! : tst_Cmptest::compareQStringLists(short list second) Compared lists have different sizes. - Actual (opA) size: '12' - Expected (opB) size: '1' + Actual (opA) size: 12 + Expected (opB) size: 1 Loc: [tst_cmptest.cpp(313)] FAIL! : tst_Cmptest::compareQStringLists(short list first) Compared lists have different sizes. - Actual (opA) size: '1' - Expected (opB) size: '12' + Actual (opA) size: 1 + Expected (opB) size: 12 Loc: [tst_cmptest.cpp(313)] FAIL! : tst_Cmptest::compareQListInt() Compared lists differ at index 2. - Actual (int1): '3' - Expected (int2): '4' + Actual (int1): 3 + Expected (int2): 4 Loc: [tst_cmptest.cpp(320)] FAIL! : tst_Cmptest::compareQListDouble() Compared lists differ at index 0. - Actual (double1): '1.5' - Expected (double2): '1' + Actual (double1): 1.5 + Expected (double2): 1 Loc: [tst_cmptest.cpp(327)] PASS : tst_Cmptest::compareQPixmaps(both null) FAIL! : tst_Cmptest::compareQPixmaps(one null) Compared QPixmaps differ. diff --git a/tests/auto/testlib/selftests/expected_cmptest.xml b/tests/auto/testlib/selftests/expected_cmptest.xml index 08a40fb466..970544e4b6 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.xml +++ b/tests/auto/testlib/selftests/expected_cmptest.xml @@ -56,48 +56,48 @@ + Actual (opA): "string3" + Expected (opB): "DIFFERS"]]> + Actual (opA): "string3" + Expected (opB): "DIFFERS"]]> + Actual (opA) size: 2 + Expected (opB) size: 1]]> + Actual (opA) size: 12 + Expected (opB) size: 1]]> + Actual (opA) size: 1 + Expected (opB) size: 12]]> + Actual (int1): 3 + Expected (int2): 4]]> + Actual (double1): 1.5 + Expected (double2): 1]]> diff --git a/tests/auto/testlib/selftests/expected_cmptest.xunitxml b/tests/auto/testlib/selftests/expected_cmptest.xunitxml index e7d76ac839..7874c6c52e 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.xunitxml +++ b/tests/auto/testlib/selftests/expected_cmptest.xunitxml @@ -23,30 +23,30 @@ + Actual (opA): "string3" + Expected (opB): "DIFFERS"" result="fail"/> + Actual (opA): "string3" + Expected (opB): "DIFFERS"" result="fail"/> + Actual (opA) size: 2 + Expected (opB) size: 1" result="fail"/> + Actual (opA) size: 12 + Expected (opB) size: 1" result="fail"/> + Actual (opA) size: 1 + Expected (opB) size: 12" result="fail"/> + Actual (int1): 3 + Expected (int2): 4" result="fail"/> + Actual (double1): 1.5 + Expected (double2): 1" result="fail"/> + Actual (str) : "hello1" + Expected (QString("hello0")): "hello0"]]> @@ -143,8 +143,8 @@ + Actual (str) : "hello2" + Expected (QString("hello0")): "hello0"]]> diff --git a/tests/auto/testlib/selftests/expected_subtest.txt b/tests/auto/testlib/selftests/expected_subtest.txt index e874e0c020..9990b5439d 100644 --- a/tests/auto/testlib/selftests/expected_subtest.txt +++ b/tests/auto/testlib/selftests/expected_subtest.txt @@ -33,15 +33,15 @@ PASS : tst_Subtest::test3(data0) QDEBUG : tst_Subtest::test3(data1) init test3 data1 QDEBUG : tst_Subtest::test3(data1) test2 test3 data1 FAIL! : tst_Subtest::test3(data1) Compared values are not the same - Actual (str) : hello1 - Expected (QString("hello0")): hello0 + Actual (str) : "hello1" + Expected (QString("hello0")): "hello0" Loc: [tst_subtest.cpp(154)] QDEBUG : tst_Subtest::test3(data1) cleanup test3 data1 QDEBUG : tst_Subtest::test3(data2) init test3 data2 QDEBUG : tst_Subtest::test3(data2) test2 test3 data2 FAIL! : tst_Subtest::test3(data2) Compared values are not the same - Actual (str) : hello2 - Expected (QString("hello0")): hello0 + Actual (str) : "hello2" + Expected (QString("hello0")): "hello0" Loc: [tst_subtest.cpp(154)] QDEBUG : tst_Subtest::test3(data2) cleanup test3 data2 QDEBUG : tst_Subtest::cleanupTestCase() cleanupTestCase cleanupTestCase (null) diff --git a/tests/auto/testlib/selftests/expected_subtest.xml b/tests/auto/testlib/selftests/expected_subtest.xml index cda4df01f3..1107bcb070 100644 --- a/tests/auto/testlib/selftests/expected_subtest.xml +++ b/tests/auto/testlib/selftests/expected_subtest.xml @@ -127,8 +127,8 @@ + Actual (str) : "hello1" + Expected (QString("hello0")): "hello0"]]> @@ -145,8 +145,8 @@ + Actual (str) : "hello2" + Expected (QString("hello0")): "hello0"]]> diff --git a/tests/auto/testlib/selftests/expected_subtest.xunitxml b/tests/auto/testlib/selftests/expected_subtest.xunitxml index 5f7024e8ba..753711f837 100644 --- a/tests/auto/testlib/selftests/expected_subtest.xunitxml +++ b/tests/auto/testlib/selftests/expected_subtest.xunitxml @@ -38,14 +38,14 @@ + Actual (str) : "hello1" + Expected (QString("hello0")): "hello0"" result="fail"/> + Actual (str) : "hello2" + Expected (QString("hello0")): "hello0"" result="fail"/> -- cgit v1.2.3