Introduce macros to simplify testing comparison

The problem with the QTestPrivate::testAllComparisonOperators() and
QTestPrivate::testEqualityOperators() functions is that if they fail,
they point into the helper function, but not into the actual test that
called the helper function. This is specially annoying when some test
calls the helper function multiple times.

This patch introduces the helper macros QT_TEST_ALL_COMPARISON_OPS and
QT_TEST_EQUALITY_OPS that wrap the respective function calls together
with the QTest::currentTestFailed() check. If the test has failed,
the macro generates a meaningful debug message with the original file
name and line number.

This patch also applies the new macros to qtbase.

Task-number: QTBUG-119433
Change-Id: Iad709de45e5bf53c82e7afa8e9f51e9275c1e619
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit 1353c6f85757ad9d9f77bc754bf16c7fb185df00)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ivan Solovev 2024-01-03 16:19:09 +01:00 committed by Qt Cherry-pick Bot
parent 84fde7ef2f
commit 15aec9a37d
7 changed files with 81 additions and 70 deletions

View File

@ -330,6 +330,46 @@ void testAllComparisonOperators(LeftType lhs, RightType rhs, OrderingType expect
} // namespace QTestPrivate
/*!
\internal
A helper macro that calls QTestPrivate::testEqualityOperators(), checks the
test's state after the function is executed, and generates a meaningful
debug message with the original file and line numbers if the test has
failed.
*/
#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); \
if (QTest::currentTestFailed()) \
return; \
report.dismiss(); \
} while (false)
/*!
\internal
A helper macro that calls QTestPrivate::testAllComparisonOperators(), checks
the test's state after the function is executed, and generates a meaningful
debug message with the original file and line numbers if the test has
failed.
*/
#define QT_TEST_ALL_COMPARISON_OPS(Left, Right, Expected) \
do { \
auto report = qScopeGuard([] { \
qDebug("testAllComparisonOperators(" #Left ", " #Right ", " #Expected ") " \
"failed in " __FILE__ " on line %d", __LINE__); \
}); \
QTestPrivate::testAllComparisonOperators(Left, Right, Expected); \
if (QTest::currentTestFailed()) \
return; \
report.dismiss(); \
} while (false)
QT_END_NAMESPACE
#endif // QCOMPARISONTESTHELPER_P_H

View File

@ -201,15 +201,10 @@ void tst_QCompareHelpers::compareImpl()
QFETCH(RightType, rhs);
QFETCH(OrderingType, expectedOrdering);
QTestPrivate::testAllComparisonOperators(lhs, rhs, expectedOrdering);
if (QTest::currentTestFailed())
return;
QT_TEST_ALL_COMPARISON_OPS(lhs, rhs, expectedOrdering);
#ifdef __cpp_lib_three_way_comparison
// Also check std types.
QTestPrivate::testAllComparisonOperators(lhs, rhs,
QtOrderingPrivate::to_std(expectedOrdering));
if (QTest::currentTestFailed())
return;
QT_TEST_ALL_COMPARISON_OPS(lhs, rhs, QtOrderingPrivate::to_std(expectedOrdering));
#endif // __cpp_lib_three_way_comparison
}

View File

@ -1000,18 +1000,12 @@ void tst_QAbstractItemModel::modelIndexComparisons()
QPersistentModelIndex pmi11 = mi11;
QPersistentModelIndex pmi22 = mi22;
QTestPrivate::testEqualityOperators(mi11, mi11, true);
if (QTest::currentTestFailed()) return;
QTestPrivate::testEqualityOperators(mi11, mi22, false);
if (QTest::currentTestFailed()) return;
QTestPrivate::testEqualityOperators(pmi11, pmi11, true);
if (QTest::currentTestFailed()) return;
QTestPrivate::testEqualityOperators(pmi11, pmi22, false);
if (QTest::currentTestFailed()) return;
QTestPrivate::testEqualityOperators(pmi11, mi11, true);
if (QTest::currentTestFailed()) return;
QTestPrivate::testEqualityOperators(pmi11, mi22, false);
if (QTest::currentTestFailed()) return;
QT_TEST_EQUALITY_OPS(mi11, mi11, true);
QT_TEST_EQUALITY_OPS(mi11, mi22, false);
QT_TEST_EQUALITY_OPS(pmi11, pmi11, true);
QT_TEST_EQUALITY_OPS(pmi11, pmi22, false);
QT_TEST_EQUALITY_OPS(pmi11, mi11, true);
QT_TEST_EQUALITY_OPS(pmi11, mi22, false);
}
void tst_QAbstractItemModel::testMoveSameParentDown_data()

