Make QUrl::errorString() usable for QtTest output in invalid URLs

If an URL is invalid, let's indicate that in the test output. To be
helpful, let's make QUrl::errorString() include the component form of
the URL.

Change-Id: Iaafe16973ded79c7ea688fbb23808d91253e8c14
Reviewed-by: David Faure <faure@kde.org>
This commit is contained in:
Thiago Macieira 2012-09-18 23:07:46 +02:00 committed by The Qt Project
parent adce435a0c
commit 59ad0019dc
6 changed files with 64 additions and 29 deletions

View File

@ -3403,27 +3403,8 @@ QDebug operator<<(QDebug d, const QUrl &url)
}
#endif
/*!
\since 4.2
Returns a text string that explains why an URL is invalid in the case being;
otherwise returns an empty string.
*/
QString QUrl::errorString() const
static QString errorMessage(QUrlPrivate::ErrorCode errorCode, QChar c)
{
if (!d)
return QString();
QUrlPrivate::ErrorCode errorCode = d->validityError();
if (errorCode == QUrlPrivate::NoError)
return QString();
// check if the error code matches a section with error
// unless it's a compound error (0x10000)
if ((d->sectionHasError & (errorCode >> 8)) == 0 && (errorCode & 0x10000) == 0)
return QString();
QChar c = d->errorSupplement;
switch (errorCode) {
case QUrlPrivate::NoError:
return QString();
@ -3444,7 +3425,7 @@ QString QUrl::errorString() const
.arg(c);
case QUrlPrivate::InvalidRegNameError:
if (d->errorSupplement)
if (!c.isNull())
return QString(QStringLiteral("Invalid hostname (character '%1' not permitted)"))
.arg(c);
else
@ -3482,6 +3463,58 @@ QString QUrl::errorString() const
return QStringLiteral("<unknown error>");
}
static inline void appendComponentIfPresent(QString &msg, bool present, const char *componentName,
const QString &component)
{
if (present) {
msg += QLatin1String(componentName);
msg += QLatin1Char('"');
msg += component;
msg += QLatin1String("\",");
}
}
/*!
\since 4.2
Returns a text string that explains why an URL is invalid in the case being;
otherwise returns an empty string.
*/
QString QUrl::errorString() const
{
if (!d)
return QString();
QUrlPrivate::ErrorCode errorCode = d->validityError();
if (errorCode == QUrlPrivate::NoError)
return QString();
// check if the error code matches a section with error
// unless it's a compound error (0x10000)
if ((d->sectionHasError & (errorCode >> 8)) == 0 && (errorCode & 0x10000) == 0)
return QString();
QString msg = errorMessage(errorCode, d->errorSupplement);
msg += QLatin1Char(';');
appendComponentIfPresent(msg, d->sectionIsPresent & QUrlPrivate::Scheme,
" scheme = ", d->scheme);
appendComponentIfPresent(msg, d->sectionIsPresent & QUrlPrivate::UserInfo,
" userinfo = ", userInfo());
appendComponentIfPresent(msg, d->sectionIsPresent & QUrlPrivate::Host,
" host = ", d->host);
appendComponentIfPresent(msg, d->port != -1,
" port = ", QString::number(d->port));
appendComponentIfPresent(msg, !d->path.isEmpty(),
" path = ", d->path);
appendComponentIfPresent(msg, d->sectionIsPresent & QUrlPrivate::Query,
" query = ", d->query);
appendComponentIfPresent(msg, d->sectionIsPresent & QUrlPrivate::Fragment,
" fragment = ", d->fragment);
if (msg.endsWith(QLatin1Char(',')))
msg.chop(1);
return msg;
}
/*!
\typedef QUrl::DataPtr
\internal

View File

@ -144,6 +144,8 @@ template<> inline char *toString(const QRectF &s)
template<> inline char *toString(const QUrl &uri)
{
if (!uri.isValid())
return qstrdup(QByteArray("Invalid URL: " + uri.errorString().toLatin1()).constData());
return qstrdup(uri.toEncoded().constData());
}

View File

@ -20,12 +20,12 @@
<DataTag><![CDATA[empty rhs]]></DataTag>
<Description><![CDATA[Compared values are not the same
Actual (operandA): http://example.com
Expected (operandB): ]]></Description>
Expected (operandB): Invalid URL: ]]></Description>
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/datetime/tst_datetime.cpp" line="74">
<DataTag><![CDATA[empty lhs]]></DataTag>
<Description><![CDATA[Compared values are not the same
Actual (operandA):
Actual (operandA): Invalid URL:
Expected (operandB): http://example.com]]></Description>
</Incident>
<Incident type="pass" file="" line="0">

View File

@ -8,10 +8,10 @@ FAIL! : tst_DateTime::dateTime() Compared values are not the same
PASS : tst_DateTime::qurl(empty urls)
FAIL! : tst_DateTime::qurl(empty rhs) Compared values are not the same
Actual (operandA): http://example.com
Expected (operandB):
Expected (operandB): Invalid URL:
Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/datetime/tst_datetime.cpp(41)]
FAIL! : tst_DateTime::qurl(empty lhs) Compared values are not the same
Actual (operandA):
Actual (operandA): Invalid URL:
Expected (operandB): http://example.com
Loc: [/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/datetime/tst_datetime.cpp(41)]
PASS : tst_DateTime::qurl(same urls)

View File

@ -22,12 +22,12 @@
<DataTag><![CDATA[empty rhs]]></DataTag>
<Description><![CDATA[Compared values are not the same
Actual (operandA): http://example.com
Expected (operandB): ]]></Description>
Expected (operandB): Invalid URL: ]]></Description>
</Incident>
<Incident type="fail" file="/home/user/dev/qt5/qtbase/tests/auto/testlib/selftests/datetime/tst_datetime.cpp" line="74">
<DataTag><![CDATA[empty lhs]]></DataTag>
<Description><![CDATA[Compared values are not the same
Actual (operandA):
Actual (operandA): Invalid URL:
Expected (operandB): http://example.com]]></Description>
</Incident>
<Incident type="pass" file="" line="0">

View File

@ -13,9 +13,9 @@
<testcase result="fail" name="qurl">
<failure tag="empty rhs" message="Compared values are not the same
Actual (operandA): http://example.com
Expected (operandB): " result="fail"/>
Expected (operandB): Invalid URL: " result="fail"/>
<failure tag="empty lhs" message="Compared values are not the same
Actual (operandA):
Actual (operandA): Invalid URL:
Expected (operandB): http://example.com" result="fail"/>
</testcase>
<testcase result="pass" name="cleanupTestCase"/>