summaryrefslogtreecommitdiffstats
path: root/tests/auto/patternistexamples/tst_patternistexamples.cpp
blob: 2b728f9990222bd54bf30c211e7e26cca2a10582 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/


#include <QtTest/QtTest>

#ifdef QTEST_XMLPATTERNS

#include <QtCore/QDir>
#include <QtXmlPatterns/QXmlQuery>
#include <QtXmlPatterns/QXmlSerializer>
#include <QtXmlPatterns/QXmlResultItems>
#include <QtXmlPatterns/QXmlFormatter>

#include "../qxmlquery/MessageSilencer.h"
#include "../qsimplexmlnodemodel/TestSimpleNodeModel.h"

/*!
 \class tst_PatternistExamples
 \internal
 \since 4.4
 \brief Verifies examples for Patternist.
 */
class tst_PatternistExamples : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void initTestCase();
    void checkQueries() const;
    void checkQueries_data() const;
    void checkXMLFiles() const;
    void checkXMLFiles_data() const;
    void buildSnippets() const;

private:
    QVector<QDir> m_dirs;
    QStringList listFiles(const QStringList &patterns) const;
    enum Constants
    {
        XMLFileCount = 12,
        XQueryFileCount = 52
    };
};

void tst_PatternistExamples::initTestCase()
{
#ifndef Q_OS_WINCE
    m_dirs.append(QDir(QLatin1String("../../../doc/src/snippets/patternist/")));
    m_dirs.append(QDir(QLatin1String("../../../examples/xmlpatterns/xquery/globalVariables/")));
    m_dirs.append(QDir(QLatin1String("../../../examples/xmlpatterns/filetree/")));
    m_dirs.append(QDir(QLatin1String("../../../examples/xmlpatterns/recipes/")));
    m_dirs.append(QDir(QLatin1String("../../../examples/xmlpatterns/recipes/files/")));
#else
    m_dirs.append(QDir(QLatin1String("patternist/")));
    m_dirs.append(QDir(QLatin1String("globalVariables/")));
    m_dirs.append(QDir(QLatin1String("filetree/")));
    m_dirs.append(QDir(QLatin1String("recipes/")));
    m_dirs.append(QDir(QLatin1String("recipes/files/")));
#endif
    for(int i = 0; i < m_dirs.size(); ++i)
        QVERIFY(m_dirs.at(i).exists());
}

/*!
  Returns a QStringList containing absolute filenames that were found in the predefined locations, when
  filtered through \a pattterns.
 */
QStringList tst_PatternistExamples::listFiles(const QStringList &patterns) const
{
    QStringList result;

    for(int i = 0; i < m_dirs.size(); ++i)
    {
        const QDir &dir = m_dirs.at(i);

        const QStringList files(dir.entryList(patterns));
        for(int s = 0; s < files.count(); ++s)
            result += dir.absoluteFilePath(files.at(s));
    }

    return result;
}

/*!
  Check that the queries contains no static errors such as
  syntax errors.
 */
void tst_PatternistExamples::checkQueries() const
{
    QFETCH(QString, queryFile);

    QFile file(queryFile);
    QVERIFY(file.open(QIODevice::ReadOnly));

    QXmlQuery query;

    /* Two queries relies on this binding, so provide it such that we don't get a compile error. */
    query.bindVariable(QLatin1String("fileToOpen"), QVariant(QString::fromLatin1("dummyString")));

    /* This is needed for the recipes example. */
    query.bindVariable(QLatin1String("inputDocument"), QVariant(QString::fromLatin1("dummString")));

    /* This is needed for literalsAndOperators.xq. */
    query.bindVariable(QLatin1String("date"), QVariant(QDate::currentDate()));

    /* These are needed for introExample2.xq. */
    query.bindVariable(QLatin1String("file"), QVariant(QLatin1String("dummy")));
    query.bindVariable(QLatin1String("publisher"), QVariant(QLatin1String("dummy")));
    query.bindVariable(QLatin1String("year"), QVariant(2000));

    /* and filetree/ needs this. */
    TestSimpleNodeModel nodeModel(query.namePool());
    query.bindVariable(QLatin1String("exampleDirectory"), nodeModel.root());

    query.setQuery(&file, queryFile);

    QVERIFY2(query.isValid(), QString::fromLatin1("%1 failed to compile").arg(queryFile).toLatin1().constData());
}

void tst_PatternistExamples::checkQueries_data() const
{
    QTest::addColumn<QString>("queryFile");

    const QStringList queryExamples(listFiles(QStringList(QLatin1String("*.xq"))));

    QCOMPARE(queryExamples.count(), int(XQueryFileCount));

    foreach(QString q, queryExamples)
        QTest::newRow(q.toLocal8Bit().constData()) << q;
}

void tst_PatternistExamples::checkXMLFiles() const
{
    QFETCH(QString, file);

    QXmlQuery query;
    /* Wrapping in QUrl ensures it gets formatted as a URI on all platforms. */
    query.setQuery(QLatin1String("doc('") + QUrl::fromLocalFile(file).toString() + QLatin1String("')"));
    QVERIFY(query.isValid());

    /* We don't care about the result, we only want to ensure the files can be parsed. */
    QByteArray dummy;
    QBuffer buffer(&dummy);
    QVERIFY(buffer.open(QIODevice::WriteOnly));

    QXmlSerializer serializer(query, &buffer);

    /* This is the important one. */
    QVERIFY(query.evaluateTo(&serializer));
}

