Change copyrights from Nokia to Digia
[profile/ivi/qtxmlpatterns.git] / tests / auto / qsourcelocation / tst_qsourcelocation.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 /* We expect these headers to be available. */
46 #include <QtXmlPatterns/QSourceLocation>
47 #include <QtXmlPatterns/qsourcelocation.h>
48 #include <QSourceLocation>
49 #include <qsourcelocation.h>
50
51 /*!
52  \class tst_QSourceLocation
53  \internal
54  \since 4.4
55  \brief Tests QSourceLocation
56
57  */
58 class tst_QSourceLocation : public QObject
59 {
60     Q_OBJECT
61
62 private Q_SLOTS:
63     void isNull() const;
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;
74     void setLine() const;
75     void setColumn() const;
76     void setUri() const;
77     void withinQVariant() const;
78     void debugStream() const;
79     void debugStream_data() const;
80     void withQHash() const;
81 };
82
83 /*!
84   We allocate a couple to catch reference counting bugs.
85  */
86 void tst_QSourceLocation::defaultConstructor() const
87 {
88     QSourceLocation def1;
89     QSourceLocation def2;
90     QSourceLocation def3;
91 }
92
93 void tst_QSourceLocation::copyConstructor() const
94 {
95     {
96         QSourceLocation def;
97         QSourceLocation copy(def);
98
99         QCOMPARE(def.line(), qint64(-1));
100         QCOMPARE(def.column(), qint64(-1));
101         QCOMPARE(def.uri(), QUrl());
102     }
103
104     {
105         QSourceLocation val;
106         val.setLine(5);
107         val.setColumn(600);
108         val.setUri(QUrl(QLatin1String("http://example.com/")));
109
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/")));
114     }
115
116     {
117         /* Construct from a const object. */
118         const QSourceLocation val;
119         const QSourceLocation val2(val);
120         QCOMPARE(val, val2);
121     }
122 }
123
124 void tst_QSourceLocation::valueConstructor() const
125 {
126     const QSourceLocation sl(QUrl(QLatin1String("http://example.com/")), 5, 4);
127
128     QCOMPARE(sl.uri(), QUrl(QLatin1String("http://example.com/")));
129     QCOMPARE(sl.line(), qint64(5));
130     QCOMPARE(sl.column(), qint64(4));
131 }
132
133 void tst_QSourceLocation::valueConstructorDefaultArguments() const
134 {
135     /* The line and column arguments are optional. */
136     const QSourceLocation sl(QUrl(QLatin1String("http://example.com/")));
137
138     QCOMPARE(sl.uri(), QUrl(QLatin1String("http://example.com/")));
139     QCOMPARE(sl.line(), qint64(-1));
140     QCOMPARE(sl.column(), qint64(-1));
141 }
142
143 void tst_QSourceLocation::assignmentOperator() const
144 {
145     /* Assign to self. */
146     {
147         QSourceLocation def;
148
149         def = def;
150
151         QVERIFY(def.isNull());
152         QCOMPARE(def.line(), qint64(-1));
153         QCOMPARE(def.column(), qint64(-1));
154         QCOMPARE(def.uri(), QUrl());
155     }
156
157     /* Assign to default constructed object. */
158     {
159         QSourceLocation val;
160         val.setLine(3);
161         val.setColumn(4);
162         val.setUri(QUrl(QLatin1String("http://example.com/2")));
163
164         QSourceLocation assigned;
165         assigned = val;
166
167         QCOMPARE(assigned.line(), qint64(3));
168         QCOMPARE(assigned.column(), qint64(4));
169         QCOMPARE(assigned.uri(), QUrl(QLatin1String("http://example.com/2")));
170     }
171
172     /* Assign to modified object. */
173     {
174         QSourceLocation val;
175         val.setLine(3);
176         val.setColumn(4);
177         val.setUri(QUrl(QLatin1String("http://example.com/2")));
178
179         QSourceLocation assigned;
180         assigned.setLine(700);
181         assigned.setColumn(4000);
182         assigned.setUri(QUrl(QLatin1String("http://example.com/3")));
183
184         assigned = val;
185
186         QCOMPARE(assigned.line(), qint64(3));
187         QCOMPARE(assigned.column(), qint64(4));
188         QCOMPARE(assigned.uri(), QUrl(QLatin1String("http://example.com/2")));
189     }
190 }
191
192 /*!
193  This includes operator!=()
194  */
195 void tst_QSourceLocation::equalnessOperator() const
196 {
197     QFETCH(QSourceLocation, v1);
198     QFETCH(QSourceLocation, v2);
199     QFETCH(bool, True);
200
201     QCOMPARE(v1 == v2, True);
202     QCOMPARE(v1 != v2, True);
203 }
204
205 void tst_QSourceLocation::equalnessOperator_data() const
206 {
207     QTest::addColumn<QSourceLocation>("v1");
208     QTest::addColumn<QSourceLocation>("v2");
209     QTest::addColumn<bool>("True");
210
211     {
212         QTest::newRow("Default constructed values")
213                 << QSourceLocation()
214                 << QSourceLocation()
215                 << true;
216     }
217
218     {
219         QSourceLocation modified;
220         modified.setColumn(4);
221
222         QTest::newRow("Default constructed, against column-modified")
223             << QSourceLocation()
224             << modified
225             << false;
226     }
227
228     {
229         QSourceLocation modified;
230         modified.setLine(5);
231
232         QTest::newRow("Default constructed, against line-modified")
233             << QSourceLocation()
234             << modified
235             << false;
236     }
237
238     {
239         QSourceLocation modified;
240         modified.setUri(QUrl(QLatin1String("http://example.com/")));
241
242         QTest::newRow("Default constructed, against line-modified")
243             << QSourceLocation()
244             << modified
245             << false;
246     }
247
248     {
249         QSourceLocation modified;
250         modified.setUri(QUrl(QLatin1String("http://example.com/")));
251         modified.setLine(5);
252         modified.setColumn(4);
253
254         QTest::newRow("Default constructed, against all-modified")
255             << QSourceLocation()
256             << modified
257             << false;
258     }
259 }
260
261 void tst_QSourceLocation::defaultValues() const
262 {
263     QSourceLocation def;
264
265     QCOMPARE(def.line(), qint64(-1));
266     QCOMPARE(def.column(), qint64(-1));
267     QCOMPARE(def.uri(), QUrl());
268 }
269
270 /*!
271   Call functions that must be const.
272  */
273 void tst_QSourceLocation::constCorrectness() const
274 {
275     const QSourceLocation def;
276
277     def.line();
278     def.column();
279     def.uri();
280     def.isNull();
281
282     const QSourceLocation def2;
283
284     /* Equalness operator. */
285     QVERIFY(def == def2);
286     QCOMPARE(def, def2);
287
288     /* Inverse equalness operator. */
289     QVERIFY(def != def2);
290 }
291
292 void tst_QSourceLocation::objectSize() const
293 {
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);
297 }
298
299 void tst_QSourceLocation::isNull() const
300 {
301     {
302         QSourceLocation def;
303         QVERIFY(def.isNull());
304
305         def.setColumn(4);
306         QVERIFY(def.isNull());
307     }
308
309     {
310         QSourceLocation def2;
311         def2.setLine(4);
312         QVERIFY(def2.isNull());
313     }
314
315     {
316         QSourceLocation def3;
317         def3.setUri(QUrl(QLatin1String("http://example.com/")));
318         QVERIFY(!def3.isNull());
319     }
320 }
321
322 void tst_QSourceLocation::setLine() const
323 {
324     QSourceLocation sl;
325     sl.setLine(8);
326     QCOMPARE(sl.line(), qint64(8));
327 }
328
329 void tst_QSourceLocation::setColumn() const
330 {
331     QSourceLocation sl;
332     sl.setColumn(5);
333     QCOMPARE(sl.column(), qint64(5));
334 }
335
336 void tst_QSourceLocation::setUri() const
337 {
338     QSourceLocation sl;
339     sl.setUri(QUrl(QLatin1String("http://example.com/")));
340     QCOMPARE(sl.uri(), QUrl(QLatin1String("http://example.com/")));
341 }
342
343 void tst_QSourceLocation::withinQVariant() const
344 {
345     QSourceLocation val;
346     const QVariant variant(qVariantFromValue(val));
347     QSourceLocation val2(qVariantValue<QSourceLocation>(variant));
348 }
349
350 void tst_QSourceLocation::debugStream() const
351 {
352     QFETCH(QSourceLocation, location);
353     QFETCH(QString, expected);
354
355     QString actual;
356     QDebug stream(&actual);
357
358 #ifndef QT_NO_DEBUG_STREAM
359     stream << location;
360     QCOMPARE(actual, expected);
361 #endif
362 }
363
364 void tst_QSourceLocation::debugStream_data() const
365 {
366     QTest::addColumn<QSourceLocation>("location");
367     QTest::addColumn<QString>("expected");
368
369     {
370         QTest::newRow("Default constructed instance")
371             << QSourceLocation()
372             << QString::fromLatin1("QSourceLocation(  QUrl( \"\" )  , line: -1 , column: -1 ) ");
373     }
374
375     {
376         QSourceLocation location(QUrl(QLatin1String("http://example.com/")), 4, 5);
377         QTest::newRow("Properties set")
378             << location
379             << QString::fromLatin1("QSourceLocation(  QUrl( \"http://example.com/\" )  , line: 4 , column: 5 ) ");
380     }
381 }
382
383 void tst_QSourceLocation::withQHash() const
384 {
385     QCOMPARE(qHash(QSourceLocation()), qHash(QSourceLocation()));
386 }
387
388 QTEST_MAIN(tst_QSourceLocation)
389
390 #include "tst_qsourcelocation.moc"
391
392 // vim: et:ts=4:sw=4:sts=4