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 void qmlParser_data();
65 QStringList excludedDirs;
67 QStringList findFiles(const QDir &);
72 using namespace QQmlJS;
74 class Check: public AST::Visitor
77 QList<AST::Node *> nodeStack;
85 void operator()(AST::Node *node)
87 AST::Node::accept(node, this);
90 void checkNode(AST::Node *node)
92 if (! nodeStack.isEmpty()) {
93 AST::Node *parent = nodeStack.last();
94 const quint32 parentBegin = parent->firstSourceLocation().begin();
95 const quint32 parentEnd = parent->lastSourceLocation().end();
97 QVERIFY(node->firstSourceLocation().begin() >= parentBegin);
98 QVERIFY(node->lastSourceLocation().end() <= parentEnd);
102 virtual bool preVisit(AST::Node *node)
105 nodeStack.append(node);
109 virtual void postVisit(AST::Node *)
111 nodeStack.removeLast();
117 tst_qqmlparser::tst_qqmlparser()
121 void tst_qqmlparser::initTestCase()
123 // Add directories you want excluded here
125 // These snippets are not expected to run on their own.
126 excludedDirs << "doc/src/snippets/qml/visualdatamodel_rootindex";
127 excludedDirs << "doc/src/snippets/qml/qtbinding";
128 excludedDirs << "doc/src/snippets/qml/imports";
129 excludedDirs << "doc/src/snippets/qtquick1/visualdatamodel_rootindex";
130 excludedDirs << "doc/src/snippets/qtquick1/qtbinding";
131 excludedDirs << "doc/src/snippets/qtquick1/imports";
134 QStringList tst_qqmlparser::findFiles(const QDir &d)
136 for (int ii = 0; ii < excludedDirs.count(); ++ii) {
137 QString s = excludedDirs.at(ii);
138 if (d.absolutePath().endsWith(s))
139 return QStringList();
144 QStringList files = d.entryList(QStringList() << QLatin1String("*.qml") << QLatin1String("*.js"),
146 foreach (const QString &file, files) {
147 rv << d.absoluteFilePath(file);
150 QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
152 foreach (const QString &dir, dirs) {
155 rv << findFiles(sub);
162 This test checks all the qml and js files in the QtQml UI source tree
163 and ensures that the subnode's source locations are inside parent node's source locations
166 void tst_qqmlparser::qmlParser_data()
168 QTest::addColumn<QString>("file");
170 QString examples = QLatin1String(SRCDIR) + "/../../../../examples/";
171 QString tests = QLatin1String(SRCDIR) + "/../../../../tests/";
174 files << findFiles(QDir(examples));
175 files << findFiles(QDir(tests));
177 foreach (const QString &file, files)
178 QTest::newRow(qPrintable(file)) << file;
181 void tst_qqmlparser::qmlParser()
183 QFETCH(QString, file);
185 #if defined(QTEST_CROSS_COMPILED)
186 QSKIP("sources not available when cross compiled");
189 using namespace QQmlJS;
194 if (f.open(QFile::ReadOnly))
195 code = QString::fromUtf8(f.readAll());
197 const bool qmlMode = file.endsWith(QLatin1String(".qml"));
200 Lexer lexer(&engine);
201 lexer.setCode(code, 1, qmlMode);
202 Parser parser(&engine);
206 parser.parseProgram();
208 check::Check chk(&engine);
209 chk(parser.rootNode());
212 QTEST_MAIN(tst_qqmlparser)
214 #include "tst_qqmlparser.moc"