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ø --- .../auto/testlib/selftests/cmptest/tst_cmptest.cpp | 48 ++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) (limited to 'tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp') diff --git a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp index ff6375292f..fca49c9e07 100644 --- a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp +++ b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp @@ -141,7 +141,12 @@ private slots: void compare_tostring_data(); void compareQStringLists(); void compareQStringLists_data(); + void compareQListInt_data(); void compareQListInt(); + void compareQListIntToArray_data(); + void compareQListIntToArray(); + void compareQListIntToInitializerList_data(); + void compareQListIntToInitializerList(); void compareQListDouble(); #ifdef QT_GUI_LIB void compareQColor_data(); @@ -425,11 +430,48 @@ void tst_Cmptest::compareQStringLists() QCOMPARE(opA, opB); } +using IntList = QList; + +void tst_Cmptest::compareQListInt_data() +{ + QTest::addColumn("actual"); + + QTest::newRow("match") << IntList{1, 2, 3}; + QTest::newRow("size mismatch") << IntList{1, 2}; + QTest::newRow("value mismatch") << IntList{1, 2, 4}; +} + void tst_Cmptest::compareQListInt() { - QList int1; int1 << 1 << 2 << 3; - QList int2; int2 << 1 << 2 << 4; - QCOMPARE(int1, int2); + QFETCH(IntList, actual); + const QList expected{1, 2, 3}; + QCOMPARE(actual, expected); +} + +void tst_Cmptest::compareQListIntToArray_data() +{ + compareQListInt_data(); +} + +void tst_Cmptest::compareQListIntToArray() +{ + QFETCH(IntList, actual); + const int expected[] = {1, 2, 3}; + QCOMPARE(actual, expected); +} + +void tst_Cmptest::compareQListIntToInitializerList_data() +{ + compareQListInt_data(); +} + +void tst_Cmptest::compareQListIntToInitializerList() +{ + QFETCH(IntList, actual); + // Protect ',' in the list +#define ARG(...) __VA_ARGS__ + QCOMPARE(actual, ARG({1, 2, 3})); +#undef ARG } void tst_Cmptest::compareQListDouble() -- cgit v1.2.3