summaryrefslogtreecommitdiffstats
path: root/src/testlib/qtestresult.cpp
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2022-07-09 16:12:40 +0200
committerTor Arne Vestbø <tor.arne.vestbo@qt.io>2022-07-10 03:08:32 +0000
commit54b276be0b5885bbaee2c38f472eb39731fd684a (patch)
tree54930bb8584873a4d4db9845d8722dae4c556fcc /src/testlib/qtestresult.cpp
parent07ada0b971a84483f3676b5fef46ff2bd6d26dbb (diff)
testlib: Don't print QCOMPARE values if they lack string representation
Before 0681a2dd5a8095baddb5905fb21a58ce19b958c5, QCOMPARE'ing types for which no QTest::toString specialization exists did not output Actual and Expected lines on failure, as that would only print <null> for both values (which then look like the same value, confusingly). Commit 0681a2dd5a8095baddb5905fb21a58ce19b958c5 changed that behavior, and started printing the confusing <null> values. Take care of the logic in the formatFailMessage function: if both values are nullptr, then print only the variable names, but not the confusing <null> text representation of the values. Remove dead and duplicated code related to the formatting logic, add a self-test function, and update the expected_cmptest files. Fixes: QTBUG-104867 Pick-to: 6.4 Change-Id: I4be98e79f91196b14690a2cc0a68ffd50b431a45 Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/testlib/qtestresult.cpp')
-rw-r--r--src/testlib/qtestresult.cpp50
1 files changed, 27 insertions, 23 deletions
diff --git a/src/testlib/qtestresult.cpp b/src/testlib/qtestresult.cpp
index 14d344f381..556cafd4d4 100644
--- a/src/testlib/qtestresult.cpp
+++ b/src/testlib/qtestresult.cpp
@@ -321,43 +321,47 @@ static const char *rightArgNameForOp(QTest::ComparisonOperation op)
return op == QTest::ComparisonOperation::CustomCompare ? "Expected " : "Right ";
}
-// 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>