Add basic Test Suite

This commit is contained in:
Florian Märkl 2018-04-13 18:02:28 +02:00
parent 2e6fd46127
commit 2d191ad17f
7 changed files with 162 additions and 0 deletions

73
src/test/AutoTest.h Normal file
View File

@ -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
src/test/CMakeLists.txt Normal file
View File

@ -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
src/test/CutterTest.cpp Normal file
View File

@ -0,0 +1,13 @@
#include "CutterTest.h"
void CutterTest::initTestCase()
{
qDebug("CutterTest::initTestCase()");
}
void CutterTest::cleanupTestCase()
{
qDebug("CutterTest::cleanupTestCase()");
}

16
src/test/CutterTest.h Normal file
View File

@ -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
src/test/CutterTest.pro Normal file
View File

@ -0,0 +1,9 @@
QT += widgets testlib
HEADERS = AutoTest.h \
CutterTest.h
SOURCES = main.cpp \
TestHexdumpWidget.cpp \
CutterTest.cpp

View File

@ -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
src/test/main.cpp Normal file
View File

@ -0,0 +1,8 @@
#include "AutoTest.h"
int main(int argc, char *argv[])
{
return AutoTest::run(argc, argv);
}