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>
This commit is contained in:
parent
3f41e8a2f5
commit
1ed8a7bff5
@ -189,6 +189,11 @@ template<> inline char *toString(const QHostAddress &addr)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
inline char *toString(std::nullptr_t)
|
||||||
|
{
|
||||||
|
return toString(QLatin1String("nullptr"));
|
||||||
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual,
|
inline bool qCompare(QString const &t1, QLatin1String const &t2, const char *actual,
|
||||||
const char *expected, const char *file, int line)
|
const char *expected, const char *file, int line)
|
||||||
|
@ -2405,6 +2405,14 @@ bool QTest::compare_string_helper(const char *t1, const char *t2, const char *ac
|
|||||||
\internal
|
\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)
|
/*! \fn bool QTest::qCompare(T *t1, T *t2, const char *actual, const char *expected, const char *file, int line)
|
||||||
\internal
|
\internal
|
||||||
*/
|
*/
|
||||||
|
@ -332,13 +332,27 @@ namespace QTest
|
|||||||
Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2,
|
Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2,
|
||||||
const char *actual, const char *expected, const char *file, int line);
|
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)
|
const char *expected, const char *file, int line)
|
||||||
{
|
{
|
||||||
return compare_helper(t1 == t2, "Compared pointers are not the same",
|
return compare_helper(t1 == t2, "Compared pointers are not the same",
|
||||||
toString(t1), toString(t2), actual, expected, file, line);
|
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,
|
Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual,
|
||||||
const char *expected, const char *file, int line);
|
const char *expected, const char *file, int line);
|
||||||
|
|
||||||
@ -388,6 +402,19 @@ namespace QTest
|
|||||||
return compare_ptr_helper(t1, t2, actual, expected, file, line);
|
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>
|
template <typename T1, typename T2>
|
||||||
inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
|
inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
|
||||||
const char *file, int line)
|
const char *file, int line)
|
||||||
|
@ -905,6 +905,14 @@
|
|||||||
Returns a textual representation of the given \a string.
|
Returns a textual representation of the given \a string.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn char *QTest::toString(std::nullptr_t)
|
||||||
|
\overload
|
||||||
|
\since 5.8
|
||||||
|
|
||||||
|
Returns a string containing \c{nullptr}.
|
||||||
|
*/
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn char *QTest::toString(const QString &string)
|
\fn char *QTest::toString(const QString &string)
|
||||||
\overload
|
\overload
|
||||||
|
@ -128,6 +128,7 @@ private slots:
|
|||||||
void compare_registered_enums();
|
void compare_registered_enums();
|
||||||
void compare_class_enums();
|
void compare_class_enums();
|
||||||
void compare_boolfuncs();
|
void compare_boolfuncs();
|
||||||
|
void compare_to_nullptr();
|
||||||
void compare_pointerfuncs();
|
void compare_pointerfuncs();
|
||||||
void compare_tostring();
|
void compare_tostring();
|
||||||
void compare_tostring_data();
|
void compare_tostring_data();
|
||||||
@ -180,6 +181,24 @@ void tst_Cmptest::compare_boolfuncs()
|
|||||||
QCOMPARE(!boolfunc(), false);
|
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 i = 0;
|
||||||
|
|
||||||
static int *intptr() { return &i; }
|
static int *intptr() { return &i; }
|
||||||
|
@ -33,6 +33,10 @@
|
|||||||
<Incident type="pass" file="" line="0" />
|
<Incident type="pass" file="" line="0" />
|
||||||
<Duration msecs="0"/>
|
<Duration msecs="0"/>
|
||||||
</TestFunction>
|
</TestFunction>
|
||||||
|
<TestFunction name="compare_to_nullptr">
|
||||||
|
<Incident type="pass" file="" line="0" />
|
||||||
|
<Duration msecs="0"/>
|
||||||
|
</TestFunction>
|
||||||
<TestFunction name="compare_pointerfuncs">
|
<TestFunction name="compare_pointerfuncs">
|
||||||
<Incident type="pass" file="" line="0" />
|
<Incident type="pass" file="" line="0" />
|
||||||
<Duration msecs="0"/>
|
<Duration msecs="0"/>
|
||||||
|
@ -12,6 +12,8 @@
|
|||||||
##teamcity[testFinished name='compare_class_enums()']
|
##teamcity[testFinished name='compare_class_enums()']
|
||||||
##teamcity[testStarted name='compare_boolfuncs()']
|
##teamcity[testStarted name='compare_boolfuncs()']
|
||||||
##teamcity[testFinished 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[testStarted name='compare_pointerfuncs()']
|
||||||
##teamcity[testFinished name='compare_pointerfuncs()']
|
##teamcity[testFinished name='compare_pointerfuncs()']
|
||||||
##teamcity[testStarted name='compare_tostring(int, string)']
|
##teamcity[testStarted name='compare_tostring(int, string)']
|
||||||
|
@ -12,6 +12,7 @@ FAIL! : tst_Cmptest::compare_class_enums() Compared values are not the same
|
|||||||
Expected (MyClassEnum::MyClassEnumValue2): MyClassEnumValue2
|
Expected (MyClassEnum::MyClassEnumValue2): MyClassEnumValue2
|
||||||
Loc: [tst_cmptest.cpp(168)]
|
Loc: [tst_cmptest.cpp(168)]
|
||||||
PASS : tst_Cmptest::compare_boolfuncs()
|
PASS : tst_Cmptest::compare_boolfuncs()
|
||||||
|
PASS : tst_Cmptest::compare_to_nullptr()
|
||||||
PASS : tst_Cmptest::compare_pointerfuncs()
|
PASS : tst_Cmptest::compare_pointerfuncs()
|
||||||
FAIL! : tst_Cmptest::compare_tostring(int, string) Compared values are not the same
|
FAIL! : tst_Cmptest::compare_tostring(int, string) Compared values are not the same
|
||||||
Actual (actual) : QVariant(int,123)
|
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)
|
FAIL! : tst_Cmptest::tryVerify2() 'opaqueFunc() < 2' returned FALSE. (42)
|
||||||
Loc: [tst_cmptest.cpp(439)]
|
Loc: [tst_cmptest.cpp(439)]
|
||||||
PASS : tst_Cmptest::cleanupTestCase()
|
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 *********
|
********* Finished testing of tst_Cmptest *********
|
||||||
|
@ -35,6 +35,10 @@
|
|||||||
<Incident type="pass" file="" line="0" />
|
<Incident type="pass" file="" line="0" />
|
||||||
<Duration msecs="0"/>
|
<Duration msecs="0"/>
|
||||||
</TestFunction>
|
</TestFunction>
|
||||||
|
<TestFunction name="compare_to_nullptr">
|
||||||
|
<Incident type="pass" file="" line="0" />
|
||||||
|
<Duration msecs="0"/>
|
||||||
|
</TestFunction>
|
||||||
<TestFunction name="compare_pointerfuncs">
|
<TestFunction name="compare_pointerfuncs">
|
||||||
<Incident type="pass" file="" line="0" />
|
<Incident type="pass" file="" line="0" />
|
||||||
<Duration msecs="0"/>
|
<Duration msecs="0"/>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8" ?>
|
<?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>
|
<properties>
|
||||||
<property value="@INSERT_QT_VERSION_HERE@" name="QTestVersion"/>
|
<property value="@INSERT_QT_VERSION_HERE@" name="QTestVersion"/>
|
||||||
<property value="@INSERT_QT_VERSION_HERE@" name="QtVersion"/>
|
<property value="@INSERT_QT_VERSION_HERE@" name="QtVersion"/>
|
||||||
@ -20,6 +20,7 @@
|
|||||||
Expected (MyClassEnum::MyClassEnumValue2): MyClassEnumValue2" result="fail"/>
|
Expected (MyClassEnum::MyClassEnumValue2): MyClassEnumValue2" result="fail"/>
|
||||||
</testcase>
|
</testcase>
|
||||||
<testcase result="pass" name="compare_boolfuncs"/>
|
<testcase result="pass" name="compare_boolfuncs"/>
|
||||||
|
<testcase result="pass" name="compare_to_nullptr"/>
|
||||||
<testcase result="pass" name="compare_pointerfuncs"/>
|
<testcase result="pass" name="compare_pointerfuncs"/>
|
||||||
<testcase result="fail" name="compare_tostring">
|
<testcase result="fail" name="compare_tostring">
|
||||||
<failure tag="int, string" message="Compared values are not the same
|
<failure tag="int, string" message="Compared values are not the same
|
||||||
|
Loading…
x
Reference in New Issue
Block a user