summaryrefslogtreecommitdiffstats
path: root/src/testlib/qtestcase.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2014-01-20 16:03:30 -0800
committerThiago Macieira <thiago.macieira@intel.com>2015-02-15 06:35:43 +0000
commitf5b609cac864e010085d5965b84832b2d85643e6 (patch)
treeccb339e9bb957c25591192b62590158d867f5d84 /src/testlib/qtestcase.cpp
parent8d9d2a7b8568c4c067252e6ca9eea45de7a743a5 (diff)
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 <rich@kde.org>
Diffstat (limited to 'src/testlib/qtestcase.cpp')
-rw-r--r--src/testlib/qtestcase.cpp66
1 files changed, 66 insertions, 0 deletions
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
@@ -2181,6 +2181,72 @@ 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}.
+*/
+char *toPrettyCString(const char *p, int length)
+{
+ bool trimmed = false;
+ QScopedArrayPointer<char> 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;
everything else is replaced with \c {\uXXXX}.