diff options
Diffstat (limited to 'src/testlib/qtestresult.cpp')
-rw-r--r-- | src/testlib/qtestresult.cpp | 110 |
1 files changed, 58 insertions, 52 deletions
diff --git a/src/testlib/qtestresult.cpp b/src/testlib/qtestresult.cpp index 14d344f381..7c5ce9ce54 100644 --- a/src/testlib/qtestresult.cpp +++ b/src/testlib/qtestresult.cpp @@ -149,13 +149,6 @@ void QTestResult::finishedCurrentTestData() addFailure("QEXPECT_FAIL was called without any subsequent verification statements"); clearExpectFail(); - - if (!QTest::hasFailed() && QTestLog::unhandledIgnoreMessages()) { - QTestLog::printUnhandledIgnoreMessages(); - addFailure("Not all expected messages were received"); - } - QTestLog::clearIgnoreMessages(); - QTestLog::clearFailOnWarnings(); } /*! @@ -175,6 +168,11 @@ void QTestResult::finishedCurrentTestData() */ void QTestResult::finishedCurrentTestDataCleanup() { + if (!QTest::hasFailed() && QTestLog::unhandledIgnoreMessages()) { + QTestLog::printUnhandledIgnoreMessages(); + addFailure("Not all expected messages were received"); + } + // If the current test hasn't failed or been skipped, then it passes. if (!QTest::hasFailed() && !QTest::skipCurrentTest) { if (QTest::blacklistCurrentTest) @@ -289,21 +287,27 @@ void QTestResult::fail(const char *msg, const char *file, int line) checkStatement(false, msg, file, line); } +// QPalette's << operator produces 1363 characters. A comparison failure +// involving two palettes can therefore require 2726 characters, not including +// the other output produced by QTest. Users might also have their own types +// with large amounts of output, so use a sufficiently high value here. +static constexpr size_t maxMsgLen = 4096; + bool QTestResult::verify(bool statement, const char *statementStr, const char *description, const char *file, int line) { QTEST_ASSERT(statementStr); - char msg[1024]; + char msg[maxMsgLen]; msg[0] = '\0'; if (QTestLog::verboseLevel() >= 2) { - qsnprintf(msg, 1024, "QVERIFY(%s)", statementStr); + qsnprintf(msg, maxMsgLen, "QVERIFY(%s)", statementStr); QTestLog::info(msg, file, line); } if (statement == !!QTest::expectFailMode) { - qsnprintf(msg, 1024, + qsnprintf(msg, maxMsgLen, statement ? "'%s' returned TRUE unexpectedly. (%s)" : "'%s' returned FALSE. (%s)", statementStr, description ? description : ""); } @@ -313,51 +317,55 @@ bool QTestResult::verify(bool statement, const char *statementStr, static const char *leftArgNameForOp(QTest::ComparisonOperation op) { - return op == QTest::ComparisonOperation::CustomCompare ? "Actual " : "Left "; + return op == QTest::ComparisonOperation::CustomCompare ? "Actual " : "Computed "; } static const char *rightArgNameForOp(QTest::ComparisonOperation op) { - return op == QTest::ComparisonOperation::CustomCompare ? "Expected " : "Right "; + return op == QTest::ComparisonOperation::CustomCompare ? "Expected " : "Baseline "; } -// Format failures using the toString() template -template <class Actual, class Expected> +// Overload to format failures for "const char *" - no need to strdup(). void formatFailMessage(char *msg, size_t maxMsgLen, const char *failureMsg, - const Actual &val1, const Expected &val2, + const char *val1, const char *val2, const char *actual, const char *expected, QTest::ComparisonOperation op) { - auto val1S = QTest::toString(val1); - auto val2S = QTest::toString(val2); - size_t len1 = mbstowcs(nullptr, actual, maxMsgLen); // Last parameter is not ignored on QNX size_t len2 = mbstowcs(nullptr, expected, maxMsgLen); // (result is never larger than this). - qsnprintf(msg, maxMsgLen, "%s\n %s(%s)%*s %s\n %s(%s)%*s %s", failureMsg, - leftArgNameForOp(op), actual, qMax(len1, len2) - len1 + 1, ":", - val1S ? val1S : "<null>", - rightArgNameForOp(op), expected, qMax(len1, len2) - len2 + 1, ":", - val2S ? val2S : "<null>"); - - delete [] val1S; - delete [] val2S; + const int written = qsnprintf(msg, maxMsgLen, "%s\n", failureMsg); + msg += written; + maxMsgLen -= written; + + if (val1 || val2) { + qsnprintf(msg, maxMsgLen, " %s(%s)%*s %s\n %s(%s)%*s %s", + leftArgNameForOp(op), actual, qMax(len1, len2) - len1 + 1, ":", + val1 ? val1 : "<null>", + rightArgNameForOp(op), expected, qMax(len1, len2) - len2 + 1, ":", + val2 ? val2 : "<null>"); + } else { + // only print variable names if neither value can be represented as a string + qsnprintf(msg, maxMsgLen, " %s: %s\n %s: %s", + leftArgNameForOp(op), actual, rightArgNameForOp(op), expected); + } } -// Overload to format failures for "const char *" - no need to strdup(). +// Format failures using the toString() template +template <class Actual, class Expected> void formatFailMessage(char *msg, size_t maxMsgLen, const char *failureMsg, - const char *val1, const char *val2, + const Actual &val1, const Expected &val2, const char *actual, const char *expected, QTest::ComparisonOperation op) { - size_t len1 = mbstowcs(nullptr, actual, maxMsgLen); // Last parameter is not ignored on QNX - size_t len2 = mbstowcs(nullptr, expected, maxMsgLen); // (result is never larger than this). - qsnprintf(msg, maxMsgLen, "%s\n %s(%s)%*s %s\n %s(%s)%*s %s", failureMsg, - leftArgNameForOp(op), actual, qMax(len1, len2) - len1 + 1, ":", - val1 ? val1 : "<null>", - rightArgNameForOp(op), expected, qMax(len1, len2) - len2 + 1, ":", - val2 ? val2 : "<null>"); + const char *val1S = QTest::toString(val1); + const char *val2S = QTest::toString(val2); + + formatFailMessage(msg, maxMsgLen, failureMsg, val1S, val2S, actual, expected, op); + + delete [] val1S; + delete [] val2S; } template <class Actual, class Expected> @@ -367,7 +375,6 @@ static bool compareHelper(bool success, const char *failureMsg, const char *file, int line, bool hasValues = true) { - const size_t maxMsgLen = 1024; char msg[maxMsgLen]; msg[0] = '\0'; @@ -530,7 +537,8 @@ bool QTestResult::compare(bool success, const char *failureMsg, void QTestResult::addFailure(const char *message, const char *file, int line) { clearExpectFail(); - QTestEventLoop::instance().exitLoop(); + if (qApp && QThread::currentThread() == qApp->thread()) + QTestEventLoop::instance().exitLoop(); if (QTest::blacklistCurrentTest) QTestLog::addBFail(message, file, line); @@ -595,8 +603,7 @@ static const char *macroNameForOp(QTest::ComparisonOperation op) case ComparisonOperation::GreaterThanOrEqual: return "QCOMPARE_GE"; } - Q_UNREACHABLE(); - return ""; + Q_UNREACHABLE_RETURN(""); } static const char *failureMessageForOp(QTest::ComparisonOperation op) @@ -606,29 +613,28 @@ static const char *failureMessageForOp(QTest::ComparisonOperation op) case ComparisonOperation::CustomCompare: return "Compared values are not the same"; /* not used */ case ComparisonOperation::Equal: - return "Left value is expected to be equal to right value, but is not"; + return "The computed value is expected to be equal to the baseline, but is not"; case ComparisonOperation::NotEqual: - return "Left value is expected to be different from right value, but is not"; + return "The computed value is expected to be different from the baseline, but is not"; case ComparisonOperation::LessThan: - return "Left value is expected to be less than right value, but is not"; + return "The computed value is expected to be less than the baseline, but is not"; case ComparisonOperation::LessThanOrEqual: - return "Left value is expected to be less than or equal to right value, but is not"; + return "The computed value is expected to be less than or equal to the baseline, but is not"; case ComparisonOperation::GreaterThan: - return "Left value is expected to be greater than right value, but is not"; + return "The computed value is expected to be greater than the baseline, but is not"; case ComparisonOperation::GreaterThanOrEqual: - return "Left value is expected to be greater than or equal to right value, but is not"; + return "The computed value is expected to be greater than or equal to the baseline, but is not"; } - Q_UNREACHABLE(); - return ""; + Q_UNREACHABLE_RETURN(""); } -bool QTestResult::reportResult(bool success, qxp::function_ref<const char *()> lhs, - qxp::function_ref<const char *()> rhs, +bool QTestResult::reportResult(bool success, const void *lhs, const void *rhs, + const char *(*lhsFormatter)(const void*), + const char *(*rhsFormatter)(const void*), const char *lhsExpr, const char *rhsExpr, QTest::ComparisonOperation op, const char *file, int line, const char *failureMessage) { - const size_t maxMsgLen = 1024; char msg[maxMsgLen]; msg[0] = '\0'; @@ -648,8 +654,8 @@ bool QTestResult::reportResult(bool success, qxp::function_ref<const char *()> l return checkStatement(success, msg, file, line); } - const std::unique_ptr<const char[]> lhsPtr{ lhs() }; - const std::unique_ptr<const char[]> rhsPtr{ rhs() }; + const std::unique_ptr<const char[]> lhsPtr{ lhsFormatter(lhs) }; + const std::unique_ptr<const char[]> rhsPtr{ rhsFormatter(rhs) }; if (!failureMessage) failureMessage = failureMessageForOp(op); |