Make QUrl::errorString() usable for QtTest output in invalid URLs
authorThiago Macieira <thiago.macieira@intel.com>
Tue, 18 Sep 2012 21:07:46 +0000 (23:07 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Tue, 2 Oct 2012 20:34:42 +0000 (22:34 +0200)
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>
src/corelib/io/qurl.cpp
src/testlib/qtest.h
tests/auto/testlib/selftests/expected_datetime.lightxml
tests/auto/testlib/selftests/expected_datetime.txt
tests/auto/testlib/selftests/expected_datetime.xml
tests/auto/testlib/selftests/expected_datetime.xunitxml

index 38eb613..0f8ed9f 100644 (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
index beb4ad5..a38b656 100644 (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());
 }
 
index 39af3a2..d8acae9 100644 (file)
     <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">
index 6bd4103..6e82bdd 100644 (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)
index ebdd5f4..d7c9afa 100644 (file)
     <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">
index b6bb600..af0e4db 100644 (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"/>