1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 /* We expect these headers to be available. */
46 #include <QtXmlPatterns/QSourceLocation>
47 #include <QtXmlPatterns/qsourcelocation.h>
48 #include <QSourceLocation>
49 #include <qsourcelocation.h>
52 \class tst_QSourceLocation
55 \brief Tests QSourceLocation
58 class tst_QSourceLocation : public QObject
64 void defaultConstructor() const;
65 void valueConstructor() const;
66 void valueConstructorDefaultArguments() const;
67 void copyConstructor() const;
68 void assignmentOperator() const;
69 void equalnessOperator() const;
70 void equalnessOperator_data() const;
71 void defaultValues() const;
72 void constCorrectness() const;
73 void objectSize() const;
75 void setColumn() const;
77 void withinQVariant() const;
78 void debugStream() const;
79 void debugStream_data() const;
80 void withQHash() const;
84 We allocate a couple to catch reference counting bugs.
86 void tst_QSourceLocation::defaultConstructor() const
93 void tst_QSourceLocation::copyConstructor() const
97 QSourceLocation copy(def);
99 QCOMPARE(def.line(), qint64(-1));
100 QCOMPARE(def.column(), qint64(-1));
101 QCOMPARE(def.uri(), QUrl());
108 val.setUri(QUrl(QLatin1String("http://example.com/")));
110 QSourceLocation copy(val);
111 QCOMPARE(copy.line(), qint64(5));
112 QCOMPARE(copy.column(), qint64(600));
113 QCOMPARE(copy.uri(), QUrl(QLatin1String("http://example.com/")));
117 /* Construct from a const object. */
118 const QSourceLocation val;
119 const QSourceLocation val2(val);
124 void tst_QSourceLocation::valueConstructor() const
126 const QSourceLocation sl(QUrl(QLatin1String("http://example.com/")), 5, 4);
128 QCOMPARE(sl.uri(), QUrl(QLatin1String("http://example.com/")));
129 QCOMPARE(sl.line(), qint64(5));
130 QCOMPARE(sl.column(), qint64(4));
133 void tst_QSourceLocation::valueConstructorDefaultArguments() const
135 /* The line and column arguments are optional. */
136 const QSourceLocation sl(QUrl(QLatin1String("http://example.com/")));
138 QCOMPARE(sl.uri(), QUrl(QLatin1String("http://example.com/")));
139 QCOMPARE(sl.line(), qint64(-1));
140 QCOMPARE(sl.column(), qint64(-1));
143 void tst_QSourceLocation::assignmentOperator() const
145 /* Assign to self. */
151 QVERIFY(def.isNull());
152 QCOMPARE(def.line(), qint64(-1));
153 QCOMPARE(def.column(), qint64(-1));
154 QCOMPARE(def.uri(), QUrl());
157 /* Assign to default constructed object. */
162 val.setUri(QUrl(QLatin1String("http://example.com/2")));
164 QSourceLocation assigned;
167 QCOMPARE(assigned.line(), qint64(3));
168 QCOMPARE(assigned.column(), qint64(4));
169 QCOMPARE(assigned.uri(), QUrl(QLatin1String("http://example.com/2")));
172 /* Assign to modified object. */
177 val.setUri(QUrl(QLatin1String("http://example.com/2")));
179 QSourceLocation assigned;
180 assigned.setLine(700);
181 assigned.setColumn(4000);
182 assigned.setUri(QUrl(QLatin1String("http://example.com/3")));
186 QCOMPARE(assigned.line(), qint64(3));
187 QCOMPARE(assigned.column(), qint64(4));
188 QCOMPARE(assigned.uri(), QUrl(QLatin1String("http://example.com/2")));
193 This includes operator!=()
195 void tst_QSourceLocation::equalnessOperator() const
197 QFETCH(QSourceLocation, v1);
198 QFETCH(QSourceLocation, v2);
201 QCOMPARE(v1 == v2, True);
202 QCOMPARE(v1 != v2, True);
205 void tst_QSourceLocation::equalnessOperator_data() const
207 QTest::addColumn<QSourceLocation>("v1");
208 QTest::addColumn<QSourceLocation>("v2");
209 QTest::addColumn<bool>("True");
212 QTest::newRow("Default constructed values")
219 QSourceLocation modified;
220 modified.setColumn(4);
222 QTest::newRow("Default constructed, against column-modified")
229 QSourceLocation modified;
232 QTest::newRow("Default constructed, against line-modified")
239 QSourceLocation modified;
240 modified.setUri(QUrl(QLatin1String("http://example.com/")));
242 QTest::newRow("Default constructed, against line-modified")
249 QSourceLocation modified;
250 modified.setUri(QUrl(QLatin1String("http://example.com/")));
252 modified.setColumn(4);
254 QTest::newRow("Default constructed, against all-modified")
261 void tst_QSourceLocation::defaultValues() const
265 QCOMPARE(def.line(), qint64(-1));
266 QCOMPARE(def.column(), qint64(-1));
267 QCOMPARE(def.uri(), QUrl());
271 Call functions that must be const.
273 void tst_QSourceLocation::constCorrectness() const
275 const QSourceLocation def;
282 const QSourceLocation def2;
284 /* Equalness operator. */
285 QVERIFY(def == def2);
288 /* Inverse equalness operator. */
289 QVERIFY(def != def2);
292 void tst_QSourceLocation::objectSize() const
294 /* We can't compare the two values. QSourceLocation is 24 on some Mac OS X, while
295 * the other operand evaluates to 20. */
296 QVERIFY(sizeof(QSourceLocation) >= sizeof(QUrl) + sizeof(qint64) * 2);
299 void tst_QSourceLocation::isNull() const
303 QVERIFY(def.isNull());
306 QVERIFY(def.isNull());
310 QSourceLocation def2;
312 QVERIFY(def2.isNull());
316 QSourceLocation def3;
317 def3.setUri(QUrl(QLatin1String("http://example.com/")));
318 QVERIFY(!def3.isNull());
322 void tst_QSourceLocation::setLine() const
326 QCOMPARE(sl.line(), qint64(8));
329 void tst_QSourceLocation::setColumn() const
333 QCOMPARE(sl.column(), qint64(5));
336 void tst_QSourceLocation::setUri() const
339 sl.setUri(QUrl(QLatin1String("http://example.com/")));
340 QCOMPARE(sl.uri(), QUrl(QLatin1String("http://example.com/")));
343 void tst_QSourceLocation::withinQVariant() const
346 const QVariant variant(qVariantFromValue(val));
347 QSourceLocation val2(qVariantValue<QSourceLocation>(variant));
350 void tst_QSourceLocation::debugStream() const
352 QFETCH(QSourceLocation, location);
353 QFETCH(QString, expected);
356 QDebug stream(&actual);
358 #ifndef QT_NO_DEBUG_STREAM
360 QCOMPARE(actual, expected);
364 void tst_QSourceLocation::debugStream_data() const
366 QTest::addColumn<QSourceLocation>("location");
367 QTest::addColumn<QString>("expected");
370 QTest::newRow("Default constructed instance")
372 << QString::fromLatin1("QSourceLocation( QUrl( \"\" ) , line: -1 , column: -1 ) ");
376 QSourceLocation location(QUrl(QLatin1String("http://example.com/")), 4, 5);
377 QTest::newRow("Properties set")
379 << QString::fromLatin1("QSourceLocation( QUrl( \"http://example.com/\" ) , line: 4 , column: 5 ) ");
383 void tst_QSourceLocation::withQHash() const
385 QCOMPARE(qHash(QSourceLocation()), qHash(QSourceLocation()));
388 QTEST_MAIN(tst_QSourceLocation)
390 #include "tst_qsourcelocation.moc"
392 // vim: et:ts=4:sw=4:sts=4