summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Faure <faure@kde.org>2011-06-23 09:55:31 (GMT)
committerDavid Faure <faure@kde.org>2011-08-06 16:54:21 (GMT)
commit7977f1a8d7145ddadebfc539eef8af7011bd897e (patch)
tree30b5fe8bbc3628199da54271840a0710411933b6
parent10ca4ddc8cca49103c2d605a64468788605f3010 (diff)
Fix null/empty confusion in KUrl::htmlRef() and its unittests.
Patch by Andrea Iacovitti <aiacovitti@libero.it>, who wrote: This aims to be a fix for bug #276204 related to htmlRef(), and the kurl unit test that return a false positive because of QCOMPARE treating QString("") == QString() After fixing the unit test related to url reference stuff, 2 of them failed. One related to htmlRef() (as in #276204), the other related to setRef() not able to set Empty (but not Null) ref. -> fixed setRef() too. CCBUG: 276204
-rw-r--r--kdecore/io/kurl.cpp8
-rw-r--r--kdecore/tests/kurltest.cpp38
-rw-r--r--kdecore/tests/kurltest.h1
3 files changed, 34 insertions, 13 deletions
diff --git a/kdecore/io/kurl.cpp b/kdecore/io/kurl.cpp
index ecf2a52..90a8d49 100644
--- a/kdecore/io/kurl.cpp
+++ b/kdecore/io/kurl.cpp
@@ -1497,11 +1497,11 @@ QString KUrl::htmlRef() const
{
if ( !hasSubUrl() )
{
- return QUrl::fromPercentEncoding( ref().toLatin1() );
+ return fragment();
}
const List lst = split( *this );
- return QUrl::fromPercentEncoding( (*lst.begin()).ref().toLatin1() );
+ return (*lst.begin()).fragment();
}
QString KUrl::encodedHtmlRef() const
@@ -1873,8 +1873,8 @@ bool KUrl::hasRef() const
void KUrl::setRef( const QString& fragment )
{
- if ( fragment.isNull() )
- setFragment( fragment ); // pass null, not empty
+ if ( fragment.isEmpty() ) // empty or null
+ setFragment( fragment );
else
setFragment( QUrl::fromPercentEncoding( fragment.toLatin1() ) );
}
diff --git a/kdecore/tests/kurltest.cpp b/kdecore/tests/kurltest.cpp
index 2a33894..c2ecaea 100644
--- a/kdecore/tests/kurltest.cpp
+++ b/kdecore/tests/kurltest.cpp
@@ -40,6 +40,9 @@ QTEST_KDEMAIN_CORE( KUrlTest )
#include <stdio.h>
#include <stdlib.h>
+//QCOMPARE cannot be used to strictly check for empty or null QString as it treats QString("") == QString()
+#define QSTREMPTY(_str) QVERIFY(!_str.isNull() && _str.isEmpty())
+
void KUrlTest::testEmptyURL()
{
KUrl emptyURL;
@@ -106,6 +109,23 @@ void KUrlTest::testSetQuery()
QCOMPARE( url1.query(), QString() );
}
+void KUrlTest::testEmptyNullReference()
+{
+ KUrl url1 = KUrl("http://www.kde.org");
+ QVERIFY( !url1.hasRef() );
+ QVERIFY( !url1.hasHTMLRef() );
+ QVERIFY( url1.ref().isNull() );
+ QVERIFY( url1.htmlRef().isNull() );
+ QVERIFY( url1.encodedHtmlRef().isNull() );
+
+ url1 = "http://www.kde.org#";
+ QVERIFY( url1.hasRef() );
+ QVERIFY( url1.hasHTMLRef() );
+ QSTREMPTY( url1.ref() );
+ QSTREMPTY( url1.htmlRef() );
+ QSTREMPTY( url1.encodedHtmlRef() );
+}
+
void KUrlTest::testSetRef()
{
KUrl url1 = KUrl( QByteArray( "http://www.kde.org/foo.cgi#foo=bar" ) );
@@ -119,9 +139,9 @@ void KUrlTest::testSetRef()
QCOMPARE( url1.ref(), QString("#" ) );
#endif
url1.setRef( "" );
- QCOMPARE( url1.ref(), QString("" ) );
+ QSTREMPTY( url1.ref() );
url1.setRef( QString() );
- QCOMPARE( url1.ref(), QString() );
+ QVERIFY( url1.ref().isNull() );
}
void KUrlTest::testSetHTMLRef()
@@ -136,9 +156,9 @@ void KUrlTest::testSetHTMLRef()
QCOMPARE( url1.htmlRef(), QString("#") );
QCOMPARE( url1.ref(), QString("%23") ); // it's encoded
url1.setHTMLRef( "" );
- QCOMPARE( url1.htmlRef(), QString("") );
+ QSTREMPTY( url1.htmlRef() );
url1.setHTMLRef( QString() );
- QCOMPARE( url1.htmlRef(), QString() );
+ QVERIFY( url1.htmlRef().isNull() );
}
void KUrlTest::testQUrl()
@@ -412,7 +432,7 @@ void KUrlTest::testEmptyQueryOrRef()
QVERIFY( waba1.hasRef() );
QVERIFY( waba1.hasFragment() );
QVERIFY( waba1.hasHTMLRef() );
- QCOMPARE( waba1.encodedHtmlRef(), QString() );
+ QSTREMPTY( waba1.encodedHtmlRef() );
//qurl = QUrl::fromEncoded("http://www.kde.org/cgi/test.cgi#", QUrl::TolerantMode);
//QCOMPARE( qurl.toEncoded(), QByteArray("http://www.kde.org/cgi/test.cgi#") );
@@ -1240,7 +1260,7 @@ void KUrlTest::testSubURL()
QVERIFY( url1.hasRef() );
QVERIFY( !url1.hasHTMLRef() );
QVERIFY( url1.hasSubUrl() );
- QCOMPARE( url1.htmlRef(), QString("") );
+ QVERIFY( url1.htmlRef().isNull() );
QCOMPARE( url1.upUrl().url(), QString("file:///home/dfaure/") );
u1 = "file:///home/dfaure/my%20tar%20file.tgz#gzip:/#tar:/";
@@ -1249,7 +1269,7 @@ void KUrlTest::testSubURL()
QVERIFY( url1.hasRef() );
QVERIFY( !url1.hasHTMLRef() );
QVERIFY( url1.hasSubUrl() );
- QCOMPARE( url1.htmlRef(), QString("") );
+ QVERIFY( url1.htmlRef().isNull() );
QCOMPARE( url1.upUrl().url(), QString("file:///home/dfaure/") );
#if 0
@@ -1260,7 +1280,7 @@ void KUrlTest::testSubURL()
QVERIFY( !url1.hasRef() );
QVERIFY( !url1.hasHTMLRef() );
QVERIFY( url1.hasSubUrl() );
- QCOMPARE( url1.htmlRef(), QString("") );
+ QVERIFY( url1.htmlRef().isNull() );
QCOMPARE( url1.upUrl().url(), QString("file:///home/dfaure/cdrdao-1.1.5/dao/#CdrDriver.cc#") );
#endif
@@ -1270,7 +1290,7 @@ void KUrlTest::testSubURL()
QVERIFY( url1.hasRef() );
QVERIFY( !url1.hasHTMLRef() );
QVERIFY( url1.hasSubUrl() );
- QCOMPARE( url1.htmlRef(), QString("") );
+ QVERIFY( url1.htmlRef().isNull() );
const KUrl::List url1Splitted = KUrl::split( url1 );
QCOMPARE( url1Splitted.count(), 3 );
//kDebug() << url1Splitted.toStringList();
diff --git a/kdecore/tests/kurltest.h b/kdecore/tests/kurltest.h
index 4f1a1fd..a158f02 100644
--- a/kdecore/tests/kurltest.h
+++ b/kdecore/tests/kurltest.h
@@ -29,6 +29,7 @@ private Q_SLOTS:
void testEmptyURL();
void testIsValid();
void testSetQuery();
+ void testEmptyNullReference();
void testSetRef();
void testSetHTMLRef();
void testDirectory();