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 ****************************************************************************/
43 #include <QtTest/QtTest>
45 #include <QtCore/QDir>
46 #include <QtXmlPatterns/QXmlQuery>
47 #include <QtXmlPatterns/QXmlSerializer>
48 #include <QtXmlPatterns/QXmlResultItems>
49 #include <QtXmlPatterns/QXmlFormatter>
51 #include "../qxmlquery/MessageSilencer.h"
52 #include "../qsimplexmlnodemodel/TestSimpleNodeModel.h"
55 \class tst_PatternistExamples
58 \brief Verifies examples for Patternist.
60 class tst_PatternistExamples : public QObject
66 void checkQueries() const;
67 void checkQueries_data() const;
68 void checkXMLFiles() const;
69 void checkXMLFiles_data() const;
70 void buildSnippets() const;
74 QStringList listFiles(const QStringList &patterns) const;
82 void tst_PatternistExamples::initTestCase()
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/")));
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/")));
97 for(int i = 0; i < m_dirs.size(); ++i)
98 QVERIFY(m_dirs.at(i).exists());
102 Returns a QStringList containing absolute filenames that were found in the predefined locations, when
103 filtered through \a pattterns.
105 QStringList tst_PatternistExamples::listFiles(const QStringList &patterns) const
109 for(int i = 0; i < m_dirs.size(); ++i)
111 const QDir &dir = m_dirs.at(i);
113 const QStringList files(dir.entryList(patterns));
114 for(int s = 0; s < files.count(); ++s)
115 result += dir.absoluteFilePath(files.at(s));
122 Check that the queries contains no static errors such as
125 void tst_PatternistExamples::checkQueries() const
127 QFETCH(QString, queryFile);
129 QFile file(queryFile);
130 QVERIFY(file.open(QIODevice::ReadOnly));
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")));
137 /* This is needed for the recipes example. */
138 query.bindVariable(QLatin1String("inputDocument"), QVariant(QString::fromLatin1("dummString")));
140 /* This is needed for literalsAndOperators.xq. */
141 query.bindVariable(QLatin1String("date"), QVariant(QDate::currentDate()));
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));
148 /* and filetree/ needs this. */
149 TestSimpleNodeModel nodeModel(query.namePool());
150 query.bindVariable(QLatin1String("exampleDirectory"), nodeModel.root());
152 query.setQuery(&file, queryFile);
154 QVERIFY2(query.isValid(), QString::fromLatin1("%1 failed to compile").arg(queryFile).toLatin1().constData());
157 void tst_PatternistExamples::checkQueries_data() const
159 QTest::addColumn<QString>("queryFile");
161 const QStringList queryExamples(listFiles(QStringList(QLatin1String("*.xq"))));
163 QCOMPARE(queryExamples.count(), int(XQueryFileCount));
165 foreach(QString q, queryExamples)
166 QTest::newRow(q.toLocal8Bit().constData()) << q;
169 void tst_PatternistExamples::checkXMLFiles() const
171 QFETCH(QString, file);
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());
178 /* We don't care about the result, we only want to ensure the files can be parsed. */
180 QBuffer buffer(&dummy);
181 QVERIFY(buffer.open(QIODevice::WriteOnly));
183 QXmlSerializer serializer(query, &buffer);
185 /* This is the important one. */
186 QVERIFY(query.evaluateTo(&serializer));
189 void tst_PatternistExamples::checkXMLFiles_data() const
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"));
199 const QStringList xmlFiles(listFiles(patterns));
201 if(xmlFiles.count() != XMLFileCount)
202 qDebug() << "These files were encountered:" << xmlFiles;
204 QCOMPARE(xmlFiles.count(), int(XMLFileCount));
206 foreach(QString q, xmlFiles)
207 QTest::newRow(q.toLocal8Bit().constData()) << q;
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.
214 An improvement could be to run them, to ensure that they behave as they intend
218 static QUrl abstractURI()
222 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qabstracturiresolver.cpp"
228 MyValue parent() const
234 static MyValue toMyValue(const QXmlNodeModelIndex &)
239 static QXmlNodeModelIndex toNodeIndex(const MyValue &)
241 return QXmlNodeModelIndex();
244 class MyTreeModel : public QSimpleXmlNodeModel
247 MyTreeModel(const QXmlNamePool &np, const QFile &f);
249 virtual QUrl documentUri(const QXmlNodeModelIndex&) const
254 virtual QXmlNodeModelIndex::NodeKind kind(const QXmlNodeModelIndex&) const
256 return QXmlNodeModelIndex::Element;
259 virtual QXmlNodeModelIndex::DocumentOrder compareOrder(const QXmlNodeModelIndex&, const QXmlNodeModelIndex&) const
261 return QXmlNodeModelIndex::Is;
264 virtual QXmlNodeModelIndex root(const QXmlNodeModelIndex&) const
266 return QXmlNodeModelIndex();
269 virtual QXmlName name(const QXmlNodeModelIndex&) const
274 virtual QVariant typedValue(const QXmlNodeModelIndex&) const
279 virtual QVector<QXmlNodeModelIndex> attributes(const QXmlNodeModelIndex&) const
281 return QVector<QXmlNodeModelIndex>();
284 QXmlNodeModelIndex nodeFor(const QString &) const
286 return QXmlNodeModelIndex();
289 virtual QXmlNodeModelIndex nextFromSimpleAxis(SimpleAxis axis, const QXmlNodeModelIndex &origin) const;
293 Exists for linking with at least msvc-2005.
295 MyTreeModel::MyTreeModel(const QXmlNamePool &np, const QFile &) : QSimpleXmlNodeModel(np)
299 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qsimplexmlnodemodel.cpp"
308 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlforwarditerator.cpp"
311 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qxmlname.cpp"
313 void tst_PatternistExamples::buildSnippets() const
315 /* We don't run this code, see comment above. */
318 /* We place a call to this function, such that GCC doesn't emit a warning. */
325 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qxmlresultitems.cpp"
332 QIODevice *myOutputDevice = 0;
333 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qxmlformatter.cpp"
337 QIODevice *myOutputDevice = 0;
338 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qxmlserializer.cpp"
342 QXmlNodeModelIndex myInstance;
343 const char **argv = 0;
344 typedef MyTreeModel ChemistryNodeModel;
345 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlnodemodel.cpp"
352 QIODevice *myOutputDevice = 0;
353 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlreceiver.cpp"
360 #include "../../../doc/src/snippets/code/src_xmlpatterns_api_qxmlquery.cpp"
364 QTEST_MAIN(tst_PatternistExamples)
366 #include "tst_patternistexamples.moc"
368 // vim: et:ts=4:sw=4:sts=4