1/****************************************************************************
2**
3** Copyright (C) 2016 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#include <qtest.h>
30#include <QQmlEngine>
31#include <QQmlComponent>
32#include <QDebug>
33#include <QDir>
34#include <QFile>
35
36class tst_parserstress : public QObject
37{
38 Q_OBJECT
39public:
40 tst_parserstress() {}
41
42private slots:
43 void ecmascript_data();
44 void ecmascript();
45
46private:
47 static QStringList findJSFiles(const QDir &);
48 QQmlEngine engine;
49};
50
51QStringList tst_parserstress::findJSFiles(const QDir &d)
52{
53 QStringList rv;
54
55 QStringList files = d.entryList(nameFilters: QStringList() << QLatin1String("*.js"),
56 filters: QDir::Files);
57 foreach (const QString &file, files) {
58 if (file == "browser.js")
59 continue;
60 rv << d.absoluteFilePath(fileName: file);
61 }
62
63 QStringList dirs = d.entryList(filters: QDir::Dirs | QDir::NoDotAndDotDot |
64 QDir::NoSymLinks);
65 foreach (const QString &dir, dirs) {
66 QDir sub = d;
67 sub.cd(dirName: dir);
68 rv << findJSFiles(d: sub);
69 }
70
71 return rv;
72}
73
74void tst_parserstress::ecmascript_data()
75{
76 QString testDataDir = QFileInfo(QFINDTESTDATA("tests/shell.js")).absolutePath();
77 QVERIFY2(!testDataDir.isEmpty(), qPrintable("Cannot find testDataDir!"));
78
79 QDir dir(testDataDir);
80 QStringList files = findJSFiles(d: dir);
81
82 QTest::addColumn<QString>(name: "file");
83 foreach (const QString &file, files)
84 QTest::newRow(qPrintable(file)) << file;
85}
86
87void tst_parserstress::ecmascript()
88{
89 QFETCH(QString, file);
90
91 QFile f(file);
92 QVERIFY(f.open(QIODevice::ReadOnly));
93
94 QByteArray data = f.readAll();
95
96 QVERIFY(!data.isEmpty());
97
98 QString dataStr = QString::fromUtf8(str: data);
99
100 QString qml = "import QtQuick 2.0\n";
101 qml+= "\n";
102 qml+= "QtObject {\n";
103 qml+= " property int test\n";
104 qml+= " test: {\n";
105 qml+= dataStr + "\n";
106 qml+= " return 1;\n";
107 qml+= " }\n";
108 qml+= " function stress() {\n";
109 qml+= dataStr;
110 qml+= " }\n";
111 qml+= "}\n";
112
113 QByteArray qmlData = qml.toUtf8();
114
115 QQmlComponent component(&engine);
116
117 component.setData(qmlData, baseUrl: QUrl());
118
119 QFileInfo info(file);
120
121 if (info.fileName() == QLatin1String("regress-352044-02-n.js")) {
122 QVERIFY(component.isError());
123
124 QCOMPARE(component.errors().length(), 2);
125
126 QCOMPARE(component.errors().at(0).description(), QString("Expected token `;'"));
127 QCOMPARE(component.errors().at(0).line(), 66);
128
129 QCOMPARE(component.errors().at(1).description(), QString("Expected token `;'"));
130 QCOMPARE(component.errors().at(1).line(), 142);
131
132 } else {
133 QVERIFY2(!component.isError(), qPrintable(component.errorString()));
134 }
135}
136
137
138QTEST_MAIN(tst_parserstress)
139
140#include "tst_parserstress.moc"
141

source code of qtdeclarative/tests/auto/qml/parserstress/tst_parserstress.cpp