Change copyrights from Nokia to Digia
[profile/ivi/qtxmlpatterns.git] / tests / auto / qxmlresultitems / tst_qxmlresultitems.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the test suite of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
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