1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
45 #include <QtXmlPatterns/QXmlItem>
46 #include <QtXmlPatterns/QXmlQuery>
47 #include <QtXmlPatterns/QXmlResultItems>
49 #include "../qxmlquery/MessageSilencer.h"
51 \class tst_QXmlResultItems
54 \brief Tests class QXmlResultItems.
57 class tst_QXmlResultItems : public QObject
62 void defaultConstructor() const;
65 void hasError() const;
66 void objectSize() const;
67 void constCorrectness() const;
69 void evalateWithInstantError() const;
70 void evalateWithQueryError() const;
71 void evaluate() const;
72 void evaluate_data() const;
75 void tst_QXmlResultItems::defaultConstructor() const
78 QXmlResultItems result;
82 QXmlResultItems result1;
83 QXmlResultItems result2;
87 QXmlResultItems result1;
88 QXmlResultItems result2;
89 QXmlResultItems result3;
93 void tst_QXmlResultItems::next() const
95 /* Check default value. */
97 QXmlResultItems result;
98 QVERIFY(result.next().isNull());
101 /* Stress it on a default constructed value. */
103 QXmlResultItems result;
104 QVERIFY(result.next().isNull());
105 QVERIFY(result.next().isNull());
106 QVERIFY(result.next().isNull());
110 void tst_QXmlResultItems::current() const
112 /* Check default value. */
114 QXmlResultItems result;
115 QVERIFY(result.current().isNull());
118 /* Stress it on a default constructed value. */
120 QXmlResultItems result;
121 QVERIFY(result.current().isNull());
122 QVERIFY(result.current().isNull());
123 QVERIFY(result.current().isNull());
127 void tst_QXmlResultItems::hasError() const
129 /* Check default value. */
131 QXmlResultItems result;
132 QVERIFY(!result.hasError());
135 /* Stress it on a default constructed value. */
137 QXmlResultItems result;
138 QVERIFY(!result.hasError());
139 QVERIFY(!result.hasError());
140 QVERIFY(!result.hasError());
144 void tst_QXmlResultItems::objectSize() const
146 /* A d-pointer plus a vtable pointer. */
147 QCOMPARE(sizeof(QXmlResultItems), sizeof(void *) * 2);
150 void tst_QXmlResultItems::constCorrectness() const
152 const QXmlResultItems result;
154 /* These functions should be const. */
160 What's special about this is that it's not the QAbstractXmlForwardIterator::next()
161 that triggers the error, it's QPatternist::Expression::evaluateSingleton() directly.
163 void tst_QXmlResultItems::evalateWithInstantError() const
166 MessageSilencer silencer;
167 query.setMessageHandler(&silencer);
168 query.setQuery(QLatin1String("fn:error()"));
170 QXmlResultItems result;
171 query.evaluateTo(&result);
173 /* Check the values, and stress it. */
174 for(int i = 0; i < 3; ++i)
176 QVERIFY(result.current().isNull());
177 QVERIFY(result.next().isNull());
178 QVERIFY(result.hasError());
182 void tst_QXmlResultItems::evaluate() const
184 QFETCH(QString, queryString);
187 query.setQuery(queryString);
189 QVERIFY(query.isValid());
191 QXmlResultItems result;
192 query.evaluateTo(&result);
193 QXmlItem item(result.next());
195 while(!item.isNull())
197 QVERIFY(!result.current().isNull());
198 QVERIFY(!result.hasError());
199 item = result.next();
202 /* Now, stress beyond the end. */
203 for(int i = 0; i < 3; ++i)
205 QVERIFY(result.current().isNull());
206 QVERIFY(result.next().isNull());
210 void tst_QXmlResultItems::evaluate_data() const
212 QTest::addColumn<QString>("queryString");
214 QTest::newRow("one atomic value") << QString::fromLatin1("1");
215 QTest::newRow("two atomic values") << QString::fromLatin1("1, xs:hexBinary('FF')");
216 QTest::newRow("one node") << QString::fromLatin1("attribute name {'body'}");
217 QTest::newRow("one node") << QString::fromLatin1("attribute name {'body'}");
218 QTest::newRow("two nodes") << QString::fromLatin1("attribute name {'body'}, <!-- comment -->");
221 void tst_QXmlResultItems::evalateWithQueryError() const
223 /* This query is invalid. */
224 const QXmlQuery query;
226 QXmlResultItems result;
227 query.evaluateTo(&result);
229 QVERIFY(result.hasError());
230 QVERIFY(result.next().isNull());
233 QTEST_MAIN(tst_QXmlResultItems)
235 #include "tst_qxmlresultitems.moc"
237 // vim: et:ts=4:sw=4:sts=4