1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <private/qqmljsengine_p.h>
43 #include <private/qqmljsparser_p.h>
44 #include <private/qqmljslexer_p.h>
45 #include <private/qqmljsastvisitor_p.h>
46 #include <private/qqmljsast_p.h>
53 class tst_qqmlparser : public QObject
61 #if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
62 void qmlParser_data();
67 QStringList excludedDirs;
69 QStringList findFiles(const QDir &);
74 using namespace QQmlJS;
76 class Check: public AST::Visitor
79 QList<AST::Node *> nodeStack;
87 void operator()(AST::Node *node)
89 AST::Node::accept(node, this);
92 void checkNode(AST::Node *node)
94 if (! nodeStack.isEmpty()) {
95 AST::Node *parent = nodeStack.last();
96 const quint32 parentBegin = parent->firstSourceLocation().begin();
97 const quint32 parentEnd = parent->lastSourceLocation().end();
99 QVERIFY(node->firstSourceLocation().begin() >= parentBegin);
100 QVERIFY(node->lastSourceLocation().end() <= parentEnd);
104 virtual bool preVisit(AST::Node *node)
107 nodeStack.append(node);
111 virtual void postVisit(AST::Node *)
113 nodeStack.removeLast();
119 tst_qqmlparser::tst_qqmlparser()
123 void tst_qqmlparser::initTestCase()
125 // Add directories you want excluded here
127 // These snippets are not expected to run on their own.
128 excludedDirs << "doc/src/snippets/qml/visualdatamodel_rootindex";
129 excludedDirs << "doc/src/snippets/qml/qtbinding";
130 excludedDirs << "doc/src/snippets/qml/imports";
131 excludedDirs << "doc/src/snippets/qtquick1/visualdatamodel_rootindex";
132 excludedDirs << "doc/src/snippets/qtquick1/qtbinding";
133 excludedDirs << "doc/src/snippets/qtquick1/imports";
136 QStringList tst_qqmlparser::findFiles(const QDir &d)
138 for (int ii = 0; ii < excludedDirs.count(); ++ii) {
139 QString s = excludedDirs.at(ii);
140 if (d.absolutePath().endsWith(s))
141 return QStringList();
146 QStringList files = d.entryList(QStringList() << QLatin1String("*.qml") << QLatin1String("*.js"),
148 foreach (const QString &file, files) {
149 rv << d.absoluteFilePath(file);
152 QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
154 foreach (const QString &dir, dirs) {
157 rv << findFiles(sub);
164 This test checks all the qml and js files in the QtQml UI source tree
165 and ensures that the subnode's source locations are inside parent node's source locations
168 #if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
169 void tst_qqmlparser::qmlParser_data()
171 QTest::addColumn<QString>("file");
173 QString examples = QLatin1String(SRCDIR) + "/../../../../examples/";
174 QString tests = QLatin1String(SRCDIR) + "/../../../../tests/";
177 files << findFiles(QDir(examples));
178 files << findFiles(QDir(tests));
180 foreach (const QString &file, files)
181 QTest::newRow(qPrintable(file)) << file;
185 #if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
186 void tst_qqmlparser::qmlParser()
188 QFETCH(QString, file);
190 using namespace QQmlJS;
195 if (f.open(QFile::ReadOnly))
196 code = QString::fromUtf8(f.readAll());
198 const bool qmlMode = file.endsWith(QLatin1String(".qml"));
201 Lexer lexer(&engine);
202 lexer.setCode(code, 1, qmlMode);
203 Parser parser(&engine);
207 parser.parseProgram();
209 check::Check chk(&engine);
210 chk(parser.rootNode());
214 QTEST_MAIN(tst_qqmlparser)
216 #include "tst_qqmlparser.moc"