QtQml tests: remove QSKIP -> wrap within !QTEST_CROSS_COMPILED guards
[profile/ivi/qtdeclarative.git] / tests / auto / qml / qqmlparser / tst_qqmlparser.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
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>
47
48 #include <qtest.h>
49 #include <QDir>
50 #include <QDebug>
51 #include <cstdlib>
52
53 class tst_qqmlparser : public QObject
54 {
55     Q_OBJECT
56 public:
57     tst_qqmlparser();
58
59 private slots:
60     void initTestCase();
61 #if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
62     void qmlParser_data();
63     void qmlParser();
64 #endif
65
66 private:
67     QStringList excludedDirs;
68
69     QStringList findFiles(const QDir &);
70 };
71
72 namespace check {
73
74 using namespace QQmlJS;
75
76 class Check: public AST::Visitor
77 {
78     Engine *engine;
79     QList<AST::Node *> nodeStack;
80
81 public:
82     Check(Engine *engine)
83         : engine(engine)
84     {
85     }
86
87     void operator()(AST::Node *node)
88     {
89         AST::Node::accept(node, this);
90     }
91
92     void checkNode(AST::Node *node)
93     {
94         if (! nodeStack.isEmpty()) {
95             AST::Node *parent = nodeStack.last();
96             const quint32 parentBegin = parent->firstSourceLocation().begin();
97             const quint32 parentEnd = parent->lastSourceLocation().end();
98
99             QVERIFY(node->firstSourceLocation().begin() >= parentBegin);
100             QVERIFY(node->lastSourceLocation().end() <= parentEnd);
101         }
102     }
103
104     virtual bool preVisit(AST::Node *node)
105     {
106         checkNode(node);
107         nodeStack.append(node);
108         return true;
109     }
110
111     virtual void postVisit(AST::Node *)
112     {
113         nodeStack.removeLast();
114     }
115 };
116
117 }
118
119 tst_qqmlparser::tst_qqmlparser()
120 {
121 }
122
123 void tst_qqmlparser::initTestCase()
124 {
125     // Add directories you want excluded here
126
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";
134 }
135
136 QStringList tst_qqmlparser::findFiles(const QDir &d)
137 {
138     for (int ii = 0; ii < excludedDirs.count(); ++ii) {
139         QString s = excludedDirs.at(ii);
140         if (d.absolutePath().endsWith(s))
141             return QStringList();
142     }
143
144     QStringList rv;
145
146     QStringList files = d.entryList(QStringList() << QLatin1String("*.qml") << QLatin1String("*.js"),
147                                     QDir::Files);
148     foreach (const QString &file, files) {
149         rv << d.absoluteFilePath(file);
150     }
151
152     QStringList dirs = d.entryList(QDir::Dirs | QDir::NoDotAndDotDot |
153                                    QDir::NoSymLinks);
154     foreach (const QString &dir, dirs) {
155         QDir sub = d;
156         sub.cd(dir);
157         rv << findFiles(sub);
158     }
159
160     return rv;
161 }
162
163 /*
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
166 */
167
168 #if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
169 void tst_qqmlparser::qmlParser_data()
170 {
171     QTest::addColumn<QString>("file");
172
173     QString examples = QLatin1String(SRCDIR) + "/../../../../examples/";
174     QString tests = QLatin1String(SRCDIR) + "/../../../../tests/";
175
176     QStringList files;
177     files << findFiles(QDir(examples));
178     files << findFiles(QDir(tests));
179
180     foreach (const QString &file, files)
181         QTest::newRow(qPrintable(file)) << file;
182 }
183 #endif
184
185 #if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
186 void tst_qqmlparser::qmlParser()
187 {
188     QFETCH(QString, file);
189
190     using namespace QQmlJS;
191
192     QString code;
193
194     QFile f(file);
195     if (f.open(QFile::ReadOnly))
196         code = QString::fromUtf8(f.readAll());
197
198     const bool qmlMode = file.endsWith(QLatin1String(".qml"));
199
200     Engine engine;
201     Lexer lexer(&engine);
202     lexer.setCode(code, 1, qmlMode);
203     Parser parser(&engine);
204     if (qmlMode)
205         parser.parse();
206     else
207         parser.parseProgram();
208
209     check::Check chk(&engine);
210     chk(parser.rootNode());
211 }
212 #endif
213
214 QTEST_MAIN(tst_qqmlparser)
215
216 #include "tst_qqmlparser.moc"