feat: unit test support

This commit is contained in:
Gary Wang 2019-07-31 13:05:39 +08:00
parent 5ecfcf6935
commit 5b8374b4dd
5 changed files with 137 additions and 1 deletions

View File

@ -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)

View File

@ -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);

25
test/CMakeLists.txt Normal file
View File

@ -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)

50
test/tst_matchgioenum.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <QString>
#include <QtTest>
#include <dgiofile.h>
#include <dgiofileinfo.h>
#include <dgiomountoperation.h>
#include <giomm.h>
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"

View File

@ -0,0 +1,37 @@
#include <QString>
#include <QtTest>
#include <dgiofile.h>
#include <dgiofileinfo.h>
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<DGioFile> file(DGioFile::createFromPath(tmpFileInfo.absoluteFilePath()));
QCOMPARE(file->basename(), tmpFileInfo.fileName());
QCOMPARE(file->path(), tmpFileInfo.absoluteFilePath());
}
QTEST_APPLESS_MAIN(DGioSimpleFileInfoTest)
#include "tst_simplefileinfo.moc"