Skip to content

Commit

Permalink
Add basic Test Suite
Browse files Browse the repository at this point in the history
  • Loading branch information
thestr4ng3r authored and yossizap committed Nov 14, 2019
1 parent 524b27f commit 49a9f18
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ qrc_*.cpp
moc_*.h
ui_*.h
build*/
cmake-build-*/
Makefile*
*build-*

# QtCreator
*.autosave
Expand Down
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}")
Expand Down Expand Up @@ -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()
73 changes: 73 additions & 0 deletions src/test/AutoTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@

#ifndef AUTOTEST_H
#define AUTOTEST_H

// see http://qtcreator.blogspot.de/2009/10/running-multiple-unit-tests.html

#include <QTest>
#include <QList>
#include <QString>
#include <QSharedPointer>

namespace AutoTest
{
typedef QList<QObject *> 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 T>
class Test
{
public:
QSharedPointer<T> child;

Test(const QString &name)
: child(new T)
{
child->setObjectName(name);
AutoTest::addTest(child.data());
}
};

#define DECLARE_TEST(className) static Test<className> t(#className);

#endif //AUTOTEST_H
15 changes: 15 additions & 0 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
13 changes: 13 additions & 0 deletions src/test/CutterTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

#include "CutterTest.h"

void CutterTest::initTestCase()
{
qDebug("CutterTest::initTestCase()");
}

void CutterTest::cleanupTestCase()
{
qDebug("CutterTest::cleanupTestCase()");
}

16 changes: 16 additions & 0 deletions src/test/CutterTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#ifndef CUTTERTEST_H
#define CUTTERTEST_H

#include <QObject>

class CutterTest: public QObject
{
Q_OBJECT

protected slots:
virtual void initTestCase();
virtual void cleanupTestCase();
};

#endif //CUTTERTEST_H
9 changes: 9 additions & 0 deletions src/test/CutterTest.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

QT += widgets testlib

HEADERS = AutoTest.h \
CutterTest.h

SOURCES = main.cpp \
TestHexdumpWidget.cpp \
CutterTest.cpp
28 changes: 28 additions & 0 deletions src/test/TestHexdumpWidget.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

#include <QtTest>

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

8 changes: 8 additions & 0 deletions src/test/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

#include "AutoTest.h"

int main(int argc, char *argv[])
{
return AutoTest::run(argc, argv);
}

0 comments on commit 49a9f18

Please sign in to comment.