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