void tst_PatternistExamples::checkXMLFiles_data() const
{
    QTest::addColumn<QString>("file");
    QStringList patterns;
    patterns.append(QLatin1String("*.xml"));
    patterns.append(QLatin1String("*.gccxml"));
    patterns.append(QLatin1String("*.svg"));
    patterns.append(QLatin1String("*.ui"));
    patterns.append(QLatin1String("*.html"));

    const QStringList xmlFiles(listFiles(patterns));

    if(xmlFiles.count() != XMLFileCount)
        qDebug() << "These files were encountered:" << xmlFiles;

    QCOMPARE(xmlFiles.count(), int(XMLFileCount));

    foreach(QString q, xmlFiles)
        QTest::newRow(q.toLocal8Bit().constData()) << q;
}

/*!
 Below, we include all the examples and ensure that they build, such that we rule
 out syntax error and that API changes has propagated into examples.

 An improvement could be to run them, to ensure that they behave as they intend
 to.
 */

static QUrl abstractURI()
{
    QUrl baseURI;
    QUrl relative;
#include "../../doc/src/snippets/code/src_xmlpatterns_api_qabstracturiresolver.cpp"
}

class MyValue
{
public:
    MyValue parent() const
    {
        return MyValue();
    }
};

static MyValue toMyValue(const QXmlNodeModelIndex &)
{
    return MyValue();
}

static QXmlNodeModelIndex toNodeIndex(const MyValue &)
{
    return QXmlNodeModelIndex();
}

class MyTreeModel : public QSimpleXmlNodeModel
{
public:
    MyTreeModel(const QXmlNamePool &np, const QFile &f);

    virtual QUrl documentUri(const QXmlNodeModelIndex&) const
    {
        return QUrl();
    }

    virtual QXmlNodeModelIndex::NodeKind kind(const QXmlNodeModelIndex&) const
    {
        return QXmlNodeModelIndex::Element;
    }

    virtual QXmlNodeModelIndex::DocumentOrder compareOrder(const QXmlNodeModelIndex&, const QXmlNodeModelIndex&) const
    {
        return QXmlNodeModelIndex::Is;
    }

    virtual QXmlNodeModelIndex root(const QXmlNodeModelIndex&) const
    {
        return QXmlNodeModelIndex();
    }

    virtual QXmlName name(const QXmlNodeModelIndex&) const
    {
        return QXmlName();
    }

    virtual QVariant typedValue(const QXmlNodeModelIndex&) const
    {
        return QVariant();
    }

    virtual QVector<QXmlNodeModelIndex> attributes(const QXmlNodeModelIndex&) const
    {
        return QVector<QXmlNodeModelIndex>();
    }

    QXmlNodeModelIndex nodeFor(const QString &) const
    {
        return QXmlNodeModelIndex();
    }

    virtual QXmlNodeModelIndex nextFromSimpleAxis(SimpleAxis axis, const QXmlNodeModelIndex &origin) const;
};

/*
 Exists for linking with at least msvc-2005.
*/
MyTreeModel::MyTreeModel(const QXmlNamePool &np, const QFile &) : QSimpleXmlNodeModel(np)
{
}

#include "../../doc/src/snippets/code/src_xmlpatterns_api_qsimplexmlnodemodel.cpp"

class MyMapper
{
public:
    class InputType;
    enum OutputType
    {
    };
#include "../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlforwarditerator.cpp"
};

#include "../../doc/src/snippets/code/src_xmlpatterns_api_qxmlname.cpp"

void tst_PatternistExamples::buildSnippets() const
{
    /* We don't run this code, see comment above. */
    return;

    /* We place a call to this function, such that GCC doesn't emit a warning. */
    abstractURI();

    {
    }

    {
#include "../../doc/src/snippets/code/src_xmlpatterns_api_qxmlresultitems.cpp"
    }

    {
    }

    {
        QIODevice *myOutputDevice = 0;
#include "../../doc/src/snippets/code/src_xmlpatterns_api_qxmlformatter.cpp"
    }

    {
        QIODevice *myOutputDevice = 0;
#include "../../doc/src/snippets/code/src_xmlpatterns_api_qxmlserializer.cpp"
    }

    {
        QXmlNodeModelIndex myInstance;
        const char **argv = 0;
        typedef MyTreeModel ChemistryNodeModel;
#include "../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlnodemodel.cpp"
    }

    {
    }

    {
        QIODevice *myOutputDevice = 0;
#include "../../doc/src/snippets/code/src_xmlpatterns_api_qabstractxmlreceiver.cpp"
    }

    {
        QXmlQuery query;
        QString localName;
        QVariant value;
#include "../../doc/src/snippets/code/src_xmlpatterns_api_qxmlquery.cpp"
    }
}

QTEST_MAIN(tst_PatternistExamples)

#include "tst_patternistexamples.moc"
#else
QTEST_NOOP_MAIN
#endif

// vim: et:ts=4:sw=4:sts=4