1 /****************************************************************************
3 ** Copyright (C) 2012 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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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