QCOMPARE: allow mixed-type comparisons

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<QLatin1Char, QLatin1Char>
   // 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<T1, T2>() overload.

[ChangeLog][QtTest] QCOMPARE can now be used for mixed-type
comparisons.

Change-Id: I0413dbd3689809852413eaca22827257f1527720
Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@qt.io>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Marc Mutz 2017-02-22 15:40:43 +01:00
parent 3f135dbcb9
commit a611a9f537
2 changed files with 9 additions and 6 deletions

View File

@ -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 <T1, T2> out of paranoia:
template <typename T>
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 <typename T1, typename T2>
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)

View File

@ -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.