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 <thiago.macieira@intel.com>
This commit is contained in:
parent
49057a1eab
commit
ba7653fb74
@ -49,8 +49,72 @@
|
||||
#pragma qt_no_master_include
|
||||
#endif
|
||||
|
||||
#include <QtWidgets/QSizePolicy>
|
||||
#include <QtCore/QMetaEnum>
|
||||
|
||||
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
|
||||
|
@ -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
|
||||
|
@ -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 <typename T>
|
||||
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 : "<null>" ; }
|
||||
};
|
||||
|
||||
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()
|
||||
{
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user