Unit testing

This commit is contained in:
Jan Dalheimer 2013-12-02 11:09:56 +01:00
parent 613699b362
commit 0a6399b52c
13 changed files with 271 additions and 36 deletions

1
.gitignore vendored
View File

@ -10,6 +10,7 @@ resources/CMakeFiles
resources/MultiMCLauncher.jar
*~
*.swp
html/
# Ctags File
tags

View File

@ -449,6 +449,15 @@ IF(WIN32)
)
ENDIF(WIN32)
OPTION(MultiMC_CODE_COVERAGE "Compiles for code coverage" OFF)
IF(MultiMC_CODE_COVERAGE)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 --coverage")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage")
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 --coverage")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -g -O0 --coverage")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -O0 --coverage")
ENDIF(MultiMC_CODE_COVERAGE)
# Tell CMake that MultiMCLauncher.jar is generated.
SET_SOURCE_FILES_PROPERTIES(${PROJECT_BINARY_DIR}/depends/launcher/MultiMCLauncher.jar GENERATED)
SET_SOURCE_FILES_PROPERTIES(${PROJECT_BINARY_DIR}/depends/javacheck/JavaCheck.jar GENERATED)
@ -459,14 +468,18 @@ CONFIGURE_FILE(generated.qrc.in generated.qrc)
QT5_ADD_RESOURCES(GENERATED_QRC ${CMAKE_CURRENT_BINARY_DIR}/generated.qrc)
QT5_ADD_RESOURCES(GRAPHICS_QRC graphics.qrc)
# Add common library
ADD_LIBRARY(MultiMC_common STATIC ${MULTIMC_SOURCES} ${MULTIMC_UI} ${GENERATED_QRC} ${GRAPHICS_QRC} ${MULTIMC_RCS})
# Add executable
ADD_EXECUTABLE(MultiMC MACOSX_BUNDLE WIN32
${MULTIMC_SOURCES} ${MULTIMC_UI} ${GRAPHICS_QRC} ${GENERATED_QRC} ${MULTIMC_RCS})
ADD_EXECUTABLE(MultiMC MACOSX_BUNDLE WIN32 main.cpp)
# Link
TARGET_LINK_LIBRARIES(MultiMC xz-embedded unpack200 quazip libUtil libSettings libGroupView ${MultiMC_LINK_ADDITIONAL_LIBS})
TARGET_LINK_LIBRARIES(MultiMC MultiMC_common)
TARGET_LINK_LIBRARIES(MultiMC_common xz-embedded unpack200 quazip libUtil libSettings libGroupView ${MultiMC_LINK_ADDITIONAL_LIBS})
QT5_USE_MODULES(MultiMC Core Widgets Network Xml ${MultiMC_QT_ADDITIONAL_MODULES})
ADD_DEPENDENCIES(MultiMC MultiMCLauncher JavaCheck)
QT5_USE_MODULES(MultiMC_common Core Widgets Network Xml ${MultiMC_QT_ADDITIONAL_MODULES})
ADD_DEPENDENCIES(MultiMC_common MultiMCLauncher JavaCheck)
################################ INSTALLATION AND PACKAGING ################################
@ -636,3 +649,6 @@ ENDIF()
add_custom_target (translations DEPENDS ${QM_FILES})
install(FILES ${QM_FILES} DESTINATION ${CMAKE_INSTALL_PREFIX}/translations)
# Tests
add_subdirectory(tests)

View File

