diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 000000000..8b6659a62 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,4 @@ +set( EXECUTABLE_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR} ) + +kde4_add_unit_test( shelltest shelltest.cpp ) +target_link_libraries( shelltest ${KDE4_KDECORE_LIBS} ${QT_QTTEST_LIBRARY} ) diff --git a/tests/shelltest.cpp b/tests/shelltest.cpp new file mode 100644 index 000000000..64c12e646 --- /dev/null +++ b/tests/shelltest.cpp @@ -0,0 +1,105 @@ +/*************************************************************************** + * Copyright (C) 2009 by Pino Toscano * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + ***************************************************************************/ + +#include +#include +#include + +namespace QTest +{ +template<> +char* toString( const KUrl& url ) +{ + return qstrdup( url.url().toLocal8Bit() ); +} +} + +static const KUrl makeUrlFromCwd( const QString& u, const QString& ref = QString() ) +{ + KUrl url( KUrl( QDir::currentPath() + "/" ), u ); + if ( !ref.isEmpty() ) + url.setRef( ref ); + url.cleanPath(); + return url; +} + +class ShellTest + : public QObject +{ + Q_OBJECT + + private slots: + void initTestCase(); + void testUrlArgs_data(); + void testUrlArgs(); +}; + +void ShellTest::initTestCase() +{ + qRegisterMetaType(); + + KCmdLineArgs::setCwd( QDir::currentPath().toLocal8Bit() ); +} + +void ShellTest::testUrlArgs_data() +{ + QTest::addColumn( "arg" ); + QTest::addColumn( "exists" ); + QTest::addColumn( "resUrl" ); + + // local files + QTest::newRow( "foo.pdf" ) + << "foo.pdf" + << true + << makeUrlFromCwd( "foo.pdf" ); + QTest::newRow( "foo.pdf" ) + << "foo.pdf" + << false + << makeUrlFromCwd( "foo.pdf" ); + QTest::newRow( "foo#bar.pdf" ) + << "foo#bar.pdf" + << false + << makeUrlFromCwd( "foo#bar.pdf" ); + + // non-local files + QTest::newRow( "http://kde.org/foo.pdf" ) + << "http://kde.org/foo.pdf" + << true + << makeUrlFromCwd( "http://kde.org/foo.pdf" ); + QTest::newRow( "http://kde.org/foo#bar.pdf" ) + << "http://kde.org/foo#bar.pdf" + << true + << makeUrlFromCwd( "http://kde.org/foo#bar.pdf" ); +} + +void ShellTest::testUrlArgs() +{ + QFETCH( QString, arg ); + QFETCH( bool, exists ); + QFETCH( KUrl, resUrl ); + + // note: below is a snippet taken from the Shell ctor + arg.replace(QRegExp("^file:/{1,3}"), "/"); + KUrl url = KCmdLineArgs::makeURL(arg.toUtf8()); + int sharpPos = -1; + if (!url.isLocalFile() || !exists) + { + sharpPos = arg.lastIndexOf(QLatin1Char('#')); + } + if (sharpPos != -1) + { + url = KCmdLineArgs::makeURL(arg.left(sharpPos).toUtf8()); + url.setHTMLRef(arg.mid(sharpPos + 1)); + } + QCOMPARE( url, resUrl ); +} + +QTEST_KDEMAIN_CORE( ShellTest ) + +#include "shelltest.moc"