diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2024-01-03 16:19:09 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2024-01-19 12:58:57 +0000 |
commit | 1353c6f85757ad9d9f77bc754bf16c7fb185df00 (patch) | |
tree | beb14170b330cbfd50826b624676cb430237ccf2 | |
parent | a73f10aee422bbbee22c63ff1c436cb3193938e6 (diff) |
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
Pick-to: 6.7
Change-Id: Iad709de45e5bf53c82e7afa8e9f51e9275c1e619
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
7 files changed, 81 insertions, 70 deletions
diff --git a/src/testlib/qcomparisontesthelper_p.h b/src/testlib/qcomparisontesthelper_p.h index 9658db9b88..b422fc4049 100644 --- a/src/testlib/qcomparisontesthelper_p.h +++ b/src/testlib/qcomparisontesthelper_p.h @@ -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 diff --git a/tests/auto/corelib/global/qcomparehelpers/tst_qcomparehelpers.cpp b/tests/auto/corelib/global/qcomparehelpers/tst_qcomparehelpers.cpp index 8da320a63a..16dbd9a93e 100644 --- a/tests/auto/corelib/global/qcomparehelpers/tst_qcomparehelpers.cpp +++ b/tests/auto/corelib/global/qcomparehelpers/tst_qcomparehelpers.cpp @@ -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 } diff --git a/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp b/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp index 4b810678d7..98bb9d3013 100644 --- a/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp +++ b/tests/auto/corelib/itemmodels/qabstractitemmodel/tst_qabstractitemmodel.cpp @@ -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() diff --git a/tests/auto/corelib/time/qdate/tst_qdate.cpp b/tests/auto/corelib/time/qdate/tst_qdate.cpp index 42fbdba3a9..f80b42c7d9 100644 --- a/tests/auto/corelib/time/qdate/tst_qdate.cpp +++ b/tests/auto/corelib/time/qdate/tst_qdate.cpp @@ -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 diff --git a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp index 7c21d413ff..efdfca6b3c 100644 --- a/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/time/qdatetime/tst_qdatetime.cpp @@ -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) diff --git a/tests/auto/corelib/time/qtime/tst_qtime.cpp b/tests/auto/corelib/time/qtime/tst_qtime.cpp index c1cb5d7d40..30fee92049 100644 --- a/tests/auto/corelib/time/qtime/tst_qtime.cpp +++ b/tests/auto/corelib/time/qtime/tst_qtime.cpp @@ -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) diff --git a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp index c4ba809bb5..f52226e0e8 100644 --- a/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp +++ b/tests/auto/corelib/time/qtimezone/tst_qtimezone.cpp @@ -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() |