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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
|
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "abstracttestsuite.h"
#include <QtTest/QtTest>
#include <QtScript>
#if defined(Q_OS_SYMBIAN)
# define SRCDIR "."
#endif
//TESTED_CLASS=
//TESTED_FILES=
struct TestRecord
{
TestRecord() : lineNumber(-1) { }
TestRecord(const QString &desc,
bool pass,
const QString &act,
const QString &exp,
const QString &fn, int ln)
: description(desc), passed(pass),
actual(act), expected(exp),
fileName(fn), lineNumber(ln)
{ }
TestRecord(const QString &skipReason, const QString &fn)
: description(skipReason), actual("QSKIP"),
fileName(fn), lineNumber(-1)
{ }
QString description;
bool passed;
QString actual;
QString expected;
QString fileName;
int lineNumber;
};
Q_DECLARE_METATYPE(TestRecord)
struct FailureItem
{
enum Action {
ExpectFail,
Skip
};
FailureItem(Action act, const QRegExp &rx, const QString &desc, const QString &msg)
: action(act), pathRegExp(rx), description(desc), message(msg)
{ }
Action action;
QRegExp pathRegExp;
QString description;
QString message;
};
class tst_QScriptJSTestSuite : public AbstractTestSuite
{
public:
tst_QScriptJSTestSuite();
virtual ~tst_QScriptJSTestSuite();
protected:
virtual void configData(TestConfig::Mode mode, const QStringList &parts);
virtual void writeSkipConfigFile(QTextStream &);
virtual void writeExpectFailConfigFile(QTextStream &);
virtual void runTestFunction(int testIndex);
private:
void addExpectedFailure(const QString &fileName, const QString &description, const QString &message);
void addExpectedFailure(const QRegExp &path, const QString &description, const QString &message);
void addSkip(const QString &fileName, const QString &description, const QString &message);
void addSkip(const QRegExp &path, const QString &description, const QString &message);
bool isExpectedFailure(const QString &fileName, const QString &description,
QString *message, FailureItem::Action *action) const;
void addFileExclusion(const QString &fileName, const QString &message);
void addFileExclusion(const QRegExp &rx, const QString &message);
bool isExcludedFile(const QString &fileName, QString *message) const;
QList<QString> subSuitePaths;
QList<FailureItem> expectedFailures;
QList<QPair<QRegExp, QString> > fileExclusions;
};
static QScriptValue qscript_void(QScriptContext *, QScriptEngine *eng)
{
return eng->undefinedValue();
}
static QScriptValue qscript_quit(QScriptContext *ctx, QScriptEngine *)
{
return ctx->throwError("Test quit");
}
static QString optionsToString(int options)
{
QSet<QString> set;
if (options & 1)
set.insert("strict");
if (options & 2)
set.insert("werror");
if (options & 4)
set.insert("atline");
if (options & 8)
set.insert("xml");
return QStringList(set.values()).join(",");
}
static QScriptValue qscript_options(QScriptContext *ctx, QScriptEngine *)
{
static QHash<QString, int> stringToFlagHash;
if (stringToFlagHash.isEmpty()) {
stringToFlagHash["strict"] = 1;
stringToFlagHash["werror"] = 2;
stringToFlagHash["atline"] = 4;
stringToFlagHash["xml"] = 8;
}
QScriptValue callee = ctx->callee();
int opts = callee.data().toInt32();
QString result = optionsToString(opts);
for (int i = 0; i < ctx->argumentCount(); ++i)
opts |= stringToFlagHash.value(ctx->argument(0).toString());
callee.setData(opts);
return result;
}
static QScriptValue qscript_TestCase(QScriptContext *ctx, QScriptEngine *eng)
{
QScriptValue origTestCaseCtor = ctx->callee().data();
QScriptValue kase = ctx->thisObject();
QScriptValue ret = origTestCaseCtor.call(kase, ctx->argumentsObject());
QScriptContextInfo info(ctx->parentContext());
kase.setProperty("__lineNumber__", QScriptValue(eng, info.lineNumber()));
return ret;
}
void tst_QScriptJSTestSuite::runTestFunction(int testIndex)
{
if (!(testIndex & 1)) {
// data
QTest::addColumn<TestRecord>("record");
bool hasData = false;
QString testsShellPath = testsDir.absoluteFilePath("shell.js");
QString testsShellContents = readFile(testsShellPath);
QDir subSuiteDir(subSuitePaths.at(testIndex / 2));
QString subSuiteShellPath = subSuiteDir.absoluteFilePath("shell.js");
QString subSuiteShellContents = readFile(subSuiteShellPath);
QDir testSuiteDir(subSuiteDir);
testSuiteDir.cdUp();
QString suiteJsrefPath = testSuiteDir.absoluteFilePath("jsref.js");
QString suiteJsrefContents = readFile(suiteJsrefPath);
QString suiteShellPath = testSuiteDir.absoluteFilePath("shell.js");
QString suiteShellContents = readFile(suiteShellPath);
QFileInfoList testFileInfos = subSuiteDir.entryInfoList(QStringList() << "*.js", QDir::Files);
foreach (QFileInfo tfi, testFileInfos) {
if ((tfi.fileName() == "shell.js") || (tfi.fileName() == "browser.js"))
continue;
QString abspath = tfi.absoluteFilePath();
QString relpath = testsDir.relativeFilePath(abspath);
QString excludeMessage;
if (isExcludedFile(relpath, &excludeMessage)) {
QTest::newRow(relpath.toLatin1()) << TestRecord(excludeMessage, relpath);
continue;
}
QScriptEngine eng;
QScriptValue global = eng.globalObject();
global.setProperty("print", eng.newFunction(qscript_void));
global.setProperty("quit", eng.newFunction(qscript_quit));
global.setProperty("options", eng.newFunction(qscript_options));
eng.evaluate(testsShellContents, testsShellPath);
if (eng.hasUncaughtException()) {
QStringList bt = eng.uncaughtExceptionBacktrace();
QString err = eng.uncaughtException().toString();
qWarning("%s\n%s", qPrintable(err), qPrintable(bt.join("\n")));
break;
}
eng.evaluate(suiteJsrefContents, suiteJsrefPath);
if (eng.hasUncaughtException()) {
QStringList bt = eng.uncaughtExceptionBacktrace();
QString err = eng.uncaughtException().toString();
qWarning("%s\n%s", qPrintable(err), qPrintable(bt.join("\n")));
break;
}
eng.evaluate(suiteShellContents, suiteShellPath);
if (eng.hasUncaughtException()) {
QStringList bt = eng.uncaughtExceptionBacktrace();
QString err = eng.uncaughtException().toString();
qWarning("%s\n%s", qPrintable(err), qPrintable(bt.join("\n")));
break;
}
eng.evaluate(subSuiteShellContents, subSuiteShellPath);
if (eng.hasUncaughtException()) {
QStringList bt = eng.uncaughtExceptionBacktrace();
QString err = eng.uncaughtException().toString();
qWarning("%s\n%s", qPrintable(err), qPrintable(bt.join("\n")));
break;
}
QScriptValue origTestCaseCtor = global.property("TestCase");
QScriptValue myTestCaseCtor = eng.newFunction(qscript_TestCase);
myTestCaseCtor.setData(origTestCaseCtor);
global.setProperty("TestCase", myTestCaseCtor);
global.setProperty("gTestfile", tfi.fileName());
global.setProperty("gTestsuite", testSuiteDir.dirName());
global.setProperty("gTestsubsuite", subSuiteDir.dirName());
QString testFileContents = readFile(abspath);
// qDebug() << relpath;
eng.evaluate(testFileContents, abspath);
if (eng.hasUncaughtException() && !relpath.endsWith("-n.js")) {
QStringList bt = eng.uncaughtExceptionBacktrace();
QString err = eng.uncaughtException().toString();
qWarning("%s\n%s\n", qPrintable(err), qPrintable(bt.join("\n")));
continue;
}
QScriptValue testcases = global.property("testcases");
if (!testcases.isArray())
testcases = global.property("gTestcases");
int count = testcases.property("length").toInt32();
if (count == 0)
continue;
hasData = true;
QString title = global.property("TITLE").toString();
for (int i = 0; i < count; ++i) {
QScriptValue kase = testcases.property(i);
QString description = kase.property("description").toString();
QScriptValue expect = kase.property("expect");
QScriptValue actual = kase.property("actual");
bool passed = kase.property("passed").toBoolean();
int lineNumber = kase.property("__lineNumber__").toInt32();
TestRecord rec(description, passed,
actual.toString(), expect.toString(),
relpath, lineNumber);
QTest::newRow(description.toLatin1()) << rec;
}
}
if (!hasData)
QTest::newRow("") << TestRecord(); // dummy
} else {
QFETCH(TestRecord, record);
if ((record.lineNumber == -1) && (record.actual == "QSKIP")) {
QTest::qSkip(record.description.toLatin1(), QTest::SkipAll, record.fileName.toLatin1(), -1);
} else {
QString msg;
FailureItem::Action failAct;
bool expectFail = isExpectedFailure(record.fileName, record.description, &msg, &failAct);
if (expectFail) {
switch (failAct) {
case FailureItem::ExpectFail:
QTest::qExpectFail("", msg.toLatin1(),
QTest::Continue, record.fileName.toLatin1(),
record.lineNumber);
break;
case FailureItem::Skip:
QTest::qSkip(msg.toLatin1(), QTest::SkipSingle,
record.fileName.toLatin1(), record.lineNumber);
break;
}
}
if (!expectFail || (failAct == FailureItem::ExpectFail)) {
if (!record.passed) {
if (!expectFail && shouldGenerateExpectedFailures) {
addExpectedFailure(record.fileName,
record.description,
QString());
}
QTest::qCompare(record.actual, record.expected, "actual", "expect",
record.fileName.toLatin1(), record.lineNumber);
} else {
QTest::qCompare(record.actual, record.actual, "actual", "expect",
record.fileName.toLatin1(), record.lineNumber);
}
}
}
}
}
tst_QScriptJSTestSuite::tst_QScriptJSTestSuite()
: AbstractTestSuite("tst_QScriptJsTestSuite",
QString::fromLatin1("%0/tests").arg(SRCDIR),
":/")
{
// don't execute any tests on slow machines
#if !defined(Q_OS_IRIX)
// do all the test suites
QFileInfoList testSuiteDirInfos = testsDir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot);
foreach (QFileInfo tsdi, testSuiteDirInfos) {
QDir testSuiteDir(tsdi.absoluteFilePath());
// do all the dirs in the test suite
QFileInfoList subSuiteDirInfos = testSuiteDir.entryInfoList(QDir::AllDirs | QDir::NoDotAndDotDot);
foreach (QFileInfo ssdi, subSuiteDirInfos) {
subSuitePaths.append(ssdi.absoluteFilePath());
QString function = QString::fromLatin1("%0/%1")
.arg(testSuiteDir.dirName()).arg(ssdi.fileName());
addTestFunction(function, CreateDataFunction);
}
}
#endif
finalizeMetaObject();
}
tst_QScriptJSTestSuite::~tst_QScriptJSTestSuite()
{
}
void tst_QScriptJSTestSuite::configData(TestConfig::Mode mode, const QStringList &parts)
{
switch (mode) {
case TestConfig::Skip:
addFileExclusion(parts.at(0), parts.value(1));
break;
case TestConfig::ExpectFail:
addExpectedFailure(parts.at(0), parts.value(1), parts.value(2));
break;
}
}
void tst_QScriptJSTestSuite::writeSkipConfigFile(QTextStream &stream)
{
stream << QString::fromLatin1("# testcase | message") << endl;
}
void tst_QScriptJSTestSuite::writeExpectFailConfigFile(QTextStream &stream)
{
stream << QString::fromLatin1("# testcase | description | message") << endl;
for (int i = 0; i < expectedFailures.size(); ++i) {
const FailureItem &fail = expectedFailures.at(i);
if (fail.pathRegExp.pattern().isEmpty())
continue;
stream << QString::fromLatin1("%0 | %1")
.arg(fail.pathRegExp.pattern())
.arg(escape(fail.description));
if (!fail.message.isEmpty())
stream << QString::fromLatin1(" | %0").arg(escape(fail.message));
stream << endl;
}
}
void tst_QScriptJSTestSuite::addExpectedFailure(const QRegExp &path, const QString &description, const QString &message)
{
expectedFailures.append(FailureItem(FailureItem::ExpectFail, path, description, message));
}
void tst_QScriptJSTestSuite::addExpectedFailure(const QString &fileName, const QString &description, const QString &message)
{
expectedFailures.append(FailureItem(FailureItem::ExpectFail, QRegExp(fileName), description, message));
}
void tst_QScriptJSTestSuite::addSkip(const QRegExp &path, const QString &description, const QString &message)
{
expectedFailures.append(FailureItem(FailureItem::Skip, path, description, message));
}
void tst_QScriptJSTestSuite::addSkip(const QString &fileName, const QString &description, const QString &message)
{
expectedFailures.append(FailureItem(FailureItem::Skip, QRegExp(fileName), description, message));
}
bool tst_QScriptJSTestSuite::isExpectedFailure(const QString &fileName, const QString &description,
QString *message, FailureItem::Action *action) const
{
for (int i = 0; i < expectedFailures.size(); ++i) {
if (expectedFailures.at(i).pathRegExp.indexIn(fileName) != -1) {
if (description == expectedFailures.at(i).description) {
if (message)
*message = expectedFailures.at(i).message;
if (action)
*action = expectedFailures.at(i).action;
return true;
}
}
}
return false;
}
void tst_QScriptJSTestSuite::addFileExclusion(const QString &fileName, const QString &message)
{
fileExclusions.append(qMakePair(QRegExp(fileName), message));
}
void tst_QScriptJSTestSuite::addFileExclusion(const QRegExp &rx, const QString &message)
{
fileExclusions.append(qMakePair(rx, message));
}
bool tst_QScriptJSTestSuite::isExcludedFile(const QString &fileName, QString *message) const
{
for (int i = 0; i < fileExclusions.size(); ++i) {
if (fileExclusions.at(i).first.indexIn(fileName) != -1) {
if (message)
*message = fileExclusions.at(i).second;
return true;
}
}
return false;
}
QTEST_MAIN(tst_QScriptJSTestSuite)
|