diff options
author | Samuel Gaist <samuel.gaist@edeltech.ch> | 2017-12-07 09:46:30 +0100 |
---|---|---|
committer | Samuel Gaist <samuel.gaist@edeltech.ch> | 2018-06-03 20:26:38 +0000 |
commit | 04b180f7f25d73d002df31085cf1c352e075d4e5 (patch) | |
tree | 63b20b4fb53704192e231b1fa208885698066c23 /src | |
parent | 9998654eacfa07a567e25ac5f2e557f61091145d (diff) |
Improve std::tuple handling in tests
Currently when doing comparison with std::tuple the fallback toString
method is called which returns a Q_NULLPTR thus not allowing proper
diagnostic of the values that triggered an error. This patch
adds support for std::tuple to improve the tests output readability.
[ChangeLog][QtTest][QCOMPARE] Now outputs contents of std::tuple on
failure.
Change-Id: I046a55e2ce44c3f7728d51e4745120d38aa5e007
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/testlib/qtest.h | 21 | ||||
-rw-r--r-- | src/testlib/qtestcase.cpp | 25 | ||||
-rw-r--r-- | src/testlib/qtestcase.h | 5 |
3 files changed, 51 insertions, 0 deletions
diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index 1cb6a91d33..927b68bd27 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -59,6 +59,8 @@ #include <QtCore/qsize.h> #include <QtCore/qrect.h> +#include <memory> + QT_BEGIN_NAMESPACE @@ -215,6 +217,25 @@ inline char *toString(const std::pair<T1, T2> &pair) return toString(QString::asprintf("std::pair(%s,%s)", first.data(), second.data())); } +template <typename Tuple, int... I> +inline char *toString(const Tuple & tuple, QtPrivate::IndexesList<I...>) { + using UP = std::unique_ptr<char[]>; + // Generate a table of N + 1 elements where N is the number of + // elements in the tuple. + // The last element is needed to support the empty tuple use case. + const UP data[] = { + UP(toString(std::get<I>(tuple)))..., UP{} + }; + return formatString("std::tuple(", ")", sizeof...(I), data[I].get()...); +} + +template <class... Types> +inline char *toString(const std::tuple<Types...> &tuple) +{ + static const std::size_t params_count = sizeof...(Types); + return toString(tuple, typename QtPrivate::Indexes<params_count>::Value()); +} + inline char *toString(std::nullptr_t) { return toString(QLatin1String("nullptr")); diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 0866176b6b..469c423109 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -1147,6 +1147,31 @@ void *fetchData(QTestData *data, const char *tagName, int typeId) } /*! + * \internal + */ +char *formatString(const char *prefix, const char *suffix, size_t numArguments, ...) +{ + va_list ap; + va_start(ap, numArguments); + + QByteArray arguments; + arguments += prefix; + + if (numArguments > 0) { + arguments += va_arg(ap, const char *); + + for (size_t i = 1; i < numArguments; ++i) { + arguments += ", "; + arguments += va_arg(ap, const char *); + } + } + + va_end(ap); + arguments += suffix; + return qstrdup(arguments.constData()); +} + +/*! \fn char* QTest::toHexRepresentation(const char *ba, int length) Returns a pointer to a string that is the string \a ba represented diff --git a/src/testlib/qtestcase.h b/src/testlib/qtestcase.h index 4bf816a850..f6891dc941 100644 --- a/src/testlib/qtestcase.h +++ b/src/testlib/qtestcase.h @@ -283,6 +283,9 @@ namespace QTest template <typename T1, typename T2> inline char *toString(const std::pair<T1, T2> &pair); + template <class... Types> + inline char *toString(const std::tuple<Types...> &tuple); + Q_TESTLIB_EXPORT char *toHexRepresentation(const char *ba, int length); Q_TESTLIB_EXPORT char *toPrettyCString(const char *unicode, int length); Q_TESTLIB_EXPORT char *toPrettyUnicode(QStringView string); @@ -388,6 +391,8 @@ namespace QTest Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line); + Q_TESTLIB_EXPORT char *formatString(const char *prefix, const char *suffix, size_t numArguments, ...); + #ifndef Q_QDOC QTEST_COMPARE_DECL(short) QTEST_COMPARE_DECL(ushort) |