AutoTest.h 1.57 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/**
* @author Rob Caldecott
* @note This was obtained from http://qtcreator.blogspot.com/2010/04/sample-multiple-unit-test-project.html
*
*/

#ifndef AUTOTEST_H
#define AUTOTEST_H

#include <QTest>
#include <QList>
#include <QString>
#include <QSharedPointer>
14
#include "QGCApplication.h"
15 16 17 18

namespace AutoTest
{
    typedef QList<QObject*> TestList;
19
    
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
    inline TestList& testList()
    {
	static TestList list;
	return list;
    }

    inline bool findObject(QObject* object)
    {
	TestList& list = testList();
	if (list.contains(object))
	{
	    return true;
	}
	foreach (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);
	}
    }

Don Gagne's avatar
Don Gagne committed
52
    inline int run(int argc, char *argv[], QString& singleTest)
53
    { 
54 55
	int ret = 0;
	foreach (QObject* test, testList())
56
	{
Don Gagne's avatar
Don Gagne committed
57 58 59 60 61
        if (singleTest.isEmpty() || singleTest == test->objectName()) {
            qgcApp()->destroySingletonsForUnitTest();
            qgcApp()->createSingletonsForUnitTest();
            ret += QTest::qExec(test, argc, argv);
        }
62
	}
63
	
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
	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