From f5b609cac864e010085d5965b84832b2d85643e6 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 byte arrays prettier Similar to what we've done to QString, only we print each byte that is not ASCII as \OOO (octal representation). [ChangeLog][QtTest] QtTest now prints an escaped version of QByteArrays that failed to compare with QCOMPARE, instead of the hex dump. Change-Id: I6a8c43f138c66c998280998a242b43cd579666a0 Reviewed-by: Richard J. Moore --- src/testlib/qtestcase.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'src/testlib/qtestcase.cpp') diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 59f20aae93..16dade9154 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2179,6 +2179,72 @@ char *toHexRepresentation(const char *ba, int length) return result; } +/*! + \internal + Returns the same QByteArray but with only the ASCII characters still shown; + everything else is replaced with \c {\OOO}. +*/ +char *toPrettyCString(const char *p, int length) +{ + bool trimmed = false; + QScopedArrayPointer buffer(new char[256]); + const char *end = p + length; + char *dst = buffer.data(); + + *dst++ = '"'; + for ( ; p != end; ++p) { + if (dst - buffer.data() > 246) { + // plus the the quote, the three dots and NUL, it's 251, 252 or 255 + trimmed = true; + break; + } + + if (*p < 0x7f && *p >= 0x20 && *p != '\\' && *p != '"') { + *dst++ = *p; + continue; + } + + // write as an escape sequence + // this means we may advance dst to buffer.data() + 247 or 250 + *dst++ = '\\'; + switch (*p) { + case 0x5c: + case 0x22: + *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: + // write as octal + *dst++ = '0' + ((uchar(*p) >> 6) & 7); + *dst++ = '0' + ((uchar(*p) >> 3) & 7); + *dst++ = '0' + ((uchar(*p)) & 7); + } + } + + *dst++ = '"'; + if (trimmed) { + *dst++ = '.'; + *dst++ = '.'; + *dst++ = '.'; + } + *dst++ = '\0'; + return buffer.take(); +} + /*! \internal Returns the same QString but with only the ASCII characters still shown; -- cgit v1.2.3