1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 #include <QtXmlPatterns/QXmlNamePool>
46 #include <QtXmlPatterns/QXmlName>
52 \brief Tests class QXmlName.
54 This test is not intended for testing the engine, but the functionality specific
55 to the QXmlName class.
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.
61 class tst_QXmlName : public QObject
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;
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;
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;
93 void tst_QXmlName::defaultConstructor() const
95 /* Allocate instance in different orders. */
107 Q_DECLARE_METATYPE(QXmlNamePool)
108 void tst_QXmlName::argumentConstructor() const
110 QFETCH(QString, namespaceURI);
111 QFETCH(QString, localName);
112 QFETCH(QString, prefix);
113 QFETCH(QXmlNamePool, namePool);
115 const QXmlName name(namePool, localName, namespaceURI, prefix);
117 QCOMPARE(name.namespaceUri(namePool), namespaceURI);
118 QCOMPARE(name.localName(namePool), localName);
119 QCOMPARE(name.prefix(namePool), prefix);
125 Below we use the same QXmlNamePool instance. This means the same name pool
128 void tst_QXmlName::argumentConstructor_data() const
130 QTest::addColumn<QString>("namespaceURI");
131 QTest::addColumn<QString>("localName");
132 QTest::addColumn<QString>("prefix");
133 QTest::addColumn<QXmlNamePool>("namePool");
135 QXmlNamePool namePool;
136 QTest::newRow("Basic test")
137 << QString::fromLatin1("http://example.com/Namespace1")
138 << QString::fromLatin1("localName1")
139 << QString::fromLatin1("prefix1")
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")
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")
154 QTest::newRow("No prefix")
155 << QString::fromLatin1("http://example.com/Namespace2")
156 << QString::fromLatin1("localName3")
162 Ensure that the three last arguments have default values, and that they are null strings.
164 void tst_QXmlName::argumentConstructorDefaultArguments() const
167 const QXmlName n1(np, QLatin1String("localName"));
168 const QXmlName n2(np, QLatin1String("localName"), QString(), QString());
171 QCOMPARE(n1.toClarkName(np), QString::fromLatin1("localName"));
174 void tst_QXmlName::equalnessoperator() const
182 void tst_QXmlName::inequalnessoperator() const
190 void tst_QXmlName::isNull() const
192 /* Check default value. */
194 QVERIFY(name.isNull());
197 void tst_QXmlName::operatorEqual() const
199 QFETCH(QXmlName, op1);
200 QFETCH(QXmlName, op2);
201 QFETCH(bool, expected);
203 QCOMPARE(op1 == op2, expected);
206 void tst_QXmlName::operatorEqual_data() const
208 QTest::addColumn<QXmlName>("op1");
209 QTest::addColumn<QXmlName>("op2");
210 QTest::addColumn<bool>("expected");
212 QXmlNamePool namePool;
213 const QXmlName n1(namePool, QString::fromLatin1("localName1"),
214 QString::fromLatin1("http://example.com/Namespace1"),
215 QString::fromLatin1("prefix1"));
217 const QXmlName n2(namePool, QString::fromLatin1("localName2"),
218 QString::fromLatin1("http://example.com/Namespace1"),
219 QString::fromLatin1("prefix1"));
221 const QXmlName n3(namePool, QString::fromLatin1("localName2"),
222 QString::fromLatin1("http://example.com/Namespace1"),
223 QString::fromLatin1("prefix2"));
225 const QXmlName n4(namePool, QString::fromLatin1("localName3"),
226 QString::fromLatin1("http://example.com/Namespace2"));
228 const QXmlName n5(namePool, QString::fromLatin1("localName4"),
229 QString::fromLatin1("http://example.com/Namespace2"));
231 const QXmlName n6(namePool, QString::fromLatin1("localName4"),
232 QString::fromLatin1("http://example.com/Namespace2"),
233 QString::fromLatin1("prefix3"));
235 const QXmlName n7(namePool, QString::fromLatin1("localName2"),
236 QString::fromLatin1("http://example.com/Namespace2"),
237 QString::fromLatin1("prefix3"));
239 QTest::newRow(qPrintable(n1.toClarkName(namePool)))
244 QTest::newRow(qPrintable(n2.toClarkName(namePool)))
249 QTest::newRow(qPrintable(n3.toClarkName(namePool)))
254 QTest::newRow(qPrintable(n4.toClarkName(namePool)))
259 QTest::newRow(qPrintable(n5.toClarkName(namePool)))
264 QTest::newRow(qPrintable(n6.toClarkName(namePool)))
269 QTest::newRow(qPrintable(n7.toClarkName(namePool)))
274 QTest::newRow("Prefix differs")
279 QTest::newRow("No prefix vs. prefix")
284 QTest::newRow("Local name differs")
289 QTest::newRow("Namespace differs")
295 void tst_QXmlName::operatorNotEqual() const
297 QFETCH(QXmlName, op1);
298 QFETCH(QXmlName, op2);
299 QFETCH(bool, expected);
301 QCOMPARE(op1 != op2, !expected);
304 void tst_QXmlName::operatorNotEqual_data() const
306 operatorEqual_data();
310 Check that functions have the correct const qualification.
312 void tst_QXmlName::constCorrectness() const
317 QVERIFY(name.isNull());
320 QVERIFY(name == name);
323 QVERIFY(!(name != name));
325 QXmlNamePool namePool;
326 const QXmlName name2(namePool, QLatin1String("localName"), QLatin1String("http://example.com/"), QLatin1String("prefix"));
328 /* namespaceUri(). */
329 QCOMPARE(name2.namespaceUri(namePool), QLatin1String("http://example.com/"));
332 QCOMPARE(name2.localName(namePool), QLatin1String("localName"));
335 QCOMPARE(name2.prefix(namePool), QLatin1String("prefix"));
338 QCOMPARE(name2.toClarkName(namePool), QLatin1String("{http://example.com/}prefix:localName"));
341 void tst_QXmlName::qHash() const
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. */
349 void tst_QXmlName::objectSize() const
351 QVERIFY2(sizeof(QXmlName) == sizeof(qint64), "QXmlName should have at least a d-pointer.");
354 void tst_QXmlName::toClarkName() const
356 QFETCH(QString, produced);
357 QFETCH(QString, expected);
359 QCOMPARE(produced, expected);
362 void tst_QXmlName::toClarkName_data() const
364 QTest::addColumn<QString>("produced");
365 QTest::addColumn<QString>("expected");
369 /* A null QXmlName. */
372 QTest::newRow("") << n.toClarkName(np)
373 << QString::fromLatin1("QXmlName(null)");
377 const QXmlName n(np, QLatin1String("localName"));
378 QTest::newRow("") << n.toClarkName(np)
379 << QString::fromLatin1("localName");
382 /* Local name with namespace URI, empty prefix. */
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");
390 /* Local name with namespace URI and prefix. */
392 const QXmlName n(np, QLatin1String("localName"),
393 QLatin1String("http://example.com/"),
395 QTest::newRow("") << n.toClarkName(np)
396 << QString::fromLatin1("{http://example.com/}p:localName");
401 Check that QXmlName can be used inside QVariant.
403 void tst_QXmlName::withinQVariant() const
405 /* The extra paranthesis silences a warning on win32-msvc2005. */
406 QVariant value(qVariantFromValue(QXmlName()));
410 Check that the user type of QXmlName holds.
412 void tst_QXmlName::typeWithinQVariant() const
414 const int qxmlNameType = QVariant(qVariantFromValue(QXmlName())).userType();
416 const QVariant value(qVariantFromValue(QXmlName()));
418 QCOMPARE(value.userType(), qxmlNameType);
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.
425 void tst_QXmlName::isNCName() const
427 QFETCH(QString, input);
428 QFETCH(bool, expectedValidity);
430 QCOMPARE(QXmlName::isNCName(input), expectedValidity);
433 void tst_QXmlName::isNCName_data() const
435 QTest::addColumn<QString>("input");
436 QTest::addColumn<bool>("expectedValidity");
438 QTest::newRow("empty string")
442 QTest::newRow("A number")
443 << QString::fromLatin1("1")
446 QTest::newRow("Simple valid string")
447 << QString::fromLatin1("abc")
450 QTest::newRow("Simple valid string")
451 << QString::fromLatin1("abc.123")
455 void tst_QXmlName::isNCNameSignature() const
457 const QString constQString;
459 /* Verify that we can take a const QString. */
460 QXmlName::isNCName(constQString);
462 /* Verify that we can take a temporary QString. */
463 QXmlName::isNCName(QString());
466 void tst_QXmlName::fromClarkName() const
468 QFETCH(QString, input);
469 QFETCH(QXmlName, expected);
470 QFETCH(QXmlNamePool, namePool);
472 QCOMPARE(QXmlName::fromClarkName(input, namePool), expected);
475 void tst_QXmlName::fromClarkName_data() const
477 QTest::addColumn<QString>("input");
478 QTest::addColumn<QXmlName>("expected");
479 QTest::addColumn<QXmlNamePool>("namePool");
483 QTest::newRow("A null string")
488 QTest::newRow("An empty string")
489 << QString(QLatin1String(""))
493 QTest::newRow("A single local name")
494 << QString(QLatin1String("foo"))
495 << QXmlName(np, QLatin1String("foo"))
498 QTest::newRow("Has prefix, but no namespace, that's invalid")
499 << QString(QLatin1String("prefix:foo"))
503 QTest::newRow("Namespace, local name, no prefix")
504 << QString(QLatin1String("{def}abc"))
505 << QXmlName(np, QLatin1String("abc"), QLatin1String("def"))
508 QTest::newRow("Namespace, local name, prefix")
509 << QString(QLatin1String("{def}p:abc"))
510 << QXmlName(np, QLatin1String("abc"), QLatin1String("def"), QLatin1String("p"))
513 QTest::newRow("Namespace, local name, prefix syntax error")
514 << QString(QLatin1String("{def}:abc"))
518 QTest::newRow("Namespace, local name syntax error, prefix")
519 << QString(QLatin1String("{def}p:"))
523 QTest::newRow("Only local name which is invalid")
524 << QString(QLatin1String(":::"))
528 QTest::newRow("Namespace, invalid local name")
529 << QString(QLatin1String("{def}a|bc"))
533 QTest::newRow("Namespace, local name, invalid prefix")
534 << QString(QLatin1String("{def}a|b:c"))
538 QTest::newRow("A single left curly, invalid")
539 << QString(QLatin1String("{"))
543 QTest::newRow("A single left curly, invalid")
544 << QString(QLatin1String("{aaswd"))
549 void tst_QXmlName::fromClarkNameSignature() const
551 /* We should take const references. */
552 const QXmlNamePool np;
555 QXmlName::fromClarkName(in, np);
558 QTEST_MAIN(tst_QXmlName)
560 #include "tst_qxmlname.moc"
562 // vim: et:ts=4:sw=4:sts=4