From 5b8374b4dd7cd6c1e22bc11a2efb906eb8f51735 Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Wed, 31 Jul 2019 13:05:39 +0800 Subject: [PATCH] feat: unit test support --- CMakeLists.txt | 8 +++++- gio-qt/source/dgiofile.cpp | 18 +++++++++++++ test/CMakeLists.txt | 25 +++++++++++++++++++ test/tst_matchgioenum.cpp | 50 +++++++++++++++++++++++++++++++++++++ test/tst_simplefileinfo.cpp | 37 +++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 test/CMakeLists.txt create mode 100644 test/tst_matchgioenum.cpp create mode 100644 test/tst_simplefileinfo.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 406713f..cb1b651 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,6 +2,7 @@ project(gio-qt) cmake_minimum_required(VERSION 3.9.5) +option(BUILD_TESTS "Build tests" OFF) option(BUILD_UTILS "Build utilities" ON) option(BUILD_DOCS "Build documentation" OFF) @@ -37,9 +38,14 @@ pkg_check_modules(GIOMM giomm-2.4) # look into FindPkgConfig.cmake add_subdirectory (gio-qt) +if (BUILD_TESTS) + find_package(Qt5 ${QT_MINIMUM_VERSION} CONFIG REQUIRED Test) + add_subdirectory(test) +endif () + if (BUILD_UTILS) add_subdirectory(qgio-tools) -endif() +endif () if (BUILD_DOCS) if (NOT DOXYGEN_FOUND) diff --git a/gio-qt/source/dgiofile.cpp b/gio-qt/source/dgiofile.cpp index a995a13..0ef176d 100644 --- a/gio-qt/source/dgiofile.cpp +++ b/gio-qt/source/dgiofile.cpp @@ -119,6 +119,16 @@ DGioFile *DGioFile::createFromUri(QString uri, QObject *parent) return new DGioFile(gmmFile.release(), parent); } +/*! + * \brief Gets the base name (the last component of the path) of the DGioFile + * + * Wrapper of Gio::File::get_basename(), normally return filename with suffix (without path). + * + * If called for the top level of a system (such as the filesystem root or a uri like sftp://host/) + * it will return a single directory separator (and on Windows, possibly a drive letter). + * + * If you want to use filenames in a user interface you should use DGioFileInfo::displayName() instead. + */ QString DGioFile::basename() const { Q_D(const DGioFile); @@ -126,6 +136,14 @@ QString DGioFile::basename() const return QString::fromStdString(d->getGmmFileInstance()->get_basename()); } +/*! + * \brief Gets the local pathname of the DGioFile, if one exists. + * + * Wrapper of Gio::File::get_path(). For local file it gets the local pathname with filename included, + * for filesystem it gets the mount point path. + * + * If valid, this is guaranteed to be an absolute, canonical path. It might contain symlinks. + */ QString DGioFile::path() const { Q_D(const DGioFile); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..ab35845 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,25 @@ +enable_testing () + +function(DGIO_CREATE_TEST _generated_target_name _input_file_name _use_giomm) + set (extra_libraries) + if (_use_giomm) + list(APPEND extra_libraries ${GIOMM_LIBRARIES}) + endif () + + add_executable (${_generated_target_name} + ${_input_file_name} + ) + add_test (NAME ${_generated_target_name} COMMAND ${_generated_target_name} ) + target_link_libraries (${_generated_target_name} gio-qt Qt5::Test ${extra_libraries}) + if (_use_giomm) + target_include_directories(${_generated_target_name} PRIVATE ${GIOMM_INCLUDE_DIRS}) + target_compile_definitions(${_generated_target_name} PRIVATE QT_NO_KEYWORDS) + endif () +endfunction() + + +# Match GIO Enum +dgio_create_test (tst_matchgioenum tst_matchgioenum.cpp YES) + +# Simple FileInfo +dgio_create_test (tst_simplefileinfo tst_simplefileinfo.cpp NO) diff --git a/test/tst_matchgioenum.cpp b/test/tst_matchgioenum.cpp new file mode 100644 index 0000000..3b2fef2 --- /dev/null +++ b/test/tst_matchgioenum.cpp @@ -0,0 +1,50 @@ +#include +#include + +#include +#include +#include + +#include + +class DGioMatchGioEnumTest : public QObject +{ + Q_OBJECT + +public: + DGioMatchGioEnumTest(); + +private Q_SLOTS: + void testCase_DGioFileClass(); + void testCase_DGioFileInfoClass(); + void testCase_DGioMountOperationClass(); +}; + +DGioMatchGioEnumTest::DGioMatchGioEnumTest() +{ + // +} + +void DGioMatchGioEnumTest::testCase_DGioFileClass() +{ + QCOMPARE(DGioFileQueryInfoFlag::FILE_QUERY_INFO_NONE, Gio::FILE_QUERY_INFO_NONE); + QCOMPARE(DGioFileQueryInfoFlag::FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, Gio::FILE_QUERY_INFO_NOFOLLOW_SYMLINKS); +} + +void DGioMatchGioEnumTest::testCase_DGioFileInfoClass() +{ + QCOMPARE(DGioFileType::FILE_TYPE_NOT_KNOWN, Gio::FILE_TYPE_NOT_KNOWN); + QCOMPARE(DGioFileType::FILE_TYPE_SYMBOLIC_LINK, Gio::FILE_TYPE_SYMBOLIC_LINK); + QCOMPARE(DGioFileType::FILE_TYPE_MOUNTABLE, Gio::FILE_TYPE_MOUNTABLE); +} + +void DGioMatchGioEnumTest::testCase_DGioMountOperationClass() +{ + QCOMPARE(DGioAskPasswordFlag::ASK_PASSWORD_NEED_PASSWORD, Gio::ASK_PASSWORD_NEED_PASSWORD); + QCOMPARE(DGioAskPasswordFlag::ASK_PASSWORD_NEED_DOMAIN, Gio::ASK_PASSWORD_NEED_DOMAIN); + QCOMPARE(DGioAskPasswordFlag::ASK_PASSWORD_ANONYMOUS_SUPPORTED, Gio::ASK_PASSWORD_ANONYMOUS_SUPPORTED); +} + +QTEST_APPLESS_MAIN(DGioMatchGioEnumTest) + +#include "tst_matchgioenum.moc" diff --git a/test/tst_simplefileinfo.cpp b/test/tst_simplefileinfo.cpp new file mode 100644 index 0000000..c2c85e8 --- /dev/null +++ b/test/tst_simplefileinfo.cpp @@ -0,0 +1,37 @@ +#include +#include + +#include +#include + +class DGioSimpleFileInfoTest : public QObject +{ + Q_OBJECT + +public: + DGioSimpleFileInfoTest(); + +private Q_SLOTS: + void testCase_RegularFile(); +}; + +DGioSimpleFileInfoTest::DGioSimpleFileInfoTest() +{ + // +} + +void DGioSimpleFileInfoTest::testCase_RegularFile() +{ + QTemporaryFile tmpFile("test_RegularFile.txt"); + QVERIFY(tmpFile.open()); + QFileInfo tmpFileInfo(tmpFile); + + QScopedPointer file(DGioFile::createFromPath(tmpFileInfo.absoluteFilePath())); + + QCOMPARE(file->basename(), tmpFileInfo.fileName()); + QCOMPARE(file->path(), tmpFileInfo.absoluteFilePath()); +} + +QTEST_APPLESS_MAIN(DGioSimpleFileInfoTest) + +#include "tst_simplefileinfo.moc"