From ba7653fb74a288fbb0c7df3c24001b7170e087ae Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 17 Dec 2014 15:49:50 +0100 Subject: [PATCH] qtest_widgets.h: add support for pretty-printing QSizePolicy ... in QCOMPAREs. The implementation is hidden in a nested Internal namespace that retrieves the strings without strdup()ing. That makes it easier to compose these functions as there is no need to delete character arrays when using them. The public interface (which qstrdup()s) is implemented on top of these. [ChangeLog][QtTest] QCOMPARE now pretty-prints QSizePolicy{,::Policy,::ControlType{,s}}. Change-Id: Ib03d969847e5a12474c71a7921366b400025f680 Reviewed-by: Thiago Macieira --- src/testlib/qtest_widgets.h | 64 +++++++++++++++++++ src/testlib/qtestcase.cpp | 37 +++++++++++ .../kernel/qsizepolicy/tst_qsizepolicy.cpp | 50 +++++++++++++++ 3 files changed, 151 insertions(+) diff --git a/src/testlib/qtest_widgets.h b/src/testlib/qtest_widgets.h index f188e60b164..8d7752f9645 100644 --- a/src/testlib/qtest_widgets.h +++ b/src/testlib/qtest_widgets.h @@ -49,8 +49,72 @@ #pragma qt_no_master_include #endif +#include +#include + QT_BEGIN_NAMESPACE +namespace QTest +{ + +// +// QSizePolicy & friends: +// + +namespace Internal +{ + +inline const char *toString(QSizePolicy::Policy p) +{ + static const QMetaEnum me = QSizePolicy::staticMetaObject.enumerator(QSizePolicy::staticMetaObject.indexOfEnumerator("Policy")); + return me.valueToKey(int(p)); +} + +inline QByteArray toString(QSizePolicy::ControlTypes ct) +{ + static const QMetaEnum me = QSizePolicy::staticMetaObject.enumerator(QSizePolicy::staticMetaObject.indexOfEnumerator("ControlTypes")); + return me.valueToKeys(int(ct)); +} + +inline QByteArray toString(QSizePolicy sp) +{ + static const char comma[] = ", "; + return QByteArray("QSizePolicy(") + + Internal::toString(sp.horizontalPolicy()) + comma + + Internal::toString(sp.verticalPolicy()) + comma + + QByteArray::number(sp.horizontalStretch()) + comma + + QByteArray::number(sp.verticalStretch()) + comma + + Internal::toString(QSizePolicy::ControlTypes(sp.controlType())) + comma + + "height for width: " + (sp.hasHeightForWidth() ? "yes" : "no") + comma + + "width for height: " + (sp.hasWidthForHeight() ? "yes" : "no") + comma + + (sp.retainSizeWhenHidden() ? "" : "don't " ) + "retain size when hidden" + + ')'; +} + +} // namespace Internal + +inline char *toString(QSizePolicy::Policy p) +{ + return qstrdup(Internal::toString(p)); +} + +inline char *toString(QSizePolicy::ControlTypes ct) +{ + return qstrdup(Internal::toString(ct).constData()); +} + +inline char *toString(QSizePolicy::ControlType ct) +{ + return toString(QSizePolicy::ControlTypes(ct)); +} + +inline char *toString(QSizePolicy sp) +{ + return qstrdup(Internal::toString(sp).constData()); +} + +} // namespace QTest + QT_END_NAMESPACE #endif diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 4e7ab18e9b2..dc039682583 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -97,6 +97,11 @@ using QtMiscUtils::toHexUpper; See the \l{Qt Test Overview} for information about how to write unit tests. */ +/*! + \namespace QTest::Internal + \internal +*/ + /*! \macro QVERIFY(condition) \relates QTest @@ -1041,6 +1046,38 @@ using QtMiscUtils::toHexUpper; Returns a textual representation of the given \a variant. */ +/*! + \fn char *QTest::toString(QSizePolicy::ControlType ct) + \overload + \since 5.5 + + Returns a textual representation of control type \a ct. +*/ + +/*! + \fn char *QTest::toString(QSizePolicy::ControlTypes cts) + \overload + \since 5.5 + + Returns a textual representation of control types \a cts. +*/ + +/*! + \fn char *QTest::toString(QSizePolicy::Policy p) + \overload + \since 5.5 + + Returns a textual representation of policy \a p. +*/ + +/*! + \fn char *QTest::toString(QSizePolicy sp) + \overload + \since 5.5 + + Returns a textual representation of size policy \a sp. +*/ + /*! \fn void QTest::qWait(int ms) Waits for \a ms milliseconds. While waiting, events will be processed and diff --git a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp index 022bd7f61ed..eacbd645fa5 100644 --- a/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp +++ b/tests/auto/widgets/kernel/qsizepolicy/tst_qsizepolicy.cpp @@ -40,6 +40,7 @@ class tst_QSizePolicy : public QObject Q_OBJECT private Q_SLOTS: + void qtest(); void defaultValues(); void getSetCheck(); void dataStream(); @@ -47,6 +48,55 @@ private Q_SLOTS: void verticalStretch(); }; + +struct PrettyPrint { + const char *m_s; + template + explicit PrettyPrint(const T &t) : m_s(Q_NULLPTR) + { + using QT_PREPEND_NAMESPACE(QTest)::toString; + m_s = toString(t); + } + ~PrettyPrint() { delete[] m_s; } + const char* s() const { return m_s ? m_s : "" ; } +}; + +void tst_QSizePolicy::qtest() +{ +#define CHECK(x) QCOMPARE(PrettyPrint(QSizePolicy::x).s(), #x) + // Policy: + CHECK(Fixed); + CHECK(Minimum); + CHECK(Ignored); + CHECK(MinimumExpanding); + CHECK(Expanding); + CHECK(Maximum); + CHECK(Preferred); + // ControlType: + CHECK(ButtonBox); + CHECK(CheckBox); + CHECK(ComboBox); + CHECK(Frame); + CHECK(GroupBox); + CHECK(Label); + CHECK(Line); + CHECK(LineEdit); + CHECK(PushButton); + CHECK(RadioButton); + CHECK(Slider); + CHECK(SpinBox); + CHECK(TabWidget); + CHECK(ToolButton); +#undef CHECK +#define CHECK2(x, y) QCOMPARE(PrettyPrint(QSizePolicy::x|QSizePolicy::y).s(), \ + QSizePolicy::x < QSizePolicy::y ? #x "|" #y : #y "|" #x) + // ControlTypes (sample) + CHECK2(ButtonBox, CheckBox); + CHECK2(CheckBox, ButtonBox); + CHECK2(ToolButton, Slider); +#undef CHECK2 +} + void tst_QSizePolicy::defaultValues() { {