/**************************************************************************** ** ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of Qt Creator. ** ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Digia. For licensing terms and ** conditions see http://www.qt.io/licensing. For further information ** use the contact form at http://www.qt.io/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 or version 3 as published by the Free ** Software Foundation and appearing in the file LICENSE.LGPLv21 and ** LICENSE.LGPLv3 included in the packaging of this file. Please review the ** following information to ensure the GNU Lesser General Public License ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ****************************************************************************/ #include #include #include //TESTED_COMPONENT=src/libs/utils using namespace Utils; class tst_fileutils : public QObject { Q_OBJECT public: private slots: void parentDir_data(); void parentDir(); void isChildOf_data(); void isChildOf(); }; void tst_fileutils::parentDir_data() { QTest::addColumn("path"); QTest::addColumn("parentPath"); QTest::addColumn("expectFailMessage"); QTest::newRow("empty path") << "" << "" << ""; QTest::newRow("root only") << "/" << "" << ""; QTest::newRow("//") << "//" << "" << ""; QTest::newRow("/tmp/dir") << "/tmp/dir" << "/tmp" << ""; QTest::newRow("relative/path") << "relative/path" << "relative" << ""; QTest::newRow("relativepath") << "relativepath" << "." #if QT_VERSION < QT_VERSION_CHECK(5, 4, 0) << "see QTBUG-23892"; #else << ""; #endif // Windows stuff: #ifdef Q_OS_WIN QTest::newRow("C:/data") << "C:/data" << "C:/" << ""; QTest::newRow("C:/") << "C:/" << "" << ""; QTest::newRow("//./com1") << "//./com1" << "/" << ""; QTest::newRow("//?/path") << "//?/path" << "/" << "Qt 4 can not handle this path."; QTest::newRow("/Global??/UNC/host") << "/Global??/UNC/host" << "/Global??/UNC/host" << "Qt 4 can not handle this path."; QTest::newRow("//server/directory/file") << "//server/directory/file" << "//server/directory" << ""; QTest::newRow("//server/directory") << "//server/directory" << "//server" << ""; QTest::newRow("//server") << "//server" << "" << ""; #endif } void tst_fileutils::parentDir() { QFETCH(QString, path); QFETCH(QString, parentPath); QFETCH(QString, expectFailMessage); FileName result = FileName::fromString(path).parentDir(); if (!expectFailMessage.isEmpty()) QEXPECT_FAIL("", expectFailMessage.toUtf8().constData(), Continue); QCOMPARE(result.toString(), parentPath); } void tst_fileutils::isChildOf_data() { QTest::addColumn("path"); QTest::addColumn("childPath"); QTest::addColumn("result"); QTest::newRow("empty path") << "" << "/tmp" << false; QTest::newRow("root only") << "/" << "/tmp" << true; QTest::newRow("/tmp/dir") << "/tmp" << "/tmp/dir" << true; QTest::newRow("relative/path") << "relative" << "relative/path" << true; QTest::newRow("/tmpdir") << "/tmp" << "/tmpdir" << false; QTest::newRow("same") << "/tmp/dir" << "/tmp/dir" << false; // Windows stuff: #ifdef Q_OS_WIN QTest::newRow("C:/data") << "C:/" << "C:/data" << true; QTest::newRow("C:/") << "" << "C:/" << false; QTest::newRow("//./com1") << "/" << "//./com1" << true; QTest::newRow("//?/path") << "/" << "//?/path" << true; QTest::newRow("/Global??/UNC/host") << "/Global??/UNC/host" << "/Global??/UNC/host/file" << true; QTest::newRow("//server/directory/file") << "//server/directory" << "//server/directory/file" << true; QTest::newRow("//server/directory") << "//server" << "//server/directory" << true; #endif } void tst_fileutils::isChildOf() { QFETCH(QString, path); QFETCH(QString, childPath); QFETCH(bool, result); bool res = FileName::fromString(childPath).isChildOf(FileName::fromString(path)); QCOMPARE(res, result); } QTEST_APPLESS_MAIN(tst_fileutils) #include "tst_fileutils.moc"