QT_TEST_EQUALITY_OPS: insert QCOMPARE before the current code

The macro, as it was, makes debugging REALLY hard:

FAIL!  : tst_QStorageInfo::operatorEqual()
 The computed value is expected to be equal to the baseline, but is not
  Computed (lhs == rhs)   : 0
  Baseline (expectedEqual): 1
  Loc: [qcomparisontesthelper_p.h(178)]

It prints the location of the macro helper, not the line that called it,
so one has to guess which of the many uses in a function has failed. And
it prints the boolean of whether the comparison result is the expected
value, not the values of left and right.

QT_TEST_ALL_COMPARISON_OPS also needs a similar fix, but I don't have
time for it. Ideally we'd replace the qScopeGuard with a proper QTest
failure report, which would extract the printers for Left and Right.

Change-Id: I7a643066137c5fcdb9e7fffd83d9bf7cf4cccd6c
Reviewed-by: Rym Bouabid <rym.bouabid@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
(cherry picked from commit 4a4c4252919d4894c7e5c6f2e636dda5dc1b1d0d)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Thiago Macieira 2025-02-12 10:00:43 -08:00 committed by Qt Cherry-pick Bot
parent 5518a384b2
commit fab3e958da

View File

@ -173,12 +173,27 @@ void testAllComparisonOperatorsCompile()
\endcode
*/
template <typename LeftType, typename RightType>
void testEqualityOperators(LeftType lhs, RightType rhs, bool expectedEqual)
void testEqualityOperators(LeftType lhs, RightType rhs, bool expectedEqual,
const char *lhsExpr, const char *rhsExpr, const char *expected,
const char *file, int line)
{
if (expectedEqual) {
if (!QTest::qCompareOp<QTest::ComparisonOperation::Equal>(lhs, rhs, lhsExpr, rhsExpr, file, line))
QTEST_FAIL_ACTION;
} else {
if (!QTest::qCompareOp<QTest::ComparisonOperation::NotEqual>(lhs, rhs, lhsExpr, rhsExpr, file, line))
QTEST_FAIL_ACTION;
}
auto report = qScopeGuard([=] {
qDebug("testEqualityOperators(%s,%s,%s) failed in %s on line %d", lhsExpr, rhsExpr,
expected, file, line);
});
CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, ==, expectedEqual);
CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, lhs, rhs, !=, !expectedEqual);
CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, ==, expectedEqual);
CHECK_RUNTIME_CREF(CHECK_RUNTIME_LR, rhs, lhs, !=, !expectedEqual);
report.dismiss();
}
/*!
@ -327,14 +342,10 @@ void testAllComparisonOperators(LeftType lhs, RightType rhs, OrderingType expect
*/
#define QT_TEST_EQUALITY_OPS(Left, Right, Expected) \
do { \
auto report = qScopeGuard([] { \
qDebug("testEqualityOperators(" #Left ", " #Right ", " #Expected ") " \
"failed in " __FILE__ " on line %d", __LINE__); \
}); \
QTestPrivate::testEqualityOperators(Left, Right, Expected); \
QTestPrivate::testEqualityOperators(Left, Right, Expected, #Left, #Right, #Expected, \
__FILE__, __LINE__); \
if (QTest::currentTestFailed()) \
return; \
report.dismiss(); \
} while (false)
/*!