1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31
32#include <QtXmlPatterns/QXmlItem>
33#include <QtXmlPatterns/QXmlQuery>
34#include <QtXmlPatterns/QXmlResultItems>
35
36#include "../qxmlquery/MessageSilencer.h"
37/*!
38 \class tst_QXmlResultItems
39 \internal
40 \since 4.4
41 \brief Tests class QXmlResultItems.
42
43 */
44class tst_QXmlResultItems : public QObject
45{
46 Q_OBJECT
47
48private Q_SLOTS:
49 void defaultConstructor() const;
50 void next() const;
51 void current() const;
52 void hasError() const;
53 void objectSize() const;
54 void constCorrectness() const;
55
56 void evalateWithInstantError() const;
57 void evalateWithQueryError() const;
58 void evaluate() const;
59 void evaluate_data() const;
60};
61
62void tst_QXmlResultItems::defaultConstructor() const
63{
64 {
65 QXmlResultItems result;
66 }
67
68 {
69 QXmlResultItems result1;
70 QXmlResultItems result2;
71 }
72
73 {
74 QXmlResultItems result1;
75 QXmlResultItems result2;
76 QXmlResultItems result3;
77 }
78}
79
80void tst_QXmlResultItems::next() const
81{
82 /* Check default value. */
83 {
84 QXmlResultItems result;
85 QVERIFY(result.next().isNull());
86 }
87
88 /* Stress it on a default constructed value. */
89 {
90 QXmlResultItems result;
91 QVERIFY(result.next().isNull());
92 QVERIFY(result.next().isNull());
93 QVERIFY(result.next().isNull());
94 }
95}
96
97void tst_QXmlResultItems::current() const
98{
99 /* Check default value. */
100 {
101 QXmlResultItems result;
102 QVERIFY(result.current().isNull());
103 }
104
105 /* Stress it on a default constructed value. */
106 {
107 QXmlResultItems result;
108 QVERIFY(result.current().isNull());
109 QVERIFY(result.current().isNull());
110 QVERIFY(result.current().isNull());
111 }
112}
113
114void tst_QXmlResultItems::hasError() const
115{
116 /* Check default value. */
117 {
118 QXmlResultItems result;
119 QVERIFY(!result.hasError());
120 }
121
122 /* Stress it on a default constructed value. */
123 {
124 QXmlResultItems result;
125 QVERIFY(!result.hasError());
126 QVERIFY(!result.hasError());
127 QVERIFY(!result.hasError());
128 }
129}
130
131void tst_QXmlResultItems::objectSize() const
132{
133 /* A d-pointer plus a vtable pointer. */
134 QCOMPARE(sizeof(QXmlResultItems), sizeof(void *) * 2);
135}
136
137void tst_QXmlResultItems::constCorrectness() const
138{
139 const QXmlResultItems result;
140
141 /* These functions should be const. */
142 result.current();
143 result.hasError();
144}
145
146/*!
147 What's special about this is that it's not the QAbstractXmlForwardIterator::next()
148 that triggers the error, it's QPatternist::Expression::evaluateSingleton() directly.
149 */
150void tst_QXmlResultItems::evalateWithInstantError() const
151{
152 QXmlQuery query;
153 MessageSilencer silencer;
154 query.setMessageHandler(&silencer);
155 query.setQuery(sourceCode: QLatin1String("fn:error()"));
156
157 QXmlResultItems result;
158 query.evaluateTo(result: &result);
159
160 /* Check the values, and stress it. */
161 for(int i = 0; i < 3; ++i)
162 {
163 QVERIFY(result.current().isNull());
164 QVERIFY(result.next().isNull());
165 QVERIFY(result.hasError());
166 }
167}
168
169void tst_QXmlResultItems::evaluate() const
170{
171 QFETCH(QString, queryString);
172
173 QXmlQuery query;
174 query.setQuery(sourceCode: queryString);
175
176 QVERIFY(query.isValid());
177
178 QXmlResultItems result;
179 query.evaluateTo(result: &result);
180 QXmlItem item(result.next());
181
182 while(!item.isNull())
183 {
184 QVERIFY(!result.current().isNull());
185 QVERIFY(!result.hasError());
186 item = result.next();
187 }
188
189 /* Now, stress beyond the end. */
190 for(int i = 0; i < 3; ++i)
191 {
192 QVERIFY(result.current().isNull());
193 QVERIFY(result.next().isNull());
194 }
195}
196
197void tst_QXmlResultItems::evaluate_data() const
198{
199 QTest::addColumn<QString>(name: "queryString");
200
201 QTest::newRow(dataTag: "one atomic value") << QString::fromLatin1(str: "1");
202 QTest::newRow(dataTag: "two atomic values") << QString::fromLatin1(str: "1, xs:hexBinary('FF')");
203 QTest::newRow(dataTag: "one node") << QString::fromLatin1(str: "attribute name {'body'}");
204 QTest::newRow(dataTag: "one node") << QString::fromLatin1(str: "attribute name {'body'}");
205 QTest::newRow(dataTag: "two nodes") << QString::fromLatin1(str: "attribute name {'body'}, <!-- comment -->");
206}
207
208void tst_QXmlResultItems::evalateWithQueryError() const
209{
210 /* This query is invalid. */
211 const QXmlQuery query;
212
213 QXmlResultItems result;
214 query.evaluateTo(result: &result);
215
216 QVERIFY(result.hasError());
217 QVERIFY(result.next().isNull());
218}
219
220QTEST_MAIN(tst_QXmlResultItems)
221
222#include "tst_qxmlresultitems.moc"
223
224// vim: et:ts=4:sw=4:sts=4
225

source code of qtxmlpatterns/tests/auto/qxmlresultitems/tst_qxmlresultitems.cpp