1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QtTest/QtTest>
51 \brief Tests class QXmlItem.
53 class tst_QXmlItem : public QObject
58 void defaultConstructor() const;
59 void copyConstructor() const;
60 void copyConstructorFromQVariant() const;
61 void copyConstructorFromQXmlNodeModelIndex() const;
62 void assignmentOperator() const;
65 void isAtomicValue() const;
66 void toAtomicValue() const;
67 void toNodeModelIndex() const;
69 void objectSize() const;
70 void constCorrectness() const;
71 void withinQVariant() const;
74 void tst_QXmlItem::defaultConstructor() const
92 void tst_QXmlItem::copyConstructor() const
94 /* Check that we can copy from a const reference. */
97 const QXmlItem copy(item);
100 /* On a QXmlItem constructed from a null QVariant. */
102 const QXmlItem item((QVariant()));
103 const QXmlItem copy(item);
106 /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
108 const QXmlItem item((QXmlNodeModelIndex()));
109 const QXmlItem copy(item);
113 void tst_QXmlItem::copyConstructorFromQVariant() const
115 /* Construct & destruct a single value. */
117 const QXmlItem item(QVariant(QString::fromLatin1("dummy")));
120 /* Copy a null QVariant. */
122 const QXmlItem item((QVariant()));
123 QVERIFY(item.isNull());
128 void tst_QXmlItem::copyConstructorFromQXmlNodeModelIndex() const
130 // TODO copy a valid model index.
132 /* Construct from a null QXmlNodeModelIndex. */
134 const QXmlItem item((QXmlNodeModelIndex()));
135 QVERIFY(item.isNull());
139 void tst_QXmlItem::assignmentOperator() const
141 /* Assign to self. */
143 /* With null value. */
153 /* With the same atomic value. */
155 QXmlItem item(QVariant(QString::fromLatin1("dummy")));
163 /* With the same node. */
168 /* With a QXmlItem constructed from a null QVariant. */
170 QXmlItem item((QVariant()));
178 /* With a QXmlItem constructed from a null QXmlNodeModelIndex. */
180 QXmlItem item((QXmlNodeModelIndex()));
190 void tst_QXmlItem::isNull() const
192 /* Check default value. */
195 QVERIFY(item.isNull());
198 /* On atomic value. */
200 const QXmlItem item(QVariant(3));
201 QVERIFY(!item.isNull());
204 /* On a QXmlItem constructed from a null QVariant. */
206 const QXmlItem item((QVariant()));
207 QVERIFY(item.isNull());
210 /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
212 const QXmlItem item((QXmlNodeModelIndex()));
213 QVERIFY(item.isNull());
217 void tst_QXmlItem::isNode() const
219 /* Check default value. */
222 QVERIFY(!item.isNode());
225 /* On atomic value. */
227 const QXmlItem item(QVariant(3));
228 QVERIFY(!item.isNode());
230 // TODO on valid node index
232 /* On a QXmlItem constructed from a null QVariant. */
234 const QXmlItem item((QVariant()));
235 QVERIFY(!item.isNode());
238 /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
240 const QXmlItem item((QXmlNodeModelIndex()));
241 QVERIFY(!item.isNode());
245 void tst_QXmlItem::isAtomicValue() const
247 /* Check default value. */
250 QVERIFY(!item.isAtomicValue());
253 /* On valid atomic value. */
255 const QXmlItem item(QVariant(3));
256 QVERIFY(item.isAtomicValue());
259 // TODO on valid node index
261 /* On a QXmlItem constructed from a null QVariant. */
263 const QXmlItem item((QVariant()));
264 QVERIFY(!item.isAtomicValue());
267 /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
269 const QXmlItem item((QXmlNodeModelIndex()));
270 QVERIFY(!item.isAtomicValue());
274 void tst_QXmlItem::toAtomicValue() const
276 /* Check default value. */
279 QVERIFY(item.toAtomicValue().isNull());
282 /* On atomic value. */
284 const QXmlItem item(QVariant(3));
285 QCOMPARE(item.toAtomicValue(), QVariant(3));
288 /* On a QXmlItem constructed from a null QVariant. */
290 const QXmlItem item((QVariant()));
291 QVERIFY(item.toAtomicValue().isNull());
294 /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
296 const QXmlItem item((QXmlNodeModelIndex()));
297 QVERIFY(item.toAtomicValue().isNull());
301 void tst_QXmlItem::toNodeModelIndex() const
303 /* Check default value. */
306 QVERIFY(item.toNodeModelIndex().isNull());
309 /* On valid atomic value. */
311 const QXmlItem item(QVariant(3));
312 QVERIFY(item.toNodeModelIndex().isNull());
315 /* On a QXmlItem constructed from a null QVariant. */
317 const QXmlItem item((QVariant()));
318 QVERIFY(item.isNull());
321 /* On a QXmlItem constructed from a null QXmlNodeModelIndex. */
323 const QXmlItem item((QXmlNodeModelIndex()));
324 QVERIFY(item.isNull());
328 void tst_QXmlItem::objectSize() const
330 /* We can't currently test this in portable way,
334 QCOMPARE(sizeof(QPatternist::NodeIndexStorage), sizeof(QXmlItem));
336 /* Data, additional data, and pointer to model. We test for two, such that we
337 * account for the padding that MSVC do. */
338 QVERIFY(sizeof(QXmlItem) == sizeof(qint64) * 2 + sizeof(QAbstractXmlNodeModel *) * 2
339 || sizeof(QXmlItem) == sizeof(qint64) * 2 + sizeof(QAbstractXmlNodeModel *) * 2);
343 Check that the functions that should be const, are.
345 void tst_QXmlItem::constCorrectness() const
350 item.isAtomicValue();
352 item.toAtomicValue();
353 item.toNodeModelIndex();
357 Check that QXmlItem can be used inside QVariant.
359 void tst_QXmlItem::withinQVariant() const
362 const QVariant variant(qVariantFromValue(val));
363 QXmlItem val2(qVariantValue<QXmlItem>(variant));
366 QTEST_MAIN(tst_QXmlItem)
368 #include "tst_qxmlitem.moc"