tests/auto/qxmlresultitems/tst_qxmlresultitems.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/QXmlItem>
       
    47 #include <QtXmlPatterns/QXmlQuery>
       
    48 #include <QtXmlPatterns/QXmlResultItems>
       
    49 
       
    50 #include "../qxmlquery/MessageSilencer.h"
       
    51 /*!
       
    52  \class tst_QXmlResultItems
       
    53  \internal
       
    54  \since 4.4
       
    55  \brief Tests class QXmlResultItems.
       
    56 
       
    57  */
       
    58 class tst_QXmlResultItems : public QObject
       
    59 {
       
    60     Q_OBJECT
       
    61 
       
    62 private Q_SLOTS:
       
    63     void defaultConstructor() const;
       
    64     void next() const;
       
    65     void current() const;
       
    66     void hasError() const;
       
    67     void objectSize() const;
       
    68     void constCorrectness() const;
       
    69 
       
    70     void evalateWithInstantError() const;
       
    71     void evalateWithQueryError() const;
       
    72     void evaluate() const;
       
    73     void evaluate_data() const;
       
    74 };
       
    75 
       
    76 void tst_QXmlResultItems::defaultConstructor() const
       
    77 {
       
    78     {
       
    79         QXmlResultItems result;
       
    80     }
       
    81 
       
    82     {
       
    83         QXmlResultItems result1;
       
    84         QXmlResultItems result2;
       
    85     }
       
    86 
       
    87     {
       
    88         QXmlResultItems result1;
       
    89         QXmlResultItems result2;
       
    90         QXmlResultItems result3;
       
    91     }
       
    92 }
       
    93 
       
    94 void tst_QXmlResultItems::next() const
       
    95 {
       
    96     /* Check default value. */
       
    97     {
       
    98         QXmlResultItems result;
       
    99         QVERIFY(result.next().isNull());
       
   100     }
       
   101 
       
   102     /* Stress it on a default constructed value. */
       
   103     {
       
   104         QXmlResultItems result;
       
   105         QVERIFY(result.next().isNull());
       
   106         QVERIFY(result.next().isNull());
       
   107         QVERIFY(result.next().isNull());
       
   108     }
       
   109 }
       
   110 
       
   111 void tst_QXmlResultItems::current() const
       
   112 {
       
   113     /* Check default value. */
       
   114     {
       
   115         QXmlResultItems result;
       
   116         QVERIFY(result.current().isNull());
       
   117     }
       
   118 
       
   119     /* Stress it on a default constructed value. */
       
   120     {
       
   121         QXmlResultItems result;
       
   122         QVERIFY(result.current().isNull());
       
   123         QVERIFY(result.current().isNull());
       
   124         QVERIFY(result.current().isNull());
       
   125     }
       
   126 }
       
   127 
       
   128 void tst_QXmlResultItems::hasError() const
       
   129 {
       
   130     /* Check default value. */
       
   131     {
       
   132         QXmlResultItems result;
       
   133         QVERIFY(!result.hasError());
       
   134     }
       
   135 
       
   136     /* Stress it on a default constructed value. */
       
   137     {
       
   138         QXmlResultItems result;
       
   139         QVERIFY(!result.hasError());
       
   140         QVERIFY(!result.hasError());
       
   141         QVERIFY(!result.hasError());
       
   142     }
       
   143 }
       
   144 
       
   145 void tst_QXmlResultItems::objectSize() const
       
   146 {
       
   147     /* A d-pointer plus a vtable pointer. */
       
   148     QCOMPARE(sizeof(QXmlResultItems), sizeof(void *) * 2);
       
   149 }
       
   150 
       
   151 void tst_QXmlResultItems::constCorrectness() const
       
   152 {
       
   153     const QXmlResultItems result;
       
   154 
       
   155     /* These functions should be const. */
       
   156     result.current();
       
   157     result.hasError();
       
   158 }
       
   159 
       
   160 /*!
       
   161  What's special about this is that it's not the QAbstractXmlForwardIterator::next()
       
   162  that triggers the error, it's QPatternist::Expression::evaluateSingleton() directly.
       
   163  */
       
   164 void tst_QXmlResultItems::evalateWithInstantError() const
       
   165 {
       
   166     QXmlQuery query;
       
   167     MessageSilencer silencer;
       
   168     query.setMessageHandler(&silencer);
       
   169     query.setQuery(QLatin1String("fn:error()"));
       
   170 
       
   171     QXmlResultItems result;
       
   172     query.evaluateTo(&result);
       
   173 
       
   174     /* Check the values, and stress it. */
       
   175     for(int i = 0; i < 3; ++i)
       
   176     {
       
   177         QVERIFY(result.current().isNull());
       
   178         QVERIFY(result.next().isNull());
       
   179         QVERIFY(result.hasError());
       
   180     }
       
   181 }
       
   182 
       
   183 void tst_QXmlResultItems::evaluate() const
       
   184 {
       
   185     QFETCH(QString, queryString);
       
   186 
       
   187     QXmlQuery query;
       
   188     query.setQuery(queryString);
       
   189 
       
   190     QVERIFY(query.isValid());
       
   191 
       
   192     QXmlResultItems result;
       
   193     query.evaluateTo(&result);
       
   194     QXmlItem item(result.next());
       
   195 
       
   196     while(!item.isNull())
       
   197     {
       
   198         QVERIFY(!result.current().isNull());
       
   199         QVERIFY(!result.hasError());
       
   200         item = result.next();
       
   201     }
       
   202 
       
   203     /* Now, stress beyond the end. */
       
   204     for(int i = 0; i < 3; ++i)
       
   205     {
       
   206         QVERIFY(result.current().isNull());
       
   207         QVERIFY(result.next().isNull());
       
   208     }
       
   209 }
       
   210 
       
   211 void tst_QXmlResultItems::evaluate_data() const
       
   212 {
       
   213     QTest::addColumn<QString>("queryString");
       
   214 
       
   215     QTest::newRow("one atomic value") << QString::fromLatin1("1");
       
   216     QTest::newRow("two atomic values") << QString::fromLatin1("1, xs:hexBinary('FF')");
       
   217     QTest::newRow("one node") << QString::fromLatin1("attribute name {'body'}");
       
   218     QTest::newRow("one node") << QString::fromLatin1("attribute name {'body'}");
       
   219     QTest::newRow("two nodes") << QString::fromLatin1("attribute name {'body'}, <!-- comment -->");
       
   220 }
       
   221 
       
   222 void tst_QXmlResultItems::evalateWithQueryError() const
       
   223 {
       
   224     /* This query is invalid. */
       
   225     const QXmlQuery query;
       
   226 
       
   227     QXmlResultItems result;
       
   228     query.evaluateTo(&result);
       
   229 
       
   230     QVERIFY(result.hasError());
       
   231     QVERIFY(result.next().isNull());
       
   232 }
       
   233 
       
   234 QTEST_MAIN(tst_QXmlResultItems)
       
   235 
       
   236 #include "tst_qxmlresultitems.moc"
       
   237 #else
       
   238 QTEST_NOOP_MAIN
       
   239 #endif
       
   240 // vim: et:ts=4:sw=4:sts=4