Rewrite test tst_QDir::cdBelowRoot() to be data-driven.

Limit the macro #ifdefery and allow for more test cases.

Task-number: QTBUG-53712
Change-Id: I2c185efc7c3b8fcd0217d2021bd98ab6044b5aee
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Friedemann Kleint 2016-06-06 16:17:57 +02:00
parent 15a76e5719
commit ae0a2fe041

View File

@ -213,6 +213,7 @@ private slots:
void cdNonreadable(); void cdNonreadable();
void cdBelowRoot_data();
void cdBelowRoot(); void cdBelowRoot();
private: private:
@ -2264,31 +2265,37 @@ void tst_QDir::cdNonreadable()
#endif #endif
} }
void tst_QDir::cdBelowRoot_data()
{
QTest::addColumn<QString>("rootPath");
QTest::addColumn<QString>("cdInto");
QTest::addColumn<QString>("targetPath");
#if defined(Q_OS_ANDROID)
QTest::newRow("android") << "/" << "system" << "/system";
#elif defined(Q_OS_UNIX)
QTest::newRow("unix") << "/" << "tmp" << "/tmp";
#elif defined(Q_OS_WINRT)
QTest::newRow("winrt") << QDir::rootPath() << QDir::rootPath() << QDir::rootPath();
#else // Windows+CE
const QString systemDrive = QString::fromLocal8Bit(qgetenv("SystemDrive")) + QLatin1Char('/');
const QString systemRoot = QString::fromLocal8Bit(qgetenv("SystemRoot"));
QTest::newRow("windows-drive")
<< systemDrive << systemRoot.mid(3) << QDir::cleanPath(systemRoot);
#endif // Windows
}
void tst_QDir::cdBelowRoot() void tst_QDir::cdBelowRoot()
{ {
#if defined (Q_OS_ANDROID) QFETCH(QString, rootPath);
#define ROOT QString("/") QFETCH(QString, cdInto);
#define DIR QString("/system") QFETCH(QString, targetPath);
#define CD_INTO "system"
#elif defined (Q_OS_UNIX)
#define ROOT QString("/")
#define DIR QString("/tmp")
#define CD_INTO "tmp"
#elif defined (Q_OS_WINRT)
#define ROOT QDir::rootPath()
#define DIR QDir::rootPath()
#define CD_INTO QDir::rootPath()
#else
#define ROOT QString::fromLocal8Bit(qgetenv("SystemDrive"))+"/"
#define DIR QString::fromLocal8Bit(qgetenv("SystemRoot")).replace('\\', '/')
#define CD_INTO QString::fromLocal8Bit(qgetenv("SystemRoot")).mid(3)
#endif
QDir root(ROOT); QDir root(rootPath);
QVERIFY(!root.cd("..")); QVERIFY2(!root.cd(".."), qPrintable(root.absolutePath()));
QCOMPARE(root.path(), ROOT); QCOMPARE(root.path(), rootPath);
QVERIFY(root.cd(CD_INTO)); QVERIFY(root.cd(cdInto));
QCOMPARE(root.path(), DIR); QCOMPARE(root.path(), targetPath);
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
if (::getuid() == 0) if (::getuid() == 0)
QSKIP("Running this test as root doesn't make sense"); QSKIP("Running this test as root doesn't make sense");
@ -2296,13 +2303,13 @@ void tst_QDir::cdBelowRoot()
#ifdef Q_OS_WINRT #ifdef Q_OS_WINRT
QSKIP("WinRT has no concept of system root"); QSKIP("WinRT has no concept of system root");
#endif #endif
QDir dir(DIR); QDir dir(targetPath);
QVERIFY(!dir.cd("../..")); QVERIFY2(!dir.cd("../.."), qPrintable(dir.absolutePath()));
QCOMPARE(dir.path(), DIR); QCOMPARE(dir.path(), targetPath);
QVERIFY(!dir.cd("../abs/../..")); QVERIFY2(!dir.cd("../abs/../.."), qPrintable(dir.absolutePath()));
QCOMPARE(dir.path(), DIR); QCOMPARE(dir.path(), targetPath);
QVERIFY(dir.cd("..")); QVERIFY(dir.cd(".."));
QCOMPARE(dir.path(), ROOT); QCOMPARE(dir.path(), rootPath);
} }
QTEST_MAIN(tst_QDir) QTEST_MAIN(tst_QDir)