-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtestrunner.cpp
More file actions
209 lines (166 loc) · 4.28 KB
/
testrunner.cpp
File metadata and controls
209 lines (166 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <QTest>
#include <QtQuickTest/quicktest.h>
#include <QMetaObject>
#include <QMetaMethod>
#include <QPointer>
#include <QCommandLineParser>
#include "testrunner.h"
#include "automator.h"
#include "priv/objectutils.h"
static TestRunner *m_defaultInstance = 0;
TestRunner::TestRunner()
{
if (m_defaultInstance == 0) {
m_defaultInstance = this;
}
m_engineHook = 0;
}
TestRunner::~TestRunner()
{
QObject* object;
QVariant item;
foreach (item, m_testObjects) {
object = item.value<QObject*>();
if (object) {
delete object;
}
}
m_testObjects.clear();
if (m_defaultInstance == this) {
m_defaultInstance = 0;
}
}
void TestRunner::add(QObject *object)
{
QVariant value = QVariant::fromValue<QObject*>(object);
m_testObjects.append(value);
}
void TestRunner::add(const QString &path)
{
add(QVariant(path));
}
bool TestRunner::exec(QStringList arguments)
{
m_arguments = arguments;
QObject *object;
QVariant item;
bool error = false;
foreach (item,m_testObjects) {
object = item.value<QObject*>();
if (object) {
error |= run(object, m_arguments);
} else if (item.type() == (int) QMetaType::QString) {
error |= run(item.toString(), m_arguments);
}
}
return error;
}
void TestRunner::addImportPath(const QString &path)
{
m_importPaths << path;
}
int TestRunner::count() const
{
return m_testObjects.size();
}
void TestRunner::add(QVariant value)
{
m_testObjects.append(value);
}
bool TestRunner::run(QObject *object, const QStringList& arguments)
{
const QMetaObject *meta = object->metaObject();
QStringList args = arguments;
QString executable = args.takeAt(0);
bool execute; // Should it execute this test object?
QStringList params; // params for qTest
params << executable;
QStringList tmp;
// Always add "-*" to params.
foreach (QString arg, args) {
if (arg.indexOf("-") == 0) {
params << arg;
} else {
tmp << arg;
}
}
args = tmp;
execute = args.size() > 0 ? false : true; // If no argument passed , it should always execute a test object
foreach (QString arg, args) {
if (arg == meta->className()) {
execute = true;
break;
}
for (int i = 0 ; i < meta->methodCount();i++){
if (meta->method(i).name() == arg) {
params << arg;
execute = true;
}
}
}
if (!execute) {
return false;
}
return QTest::qExec(object,params);
}
bool TestRunner::run(QString path, const QStringList &arguments)
{
QStringList args(arguments);
QString executable = args.takeAt(0);
QStringList testcases = arguments.filter(QRegExp("::"));
QStringList nonOptionArgs; // Filter all "-" parameter
foreach (QString arg, args) {
if (arg.indexOf("-") != 0) {
nonOptionArgs << arg;
}
}
if (nonOptionArgs.size() !=0 && testcases.size() == 0) {
// If you non-option args, but not a quick test case. Return
return false;
}
QStringList paths;
paths << path;
paths << m_importPaths;
char **s = (char**) malloc(sizeof(char*) * (10 + args.size() + paths.size() * 2));
int idx = 0;
s[idx++] = executable.toUtf8().data();
foreach (QString p , paths) {
s[idx++] = strdup("-import");
s[idx++] = strdup(p.toUtf8().data());
}
foreach( QString arg,args) {
s[idx++] = strdup(arg.toUtf8().data());
}
s[idx++] = 0;
const char *name = "QuickTests";
const char *source = strdup(path.toUtf8().data());
bool error = quick_test_main( idx-1, s, name, source);
free(s);
return error;
}
QVariantMap TestRunner::config() const
{
return m_config;
}
void TestRunner::setConfig(const QVariantMap &config)
{
m_config = config;
}
void TestRunner::execEngineHook(QQmlEngine *engine)
{
if (m_engineHook != 0) {
m_engineHook(engine);
}
}
void TestRunner::setEngineHook(std::function<void(QQmlEngine*)> func)
{
m_engineHook = func;
}
QStringList TestRunner::arguments() const
{
return m_arguments;
}
TestRunner *TestRunner::defaultInstance()
{
return m_defaultInstance;
}