@ -7,7 +7,6 @@
#include <QLibraryInfo>
#include <QMessageBox>
#include "gui/MainWindow.h"
#include "gui/dialogs/VersionSelectDialog.h"
#include "logic/lists/InstanceList.h"
#include "logic/lists/MojangAccountList.h"
@ -31,7 +30,7 @@
#include "config.h"
using namespace Util::Commandline;
MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv),
MultiMC::MultiMC(int &argc, char **argv, const QString &root) : QApplication(argc, argv),
m_version{VERSION_MAJOR, VERSION_MINOR, VERSION_BUILD, VERSION_BUILD_TYPE}
{
setOrganizationName("MultiMC");
@ -130,7 +129,9 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv),
}
// change directory
QDir::setCurrent(args["dir"].toString());
QDir::setCurrent(args["dir"].toString().isEmpty() ?
(root.isEmpty() ? QDir::currentPath() : QDir::current().absoluteFilePath(root))
: args["dir"].toString());
// init the logger
initLogger();
@ -404,31 +405,4 @@ std::shared_ptr<JavaVersionList> MultiMC::javalist()
return m_javalist;
}
int main_gui(MultiMC &app)
{
// show main window
MainWindow mainWin;
mainWin.restoreState(QByteArray::fromBase64(MMC->settings()->get("MainWindowState").toByteArray()));
mainWin.restoreGeometry(QByteArray::fromBase64(MMC->settings()->get("MainWindowGeometry").toByteArray()));
mainWin.show();
mainWin.checkSetDefaultJava();
return app.exec();
}
int main(int argc, char *argv[])
{
// initialize Qt
MultiMC app(argc, argv);
switch (app.status())
{
case MultiMC::Initialized:
return main_gui(app);
case MultiMC::Failed:
return 1;
case MultiMC::Succeeded:
return 0;
}
}
#include "MultiMC.moc"

View File

@ -44,7 +44,7 @@ public:
};
public:
MultiMC(int &argc, char **argv);
MultiMC(int &argc, char **argv, const QString &root = QString());
virtual ~MultiMC();
std::shared_ptr<SettingsObject> settings()

View File

@ -51,6 +51,14 @@ add_definitions(-DLIBSETTINGS_LIBRARY)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
IF(MultiMC_CODE_COVERAGE)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 --coverage")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage")
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 --coverage")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -g -O0 --coverage")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -O0 --coverage")
ENDIF(MultiMC_CODE_COVERAGE)
add_library(libSettings STATIC ${LIBSETTINGS_SOURCES} ${LIBSETTINGS_HEADERS} ${LIBSETTINGS_HEADERS_PRIVATE})
qt5_use_modules(libSettings Core)
target_link_libraries(libSettings)

View File

@ -47,6 +47,14 @@ add_definitions(-DLIBUTIL_LIBRARY)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
IF(MultiMC_CODE_COVERAGE)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 --coverage")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 --coverage")
SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g -O0 --coverage")
SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -g -O0 --coverage")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -O0 --coverage")
ENDIF(MultiMC_CODE_COVERAGE)
add_library(libUtil STATIC ${LIBUTIL_SOURCES})
# qt5_use_modules(libUtil Core Network)
qt5_use_modules(libUtil Core)

View File

@ -36,11 +36,11 @@ const static int GROUP_FILE_FORMAT_VERSION = 1;
InstanceList::InstanceList(const QString &instDir, QObject *parent)
: QAbstractListModel(parent), m_instDir(instDir)
{
connect(MMC, &MultiMC::aboutToQuit, this, &InstanceList::saveGroupList);
}
InstanceList::~InstanceList()
{
saveGroupList();
}
int InstanceList::rowCount(const QModelIndex &parent) const

View File

@ -29,6 +29,9 @@ class InstanceList : public QAbstractListModel
Q_OBJECT
private:
void loadGroupList(QMap<QString, QString> &groupList);
private
slots:
void saveGroupList();
public:

32
main.cpp Normal file
View File

@ -0,0 +1,32 @@
#include "MultiMC.h"
#include "gui/MainWindow.h"
int main_gui(MultiMC &app)
{
// show main window
MainWindow mainWin;
mainWin.restoreState(QByteArray::fromBase64(MMC->settings()->get("MainWindowState").toByteArray()));
mainWin.restoreGeometry(QByteArray::fromBase64(MMC->settings()->get("MainWindowGeometry").toByteArray()));
mainWin.show();
mainWin.checkSetDefaultJava();
return app.exec();
}
int main(int argc, char *argv[])
{
// initialize Qt
MultiMC app(argc, argv);
Q_INIT_RESOURCE(graphics);
Q_INIT_RESOURCE(generated);
switch (app.status())
{
case MultiMC::Initialized:
return main_gui(app);
case MultiMC::Failed:
return 1;
case MultiMC::Succeeded:
return 0;
}
}

82
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,82 @@
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)
# Tests END #
set(COVERAGE_SOURCE_DIRS
${MMC_SRC}/logic/*
${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)

31
tests/TestUtil.h Normal file
View File

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

View File

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

76
tests/tst_pathutils.cpp Normal file
View File

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