Update copyright year in license headers.
[profile/ivi/qtxmlpatterns.git] / tests / auto / qxmlresultitems / tst_qxmlresultitems.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the test suite of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42
43 #include <QtTest/QtTest>
44
45 #include <QtXmlPatterns/QXmlItem>
46 #include <QtXmlPatterns/QXmlQuery>
47 #include <QtXmlPatterns/QXmlResultItems>
48
49 #include "../qxmlquery/MessageSilencer.h"
50 /*!
51  \class tst_QXmlResultItems
52  \internal
53  \since 4.4
54  \brief Tests class QXmlResultItems.
55
56  */
57 class tst_QXmlResultItems : public QObject
58 {
59     Q_OBJECT
60
61 private Q_SLOTS:
62     void defaultConstructor() const;
63     void next() const;
64     void current() const;
65     void hasError() const;
66     void objectSize() const;
67     void constCorrectness() const;
68
69     void evalateWithInstantError() const;
70     void evalateWithQueryError() const;
71     void evaluate() const;
72     void evaluate_data() const;
73 };
74
75 void tst_QXmlResultItems::defaultConstructor() const
76 {
77     {
78         QXmlResultItems result;
79     }
80
81     {
82         QXmlResultItems result1;
83         QXmlResultItems result2;
84     }
85
86     {
87         QXmlResultItems result1;
88         QXmlResultItems result2;
89         QXmlResultItems result3;
90     }
91 }
92
93 void tst_QXmlResultItems::next() const
94 {
95     /* Check default value. */
96     {
97         QXmlResultItems result;
98         QVERIFY(result.next().isNull());
99     }
100
101     /* Stress it on a default constructed value. */
102     {
103         QXmlResultItems result;
104         QVERIFY(result.next().isNull());
105         QVERIFY(result.next().isNull());
106         QVERIFY(result.next().isNull());
107     }
108 }
109
110 void tst_QXmlResultItems::current() const
111 {
112     /* Check default value. */
113     {
114         QXmlResultItems result;
115         QVERIFY(result.current().isNull());
116     }
117
118     /* Stress it on a default constructed value. */
119     {
120         QXmlResultItems result;
121         QVERIFY(result.current().isNull());
122         QVERIFY(result.current().isNull());
123         QVERIFY(result.current().isNull());
124     }
125 }
126
127 void tst_QXmlResultItems::hasError() const
128 {
129     /* Check default value. */
130     {
131         QXmlResultItems result;
132         QVERIFY(!result.hasError());
133     }
134
135     /* Stress it on a default constructed value. */
136     {
137         QXmlResultItems result;
138         QVERIFY(!result.hasError());
139         QVERIFY(!result.hasError());
140         QVERIFY(!result.hasError());
141     }
142 }
143
144 void tst_QXmlResultItems::objectSize() const
145 {
146     /* A d-pointer plus a vtable pointer. */
147     QCOMPARE(sizeof(QXmlResultItems), sizeof(void *) * 2);
148 }
149
150 void tst_QXmlResultItems::constCorrectness() const
151 {
152     const QXmlResultItems result;
153
154     /* These functions should be const. */
155     result.current();
156     result.hasError();
157 }
158
159 /*!
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.
162  */
163 void tst_QXmlResultItems::evalateWithInstantError() const
164 {
165     QXmlQuery query;
166     MessageSilencer silencer;
167     query.setMessageHandler(&silencer);
168     query.setQuery(QLatin1String("fn:error()"));
169
170     QXmlResultItems result;
171     query.evaluateTo(&result);
172
173     /* Check the values, and stress it. */
174     for(int i = 0; i < 3; ++i)
175     {
176         QVERIFY(result.current().isNull());
177         QVERIFY(result.next().isNull());
178         QVERIFY(result.hasError());
179     }
180 }
181
182 void tst_QXmlResultItems::evaluate() const
183 {
184     QFETCH(QString, queryString);
185
186     QXmlQuery query;
187     query.setQuery(queryString);
188
189     QVERIFY(query.isValid());
190
191     QXmlResultItems result;
192     query.evaluateTo(&result);
193     QXmlItem item(result.next());
194
195     while(!item.isNull())
196     {
197         QVERIFY(!result.current().isNull());
198         QVERIFY(!result.hasError());
199         item = result.next();
200     }
201
202     /* Now, stress beyond the end. */
203     for(int i = 0; i < 3; ++i)
204     {
205         QVERIFY(result.current().isNull());
206         QVERIFY(result.next().isNull());
207     }
208 }
209
210 void tst_QXmlResultItems::evaluate_data() const
211 {
212     QTest::addColumn<QString>("queryString");
213
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 -->");
219 }
220
221 void tst_QXmlResultItems::evalateWithQueryError() const
222 {
223     /* This query is invalid. */
224     const QXmlQuery query;
225
226     QXmlResultItems result;
227     query.evaluateTo(&result);
228
229     QVERIFY(result.hasError());
230     QVERIFY(result.next().isNull());
231 }
232
233 QTEST_MAIN(tst_QXmlResultItems)
234
235 #include "tst_qxmlresultitems.moc"
236
237 // vim: et:ts=4:sw=4:sts=4