From d25589e0529732996e405aaff8d6c46b012e1601 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Fri, 10 Jul 2020 11:41:33 +0200 Subject: QTestlib: Enable comparing QList against initializer lists/arrays MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It is unnecessary to create a QList container just for comparison. Split out helpers for comparing sequence sizes and sequences from qCompare(QList) and add a template for an array with a non-type template parameter for the size. One can then write something like: const int expected[] = {10, 12,...}; QCOMPARE(QFontDatabase.pointSizes(...), expected) Unfortunately, any commas in such an array will be misread by macro expansion as macro argument separators, so any expected array with more than one entry needs an extra macro expanding __VA_ARGS__. Change-Id: Ie7c8dc20bf669bbb25f6d7f8562455f8d03968c8 Reviewed-by: Qt CI Bot Reviewed-by: Tor Arne Vestbø --- src/testlib/qtestcase.qdoc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src/testlib/qtestcase.qdoc') diff --git a/src/testlib/qtestcase.qdoc b/src/testlib/qtestcase.qdoc index 98112027a5..8c89b37baf 100644 --- a/src/testlib/qtestcase.qdoc +++ b/src/testlib/qtestcase.qdoc @@ -116,19 +116,28 @@ Expected. If the parameter order is swapped, debugging a failing test can be confusing and tests expecting zero may fail due to rounding errors. + QCOMPARE() tries to output the contents of the values if the comparison fails, + so it is visible from the test log why the comparison failed. + + Example: + \snippet code/src_qtestlib_qtestcase.cpp 2 + When comparing floating-point types (\c float, \c double, and \c qfloat16), \l qFuzzyCompare() is used for finite values. If qFuzzyIsNull() is true for both values, they are also considered equal. Infinities match if they have the same sign, and any NaN as actual value matches with any NaN as expected value (even though NaN != NaN, even when they're identical). - QCOMPARE() tries to output the contents of the values if the comparison fails, - so it is visible from the test log why the comparison failed. + When comparing QList, arrays and initializer lists of the value type + can be passed as expected value: + \snippet code/src_qtestlib_qtestcase.cpp 34 - Example: - \snippet code/src_qtestlib_qtestcase.cpp 2 + Note that using initializer lists requires a defining a helper macro + to prevent the preprocessor from interpreting the commas as macro argument + delimiters: + \snippet code/src_qtestlib_qtestcase.cpp 35 - \note This macro can only be used in a test function that is invoked + \note QCOMPARE() can only be used in a test function that is invoked by the test framework. For your own classes, you can use \l QTest::toString() to format values for -- cgit v1.2.3