diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2016-02-22 20:29:32 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2016-04-08 05:38:33 +0000 |
commit | 1ed8a7bff503aacf55f7f880ddaad461ea15e5e1 (patch) | |
tree | 06b264813602be5a9c442b3ba395e3b7227943a2 | |
parent | 3f41e8a2f51698d690a6b1f6a1f4acfe13ff50af (diff) |
QTest: support nullptr in QCOMPARE
This allows to write
QCOMPARE(ptr, nullptr);
instead of
QVERIFY(ptr);
Task-number: QTBUG-49973
Change-Id: I6e1327d4327bcf17bd9b59de4352fdcaae98ac27
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
-rw-r--r-- | src/testlib/qtest.h | 5 | ||||
-rw-r--r-- | src/testlib/qtestcase.cpp | 8 | ||||
-rw-r--r-- | src/testlib/qtestcase.h | 29 | ||||
-rw-r--r-- | src/testlib/qtestcase.qdoc | 8 | ||||
-rw-r--r-- | tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp | 19 | ||||
-rw-r--r-- | tests/auto/testlib/selftests/expected_cmptest.lightxml | 4 | ||||
-rw-r--r-- | tests/auto/testlib/selftests/expected_cmptest.teamcity | 2 | ||||
-rw-r--r-- | tests/auto/testlib/selftests/expected_cmptest.txt | 3 | ||||
-rw-r--r-- | tests/auto/testlib/selftests/expected_cmptest.xml | 4 | ||||
-rw-r--r-- | tests/auto/testlib/selftests/expected_cmptest.xunitxml | 3 |
10 files changed, 82 insertions, 3 deletions
diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index 0c03ab620b..7352e89016 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -189,6 +189,11 @@ template<> inline char *toString(const QHostAddress &addr) } #endif +inline char *toString(std::nullptr_t) +{ + return toString(QLatin1String("nullptr")); +} + template<> inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual, const char *expected, const char *file, int line) diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 0ee75d484a..385e456923 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -2405,6 +2405,14 @@ bool QTest::compare_string_helper(const char *t1, const char *t2, const char *ac \internal */ +/*! \fn bool QTest::qCompare(T *t, std::nullptr_t, const char *actual, const char *expected, const char *file, int line) + \internal +*/ + +/*! \fn bool QTest::qCompare(std::nullptr_t, T *t, const char *actual, const char *expected, const char *file, int line) + \internal +*/ + /*! \fn bool QTest::qCompare(T *t1, T *t2, const char *actual, const char *expected, const char *file, int line) \internal */ diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index a27f7820f9..0395875cb3 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -332,13 +332,27 @@ namespace QTest Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line); - inline bool compare_ptr_helper(const void *t1, const void *t2, const char *actual, + inline bool compare_ptr_helper(const volatile void *t1, const volatile void *t2, const char *actual, const char *expected, const char *file, int line) { return compare_helper(t1 == t2, "Compared pointers are not the same", toString(t1), toString(t2), actual, expected, file, line); } + inline bool compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual, + const char *expected, const char *file, int line) + { + return compare_helper(t1 == nullptr, "Compared pointers are not the same", + toString(t1), toString(nullptr), actual, expected, file, line); + } + + inline bool compare_ptr_helper(std::nullptr_t, const volatile void *t2, const char *actual, + const char *expected, const char *file, int line) + { + return compare_helper(nullptr == t2, "Compared pointers are not the same", + toString(nullptr), toString(t2), actual, expected, file, line); + } + Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line); @@ -388,6 +402,19 @@ namespace QTest return compare_ptr_helper(t1, t2, actual, expected, file, line); } + template <typename T> + inline bool qCompare(T *t1, std::nullptr_t, const char *actual, const char *expected, + const char *file, int line) + { + return compare_ptr_helper(t1, nullptr, actual, expected, file, line); + } + template <typename T> + inline bool qCompare(std::nullptr_t, T *t2, const char *actual, const char *expected, + const char *file, int line) + { + return compare_ptr_helper(nullptr, t2, actual, expected, file, line); + } + template <typename T1, typename T2> inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected, const char *file, int line) diff --git a/src/testlib/qtestcase.qdoc b/src/testlib/qtestcase.qdoc index f3de8964ac..e2fc6e107f 100644 --- a/src/testlib/qtestcase.qdoc +++ b/src/testlib/qtestcase.qdoc @@ -906,6 +906,14 @@ */ /*! + \fn char *QTest::toString(std::nullptr_t) + \overload + \since 5.8 + + Returns a string containing \c{nullptr}. +*/ + +/*! \fn char *QTest::toString(const QString &string) \overload diff --git a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp index 204758f68a..9ada718682 100644 --- a/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp +++ b/tests/auto/testlib/selftests/cmptest/tst_cmptest.cpp @@ -128,6 +128,7 @@ private slots: void compare_registered_enums(); void compare_class_enums(); void compare_boolfuncs(); + void compare_to_nullptr(); void compare_pointerfuncs(); void compare_tostring(); void compare_tostring_data(); @@ -180,6 +181,24 @@ void tst_Cmptest::compare_boolfuncs() QCOMPARE(!boolfunc(), false); } +namespace { +template <typename T> +T *null() Q_DECL_NOTHROW { return nullptr; } +} + +void tst_Cmptest::compare_to_nullptr() +{ + QCOMPARE(null<int>(), nullptr); + QCOMPARE(null<const int>(), nullptr); + QCOMPARE(null<volatile int>(), nullptr); + QCOMPARE(null<const volatile int>(), nullptr); + + QCOMPARE(nullptr, null<int>()); + QCOMPARE(nullptr, null<const int>()); + QCOMPARE(nullptr, null<volatile int>()); + QCOMPARE(nullptr, null<const volatile int>()); +} + static int i = 0; static int *intptr() { return &i; } diff --git a/tests/auto/testlib/selftests/expected_cmptest.lightxml b/tests/auto/testlib/selftests/expected_cmptest.lightxml index 440429e430..d4ba020000 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.lightxml +++ b/tests/auto/testlib/selftests/expected_cmptest.lightxml @@ -33,6 +33,10 @@ <Incident type="pass" file="" line="0" /> <Duration msecs="0"/> </TestFunction> +<TestFunction name="compare_to_nullptr"> +<Incident type="pass" file="" line="0" /> + <Duration msecs="0"/> +</TestFunction> <TestFunction name="compare_pointerfuncs"> <Incident type="pass" file="" line="0" /> <Duration msecs="0"/> diff --git a/tests/auto/testlib/selftests/expected_cmptest.teamcity b/tests/auto/testlib/selftests/expected_cmptest.teamcity index dea19b60b4..c64060fdf2 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.teamcity +++ b/tests/auto/testlib/selftests/expected_cmptest.teamcity @@ -12,6 +12,8 @@ ##teamcity[testFinished name='compare_class_enums()'] ##teamcity[testStarted name='compare_boolfuncs()'] ##teamcity[testFinished name='compare_boolfuncs()'] +##teamcity[testStarted name='compare_to_nullptr()'] +##teamcity[testFinished name='compare_to_nullptr()'] ##teamcity[testStarted name='compare_pointerfuncs()'] ##teamcity[testFinished name='compare_pointerfuncs()'] ##teamcity[testStarted name='compare_tostring(int, string)'] diff --git a/tests/auto/testlib/selftests/expected_cmptest.txt b/tests/auto/testlib/selftests/expected_cmptest.txt index 100fd5e8ef..ceee0f1efa 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.txt +++ b/tests/auto/testlib/selftests/expected_cmptest.txt @@ -12,6 +12,7 @@ FAIL! : tst_Cmptest::compare_class_enums() Compared values are not the same Expected (MyClassEnum::MyClassEnumValue2): MyClassEnumValue2 Loc: [tst_cmptest.cpp(168)] PASS : tst_Cmptest::compare_boolfuncs() +PASS : tst_Cmptest::compare_to_nullptr() PASS : tst_Cmptest::compare_pointerfuncs() FAIL! : tst_Cmptest::compare_tostring(int, string) Compared values are not the same Actual (actual) : QVariant(int,123) @@ -109,5 +110,5 @@ FAIL! : tst_Cmptest::tryVerify() 'opaqueFunc() < 2' returned FALSE. () FAIL! : tst_Cmptest::tryVerify2() 'opaqueFunc() < 2' returned FALSE. (42) Loc: [tst_cmptest.cpp(439)] PASS : tst_Cmptest::cleanupTestCase() -Totals: 11 passed, 28 failed, 0 skipped, 0 blacklisted, 247ms +Totals: 12 passed, 28 failed, 0 skipped, 0 blacklisted, 247ms ********* Finished testing of tst_Cmptest ********* diff --git a/tests/auto/testlib/selftests/expected_cmptest.xml b/tests/auto/testlib/selftests/expected_cmptest.xml index f45d9ba1da..2eddfa67d9 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.xml +++ b/tests/auto/testlib/selftests/expected_cmptest.xml @@ -35,6 +35,10 @@ <Incident type="pass" file="" line="0" /> <Duration msecs="0"/> </TestFunction> +<TestFunction name="compare_to_nullptr"> +<Incident type="pass" file="" line="0" /> + <Duration msecs="0"/> +</TestFunction> <TestFunction name="compare_pointerfuncs"> <Incident type="pass" file="" line="0" /> <Duration msecs="0"/> diff --git a/tests/auto/testlib/selftests/expected_cmptest.xunitxml b/tests/auto/testlib/selftests/expected_cmptest.xunitxml index ec2c3f023c..d044771b14 100644 --- a/tests/auto/testlib/selftests/expected_cmptest.xunitxml +++ b/tests/auto/testlib/selftests/expected_cmptest.xunitxml @@ -1,5 +1,5 @@ <?xml version="1.0" encoding="UTF-8" ?> -<testsuite errors="0" failures="28" tests="18" name="tst_Cmptest"> +<testsuite errors="0" failures="28" tests="19" name="tst_Cmptest"> <properties> <property value="@INSERT_QT_VERSION_HERE@" name="QTestVersion"/> <property value="@INSERT_QT_VERSION_HERE@" name="QtVersion"/> @@ -20,6 +20,7 @@ Expected (MyClassEnum::MyClassEnumValue2): MyClassEnumValue2" result="fail"/> </testcase> <testcase result="pass" name="compare_boolfuncs"/> + <testcase result="pass" name="compare_to_nullptr"/> <testcase result="pass" name="compare_pointerfuncs"/> <testcase result="fail" name="compare_tostring"> <failure tag="int, string" message="Compared values are not the same |