summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2015-02-25 20:55:34 -0800
committerThiago Macieira <thiago.macieira@intel.com>2015-03-11 01:50:29 +0000
commit78588382938675648b14d1ccaa45be6ba09844df (patch)
tree40aa6530bfaba5528977a84a7a5a95f6b88bb669 /src
parent7acc7a8eb3da62bde5b278da46d4b7b133c7a952 (diff)
QTest: Print hex instead of octal for QByteArray QCOMPARE failures
Change-Id: Ia0aac2f09e9245339951ffff13c65b2234d01ad0 Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Diffstat (limited to 'src')
-rw-r--r--src/testlib/qtestcase.cpp31
1 files changed, 25 insertions, 6 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp
index fa2080fe69..6250be5853 100644
--- a/src/testlib/qtestcase.cpp
+++ b/src/testlib/qtestcase.cpp
@@ -88,6 +88,7 @@
QT_BEGIN_NAMESPACE
using QtMiscUtils::toHexUpper;
+using QtMiscUtils::fromHex;
/*!
\namespace QTest
@@ -2184,7 +2185,7 @@ char *toHexRepresentation(const char *ba, int length)
/*!
\internal
Returns the same QByteArray but with only the ASCII characters still shown;
- everything else is replaced with \c {\OOO}.
+ everything else is replaced with \c {\xHH}.
*/
char *toPrettyCString(const char *p, int length)
{
@@ -2193,14 +2194,30 @@ char *toPrettyCString(const char *p, int length)
const char *end = p + length;
char *dst = buffer.data();
+ bool lastWasHexEscape = false;
*dst++ = '"';
for ( ; p != end; ++p) {
+ // we can add:
+ // 1 byte: a single character
+ // 2 bytes: a simple escape sequence (\n)
+ // 3 bytes: "" and a character
+ // 4 bytes: an hex escape sequence (\xHH)
if (dst - buffer.data() > 246) {
- // plus the the quote, the three dots and NUL, it's 251, 252 or 255
+ // plus the the quote, the three dots and NUL, it's 255 in the worst case
trimmed = true;
break;
}
+ // check if we need to insert "" to break an hex escape sequence
+ if (Q_UNLIKELY(lastWasHexEscape)) {
+ if (fromHex(*p) != -1) {
+ // yes, insert it
+ *dst++ = '"';
+ *dst++ = '"';
+ }
+ lastWasHexEscape = false;
+ }
+
if (*p < 0x7f && *p >= 0x20 && *p != '\\' && *p != '"') {
*dst++ = *p;
continue;
@@ -2230,10 +2247,12 @@ char *toPrettyCString(const char *p, int length)
*dst++ = 't';
break;
default:
- // write as octal
- *dst++ = '0' + ((uchar(*p) >> 6) & 7);
- *dst++ = '0' + ((uchar(*p) >> 3) & 7);
- *dst++ = '0' + ((uchar(*p)) & 7);
+ // print as hex escape
+ *dst++ = 'x';
+ *dst++ = toHexUpper(uchar(*p) >> 4);
+ *dst++ = toHexUpper(uchar(*p));
+ lastWasHexEscape = true;
+ break;
}
}