Debian Codegraph @

tests/auto/qscriptv8testsuite/tst_qscriptv8testsuite.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28
29
30 #include "abstracttestsuite.h"
31 #include <QtTest/QtTest>
32 #include <QtScript>
33
34 class tst_QScriptV8TestSuite : public AbstractTestSuite
35 {
36 public:
37 tst_QScriptV8TestSuite();
38 virtual ~tst_QScriptV8TestSuite();
39
40 protected:
41 struct ExpectedFailure
42 {
43 ExpectedFailure(const QString &name, const QString &act,
44 const QString &exp, const QString &msg)
45 : testName(name), actual(act), expected(exp), message(msg)
46 { }
47
48 QString testName;
49 QString actual;
50 QString expected;
51 QString message;
52 };
53
54 void addExpectedFailure(const QString &testName, const QString &actual,
55 const QString &expected, const QString &message);
56 bool isExpectedFailure(const QString &testName, const QString &actual,
57 const QString &expected, QString *message) const;
58 void addTestExclusion(const QString &testName, const QString &message);
59 void addTestExclusion(const QRegExp &rx, const QString &message);
60 bool isExcludedTest(const QString &testName, QString *message) const;
61
62 virtual void initTestCase();
63 virtual void configData(TestConfig::Mode mode, const QStringList &parts);
64 virtual void writeSkipConfigFile(QTextStream &);
65 virtual void writeExpectFailConfigFile(QTextStream &);
66 virtual void runTestFunction(int testIndex);
67
68 QStringList testNames;
69 QList<ExpectedFailure> expectedFailures;
70 QList<QPair<QRegExp, QString> > testExclusions;
71 QString mjsunitContents;
72 };
73
74 // We expect failing tests to call the fail() function (defined in
75 // mjsunit.js) with arguments expected, actual, message_opt. This
76 // function intercepts the call, calls the real fail() function (which
77 // will throw an exception), and sets the original arguments on the
78 // exception object so that we can process them later.
79 static QScriptValue qscript_fail(QScriptContext *ctx, QScriptEngine *eng)
80 {
81 QScriptValue realFail = ctx->callee().data();
82 if (!realFail.isFunction())
83 qFatal("%s: realFail must be a function", Q_FUNC_INFO);
84 QScriptValue ret = realFail.call(ctx->thisObject(), ctx->argumentsObject());
85 if (!eng->hasUncaughtException())
86 qFatal("%s: realFail function did not throw an exception", Q_FUNC_INFO);
87 ret.setProperty("expected", ctx->argument(0));
88 ret.setProperty("actual", ctx->argument(1));
89 ret.setProperty("message", ctx->argument(2));
90 QScriptContextInfo info(ctx->parentContext()->parentContext());
91 ret.setProperty("lineNumber", info.lineNumber());
92 return ret;
93 }
94
95 void tst_QScriptV8TestSuite::writeSkipConfigFile(QTextStream &stream)
96 {
97 stream << QString::fromLatin1("# testcase | message") << endl;
98 }
99
100 void tst_QScriptV8TestSuite::writeExpectFailConfigFile(QTextStream &stream)
101 {
102 stream << QString::fromLatin1("# testcase | actual | expected | message") << endl;
103 for (int i = 0; i < expectedFailures.size(); ++i) {
104 const ExpectedFailure &fail = expectedFailures.at(i);
105 stream << QString::fromLatin1("%0 | %1 | %2")
106 .arg(fail.testName)
107 .arg(escape(fail.actual))
108 .arg(escape(fail.expected));
109 if (!fail.message.isEmpty())
110 stream << QString::fromLatin1(" | %0").arg(escape(fail.message));
111 stream << endl;
112 }
113 }
114
115 void tst_QScriptV8TestSuite::runTestFunction(int testIndex)
116 {
117 QString name = testNames.at(testIndex);
118 QString path = testsDir.absoluteFilePath(name + ".js");
119
120 QString excludeMessage;
121 if (isExcludedTest(name, &excludeMessage)) {
122 QTest::qSkip(excludeMessage.toLatin1(), path.toLatin1(), -1);
123 return;
124 }
125
126 QScriptEngine engine;
127 engine.evaluate(mjsunitContents);
128 if (engine.hasUncaughtException()) {
129 QStringList bt = engine.uncaughtExceptionBacktrace();
130 QString err = engine.uncaughtException().toString();
131 qWarning("%s\n%s", qPrintable(err), qPrintable(bt.join("\n")));
132 } else {
133 // Prepare to intercept calls to mjsunit's fail() function.
134 QScriptValue fakeFail = engine.newFunction(qscript_fail);
135 fakeFail.setData(engine.globalObject().property("fail"));
136 engine.globalObject().setProperty("fail", fakeFail);
137
138 QString contents = readFile(path);
139 QScriptValue ret = engine.evaluate(contents);
140 if (engine.hasUncaughtException()) {
141 if (!ret.isError()) {
142 int lineNumber = ret.property("lineNumber").toInt32();
143 QTest::qVerify(ret.instanceOf(engine.globalObject().property("MjsUnitAssertionError")),
144 ret.toString().toLatin1(),
145 "",
146 path.toLatin1(),
147 lineNumber);
148 QString actual = ret.property("actual").toString();
149 QString expected = ret.property("expected").toString();
150 QString failMessage;
151 if (shouldGenerateExpectedFailures) {
152 if (ret.property("message").isString())
153 failMessage = ret.property("message").toString();
154 addExpectedFailure(name, actual, expected, failMessage);
155 } else if (isExpectedFailure(name, actual, expected, &failMessage)) {
156 QTest::qExpectFail("", failMessage.toLatin1(),
157 QTest::Continue, path.toLatin1(),
158 lineNumber);
159 }
160 QTest::qCompare(actual, expected, "actual", "expect",
161 path.toLatin1(), lineNumber);
162 } else {
163 int lineNumber = ret.property("lineNumber").toInt32();
164 QTest::qExpectFail("", ret.toString().toLatin1(),
165 QTest::Continue, path.toLatin1(), lineNumber);
166 QTest::qVerify(false, ret.toString().toLatin1(), "", path.toLatin1(), lineNumber);
167 }
168 }
169 }
170 }
171
172 tst_QScriptV8TestSuite::tst_QScriptV8TestSuite()
173 : AbstractTestSuite("tst_QScriptV8TestSuite",
174 ":/tests", ":/")
175 {
176 // One test function per test file.
177 const QFileInfoList testFileInfos = testsDir.entryInfoList(QStringList() << "*.js", QDir::Files);
178 for (const QFileInfo &tfi : testFileInfos) {
179 QString name = tfi.baseName();
180 addTestFunction(name);
181 testNames.append(name);
182 }
183
184 finalizeMetaObject();
185 }
186
187 tst_QScriptV8TestSuite::~tst_QScriptV8TestSuite()
188 {
189 }
190
191 void tst_QScriptV8TestSuite::initTestCase()
192 {
193 AbstractTestSuite::initTestCase();
194
195 // FIXME: These warnings should be QFAIL, but that would make the
196 // test fail right now.
197 if (!testsDir.exists("mjsunit.js"))
198 qWarning("*** no tests/mjsunit.js file!");
199 else {
200 mjsunitContents = readFile(testsDir.absoluteFilePath("mjsunit.js"));
201 if (mjsunitContents.isEmpty())
202 qWarning("*** tests/mjsunit.js is empty!");
203 }
204 }
205
206 void tst_QScriptV8TestSuite::configData(TestConfig::Mode mode, const QStringList &parts)
207 {
208 switch (mode) {
209 case TestConfig::Skip:
210 addTestExclusion(parts.at(0), parts.value(1));
211 break;
212
213 case TestConfig::ExpectFail:
214 addExpectedFailure(parts.at(0), parts.value(1),
215 parts.value(2), parts.value(3));
216 break;
217 }
218 }
219
220 void tst_QScriptV8TestSuite::addExpectedFailure(const QString &testName, const QString &actual,
221 const QString &expected, const QString &message)
222 {
223 expectedFailures.append(ExpectedFailure(testName, actual, expected, message));
224 }
225
226 bool tst_QScriptV8TestSuite::isExpectedFailure(const QString &testName, const QString &actual,
227 const QString &expected, QString *message) const
228 {
229 for (int i = 0; i < expectedFailures.size(); ++i) {
230 const ExpectedFailure &ef = expectedFailures.at(i);
231 if ((testName == ef.testName) && (actual == ef.actual) && (expected == ef.expected)) {
232 if (message)
233 *message = ef.message;
234 return true;
235 }
236 }
237 return false;
238 }
239
240 void tst_QScriptV8TestSuite::addTestExclusion(const QString &testName, const QString &message)
241 {
242 testExclusions.append(qMakePair(QRegExp(testName), message));
243 }
244
245 void tst_QScriptV8TestSuite::addTestExclusion(const QRegExp &rx, const QString &message)
246 {
247 testExclusions.append(qMakePair(rx, message));
248 }
249
250 bool tst_QScriptV8TestSuite::isExcludedTest(const QString &testName, QString *message) const
251 {
252 for (int i = 0; i < testExclusions.size(); ++i) {
253 if (QRegExp(testExclusions.at(i).first).indexIn(testName) != -1) {
254 if (message)
255 *message = testExclusions.at(i).second;
256 return true;
257 }
258 }
259 return false;
260 }
261
262 QTEST_MAIN(tst_QScriptV8TestSuite)