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