testlib: Split out test identifier buildup into standalone function

Change-Id: I99aa106d5aab8f299e61835680709e4fd856defe
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
Tor Arne Vestbø 2018-02-09 15:09:25 +01:00
parent c6de55a0bb
commit cf4a611115

View File

@ -223,38 +223,47 @@ void QPlainTestLogger::outputMessage(const char *str)
outputString(str); outputString(str);
} }
static QTestCharBuffer testIdentifier()
{
QTestCharBuffer identifier;
const char *testObject = QTestResult::currentTestObjectName();
const char *testFunction = QTestResult::currentTestFunction() ? QTestResult::currentTestFunction() : "UnknownTestFunc";
const char *dataTag = QTestResult::currentDataTag() ? QTestResult::currentDataTag() : "";
const char *globalDataTag = QTestResult::currentGlobalDataTag() ? QTestResult::currentGlobalDataTag() : "";
const char *tagFiller = (dataTag[0] && globalDataTag[0]) ? ":" : "";
QTest::qt_asprintf(&identifier, "%s::%s(%s%s%s)", testObject, testFunction, globalDataTag, tagFiller, dataTag);
return identifier;
}
void QPlainTestLogger::printMessage(const char *type, const char *msg, const char *file, int line) void QPlainTestLogger::printMessage(const char *type, const char *msg, const char *file, int line)
{ {
QTEST_ASSERT(type); QTEST_ASSERT(type);
QTEST_ASSERT(msg); QTEST_ASSERT(msg);
QTestCharBuffer buf; QTestCharBuffer messagePrefix;
const char *fn = QTestResult::currentTestFunction() ? QTestResult::currentTestFunction() QTestCharBuffer failureLocation;
: "UnknownTestFunc";
const char *tag = QTestResult::currentDataTag() ? QTestResult::currentDataTag() : "";
const char *gtag = QTestResult::currentGlobalDataTag()
? QTestResult::currentGlobalDataTag()
: "";
const char *filler = (tag[0] && gtag[0]) ? ":" : "";
if (file) { if (file) {
QTest::qt_asprintf(&buf, "%s: %s::%s(%s%s%s)%s%s\n"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
"%s(%d) : failure location\n" #define FAILURE_LOCATION_STR "\n%s(%d) : failure location"
#else #else
" Loc: [%s(%d)]\n" #define FAILURE_LOCATION_STR "\n Loc: [%s(%d)]"
#endif #endif
, type, QTestResult::currentTestObjectName(), fn, gtag, filler, tag, QTest::qt_asprintf(&failureLocation, FAILURE_LOCATION_STR, file, line);
msg[0] ? " " : "", msg, file, line);
} else {
QTest::qt_asprintf(&buf, "%s: %s::%s(%s%s%s)%s%s\n",
type, QTestResult::currentTestObjectName(), fn, gtag, filler, tag,
msg[0] ? " " : "", msg);
} }
const char *msgFiller = msg[0] ? " " : "";
QTest::qt_asprintf(&messagePrefix, "%s: %s%s%s%s\n",
type, testIdentifier().data(), msgFiller, msg, failureLocation.data());
// In colored mode, printf above stripped our nonprintable control characters. // In colored mode, printf above stripped our nonprintable control characters.
// Put them back. // Put them back.
memcpy(buf.data(), type, strlen(type)); memcpy(messagePrefix.data(), type, strlen(type));
outputMessage(buf.data());
outputMessage(messagePrefix.data());
} }
void QPlainTestLogger::printBenchmarkResult(const QBenchmarkResult &result) void QPlainTestLogger::printBenchmarkResult(const QBenchmarkResult &result)