1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the test suite of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
45 #ifdef QTEST_XMLPATTERNS
47 #include <QtCore/QDir>
48 #include <QtXmlPatterns/QXmlQuery>
49 #include <QtXmlPatterns/QXmlSerializer>
50 #include <QtXmlPatterns/QXmlResultItems>
51 #include <QtXmlPatterns/QXmlFormatter>
53 #include "../qxmlquery/MessageSilencer.h"
54 #include "../qsimplexmlnodemodel/TestSimpleNodeModel.h"
57 \class tst_PatternistExamples
60 \brief Verifies examples for Patternist.
62 class tst_PatternistExamples : public QObject
68 void checkQueries() const;
69 void checkQueries_data() const;
70 void checkXMLFiles() const;
71 void checkXMLFiles_data() const;
72 void buildSnippets() const;
76 QStringList listFiles(const QStringList &patterns) const;
84 void tst_PatternistExamples::initTestCase()
87 m_dirs.append(QDir(QLatin1String("../../../doc/src/snippets/patternist/")));
88 m_dirs.append(QDir(QLatin1String("../../../examples/xmlpatterns/xquery/globalVariables/")));
89 m_dirs.append(QDir(QLatin1String("../../../examples/xmlpatterns/filetree/")));
90 m_dirs.append(QDir(QLatin1String("../../../examples/xmlpatterns/recipes/")));
91 m_dirs.append(QDir(QLatin1String("../../../examples/xmlpatterns/recipes/files/")));
93 m_dirs.append(QDir(QLatin1String("patternist/")));
94 m_dirs.append(QDir(QLatin1String("globalVariables/")));
95 m_dirs.append(QDir(QLatin1String("filetree/")));
96 m_dirs.append(QDir(QLatin1String("recipes/")));
97 m_dirs.append(QDir(QLatin1String("recipes/files/")));
99 for(int i = 0; i < m_dirs.size(); ++i)
100 QVERIFY(m_dirs.at(i).exists());
104 Returns a QStringList containing absolute filenames that were found in the predefined locations, when
105 filtered through \a pattterns.
107 QStringList tst_PatternistExamples::listFiles(const QStringList &patterns) const
111 for(int i = 0; i < m_dirs.size(); ++i)
113 const QDir &dir = m_dirs.at(i);
115 const QStringList files(dir.entryList(patterns));
116 for(int s = 0; s < files.count(); ++s)
117 result += dir.absoluteFilePath(files.at(s));
124 Check that the queries contains no static errors such as
127 void tst_PatternistExamples::checkQueries() const
129 QFETCH(QString, queryFile);
131 QFile file(queryFile);
132 QVERIFY(file.open(QIODevice::ReadOnly));
136 /* Two queries relies on this binding, so provide it such that we don't get a compile error. */
137 query.bindVariable(QLatin1String("fileToOpen"), QVariant(QString::fromLatin1("dummyString")));
139 /* This is needed for the recipes example. */
140 query.bindVariable(QLatin1String("inputDocument"), QVariant(QString::fromLatin1("dummString")));
142 /* This is needed for literalsAndOperators.xq. */
143 query.bindVariable(QLatin1String("date"), QVariant(QDate::currentDate()));
145 /* These are needed for introExample2.xq. */
146 query.bindVariable(QLatin1String("file"), QVariant(QLatin1String("dummy")));
147 query.bindVariable(QLatin1String("publisher"), QVariant(QLatin1String("dummy")));
148 query.bindVariable(QLatin1String("year"), QVariant(2000));
150 /* and filetree/ needs this. */
151 TestSimpleNodeModel nodeModel(query.namePool());
152 query.bindVariable(QLatin1String("exampleDirectory"), nodeModel.root());
154 query.setQuery(&file, queryFile);
156 QVERIFY2(query.isValid(), QString::fromLatin1("%1 failed to compile").arg(queryFile).toLatin1().constData());
159 void tst_PatternistExamples::checkQueries_data() const
161 QTest::addColumn<QString>("queryFile");
163 const QStringList queryExamples(listFiles(QStringList(QLatin1String("*.xq"))));
165 QCOMPARE(queryExamples.count(), int(XQueryFileCount));
167 foreach(QString q, queryExamples)
168 QTest::newRow(q.toLocal8Bit().constData()) << q;
171 void tst_PatternistExamples::checkXMLFiles() const
173 QFETCH(QString, file);
176 /* Wrapping in QUrl ensures it gets formatted as a URI on all platforms. */
177 query.setQuery(QLatin1String("doc('") + QUrl::fromLocalFile(file).toString() + QLatin1String("')"));
178 QVERIFY(query.isValid());
180 /* We don't care about the result, we only want to ensure the files can be parsed. */
182 QBuffer buffer(&dummy);
183 QVERIFY(buffer.open(QIODevice::WriteOnly));
185 QXmlSerializer serializer(query, &buffer);
187 /* This is the important one. */
188 QVERIFY(query.evaluateTo(&serializer));
191 void tst_PatternistExamples::checkXMLFiles_data() const
193 QTest::addColumn<QString>("file");
194 QStringList patterns;
195 patterns.append(QLatin1String("*.xml"));
196 patterns.append(QLatin1String("*.gccxml"));
197 patterns.append(QLatin1String("*.svg"));
198 patterns.append(QLatin1String("*.ui"));
199 patterns.append(QLatin1String("*.html"));
201 const QStringList xmlFiles(listFiles(patterns));
203 if(xmlFiles.count() != XMLFileCount)
204 qDebug() << "These files were encountered:" << xmlFiles;
206 QCOMPARE(xmlFiles.count(), int(XMLFileCount));
208 foreach(QString q, xmlFiles)
209 QTest::newRow(q.toLocal8Bit().constData()) << q;
213 Below, we include all the examples and ensure that they build, such that we rule
214 out syntax error and that API changes has propagated into examples.
216 An improvement could be to run them, to ensure that they behave as they intend
220 static QUrl abstractURI()
224 #include "../../doc/src/snippets/code/src_xmlpatterns_api_qabstracturiresolver.cpp"
230 MyValue parent() const
236 static MyValue toMyValue(const QXmlNodeModelIndex &)
241 static QXmlNodeModelIndex toNodeIndex(const MyValue &)
243 return QXmlNodeModelIndex();
246 class MyTreeModel : public QSimpleXmlNodeModel
249 MyTreeModel(const QXmlNamePool &np, const QFile &f);
251 virtual QUrl documentUri(const QXmlNodeModelIndex&) const
256 virtual QXmlNodeModelIndex::NodeKind kind(const QXmlNodeModelIndex&) const
258 return QXmlNodeModelIndex::Element;
261 virtual QXmlNodeModelIndex::DocumentOrder compareOrder(const QXmlNodeModelIndex&, const QXmlNodeModelIndex&) const
263 return QXmlNodeModelIndex::Is;
266 virtual QXmlNodeModelIndex root(const QXmlNodeModelIndex&) const
268 return QXmlNodeModelIndex();
271 virtual QXmlName name(const QXmlNodeModelIndex&) const
276 virtual QVariant typedValue(const QXmlNodeModelIndex&) const
281 virtual QVector<QXmlNodeModelIndex> attributes(const QXmlNodeModelIndex&) const
283 return QVector<QXmlNodeModelIndex>();
286 QXmlNodeModelIndex nodeFor(const QString &) const
288 return QXmlNodeModelIndex();
291 virtual QXmlNodeModelIndex nextFromSimpleAxis(SimpleAxis axis, const QXmlNodeModelIndex &origin) const;
295 Exists for linking with at least msvc-2005.
297 MyTreeModel::MyTreeModel(const QXmlNamePool &np, const QFile &) : QSimpleXmlNodeModel(np)
301 #include "../../doc/src/snippets/code/src_xmlpatterns_api_qsimplexmlnodemodel.cpp"
310 #include "../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlforwarditerator.cpp"
313 #include "../../doc/src/snippets/code/src_xmlpatterns_api_qxmlname.cpp"
315 void tst_PatternistExamples::buildSnippets() const
317 /* We don't run this code, see comment above. */
320 /* We place a call to this function, such that GCC doesn't emit a warning. */
327 #include "../../doc/src/snippets/code/src_xmlpatterns_api_qxmlresultitems.cpp"
334 QIODevice *myOutputDevice = 0;
335 #include "../../doc/src/snippets/code/src_xmlpatterns_api_qxmlformatter.cpp"
339 QIODevice *myOutputDevice = 0;
340 #include "../../doc/src/snippets/code/src_xmlpatterns_api_qxmlserializer.cpp"
344 QXmlNodeModelIndex myInstance;
345 const char **argv = 0;
346 typedef MyTreeModel ChemistryNodeModel;
347 #include "../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlnodemodel.cpp"
354 QIODevice *myOutputDevice = 0;
355 #include "../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlreceiver.cpp"
362 #include "../../doc/src/snippets/code/src_xmlpatterns_api_qxmlquery.cpp"
366 QTEST_MAIN(tst_PatternistExamples)
368 #include "tst_patternistexamples.moc"
373 // vim: et:ts=4:sw=4:sts=4