diff options
author | Rohan McGovern <rohan.mcgovern@nokia.com> | 2009-07-29 07:26:37 (GMT) |
---|---|---|
committer | Rohan McGovern <rohan.mcgovern@nokia.com> | 2009-07-30 04:54:44 (GMT) |
commit | a92117be4323e26efe3f13b5c624e5010a7cd26a (patch) | |
tree | 23aa971a07a98543324fa69761a1b4183a537991 /src | |
parent | 5246aeb198ccfd2a3fc94298161c24cb8f57f81f (diff) | |
download | Qt-a92117be4323e26efe3f13b5c624e5010a7cd26a.zip Qt-a92117be4323e26efe3f13b5c624e5010a7cd26a.tar.gz Qt-a92117be4323e26efe3f13b5c624e5010a7cd26a.tar.bz2 |
Allow testlib loggers to dynamically allocate storage for strings.
This enables very long failure messages (e.g. including an entire
build log in a failure message).
This change modifies only the plain test logger to use this feature.
Task: 253861
Reviewed-by: Michael Goddard
Diffstat (limited to 'src')
-rw-r--r-- | src/testlib/qabstracttestlogger_p.h | 36 | ||||
-rw-r--r-- | src/testlib/qplaintestlogger.cpp | 8 | ||||
-rw-r--r-- | src/testlib/qtest_global.h | 1 | ||||
-rw-r--r-- | src/testlib/qtestcase.cpp | 52 |
4 files changed, 88 insertions, 9 deletions
diff --git a/src/testlib/qabstracttestlogger_p.h b/src/testlib/qabstracttestlogger_p.h index a0e8a1e..588184e 100644 --- a/src/testlib/qabstracttestlogger_p.h +++ b/src/testlib/qabstracttestlogger_p.h @@ -99,6 +99,42 @@ public: static bool isTtyOutput(); }; +struct QTestCharBuffer +{ + inline QTestCharBuffer() + : buf(0) + {} + + inline ~QTestCharBuffer() + { + delete[] buf; + buf = 0; + } + + inline operator void*() + { + return buf; + } + + inline operator char*() + { + return buf; + } + + inline operator char**() + { + return &buf; + } + + inline const char* constData() const + { + return buf; + } + +private: + char* buf; +}; + QT_END_NAMESPACE #endif diff --git a/src/testlib/qplaintestlogger.cpp b/src/testlib/qplaintestlogger.cpp index 071b55e..7bebaa1 100644 --- a/src/testlib/qplaintestlogger.cpp +++ b/src/testlib/qplaintestlogger.cpp @@ -168,7 +168,7 @@ namespace QTest { QTEST_ASSERT(type); QTEST_ASSERT(msg); - char buf[1024]; + QTestCharBuffer buf; const char *fn = QTestResult::currentTestFunction() ? QTestResult::currentTestFunction() : "UnknownTestFunc"; @@ -178,7 +178,7 @@ namespace QTest { : ""; const char *filler = (tag[0] && gtag[0]) ? ":" : ""; if (file) { - QTest::qt_snprintf(buf, sizeof(buf), "%s: %s::%s(%s%s%s)%s%s\n" + QTest::qt_asprintf(buf, "%s: %s::%s(%s%s%s)%s%s\n" #ifdef Q_OS_WIN "%s(%d) : failure location\n" #else @@ -187,10 +187,12 @@ namespace QTest { , type, QTestResult::currentTestObjectName(), fn, gtag, filler, tag, msg[0] ? " " : "", msg, file, line); } else { - QTest::qt_snprintf(buf, sizeof(buf), "%s: %s::%s(%s%s%s)%s%s\n", + QTest::qt_asprintf(buf, "%s: %s::%s(%s%s%s)%s%s\n", type, QTestResult::currentTestObjectName(), fn, gtag, filler, tag, msg[0] ? " " : "", msg); } + // In colored mode, printf above stripped our nonprintable control characters. + // Put them back. memcpy(buf, type, strlen(type)); outputMessage(buf); } diff --git a/src/testlib/qtest_global.h b/src/testlib/qtest_global.h index b5b0fc0..c40f0ad 100644 --- a/src/testlib/qtest_global.h +++ b/src/testlib/qtest_global.h @@ -82,6 +82,7 @@ namespace QTest enum TestFailMode { Abort = 1, Continue = 2 }; int Q_TESTLIB_EXPORT qt_snprintf(char *str, int size, const char *format, ...); + int qt_asprintf(char **str, const char *format, ...); } QT_END_NAMESPACE diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index cb05400..e44be9c 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -808,6 +808,50 @@ namespace QTest static int eventDelay = -1; static int keyVerbose = -1; +void filter_unprintable(char *str) +{ + char *idx = str; + while (*idx) { + if (((*idx < 0x20 && *idx != '\n' && *idx != '\t') || *idx > 0x7e)) + *idx = '?'; + ++idx; + } +} + +int qt_asprintf(char **str, const char *format, ...) +{ + static const int MAXSIZE = 1024*1024*2; + + int size = 32; + delete[] *str; + *str = new char[size]; + + va_list ap; + int res = 0; + + for (;;) { + va_start(ap, format); + res = qvsnprintf(*str, size, format, ap); + va_end(ap); + (*str)[size - 1] = '\0'; + if (res < size) { + // We succeeded or fatally failed + break; + } + // buffer wasn't big enough, try again + size *= 2; + if (size > MAXSIZE) { + break; + } + delete[] *str; + *str = new char[size]; + } + + filter_unprintable(*str); + + return res; +} + /*! \internal */ int qt_snprintf(char *str, int size, const char *format, ...) @@ -820,12 +864,8 @@ int qt_snprintf(char *str, int size, const char *format, ...) va_end(ap); str[size - 1] = '\0'; - char *idx = str; - while (*idx) { - if (((*idx < 0x20 && *idx != '\n' && *idx != '\t') || *idx > 0x7e)) - *idx = '?'; - ++idx; - } + filter_unprintable(str); + return res; } |