From 49a9f184cec2742aa942d96e6ecb05571f11272d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Fri, 13 Apr 2018 18:02:28 +0200 Subject: [PATCH] Add basic Test Suite --- .gitignore | 3 +- src/CMakeLists.txt | 5 ++- src/test/AutoTest.h | 73 ++++++++++++++++++++++++++++++++++ src/test/CMakeLists.txt | 15 +++++++ src/test/CutterTest.cpp | 13 ++++++ src/test/CutterTest.h | 16 ++++++++ src/test/CutterTest.pro | 9 +++++ src/test/TestHexdumpWidget.cpp | 28 +++++++++++++ src/test/main.cpp | 8 ++++ 9 files changed, 168 insertions(+), 2 deletions(-) create mode 100644 src/test/AutoTest.h create mode 100644 src/test/CMakeLists.txt create mode 100644 src/test/CutterTest.cpp create mode 100644 src/test/CutterTest.h create mode 100644 src/test/CutterTest.pro create mode 100644 src/test/TestHexdumpWidget.cpp create mode 100644 src/test/main.cpp diff --git a/.gitignore b/.gitignore index f0c13de63..015bdd658 100644 --- a/.gitignore +++ b/.gitignore @@ -30,7 +30,8 @@ qrc_*.cpp moc_*.h ui_*.h build*/ -cmake-build-*/ +Makefile* +*build-* # QtCreator *.autosave diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index adef70c6e..976372ec7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -17,6 +17,7 @@ option(CUTTER_ENABLE_CRASH_REPORTS "Enable crash report system. Unused if CUTTER tri_option(CUTTER_ENABLE_KSYNTAXHIGHLIGHTING "Use KSyntaxHighlighting" AUTO) tri_option(CUTTER_ENABLE_GRAPHVIZ "Enable use of graphviz for graph layout" AUTO) option (option SHIBOKEN_EXTRA_OPTIONS "Extra options for shiboken generator") +option(CUTTER_ENABLE_TESTS "Build QtTest tests" OFF) if(NOT CUTTER_ENABLE_PYTHON) set(CUTTER_ENABLE_PYTHON_BINDINGS OFF) @@ -36,7 +37,6 @@ set(QRC_FILES ${CUTTER_PRO_RESOURCES}) set(CUTTER_VERSION_MAJOR "${CUTTER_PRO_CUTTER_VERSION_MAJOR}") set(CUTTER_VERSION_MINOR "${CUTTER_PRO_CUTTER_VERSION_MINOR}") set(CUTTER_VERSION_PATCH "${CUTTER_PRO_CUTTER_VERSION_PATCH}") - set(CUTTER_VERSION_FULL "${CUTTER_VERSION_MAJOR}.${CUTTER_VERSION_MINOR}.${CUTTER_VERSION_PATCH}") project(Cutter VERSION "${CUTTER_VERSION_FULL}") @@ -220,3 +220,6 @@ if(TARGET KF5::SyntaxHighlighting) target_compile_definitions(Cutter PRIVATE CUTTER_ENABLE_KSYNTAXHIGHLIGHTING) endif() +if(CUTTER_ENABLE_TESTS) + add_subdirectory(test) +endif() diff --git a/src/test/AutoTest.h b/src/test/AutoTest.h new file mode 100644 index 000000000..e7c46761c --- /dev/null +++ b/src/test/AutoTest.h @@ -0,0 +1,73 @@ + +#ifndef AUTOTEST_H +#define AUTOTEST_H + +// see http://qtcreator.blogspot.de/2009/10/running-multiple-unit-tests.html + +#include +#include +#include +#include + +namespace AutoTest +{ +typedef QList TestList; + +inline TestList &testList() +{ + static TestList list; + return list; +} + +inline bool findObject(QObject *object) +{ + TestList &list = testList(); + if (list.contains(object)) { + return true; + } + + for (QObject *test : list) { + if (test->objectName() == object->objectName()) { + return true; + } + } + return false; +} + +inline void addTest(QObject *object) +{ + TestList &list = testList(); + if (!findObject(object)) { + list.append(object); + } +} + +inline int run(int argc, char *argv[]) +{ + int ret = 0; + + for (QObject *test : testList()) { + ret += QTest::qExec(test, argc, argv); + } + + return ret; +} +} + +template +class Test +{ +public: + QSharedPointer child; + + Test(const QString &name) + : child(new T) + { + child->setObjectName(name); + AutoTest::addTest(child.data()); + } +}; + +#define DECLARE_TEST(className) static Test t(#className); + +#endif //AUTOTEST_H diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt new file mode 100644 index 000000000..cd37a1261 --- /dev/null +++ b/src/test/CMakeLists.txt @@ -0,0 +1,15 @@ + +find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Test) + +configure_file("${CMAKE_CURRENT_SOURCE_DIR}/CutterTest.pro" + "${CMAKE_CURRENT_BINARY_DIR}/CutterTest.pro" + COPYONLY) # trigger reconfigure if CutterTest.pro changes +parse_qmake_pro("${CMAKE_CURRENT_BINARY_DIR}/CutterTest.pro" CUTTER_TEST_PRO) +set(TEST_SOURCE_FILES ${CUTTER_TEST_PRO_SOURCES}) +set(TEST_HEADER_FILES ${CUTTER_TEST_PRO_HEADERS}) + +message(STATUS "sources from CutterTest.pro: ${TEST_SOURCE_FILES}") +message(STATUS "headers from CutterTest.pro: ${TEST_HEADER_FILES}") + +add_executable(CutterTest ${TEST_SOURCE_FILES} ${TEST_HEADER_FILES}) +qt5_use_modules(CutterTest Core Widgets Gui Test) diff --git a/src/test/CutterTest.cpp b/src/test/CutterTest.cpp new file mode 100644 index 000000000..3384b3ca5 --- /dev/null +++ b/src/test/CutterTest.cpp @@ -0,0 +1,13 @@ + +#include "CutterTest.h" + +void CutterTest::initTestCase() +{ + qDebug("CutterTest::initTestCase()"); +} + +void CutterTest::cleanupTestCase() +{ + qDebug("CutterTest::cleanupTestCase()"); +} + diff --git a/src/test/CutterTest.h b/src/test/CutterTest.h new file mode 100644 index 000000000..d1519cc43 --- /dev/null +++ b/src/test/CutterTest.h @@ -0,0 +1,16 @@ + +#ifndef CUTTERTEST_H +#define CUTTERTEST_H + +#include + +class CutterTest: public QObject +{ + Q_OBJECT + +protected slots: + virtual void initTestCase(); + virtual void cleanupTestCase(); +}; + +#endif //CUTTERTEST_H diff --git a/src/test/CutterTest.pro b/src/test/CutterTest.pro new file mode 100644 index 000000000..c8f1dcd4b --- /dev/null +++ b/src/test/CutterTest.pro @@ -0,0 +1,9 @@ + +QT += widgets testlib + +HEADERS = AutoTest.h \ + CutterTest.h + +SOURCES = main.cpp \ + TestHexdumpWidget.cpp \ + CutterTest.cpp diff --git a/src/test/TestHexdumpWidget.cpp b/src/test/TestHexdumpWidget.cpp new file mode 100644 index 000000000..7cf97c008 --- /dev/null +++ b/src/test/TestHexdumpWidget.cpp @@ -0,0 +1,28 @@ + +#include + +#include "AutoTest.h" +#include "CutterTest.h" + +class TestHexdumpWidget: public CutterTest +{ + Q_OBJECT +private slots: + void initTestCase() override; + void toUpper(); +}; + +void TestHexdumpWidget::initTestCase() +{ + qDebug("TestHexdumpWidget::initTestCase()"); +} + +void TestHexdumpWidget::toUpper() +{ + QString str = "Hello"; + QCOMPARE(str.toUpper(), QString("HELLO")); +} + +DECLARE_TEST(TestHexdumpWidget) +#include "TestHexdumpWidget.moc" + diff --git a/src/test/main.cpp b/src/test/main.cpp new file mode 100644 index 000000000..35d674277 --- /dev/null +++ b/src/test/main.cpp @@ -0,0 +1,8 @@ + +#include "AutoTest.h" + +int main(int argc, char *argv[]) +{ + return AutoTest::run(argc, argv); +} +