1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include <private/qdeclarativejsengine_p.h>
43 #include <private/qdeclarativejsparser_p.h>
44 #include <private/qdeclarativejslexer_p.h>
45 #include <private/qdeclarativejsastvisitor_p.h>
46 #include <private/qdeclarativejsast_p.h>
53 class tst_qdeclarativeparser : public QObject
57 tst_qdeclarativeparser();
61 void qmlParser_data();
65 QStringList excludedDirs;
67 QStringList findFiles(const QDir &);
72 using namespace QDeclarativeJS;
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_qdeclarativeparser::tst_qdeclarativeparser()
121 void tst_qdeclarativeparser::initTestCase()
123 // Add directories you want excluded here
125 // These snippets are not expected to run on their own.
126 excludedDirs << "doc/src/snippets/declarative/visualdatamodel_rootindex";
127 excludedDirs << "doc/src/snippets/declarative/qtbinding";
128 excludedDirs << "doc/src/snippets/declarative/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_qdeclarativeparser::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 declarative UI source tree
163 and ensures that the subnode's source locations are inside parent node's source locations
166 void tst_qdeclarativeparser::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_qdeclarativeparser::qmlParser()
183 QFETCH(QString, file);
185 using namespace QDeclarativeJS;
190 if (f.open(QFile::ReadOnly))
191 code = QString::fromUtf8(f.readAll());
193 const bool qmlMode = file.endsWith(QLatin1String(".qml"));
196 Lexer lexer(&engine);
197 lexer.setCode(code, 1, qmlMode);
198 Parser parser(&engine);
202 parser.parseProgram();
204 check::Check chk(&engine);
205 chk(parser.rootNode());
208 QTEST_MAIN(tst_qdeclarativeparser)
210 #include "tst_qdeclarativeparser.moc"