summaryrefslogtreecommitdiffstats
path: root/src/testlib
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2024-01-03 16:19:09 +0100
committerMarc Mutz <marc.mutz@qt.io>2024-01-19 12:58:57 +0000
commit1353c6f85757ad9d9f77bc754bf16c7fb185df00 (patch)
treebeb14170b330cbfd50826b624676cb430237ccf2 /src/testlib
parenta73f10aee422bbbee22c63ff1c436cb3193938e6 (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>
Diffstat (limited to 'src/testlib')
-rw-r--r--src/testlib/qcomparisontesthelper_p.h40
1 files changed, 40 insertions, 0 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