Debian Codegraph @

maz-db/tests/Mace.cpp

raw symbols SCIP
1 #include "Mace.h"
2 #include <QTest>
3 #include <QDebug>
4 #include <QStringList>
5 #include <QMetaObject>
6 #include <QMetaMethod>
7 #include <QtQuickTest/QtQuickTest>
8
9
10 static QPointer<Mace::Runner> s_instance;
11
12 Mace::Runner::Runner(QObject *parent) : QObject(parent)
13 {
14 if (s_instance.isNull()) {
15 s_instance = this;
16 }
17
18 }
19
20 Mace::Runner::~Runner()
21 {
22 for(auto test : m_tests) {
23 if (auto object = test.value<QObject*>()) {
24 delete object;
25 }
26 }
27 m_tests.clear();
28 if (s_instance == this) {
29 s_instance = 0;
30 }
31 }
32
33 void Mace::Runner::include(QObject *tests)
34 {
35 m_tests << QVariant::fromValue<QObject *>(tests);
36 emit countChanged();
37 }
38
39 void Mace::Runner::include(const QString &qmlTests)
40 {
41 m_tests << QVariant(qmlTests);
42 emit countChanged();
43 }
44
45 int Mace::Runner::count() const
46 {
47 return m_tests.size();
48 }
49
50 bool Mace::Runner::run(QStringList arguments)
51 {
52 qDebug() << "Arguments:" << arguments;
53 qDebug() << "Tests: " << m_tests;
54 bool testFailed = false;
55 foreach(const QVariant &test, m_tests) {
56 QStringList runArgs = arguments;
57 if (testFailed) {
58 break;
59 }
60 if (auto object = test.value<QObject *>()) {
61 testFailed |= runTests(object, runArgs);
62 } else if (test.type() == (int)QMetaType::QString) {
63 testFailed |= runTests(test.toString(), runArgs);
64 } else {
65 qDebug() << "unkown type:" << test.type();
66 }
67 }
68 return testFailed;
69 }
70
71 bool Mace::Runner::runTests(QObject *testObject, QStringList &args)
72 {
73 QStringList tests;
74 QStringList qtestArgs;
75 qtestArgs << args.takeAt(0);
76 for (const QString &arg : args) {
77 if (arg.startsWith('-')) {
78 qtestArgs << args;
79 } else {
80 tests << arg;
81 }
82 }
83 bool shouldRun = tests.size() > 0 ? false : true;
84 auto metaObject = testObject->metaObject();
85 for (const QString &test : tests) {
86 if (test == metaObject->className()) {
87 shouldRun = true;
88 break;
89 }
90
91 for (int i = 0; i < metaObject->methodCount(); ++i) {
92 if (metaObject->method(i).name() == test) {
93 qtestArgs << test;
94 shouldRun = true;
95 }
96 }
97 }
98 if (!shouldRun) {
99 return false;
100 }
101 return QTest::qExec(testObject, qtestArgs);
102 }
103
104 bool Mace::Runner::runTests(const QString &qmlTestPath, QStringList &arguments)
105 {
106 QStringList args(arguments);
107 QString executable;
108 if (!args.isEmpty()) {
109 executable = args.takeAt(0);
110 }
111 QStringList testcases = arguments.filter(QRegExp("::"));
112
113 QStringList tmp; // Filter all "-" parameter
114 foreach (QString arg, args) {
115 if (arg.indexOf("-") != 0) {
116 tmp << arg;
117 }
118 }
119
120 args = tmp;
121
122 if (args.size() !=0 && testcases.size() == 0) {
123 return false;
124 }
125
126 QStringList paths;
127 paths << qmlTestPath;
128 // paths << m_importPaths; TODO
129
130 char **s = (char**) malloc(sizeof(char*) * (10 + testcases.size() + paths.size() * 2));
131 int idx = 0;
132 s[idx++] = executable.toUtf8().data();
133
134 foreach (QString p , paths) {
135 s[idx++] = strdup("-import");
136 s[idx++] = strdup(p.toUtf8().data());
137 }
138
139 foreach( QString testcase,testcases) {
140 s[idx++] = strdup(testcase.toUtf8().data());
141 }
142 s[idx++] = 0;
143
144 const char *name = "Mace";
145 const char *source = strdup(qmlTestPath.toUtf8().data());
146
147 bool error = quick_test_main( idx-1, s, name, source);
148 free(s);
149
150 return error;
151 }