diff options
author | Thiago Macieira <thiago.macieira@intel.com> | 2012-09-18 23:07:46 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-10-02 22:34:42 +0200 |
commit | 59ad0019dcbb59b25a0d252575fe831b189d16f6 (patch) | |
tree | 80e9c85e11f707b11df8477426871915507062fa /src/corelib/io/qurl.cpp | |
parent | adce435a0c6f6d515fa4dd3af86c725843158514 (diff) |
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>
Diffstat (limited to 'src/corelib/io/qurl.cpp')
-rw-r--r-- | src/corelib/io/qurl.cpp | 75 |
1 files changed, 54 insertions, 21 deletions
diff --git a/src/corelib/io/qurl.cpp b/src/corelib/io/qurl.cpp index 38eb613d74..0f8ed9fb17 100644 --- a/src/corelib/io/qurl.cpp +++ b/src/corelib/io/qurl.cpp @@ -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 |