From a611a9f537f1187825339c2a2214c8ec4a23680f Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 22 Feb 2017 15:40:43 +0100 Subject: [PATCH] QCOMPARE: allow mixed-type comparisons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There's no reason to restrict the LHS and the RHS to the same type like it was done before. That just leads to lots of casting. or use of QVERIFY(. == .) instead of QCOMPARE, with all the disadvantages like no printing of the LHS and RHS values. The rationale given in the documentation for this behavior is incorrect. Ensuring that RHS and LHS have the same type by no means ensures that no implicit conversions take place when calling operator==. Proof: QCOMPARE(QLatin1Char('a'), QLatin1Char('a')); // instantiates qCompare // but calls operator==(QChar, QChar) If the intent is to disable implicit conversions of the argument types, then some serious metaprogramming magic would be needed, along the following lines: 1. find out which op== overload is actually chosen 2. find that overload's RHS parameter type, assert it's the same as (possibly cv-qualified) T1. 3. ditto for the LHS parameter type and T2 This is not attempted here. Fix the inconvenience this restriction caused by simply allowing two types. This cannot break existing code, since we didn't actually change anything in the qCompare() overload set. All we do is implement the existing qCompare() overload. [ChangeLog][QtTest] QCOMPARE can now be used for mixed-type comparisons. Change-Id: I0413dbd3689809852413eaca22827257f1527720 Reviewed-by: Jędrzej Nowacki Reviewed-by: Christian Kandeler Reviewed-by: Edward Welbourne --- src/testlib/qtestcase.h | 10 +++++++++- src/testlib/qtestcase.qdoc | 5 ----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 8347b99f17..3e2dab307a 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -335,6 +335,8 @@ namespace QTest Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag); Q_TESTLIB_EXPORT QTestData &addRow(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2); +#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) + // kept after adding implementation of out of paranoia: template inline bool qCompare(T const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line) @@ -342,6 +344,7 @@ namespace QTest return compare_helper(t1 == t2, "Compared values are not the same", toString(t1), toString(t2), actual, expected, file, line); } +#endif Q_TESTLIB_EXPORT bool qCompare(float const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line); @@ -392,7 +395,12 @@ namespace QTest #endif template - bool qCompare(T1 const &, T2 const &, const char *, const char *, const char *, int); + inline bool qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected, + const char *file, int line) + { + return compare_helper(t1 == t2, "Compared values are not the same", + toString(t1), toString(t2), actual, expected, file, line); + } inline bool qCompare(double const &t1, float const &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 8f3d140add..92c9093bc5 100644 --- a/src/testlib/qtestcase.qdoc +++ b/src/testlib/qtestcase.qdoc @@ -82,11 +82,6 @@ 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. - QCOMPARE is very strict on the data types. Both \a actual and \a expected - have to be of the same type, otherwise the test won't compile. This prohibits - unspecified behavior from being introduced; that is behavior that usually - occurs when the compiler implicitly casts the argument. - For your own classes, you can use \l QTest::toString() to format values for outputting into the test log.