From 99ce02f692bd9ad59f5e159ce65ef13f4e041a31 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Thu, 16 Apr 2020 19:09:34 -0300 Subject: QCborValue: add support for QCOMPARE string output Change-Id: Ibdc95e9af7bd456a94ecfffd16066c47ea9766d0 Reviewed-by: Ulf Hermann Reviewed-by: Edward Welbourne --- src/testlib/qtest.h | 145 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 142 insertions(+), 3 deletions(-) (limited to 'src/testlib/qtest.h') diff --git a/src/testlib/qtest.h b/src/testlib/qtest.h index cceefde84a..57612de3de 100644 --- a/src/testlib/qtest.h +++ b/src/testlib/qtest.h @@ -1,7 +1,7 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2016 Intel Corporation. +** Copyright (C) 2019 The Qt Company Ltd. +** Copyright (C) 2020 Intel Corporation. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtTest module of the Qt Toolkit. @@ -48,6 +48,10 @@ #include #include +#include +#include +#include +#include #include #include #include @@ -69,7 +73,6 @@ QT_BEGIN_NAMESPACE - namespace QTest { @@ -232,6 +235,142 @@ template<> inline char *toString(const QVariant &v) return qstrdup(vstring.constData()); } +namespace Internal { +struct QCborValueFormatter +{ + enum { BufferLen = 256 }; + static char *formatSimpleType(QCborSimpleType st) + { + char *buf = new char[BufferLen]; + qsnprintf(buf, BufferLen, "QCborValue(QCborSimpleType(%d))", int(st)); + return buf; + } + + static char *formatTag(QCborTag tag, const QCborValue &taggedValue) + { + QScopedArrayPointer hold(format(taggedValue)); + char *buf = new char[BufferLen]; + qsnprintf(buf, BufferLen, "QCborValue(QCborTag(%llu), %s)", tag, hold.get()); + return buf; + } + + static char *innerFormat(QCborValue::Type t, const char *str) + { + static const QMetaEnum typeEnum = []() { + int idx = QCborValue::staticMetaObject.indexOfEnumerator("Type"); + return QCborValue::staticMetaObject.enumerator(idx); + }(); + + char *buf = new char[BufferLen]; + const char *typeName = typeEnum.valueToKey(t); + if (typeName) + qsnprintf(buf, BufferLen, "QCborValue(%s, %s)", typeName, str); + else + qsnprintf(buf, BufferLen, "QCborValue()", t); + return buf; + } + + template static char *format(QCborValue::Type type, const T &t) + { + QScopedArrayPointer hold(QTest::toString(t)); + return innerFormat(type, hold.get()); + } + + static char *format(const QCborValue &v) + { + switch (v.type()) { + case QCborValue::Integer: + return format(v.type(), v.toInteger()); + case QCborValue::ByteArray: + return format(v.type(), v.toByteArray()); + case QCborValue::String: + return format(v.type(), v.toString()); + case QCborValue::Array: + return innerFormat(v.type(), QScopedArrayPointer(format(v.toArray())).get()); + case QCborValue::Map: + return innerFormat(v.type(), QScopedArrayPointer(format(v.toMap())).get()); + case QCborValue::Tag: + return formatTag(v.tag(), v.taggedValue()); + case QCborValue::SimpleType: + break; + case QCborValue::True: + return qstrdup("QCborValue(true)"); + case QCborValue::False: + return qstrdup("QCborValue(false)"); + case QCborValue::Null: + return qstrdup("QCborValue(nullptr)"); + case QCborValue::Undefined: + return qstrdup("QCborValue()"); + case QCborValue::Double: + return format(v.type(), v.toDouble()); + case QCborValue::DateTime: + case QCborValue::Url: + case QCborValue::RegularExpression: + return format(v.type(), v.taggedValue().toString()); + case QCborValue::Uuid: + return format(v.type(), v.toUuid()); + case QCborValue::Invalid: + return qstrdup("QCborValue()"); + } + + if (v.isSimpleType()) + return formatSimpleType(v.toSimpleType()); + return innerFormat(v.type(), ""); + } + + static char *format(const QCborArray &a) + { + QByteArray out(1, '['); + const char *comma = ""; + for (const QCborValueRef v : a) { + QScopedArrayPointer s(format(v)); + out += comma; + out += s.get(); + comma = ", "; + } + out += ']'; + return qstrdup(out.constData()); + } + + static char *format(const QCborMap &m) + { + QByteArray out(1, '{'); + const char *comma = ""; + for (auto pair : m) { + QScopedArrayPointer key(format(pair.first)); + QScopedArrayPointer value(format(pair.second)); + out += comma; + out += key.get(); + out += ": "; + out += value.get(); + comma = ", "; + } + out += '}'; + return qstrdup(out.constData()); + } +}; +} + +template<> inline char *toString(const QCborValue &v) +{ + return Internal::QCborValueFormatter::format(v); +} + +template<> inline char *toString(const QCborValueRef &v) +{ + return toString(QCborValue(v)); +} + +template<> inline char *toString(const QCborArray &a) +{ + return Internal::QCborValueFormatter::format(a); +} + +template<> inline char *toString(const QCborMap &m) +{ + return Internal::QCborValueFormatter::format(m); +} + template inline char *toString(const std::pair &pair) { -- cgit v1.2.3