View File

@ -1041,9 +1041,7 @@ void tst_QDate::operator_eq_eq()
QFETCH(QDate, d2);
QFETCH(bool, expectEqual);
QTestPrivate::testEqualityOperators(d1, d2, expectEqual);
if (QTest::currentTestFailed())
return;
QT_TEST_EQUALITY_OPS(d1, d2, expectEqual);
if (expectEqual)
QVERIFY(qHash(d1) == qHash(d2));
@ -1077,7 +1075,7 @@ void tst_QDate::ordering()
QFETCH(QDate, right);
QFETCH(Qt::strong_ordering, expectedOrdering);
QTestPrivate::testAllComparisonOperators(left, right, expectedOrdering);
QT_TEST_ALL_COMPARISON_OPS(left, right, expectedOrdering);
}
void tst_QDate::ordering_chrono_types()
@ -1086,43 +1084,37 @@ void tst_QDate::ordering_chrono_types()
using namespace std::chrono;
QDate friday(2001, 11, 30); // the 5th Friday of November 2001
// std::chrono::year_month_day
QTestPrivate::testAllComparisonOperators(friday, year_month_day(2001y, November, 29d),
Qt::strong_ordering::greater);
QTestPrivate::testAllComparisonOperators(friday, year_month_day(2001y, November, 30d),
Qt::strong_ordering::equivalent);
QTestPrivate::testAllComparisonOperators(friday, year_month_day(2001y, December, 1d),
Qt::strong_ordering::less);
QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day(2001y, November, 29d),
Qt::strong_ordering::greater);
QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day(2001y, November, 30d),
Qt::strong_ordering::equivalent);
QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day(2001y, December, 1d),
Qt::strong_ordering::less);
// std::chrono::year_month_day_last
QTestPrivate::testAllComparisonOperators(friday, year_month_day_last(2001y, {October / last}),
Qt::strong_ordering::greater);
QTestPrivate::testAllComparisonOperators(friday, year_month_day_last(2001y, {November / last}),
Qt::strong_ordering::equivalent);
QTestPrivate::testAllComparisonOperators(friday, year_month_day_last(2001y, {December / last}),
Qt::strong_ordering::less);
QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day_last(2001y, {October / last}),
Qt::strong_ordering::greater);
QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day_last(2001y, {November / last}),
Qt::strong_ordering::equivalent);
QT_TEST_ALL_COMPARISON_OPS(friday, year_month_day_last(2001y, {December / last}),
Qt::strong_ordering::less);
// std::chrono::year_month_weekday
QTestPrivate::testAllComparisonOperators(friday,
year_month_weekday(2001y, November, Thursday[5]),
Qt::strong_ordering::greater);
QTestPrivate::testAllComparisonOperators(friday,
year_month_weekday(2001y, November, Friday[5]),
Qt::strong_ordering::equivalent);
QTestPrivate::testAllComparisonOperators(friday,
year_month_weekday(2001y, December, Saturday[1]),
Qt::strong_ordering::less);
QT_TEST_ALL_COMPARISON_OPS(friday, year_month_weekday(2001y, November, Thursday[5]),
Qt::strong_ordering::greater);
QT_TEST_ALL_COMPARISON_OPS(friday, year_month_weekday(2001y, November, Friday[5]),
Qt::strong_ordering::equivalent);
QT_TEST_ALL_COMPARISON_OPS(friday, year_month_weekday(2001y, December, Saturday[1]),
Qt::strong_ordering::less);
// std::chrono::year_month_weekday_last
QDate thursday(2001, 11, 29); // the last Thursday of November 2001
QTestPrivate::testAllComparisonOperators(thursday, year_month_weekday_last(2001y, November,
Wednesday[last]),
Qt::strong_ordering::greater);
QTestPrivate::testAllComparisonOperators(thursday, year_month_weekday_last(2001y, November,
Thursday[last]),
Qt::strong_ordering::equivalent);
QTestPrivate::testAllComparisonOperators(thursday, year_month_weekday_last(2001y, November,
Friday[last]),
Qt::strong_ordering::less);
QT_TEST_ALL_COMPARISON_OPS(thursday, year_month_weekday_last(2001y, November, Wednesday[last]),
Qt::strong_ordering::greater);
QT_TEST_ALL_COMPARISON_OPS(thursday, year_month_weekday_last(2001y, November, Thursday[last]),
Qt::strong_ordering::equivalent);
QT_TEST_ALL_COMPARISON_OPS(thursday, year_month_weekday_last(2001y, November, Friday[last]),
Qt::strong_ordering::less);
#else
QSKIP("This test requires C++20-level <chrono> support enabled in the standard library.");
#endif // __cpp_lib_chrono >= 201907L

