diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 85 | ||||
-rw-r--r-- | tests/TestUtil.h | 31 | ||||
-rw-r--r-- | tests/data/CMakeLists.txt | 4 | ||||
-rwxr-xr-x | tests/data/tst_userutils-test_createShortcut-unix | 6 | ||||
-rw-r--r-- | tests/tst_pathutils.cpp | 76 | ||||
-rw-r--r-- | tests/tst_userutils.cpp | 66 |
6 files changed, 268 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 00000000..20863c73 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,85 @@ +find_package(Qt5 COMPONENTS Test Core Network Widgets) + +include_directories(${MMC_SRC}) + +unset(MultiMC_TESTS) +macro(add_unit_test name) + unset(srcs) + foreach(arg ${testname} ${ARGN}) + list(APPEND srcs ${CMAKE_CURRENT_SOURCE_DIR}/${arg}) + endforeach() + add_executable(tst_${name} ${srcs}) + qt5_use_modules(tst_${name} Test Core Network Widgets) + target_link_libraries(tst_${name} MultiMC_common) + add_test(tst_${name} tst_${name}) + list(APPEND MultiMC_TESTS tst_${name}) +endmacro() + +macro(add_unit_test2 name) + add_unit_test(${name} tst_${name}.cpp) +endmacro() + +# Tests START # + +add_unit_test2(pathutils) +add_unit_test2(userutils) + +# Tests END # + +set(COVERAGE_SOURCE_DIRS + ${MMC_SRC}/logic/* + ${MMC_SRC}/logic/auth/* + ${MMC_SRC}/logic/auth/flows/* + ${MMC_SRC}/logic/lists/* + ${MMC_SRC}/logic/net/* + ${MMC_SRC}/logic/tasks/* + ${MMC_SRC}/gui/* + ${MMC_SRC}/gui/dialogs/* + ${MMC_SRC}/gui/widgets/* + ${MMC_SRC}/depends/settings/include/* + ${MMC_SRC}/depends/settings/src/* + ${MMC_SRC}/depends/util/include/* + ${MMC_SRC}/depends/util/src/* +) + +if(MultiMC_CODE_COVERAGE) + unset(MultiMC_RUN_TESTS) + unset(MultiMC_TEST_COVERAGE_FILES) + + foreach(test ${MultiMC_TESTS}) + add_custom_target(MultiMC_RUN_TEST_${test} + COMMAND lcov -d ${CMAKE_CURRENT_BINARY_DIR} -z -q # clean test + && lcov -d ${MMC_BIN} -z -q # clean common + && lcov -d ${MMC_BIN}/depends/settings/CMakeFiles/libSettings.dir -z -q # clean settings + && lcov -d ${MMC_BIN}/depends/utils/CMakeFiles/libUtil.dir -z -q # clean utils + && ${MMC_BIN}/${test} -o coverage_${test}.out,xml # run test + && lcov -q --checksum -b ${MMC_SRC} -d ${CMAKE_CURRENT_BINARY_DIR} -c -o coverage_${test}.info # generate for test + && lcov -q --checksum -b ${MMC_SRC} -d ${MMC_BIN} -c -o coverage_common.info # generate for common + && lcov -q --checksum -b ${MMC_SRC} -d ${MMC_BIN}/depends/settings/CMakeFiles/libSettings.dir -c -o coverage_settings.info # generate for settings + && lcov -q --checksum -b ${MMC_SRC} -d ${MMC_BIN}/depends/util/CMakeFiles/libUtil.dir -c -o coverage_utils.info # generate for utils + && lcov -q --checksum -b ${MMC_SRC} -d . + -a coverage_${test}.info -a coverage_common.info -a coverage_settings.info -a coverage_utils.info + -o coverage_${test}-combined.info # combine test and common + && lcov -q --checksum -b ${MMC_SRC} --list-full-path --extract coverage_${test}-combined.info ${COVERAGE_SOURCE_DIRS} -o coverage_${test}-stripped.info # strip + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + VERBATIM + DEPENDS ${test} + COMMENT "Running ${test}..." + ) + list(APPEND MultiMC_TEST_COVERAGE_FILES coverage_${test}-stripped.info) + list(APPEND MultiMC_RUN_TESTS MultiMC_RUN_TEST_${test}) + endforeach(test) + + add_custom_target(MultiMC_GENERATE_COVERAGE + DEPENDS ${MultiMC_RUN_TESTS} + COMMENT "Generating coverage files..." + ) + add_custom_target(MultiMC_GENERATE_COVERAGE_HTML + COMMAND genhtml -t "MultiMC 5 Test Coverage" --num-spaces 4 --demangle-cpp --legend -o ${MMC_SRC}/html/coverage ${MultiMC_TEST_COVERAGE_FILES} + DEPENDS MultiMC_GENERATE_COVERAGE + COMMENT "Generating test coverage html..." + ) + add_custom_target(MultiMC_RUN_TESTS DEPENDS MultiMC_GENERATE_COVERAGE_HTML) +endif(MultiMC_CODE_COVERAGE) + +add_subdirectory(data) diff --git a/tests/TestUtil.h b/tests/TestUtil.h new file mode 100644 index 00000000..64ee1675 --- /dev/null +++ b/tests/TestUtil.h @@ -0,0 +1,31 @@ +#pragma once + +#include <QFile> +#include <QCoreApplication> +#include <QTest> +#include <QDir> + +#include "MultiMC.h" + +struct TestsInternal +{ + static QByteArray readFile(const QString &fileName) + { + QFile f(fileName); + f.open(QFile::ReadOnly); + return f.readAll(); + } +}; + +#define MULTIMC_GET_TEST_FILE(file) TestsInternal::readFile(QFINDTESTDATA( file )) + +#define QTEST_GUILESS_MAIN_MULTIMC(TestObject) \ +int main(int argc, char *argv[]) \ +{ \ + char *argv_[] = { argv[0] }; \ + int argc_ = 1; \ + MultiMC app(argc_, argv_, QDir::temp().absoluteFilePath("MultiMC_Test")); \ + app.setAttribute(Qt::AA_Use96Dpi, true); \ + TestObject tc; \ + return QTest::qExec(&tc, argc, argv); \ +} diff --git a/tests/data/CMakeLists.txt b/tests/data/CMakeLists.txt new file mode 100644 index 00000000..eee5a596 --- /dev/null +++ b/tests/data/CMakeLists.txt @@ -0,0 +1,4 @@ +add_custom_target(MultiMC_Test_Data + ALL + COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} +) diff --git a/tests/data/tst_userutils-test_createShortcut-unix b/tests/data/tst_userutils-test_createShortcut-unix new file mode 100755 index 00000000..1ce3a2bd --- /dev/null +++ b/tests/data/tst_userutils-test_createShortcut-unix @@ -0,0 +1,6 @@ +[Desktop Entry] +Type=Application +TryExec=asdfDest +Exec=asdfDest 'arg1' 'arg2' +Name=asdf +Icon= diff --git a/tests/tst_pathutils.cpp b/tests/tst_pathutils.cpp new file mode 100644 index 00000000..1e4a83bf --- /dev/null +++ b/tests/tst_pathutils.cpp @@ -0,0 +1,76 @@ +#include <QTest> +#include "TestUtil.h" + +#include "depends/util/include/pathutils.h" + +class PathUtilsTest : public QObject +{ + Q_OBJECT +private +slots: + void initTestCase() + { + + } + void cleanupTestCase() + { + + } + + void test_PathCombine1_data() + { + QTest::addColumn<QString>("result"); + QTest::addColumn<QString>("path1"); + QTest::addColumn<QString>("path2"); + +#if defined(Q_OS_UNIX) + QTest::newRow("unix 1") << "/abc/def/ghi/jkl" << "/abc/def" << "ghi/jkl"; + QTest::newRow("unix 2") << "/abc/def/ghi/jkl" << "/abc/def/" << "ghi/jkl"; +#elif defined(Q_OS_WIN) + QTest::newRow("win, from C:") << "C:\\abc" << "C:" << "abc\\def"; + QTest::newRow("win 1") << "C:\\abc\\def\\ghi\\jkl" << "C:\\abc\\def" << "ghi\\jkl"; + QTest::newRow("win 2") << "C:\\abc\\def\\ghi\\jkl" << "C:\\abc\\def\\" << "ghi\\jkl"; +#endif + } + void test_PathCombine1() + { + QFETCH(QString, result); + QFETCH(QString, path1); + QFETCH(QString, path2); + + QCOMPARE(PathCombine(path1, path2), result); + } + + void test_PathCombine2_data() + { + QTest::addColumn<QString>("result"); + QTest::addColumn<QString>("path1"); + QTest::addColumn<QString>("path2"); + QTest::addColumn<QString>("path3"); + +#if defined(Q_OS_UNIX) + QTest::newRow("unix 1") << "/abc/def/ghi/jkl" << "/abc" << "def" << "ghi/jkl"; + QTest::newRow("unix 2") << "/abc/def/ghi/jkl" << "/abc/" << "def" << "ghi/jkl"; + QTest::newRow("unix 3") << "/abc/def/ghi/jkl" << "/abc" << "def/" << "ghi/jkl"; + QTest::newRow("unix 4") << "/abc/def/ghi/jkl" << "/abc/" << "def/" << "ghi/jkl"; +#elif defined(Q_OS_WIN) + QTest::newRow("win 1") << "C:\\abc\\def\\ghi\\jkl" << "C:\\abc" << "def" << "ghi\\jkl"; + QTest::newRow("win 2") << "C:\\abc\\def\\ghi\\jkl" << "C:\\abc\\" << "def" << "ghi\\jkl"; + QTest::newRow("win 3") << "C:\\abc\\def\\ghi\\jkl" << "C:\\abc" << "def\\" << "ghi\\jkl"; + QTest::newRow("win 4") << "C:\\abc\\def\\ghi\\jkl" << "C:\\abc\\" << "def" << "ghi\\jkl"; +#endif + } + void test_PathCombine2() + { + QFETCH(QString, result); + QFETCH(QString, path1); + QFETCH(QString, path2); + QFETCH(QString, path3); + + QCOMPARE(PathCombine(path1, path2, path3), result); + } +}; + +QTEST_GUILESS_MAIN_MULTIMC(PathUtilsTest) + +#include "tst_pathutils.moc" diff --git a/tests/tst_userutils.cpp b/tests/tst_userutils.cpp new file mode 100644 index 00000000..62bee985 --- /dev/null +++ b/tests/tst_userutils.cpp @@ -0,0 +1,66 @@ +#include <QTest> +#include <QStandardPaths> +#include "TestUtil.h" + +#include "depends/util/include/userutils.h" + +class UserUtilsTest : public QObject +{ + Q_OBJECT +private +slots: + void initTestCase() + { + + } + void cleanupTestCase() + { + + } + + void test_getDesktop() + { + QCOMPARE(Util::getDesktopDir(), QStandardPaths::writableLocation(QStandardPaths::DesktopLocation)); + } + + void test_createShortcut_data() + { + QTest::addColumn<QString>("location"); + QTest::addColumn<QString>("dest"); + QTest::addColumn<QStringList>("args"); + QTest::addColumn<QString>("name"); + QTest::addColumn<QString>("iconLocation"); + QTest::addColumn<QByteArray>("result"); + + QTest::newRow("unix") << QDir::currentPath() + << "asdfDest" + << (QStringList() << "arg1" << "arg2") + << "asdf" + << QString() + #if defined(Q_OS_LINUX) + << MULTIMC_GET_TEST_FILE("data/tst_userutils-test_createShortcut-unix") + #elif defined(Q_OS_WIN) + << QString() + #endif + ; + } + + void test_createShortcut() + { + QFETCH(QString, location); + QFETCH(QString, dest); + QFETCH(QStringList, args); + QFETCH(QString, name); + QFETCH(QString, iconLocation); + QFETCH(QByteArray, result); + + QVERIFY(Util::createShortCut(location, dest, args, name, iconLocation)); + QCOMPARE(QString::fromLocal8Bit(TestsInternal::readFile(location + QDir::separator() + name + ".desktop")), QString::fromLocal8Bit(result)); + + //QDir().remove(location); + } +}; + +QTEST_GUILESS_MAIN_MULTIMC(UserUtilsTest) + +#include "tst_userutils.moc" |