8d0c46d786ff89d40a031458c626bbf67d0fde1d
[profile/ivi/qtxmlpatterns.git] / tests / auto / patternistexamples / tst_patternistexamples.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
43 #include <QtTest/QtTest>
44
45 #include <QtCore/QDir>
46 #include <QtXmlPatterns/QXmlQuery>
47 #include <QtXmlPatterns/QXmlSerializer>
48 #include <QtXmlPatterns/QXmlResultItems>
49 #include <QtXmlPatterns/QXmlFormatter>
50
51 #include "../qxmlquery/MessageSilencer.h"
52 #include "../qsimplexmlnodemodel/TestSimpleNodeModel.h"
53
54 /*!
55  \class tst_PatternistExamples
56  \internal
57  \since 4.4
58  \brief Verifies examples for Patternist.
59  */
60 class tst_PatternistExamples : public QObject
61 {
62     Q_OBJECT
63
64 private Q_SLOTS:
65     void initTestCase();
66     void checkQueries() const;
67     void checkQueries_data() const;
68     void checkXMLFiles() const;
69     void checkXMLFiles_data() const;
70     void buildSnippets() const;
71
72 private:
73     QVector<QDir> m_dirs;
74     QStringList listFiles(const QStringList &patterns) const;
75     enum Constants
76     {
77         XMLFileCount = 12,
78         XQueryFileCount = 52
79     };
80 };
81
82 void tst_PatternistExamples::initTestCase()
83 {
84 #ifndef Q_OS_WINCE
85     m_dirs.append(QDir(QLatin1String(SOURCETREE "doc/src/snippets/patternist/")));
86     m_dirs.append(QDir(QLatin1String(SOURCETREE "examples/xmlpatterns/xquery/globalVariables/")));
87     m_dirs.append(QDir(QLatin1String(SOURCETREE "examples/xmlpatterns/filetree/")));
88     m_dirs.append(QDir(QLatin1String(SOURCETREE "examples/xmlpatterns/recipes/")));
89     m_dirs.append(QDir(QLatin1String(SOURCETREE "examples/xmlpatterns/recipes/files/")));
90 #else
91     m_dirs.append(QDir(QFINDTESTDATA("patternist/")));
92     m_dirs.append(QDir(QFINDTESTDATA("globalVariables/")));
93     m_dirs.append(QDir(QFINDTESTDATA("filetree/")));
94     m_dirs.append(QDir(QFINDTESTDATA("recipes/")));
95     m_dirs.append(QDir(QFINDTESTDATA("recipes/files/")));
96 #endif
97     for(int i = 0; i < m_dirs.size(); ++i)
98         QVERIFY(m_dirs.at(i).exists());
99 }
100
101 /*!
102   Returns a QStringList containing absolute filenames that were found in the predefined locations, when
103   filtered through \a pattterns.
104  */
105 QStringList tst_PatternistExamples::listFiles(const QStringList &patterns) const
106 {
107     QStringList result;
108
109     for(int i = 0; i < m_dirs.size(); ++i)
110     {
111         const QDir &dir = m_dirs.at(i);
112
113         const QStringList files(dir.entryList(patterns));
114         for(int s = 0; s < files.count(); ++s)
115             result += dir.absoluteFilePath(files.at(s));
116     }
117
118     return result;
119 }
120
121 /*!
122   Check that the queries contains no static errors such as
123   syntax errors.
124  */
125 void tst_PatternistExamples::checkQueries() const
126 {
127     QFETCH(QString, queryFile);
128
129     QFile file(queryFile);
130     QVERIFY(file.open(QIODevice::ReadOnly));
131
132     QXmlQuery query;
133
134     /* Two queries relies on this binding, so provide it such that we don't get a compile error. */
135     query.bindVariable(QLatin1String("fileToOpen"), QVariant(QString::fromLatin1("dummyString")));
136
137     /* This is needed for the recipes example. */
138     query.bindVariable(QLatin1String("inputDocument"), QVariant(QString::fromLatin1("dummString")));
139
140     /* This is needed for literalsAndOperators.xq. */
141     query.bindVariable(QLatin1String("date"), QVariant(QDate::currentDate()));
142
143     /* These are needed for introExample2.xq. */
144     query.bindVariable(QLatin1String("file"), QVariant(QLatin1String("dummy")));
145     query.bindVariable(QLatin1String("publisher"), QVariant(QLatin1String("dummy")));
146     query.bindVariable(QLatin1String("year"), QVariant(2000));
147
148     /* and filetree/ needs this. */
149     TestSimpleNodeModel nodeModel(query.namePool());
150     query.bindVariable(QLatin1String("exampleDirectory"), nodeModel.root());
151
152     query.setQuery(&file, queryFile);
153
154     QVERIFY2(query.isValid(), QString::fromLatin1("%1 failed to compile").arg(queryFile).toLatin1().constData());
155 }
156
157 void tst_PatternistExamples::checkQueries_data() const
158 {
159     QTest::addColumn<QString>("queryFile");
160
161     const QStringList queryExamples(listFiles(QStringList(QLatin1String("*.xq"))));
162
163     QCOMPARE(queryExamples.count(), int(XQueryFileCount));
164
165     foreach(QString q, queryExamples)
166         QTest::newRow(q.toLocal8Bit().constData()) << q;
167 }
168
169 void tst_PatternistExamples::checkXMLFiles() const
170 {
171     QFETCH(QString, file);
172
173     QXmlQuery query;
174     /* Wrapping in QUrl ensures it gets formatted as a URI on all platforms. */
175     query.setQuery(QLatin1String("doc('") + QUrl::fromLocalFile(file).toString() + QLatin1String("')"));
176     QVERIFY(query.isValid());
177
178     /* We don't care about the result, we only want to ensure the files can be parsed. */
179     QByteArray dummy;
180     QBuffer buffer(&dummy);
181     QVERIFY(buffer.open(QIODevice::WriteOnly));
182
183     QXmlSerializer serializer(query, &buffer);
184
185     /* This is the important one. */
186     QVERIFY(query.evaluateTo(&serializer));
187 }
188
189 void tst_PatternistExamples::checkXMLFiles_data() const
190 {
191     QTest::addColumn<QString>("file");
192     QStringList patterns;
193     patterns.append(QLatin1String("*.xml"));
194     patterns.append(QLatin1String("*.gccxml"));
195     patterns.append(QLatin1String("*.svg"));
196     patterns.append(QLatin1String("*.ui"));
197     patterns.append(QLatin1String("*.html"));
198
199     const QStringList xmlFiles(listFiles(patterns));
200
201     if(xmlFiles.count() != XMLFileCount)
202         qDebug() << "These files were encountered:" << xmlFiles;
203
204     QCOMPARE(xmlFiles.count(), int(XMLFileCount));
205
206     foreach(QString q, xmlFiles)
207         QTest::newRow(q.toLocal8Bit().constData()) << q;
208 }
209
210 /*!
211  Below, we include all the examples and ensure that they build, such that we rule
212  out syntax error and that API changes has propagated into examples.
213
214  An improvement could be to run them, to ensure that they behave as they intend
215  to.
216  */
217
218 static QUrl abstractURI()
219 {
220     QUrl baseURI;
221     QUrl relative;
222 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qabstracturiresolver.cpp"
223 }
224
225 class MyValue
226 {
227 public:
228     MyValue parent() const
229     {
230         return MyValue();
231     }
232 };
233
234 static MyValue toMyValue(const QXmlNodeModelIndex &)
235 {
236     return MyValue();
237 }
238
239 static QXmlNodeModelIndex toNodeIndex(const MyValue &)
240 {
241     return QXmlNodeModelIndex();
242 }
243
244 class MyTreeModel : public QSimpleXmlNodeModel
245 {
246 public:
247     MyTreeModel(const QXmlNamePool &np, const QFile &f);
248
249     virtual QUrl documentUri(const QXmlNodeModelIndex&) const
250     {
251         return QUrl();
252     }
253
254     virtual QXmlNodeModelIndex::NodeKind kind(const QXmlNodeModelIndex&) const
255     {
256         return QXmlNodeModelIndex::Element;
257     }
258
259     virtual QXmlNodeModelIndex::DocumentOrder compareOrder(const QXmlNodeModelIndex&, const QXmlNodeModelIndex&) const
260     {
261         return QXmlNodeModelIndex::Is;
262     }
263
264     virtual QXmlNodeModelIndex root(const QXmlNodeModelIndex&) const
265     {
266         return QXmlNodeModelIndex();
267     }
268
269     virtual QXmlName name(const QXmlNodeModelIndex&) const
270     {
271         return QXmlName();
272     }
273
274     virtual QVariant typedValue(const QXmlNodeModelIndex&) const
275     {
276         return QVariant();
277     }
278
279     virtual QVector<QXmlNodeModelIndex> attributes(const QXmlNodeModelIndex&) const
280     {
281         return QVector<QXmlNodeModelIndex>();
282     }
283
284     QXmlNodeModelIndex nodeFor(const QString &) const
285     {
286         return QXmlNodeModelIndex();
287     }
288
289     virtual QXmlNodeModelIndex nextFromSimpleAxis(SimpleAxis axis, const QXmlNodeModelIndex &origin) const;
290 };
291
292 /*
293  Exists for linking with at least msvc-2005.
294 */
295 MyTreeModel::MyTreeModel(const QXmlNamePool &np, const QFile &) : QSimpleXmlNodeModel(np)
296 {
297 }
298
299 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qsimplexmlnodemodel.cpp"
300
301 class MyMapper
302 {
303 public:
304     class InputType;
305     enum OutputType
306     {
307     };
308 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlforwarditerator.cpp"
309 };
310
311 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qxmlname.cpp"
312
313 void tst_PatternistExamples::buildSnippets() const
314 {
315     /* We don't run this code, see comment above. */
316     return;
317
318     /* We place a call to this function, such that GCC doesn't emit a warning. */
319     abstractURI();
320
321     {
322     }
323
324     {
325 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qxmlresultitems.cpp"
326     }
327
328     {
329     }
330
331     {
332         QIODevice *myOutputDevice = 0;
333 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qxmlformatter.cpp"
334     }
335
336     {
337         QIODevice *myOutputDevice = 0;
338 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qxmlserializer.cpp"
339     }
340
341     {
342         QXmlNodeModelIndex myInstance;
343         const char **argv = 0;
344         typedef MyTreeModel ChemistryNodeModel;
345 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlnodemodel.cpp"
346     }
347
348     {
349     }
350
351     {
352         QIODevice *myOutputDevice = 0;
353 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlreceiver.cpp"
354     }
355
356     {
357         QXmlQuery query;
358         QString localName;
359         QVariant value;
360 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qxmlquery.cpp"
361     }
362 }
363
364 QTEST_MAIN(tst_PatternistExamples)
365
366 #include "tst_patternistexamples.moc"
367
368 // vim: et:ts=4:sw=4:sts=4