View File

@ -2472,17 +2472,9 @@ void tst_QDateTime::operator_eqeq()
QFETCH(bool, expectEqual);
QFETCH(bool, checkEuro);
QTestPrivate::testEqualityOperators(dt1, dt1, true);
if (QTest::currentTestFailed())
return;
QTestPrivate::testEqualityOperators(dt2, dt2, true);
if (QTest::currentTestFailed())
return;
QTestPrivate::testEqualityOperators(dt1, dt2, expectEqual);
if (QTest::currentTestFailed())
return;
QT_TEST_EQUALITY_OPS(dt1, dt1, true);
QT_TEST_EQUALITY_OPS(dt2, dt2, true);
QT_TEST_EQUALITY_OPS(dt1, dt2, expectEqual);
QVERIFY(dt1 != QDateTime::currentDateTime());
QVERIFY(dt2 != QDateTime::currentDateTime());
@ -2553,7 +2545,7 @@ void tst_QDateTime::ordering()
QFETCH(QDateTime, right);
QFETCH(Qt::weak_ordering, expectedOrdering);
QTestPrivate::testAllComparisonOperators(left, right, expectedOrdering);
QT_TEST_ALL_COMPARISON_OPS(left, right, expectedOrdering);
}
Q_DECLARE_METATYPE(QDataStream::Version)

View File

@ -350,9 +350,7 @@ void tst_QTime::operator_eq_eq()
QFETCH(QTime, t2);
QFETCH(bool, expectEqual);
QTestPrivate::testEqualityOperators(t1, t2, expectEqual);
if (QTest::currentTestFailed())
return;
QT_TEST_EQUALITY_OPS(t1, t2, expectEqual);
if (expectEqual)
QVERIFY(qHash(t1) == qHash(t2));
@ -387,7 +385,7 @@ void tst_QTime::ordering()
QFETCH(QTime, right);
QFETCH(Qt::strong_ordering, expectedOrdering);
QTestPrivate::testAllComparisonOperators(left, right, expectedOrdering);
QT_TEST_ALL_COMPARISON_OPS(left, right, expectedOrdering);
}
#if QT_CONFIG(datestring)

View File

@ -365,7 +365,7 @@ void tst_QTimeZone::compare()
QFETCH(QTimeZone, right);
QFETCH(bool, expectedEqual);
QTestPrivate::testEqualityOperators(left, right, expectedEqual);
QT_TEST_EQUALITY_OPS(left, right, expectedEqual);
}
void tst_QTimeZone::timespec()