Update copyright year in license headers.
[profile/ivi/qtxmlpatterns.git] / tests / auto / qxmlname / tst_qxmlname.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/QXmlNamePool>
46 #include <QtXmlPatterns/QXmlName>
47
48 /*!
49  \class tst_QXmlName
50  \internal
51  \since 4.4
52  \brief Tests class QXmlName.
53
54  This test is not intended for testing the engine, but the functionality specific
55  to the QXmlName class.
56
57  In other words, if you have an engine bug; don't add it here because it won't be
58  tested properly. Instead add it to the test suite.
59
60  */
61 class tst_QXmlName : public QObject
62 {
63     Q_OBJECT
64
65 private Q_SLOTS:
66     void defaultConstructor() const;
67     void argumentConstructor() const;
68     void argumentConstructor_data() const;
69     void argumentConstructorDefaultArguments() const;
70     void equalnessoperator() const;
71     void inequalnessoperator() const;
72
73     void isNull() const;
74     void operatorEqual() const;
75     void operatorEqual_data() const;
76     void operatorNotEqual() const;
77     void operatorNotEqual_data() const;
78     void toClarkName() const;
79     void toClarkName_data() const;
80     void constCorrectness() const;
81     void qHash() const;
82     void objectSize() const;
83     void withinQVariant() const;
84     void typeWithinQVariant() const;
85     void isNCName() const;
86     void isNCName_data() const;
87     void isNCNameSignature() const;
88     void fromClarkName() const;
89     void fromClarkName_data() const;
90     void fromClarkNameSignature() const;
91 };
92
93 void tst_QXmlName::defaultConstructor() const
94 {
95     /* Allocate instance in different orders. */
96     {
97         QXmlName name;
98     }
99
100     {
101         QXmlName name1;
102         QXmlName name2;
103         QXmlName name3;
104     }
105 }
106
107 Q_DECLARE_METATYPE(QXmlNamePool)
108 void tst_QXmlName::argumentConstructor() const
109 {
110     QFETCH(QString, namespaceURI);
111     QFETCH(QString, localName);
112     QFETCH(QString, prefix);
113     QFETCH(QXmlNamePool, namePool);
114
115     const QXmlName name(namePool, localName, namespaceURI, prefix);
116
117     QCOMPARE(name.namespaceUri(namePool), namespaceURI);
118     QCOMPARE(name.localName(namePool), localName);
119     QCOMPARE(name.prefix(namePool), prefix);
120 }
121
122 /*!
123   \internal
124
125  Below we use the same QXmlNamePool instance. This means the same name pool
126  is used.
127  */
128 void tst_QXmlName::argumentConstructor_data() const
129 {
130     QTest::addColumn<QString>("namespaceURI");
131     QTest::addColumn<QString>("localName");
132     QTest::addColumn<QString>("prefix");
133     QTest::addColumn<QXmlNamePool>("namePool");
134
135     QXmlNamePool namePool;
136     QTest::newRow("Basic test")
137                     << QString::fromLatin1("http://example.com/Namespace1")
138                     << QString::fromLatin1("localName1")
139                     << QString::fromLatin1("prefix1")
140                     << namePool;
141
142     QTest::newRow("Same namespace & prefix as before, different local name.")
143                     << QString::fromLatin1("http://example.com/Namespace1")
144                     << QString::fromLatin1("localName2")
145                     << QString::fromLatin1("prefix1")
146                     << namePool;
147
148     QTest::newRow("Same namespace & local name as before, different prefix.")
149                     << QString::fromLatin1("http://example.com/Namespace1")
150                     << QString::fromLatin1("localName2")
151                     << QString::fromLatin1("prefix2")
152                     << namePool;
153
154     QTest::newRow("No prefix")
155                     << QString::fromLatin1("http://example.com/Namespace2")
156                     << QString::fromLatin1("localName3")
157                     << QString()
158                     << namePool;
159 }
160
161 /*!
162  Ensure that the three last arguments have default values, and that they are null strings.
163  */
164 void tst_QXmlName::argumentConstructorDefaultArguments() const
165 {
166     QXmlNamePool np;
167     const QXmlName n1(np, QLatin1String("localName"));
168     const QXmlName n2(np, QLatin1String("localName"), QString(), QString());
169
170     QCOMPARE(n1, n2);
171     QCOMPARE(n1.toClarkName(np), QString::fromLatin1("localName"));
172 }
173
174 void tst_QXmlName::equalnessoperator() const
175 {
176     const QXmlName o1;
177     const QXmlName o2;
178     o1 == o2;
179     // TODO
180 }
181
182 void tst_QXmlName::inequalnessoperator() const
183 {
184     const QXmlName o1;
185     const QXmlName o2;
186     o1 != o2;
187     // TODO
188 }
189
190 void tst_QXmlName::isNull() const
191 {
192     /* Check default value. */
193     QXmlName name;
194     QVERIFY(name.isNull());
195 }
196
197 void tst_QXmlName::operatorEqual() const
198 {
199     QFETCH(QXmlName, op1);
200     QFETCH(QXmlName, op2);
201     QFETCH(bool, expected);
202
203     QCOMPARE(op1 == op2, expected);
204 }
205
206 void tst_QXmlName::operatorEqual_data() const
207 {
208     QTest::addColumn<QXmlName>("op1");
209     QTest::addColumn<QXmlName>("op2");
210     QTest::addColumn<bool>("expected");
211
212     QXmlNamePool namePool;
213     const QXmlName n1(namePool, QString::fromLatin1("localName1"),
214                                 QString::fromLatin1("http://example.com/Namespace1"),
215                                 QString::fromLatin1("prefix1"));
216
217     const QXmlName n2(namePool, QString::fromLatin1("localName2"),
218                                 QString::fromLatin1("http://example.com/Namespace1"),
219                                 QString::fromLatin1("prefix1"));
220
221     const QXmlName n3(namePool, QString::fromLatin1("localName2"),
222                                 QString::fromLatin1("http://example.com/Namespace1"),
223                                 QString::fromLatin1("prefix2"));
224
225     const QXmlName n4(namePool, QString::fromLatin1("localName3"),
226                                 QString::fromLatin1("http://example.com/Namespace2"));
227
228     const QXmlName n5(namePool, QString::fromLatin1("localName4"),
229                                 QString::fromLatin1("http://example.com/Namespace2"));
230
231     const QXmlName n6(namePool, QString::fromLatin1("localName4"),
232                                 QString::fromLatin1("http://example.com/Namespace2"),
233                                 QString::fromLatin1("prefix3"));
234
235     const QXmlName n7(namePool, QString::fromLatin1("localName2"),
236                                 QString::fromLatin1("http://example.com/Namespace2"),
237                                 QString::fromLatin1("prefix3"));
238
239     QTest::newRow(qPrintable(n1.toClarkName(namePool)))
240         << n1
241         << n1
242         << true;
243
244     QTest::newRow(qPrintable(n2.toClarkName(namePool)))
245         << n2
246         << n2
247         << true;
248
249     QTest::newRow(qPrintable(n3.toClarkName(namePool)))
250         << n3
251         << n3
252         << true;
253
254     QTest::newRow(qPrintable(n4.toClarkName(namePool)))
255         << n4
256         << n4
257         << true;
258
259     QTest::newRow(qPrintable(n5.toClarkName(namePool)))
260         << n5
261         << n5
262         << true;
263
264     QTest::newRow(qPrintable(n6.toClarkName(namePool)))
265         << n6
266         << n6
267         << true;
268
269     QTest::newRow(qPrintable(n7.toClarkName(namePool)))
270         << n7
271         << n7
272         << true;
273
274     QTest::newRow("Prefix differs")
275         << n2
276         << n3
277         << true;
278
279     QTest::newRow("No prefix vs. prefix")
280         << n5
281         << n6
282         << true;
283
284     QTest::newRow("Local name differs")
285         << n1
286         << n2
287         << false;
288
289     QTest::newRow("Namespace differs")
290         << n2
291         << n7
292         << false;
293 }
294
295 void tst_QXmlName::operatorNotEqual() const
296 {
297     QFETCH(QXmlName, op1);
298     QFETCH(QXmlName, op2);
299     QFETCH(bool, expected);
300
301     QCOMPARE(op1 != op2, !expected);
302 }
303
304 void tst_QXmlName::operatorNotEqual_data() const
305 {
306     operatorEqual_data();
307 }
308
309 /*!
310   Check that functions have the correct const qualification.
311  */
312 void tst_QXmlName::constCorrectness() const
313 {
314     const QXmlName name;
315
316     /* isNull() */
317     QVERIFY(name.isNull());
318
319     /* operator==() */
320     QVERIFY(name == name);
321
322     /* operator!=() */
323     QVERIFY(!(name != name));
324
325     QXmlNamePool namePool;
326     const QXmlName name2(namePool, QLatin1String("localName"), QLatin1String("http://example.com/"), QLatin1String("prefix"));
327
328     /* namespaceUri(). */
329     QCOMPARE(name2.namespaceUri(namePool), QLatin1String("http://example.com/"));
330
331     /* localName(). */
332     QCOMPARE(name2.localName(namePool), QLatin1String("localName"));
333
334     /* prefix(). */
335     QCOMPARE(name2.prefix(namePool), QLatin1String("prefix"));
336
337     /* toClarkname(). */
338     QCOMPARE(name2.toClarkName(namePool), QLatin1String("{http://example.com/}prefix:localName"));
339 }
340
341 void tst_QXmlName::qHash() const
342 {
343     /* Just call it, so we know it exist and that we don't trigger undefined
344      * behavior. We can't test the return value, since it's opaque. */
345     QXmlName name;
346     ::qHash(name);
347 }
348
349 void tst_QXmlName::objectSize() const
350 {
351     QVERIFY2(sizeof(QXmlName) == sizeof(qint64), "QXmlName should have at least a d-pointer.");
352 }
353
354 void tst_QXmlName::toClarkName() const
355 {
356     QFETCH(QString, produced);
357     QFETCH(QString, expected);
358
359     QCOMPARE(produced, expected);
360 }
361
362 void tst_QXmlName::toClarkName_data() const
363 {
364     QTest::addColumn<QString>("produced");
365     QTest::addColumn<QString>("expected");
366
367     QXmlNamePool np;
368
369     /* A null QXmlName. */
370     {
371         const QXmlName n;
372         QTest::newRow("") << n.toClarkName(np)
373                           << QString::fromLatin1("QXmlName(null)");
374     }
375
376     {
377         const QXmlName n(np, QLatin1String("localName"));
378         QTest::newRow("") << n.toClarkName(np)
379                           << QString::fromLatin1("localName");
380     }
381
382     /* Local name with namespace URI, empty prefix. */
383     {
384         const QXmlName n(np, QLatin1String("localName"),
385                              QLatin1String("http://example.com/"));
386         QTest::newRow("") << n.toClarkName(np)
387                           << QString::fromLatin1("{http://example.com/}localName");
388     }
389
390     /* Local name with namespace URI and prefix. */
391     {
392         const QXmlName n(np, QLatin1String("localName"),
393                              QLatin1String("http://example.com/"),
394                              QLatin1String("p"));
395         QTest::newRow("") << n.toClarkName(np)
396                           << QString::fromLatin1("{http://example.com/}p:localName");
397     }
398 }
399
400 /*!
401   Check that QXmlName can be used inside QVariant.
402  */
403 void tst_QXmlName::withinQVariant() const
404 {
405     /* The extra paranthesis silences a warning on win32-msvc2005. */
406     QVariant value(qVariantFromValue(QXmlName()));
407 }
408
409 /*!
410  Check that the user type of QXmlName holds.
411  */
412 void tst_QXmlName::typeWithinQVariant() const
413 {
414     const int qxmlNameType = QVariant(qVariantFromValue(QXmlName())).userType();
415
416     const QVariant value(qVariantFromValue(QXmlName()));
417
418     QCOMPARE(value.userType(), qxmlNameType);
419 }
420
421 /*!
422   We don't do full testing here. Don't have the resources for it. We simply assume
423   we use a code path which is fully tested elsewhere.
424  */
425 void tst_QXmlName::isNCName() const
426 {
427     QFETCH(QString, input);
428     QFETCH(bool, expectedValidity);
429
430     QCOMPARE(QXmlName::isNCName(input), expectedValidity);
431 }
432
433 void tst_QXmlName::isNCName_data() const
434 {
435     QTest::addColumn<QString>("input");
436     QTest::addColumn<bool>("expectedValidity");
437
438     QTest::newRow("empty string")
439         << QString()
440         << false;
441
442     QTest::newRow("A number")
443         << QString::fromLatin1("1")
444         << false;
445
446     QTest::newRow("Simple valid string")
447         << QString::fromLatin1("abc")
448         << true;
449
450     QTest::newRow("Simple valid string")
451         << QString::fromLatin1("abc.123")
452         << true;
453 }
454
455 void tst_QXmlName::isNCNameSignature() const
456 {
457     const QString constQString;
458
459     /* Verify that we can take a const QString. */
460     QXmlName::isNCName(constQString);
461
462     /* Verify that we can take a temporary QString. */
463     QXmlName::isNCName(QString());
464 }
465
466 void tst_QXmlName::fromClarkName() const
467 {
468     QFETCH(QString,         input);
469     QFETCH(QXmlName,        expected);
470     QFETCH(QXmlNamePool,    namePool);
471
472     QCOMPARE(QXmlName::fromClarkName(input, namePool), expected);
473 }
474
475 void tst_QXmlName::fromClarkName_data() const
476 {
477     QTest::addColumn<QString>("input");
478     QTest::addColumn<QXmlName>("expected");
479     QTest::addColumn<QXmlNamePool>("namePool");
480
481     QXmlNamePool np;
482
483     QTest::newRow("A null string")
484         << QString()
485         << QXmlName()
486         << np;
487
488     QTest::newRow("An empty string")
489         << QString(QLatin1String(""))
490         << QXmlName()
491         << np;
492
493     QTest::newRow("A single local name")
494         << QString(QLatin1String("foo"))
495         << QXmlName(np, QLatin1String("foo"))
496         << np;
497
498     QTest::newRow("Has prefix, but no namespace, that's invalid")
499         << QString(QLatin1String("prefix:foo"))
500         << QXmlName()
501         << np;
502
503     QTest::newRow("Namespace, local name, no prefix")
504         << QString(QLatin1String("{def}abc"))
505         << QXmlName(np, QLatin1String("abc"), QLatin1String("def"))
506         << np;
507
508     QTest::newRow("Namespace, local name, prefix")
509         << QString(QLatin1String("{def}p:abc"))
510         << QXmlName(np, QLatin1String("abc"), QLatin1String("def"), QLatin1String("p"))
511         << np;
512
513     QTest::newRow("Namespace, local name, prefix syntax error")
514         << QString(QLatin1String("{def}:abc"))
515         << QXmlName()
516         << np;
517
518     QTest::newRow("Namespace, local name syntax error, prefix")
519         << QString(QLatin1String("{def}p:"))
520         << QXmlName()
521         << np;
522
523     QTest::newRow("Only local name which is invalid")
524         << QString(QLatin1String(":::"))
525         << QXmlName()
526         << np;
527
528     QTest::newRow("Namespace, invalid local name")
529         << QString(QLatin1String("{def}a|bc"))
530         << QXmlName()
531         << np;
532
533     QTest::newRow("Namespace, local name, invalid prefix")
534         << QString(QLatin1String("{def}a|b:c"))
535         << QXmlName()
536         << np;
537
538     QTest::newRow("A single left curly, invalid")
539         << QString(QLatin1String("{"))
540         << QXmlName()
541         << np;
542
543     QTest::newRow("A single left curly, invalid")
544         << QString(QLatin1String("{aaswd"))
545         << QXmlName()
546         << np;
547 }
548
549 void tst_QXmlName::fromClarkNameSignature() const
550 {
551     /* We should take const references. */
552     const QXmlNamePool np;
553     const QString in;
554
555     QXmlName::fromClarkName(in, np);
556 }
557
558 QTEST_MAIN(tst_QXmlName)
559
560 #include "tst_qxmlname.moc"
561
562 // vim: et:ts=4:sw=4:sts=4