tests/auto/qscriptcontextinfo/tst_qscriptcontextinfo.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
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 #include <QtScript/qscriptcontextinfo.h>
       
    46 #include <QtScript/qscriptcontext.h>
       
    47 #include <QtScript/qscriptengine.h>
       
    48 #include <QtScript/qscriptable.h>
       
    49 #include <QtScript/qscriptengineagent.h>
       
    50 
       
    51 Q_DECLARE_METATYPE(QScriptValue)
       
    52 Q_DECLARE_METATYPE(QScriptContextInfo)
       
    53 Q_DECLARE_METATYPE(QList<QScriptContextInfo>)
       
    54 
       
    55 //TESTED_CLASS=
       
    56 //TESTED_FILES=
       
    57 
       
    58 class tst_QScriptContextInfo : public QObject, public QScriptable
       
    59 {
       
    60     Q_OBJECT
       
    61     Q_PROPERTY(QScriptValue testProperty READ testProperty WRITE setTestProperty)
       
    62 public:
       
    63     tst_QScriptContextInfo();
       
    64     virtual ~tst_QScriptContextInfo();
       
    65 
       
    66     QScriptValue testProperty() const
       
    67     {
       
    68         return engine()->globalObject().property("getContextInfoList").call();
       
    69     }
       
    70 
       
    71     QScriptValue setTestProperty(const QScriptValue &) const
       
    72     {
       
    73         return engine()->globalObject().property("getContextInfoList").call();
       
    74     }
       
    75 
       
    76 public slots:
       
    77     QScriptValue testSlot(double a, double b)
       
    78     {
       
    79         Q_UNUSED(a); Q_UNUSED(b);
       
    80         return engine()->globalObject().property("getContextInfoList").call();
       
    81     }
       
    82 
       
    83     QScriptValue testSlot(const QString &s)
       
    84     {
       
    85         Q_UNUSED(s);
       
    86         return engine()->globalObject().property("getContextInfoList").call();
       
    87     }
       
    88 
       
    89 private slots:
       
    90     void nativeFunction();
       
    91     void scriptFunction();
       
    92     void qtFunction();
       
    93     void qtPropertyFunction();
       
    94     void nullContext();
       
    95     void streaming();
       
    96     void assignmentAndComparison();
       
    97 };
       
    98 
       
    99 tst_QScriptContextInfo::tst_QScriptContextInfo()
       
   100 {
       
   101 }
       
   102 
       
   103 tst_QScriptContextInfo::~tst_QScriptContextInfo()
       
   104 {
       
   105 }
       
   106 
       
   107 static QScriptValue getContextInfoList(QScriptContext *ctx, QScriptEngine *eng)
       
   108 {
       
   109     QList<QScriptContextInfo> lst;
       
   110     while (ctx) {
       
   111         QScriptContextInfo info(ctx);
       
   112         lst.append(info);
       
   113         ctx = ctx->parentContext();
       
   114     }
       
   115     return qScriptValueFromValue(eng, lst);
       
   116 }
       
   117 
       
   118 void tst_QScriptContextInfo::nativeFunction()
       
   119 {
       
   120     QScriptEngine eng;
       
   121     eng.globalObject().setProperty("getContextInfoList", eng.newFunction(getContextInfoList));
       
   122 
       
   123     QString fileName = "foo.qs";
       
   124     int lineNumber = 123;
       
   125     QScriptValue ret = eng.evaluate("getContextInfoList()", fileName, lineNumber);
       
   126     QList<QScriptContextInfo> lst = qscriptvalue_cast<QList<QScriptContextInfo> >(ret);
       
   127     QCOMPARE(lst.size(), 2);
       
   128 
       
   129     {
       
   130         // getContextInfoList()
       
   131         QScriptContextInfo info = lst.at(0);
       
   132         QVERIFY(!info.isNull());
       
   133         QCOMPARE(info.functionType(), QScriptContextInfo::NativeFunction);
       
   134         QCOMPARE(info.scriptId(), (qint64)-1);
       
   135         QCOMPARE(info.fileName(), QString());
       
   136         QCOMPARE(info.lineNumber(), -1);
       
   137         QCOMPARE(info.columnNumber(), -1);
       
   138         QCOMPARE(info.functionName(), QString());
       
   139         QCOMPARE(info.functionEndLineNumber(), -1);
       
   140         QCOMPARE(info.functionStartLineNumber(), -1);
       
   141         QCOMPARE(info.functionParameterNames().size(), 0);
       
   142         QCOMPARE(info.functionMetaIndex(), -1);
       
   143     }
       
   144 
       
   145     {
       
   146         // evaluate()
       
   147         QScriptContextInfo info = lst.at(1);
       
   148         QVERIFY(!info.isNull());
       
   149         QCOMPARE(info.functionType(), QScriptContextInfo::NativeFunction);
       
   150         QVERIFY(info.scriptId() != -1);
       
   151         QCOMPARE(info.fileName(), fileName);
       
   152         QCOMPARE(info.lineNumber(), lineNumber);
       
   153         QEXPECT_FAIL("", "columnNumber doesn't work", Continue);
       
   154         QCOMPARE(info.columnNumber(), 1);
       
   155         QCOMPARE(info.functionName(), QString());
       
   156         QCOMPARE(info.functionEndLineNumber(), -1);
       
   157         QCOMPARE(info.functionStartLineNumber(), -1);
       
   158         QCOMPARE(info.functionParameterNames().size(), 0);
       
   159         QCOMPARE(info.functionMetaIndex(), -1);
       
   160     }
       
   161 }
       
   162 
       
   163 void tst_QScriptContextInfo::scriptFunction()
       
   164 {
       
   165     QScriptEngine eng;
       
   166     eng.globalObject().setProperty("getContextInfoList", eng.newFunction(getContextInfoList));
       
   167 
       
   168     QString fileName = "ciao.qs";
       
   169     int lineNumber = 456;
       
   170     QScriptValue ret = eng.evaluate("function bar(a, b, c) {\n return getContextInfoList();\n}\nbar()",
       
   171                                     fileName, lineNumber);
       
   172     QList<QScriptContextInfo> lst = qscriptvalue_cast<QList<QScriptContextInfo> >(ret);
       
   173     QCOMPARE(lst.size(), 3);
       
   174 
       
   175     // getContextInfoList()
       
   176     QCOMPARE(lst.at(0).functionType(), QScriptContextInfo::NativeFunction);
       
   177 
       
   178     {
       
   179         // bar()
       
   180         QScriptContextInfo info = lst.at(1);
       
   181         QCOMPARE(info.functionType(), QScriptContextInfo::ScriptFunction);
       
   182         QVERIFY(info.scriptId() != -1);
       
   183         QCOMPARE(info.fileName(), fileName);
       
   184         QCOMPARE(info.lineNumber(), lineNumber + 1);
       
   185         QEXPECT_FAIL("", "columnNumber doesn't work", Continue);
       
   186         QCOMPARE(info.columnNumber(), 2);
       
   187         QCOMPARE(info.functionName(), QString::fromLatin1("bar"));
       
   188         QCOMPARE(info.functionStartLineNumber(), lineNumber);
       
   189         QCOMPARE(info.functionEndLineNumber(), lineNumber + 2);
       
   190         QCOMPARE(info.functionParameterNames().size(), 3);
       
   191         QCOMPARE(info.functionParameterNames().at(0), QString::fromLatin1("a"));
       
   192         QCOMPARE(info.functionParameterNames().at(1), QString::fromLatin1("b"));
       
   193         QCOMPARE(info.functionParameterNames().at(2), QString::fromLatin1("c"));
       
   194         QCOMPARE(info.functionMetaIndex(), -1);
       
   195     }
       
   196 
       
   197     {
       
   198         // evaluate()
       
   199         QScriptContextInfo info = lst.at(2);
       
   200         QCOMPARE(info.functionType(), QScriptContextInfo::NativeFunction);
       
   201         QVERIFY(info.scriptId() != -1);
       
   202         QCOMPARE(info.fileName(), fileName);
       
   203         QCOMPARE(info.lineNumber(), lineNumber + 3);
       
   204         QEXPECT_FAIL("", "columnNumber doesn't work", Continue);
       
   205         QCOMPARE(info.columnNumber(), 1);
       
   206         QCOMPARE(info.functionName(), QString());
       
   207         QCOMPARE(info.functionEndLineNumber(), -1);
       
   208         QCOMPARE(info.functionStartLineNumber(), -1);
       
   209         QCOMPARE(info.functionParameterNames().size(), 0);
       
   210         QCOMPARE(info.functionMetaIndex(), -1);
       
   211     }
       
   212 }
       
   213 
       
   214 void tst_QScriptContextInfo::qtFunction()
       
   215 {
       
   216     QScriptEngine eng;
       
   217     eng.globalObject().setProperty("getContextInfoList", eng.newFunction(getContextInfoList));
       
   218     eng.globalObject().setProperty("qobj", eng.newQObject(this));
       
   219 
       
   220     for (int x = 0; x < 2; ++x) { // twice to test overloaded slot as well
       
   221         QString code;
       
   222         const char *sig;
       
   223         QStringList pnames;
       
   224         if (x == 0) {
       
   225             code = "qobj.testSlot(1, 2)";
       
   226             sig = "testSlot(double,double)";
       
   227             pnames << "a" << "b";
       
   228         } else {
       
   229             code = "qobj.testSlot('ciao')";
       
   230             sig = "testSlot(QString)";
       
   231             pnames << "s";
       
   232         }
       
   233         QScriptValue ret = eng.evaluate(code);
       
   234         QList<QScriptContextInfo> lst = qscriptvalue_cast<QList<QScriptContextInfo> >(ret);
       
   235         QCOMPARE(lst.size(), 3);
       
   236 
       
   237         // getContextInfoList()
       
   238         QCOMPARE(lst.at(0).functionType(), QScriptContextInfo::NativeFunction);
       
   239 
       
   240         {
       
   241             // testSlot(double a, double b)
       
   242             QScriptContextInfo info = lst.at(1);
       
   243             QCOMPARE(info.functionType(), QScriptContextInfo::QtFunction);
       
   244             QCOMPARE(info.scriptId(), (qint64)-1);
       
   245             QCOMPARE(info.fileName(), QString());
       
   246             QCOMPARE(info.lineNumber(), -1);
       
   247             QCOMPARE(info.columnNumber(), -1);
       
   248             QCOMPARE(info.functionName(), QString::fromLatin1("testSlot"));
       
   249             QCOMPARE(info.functionEndLineNumber(), -1);
       
   250             QCOMPARE(info.functionStartLineNumber(), -1);
       
   251             if (x == 0)
       
   252                 QEXPECT_FAIL("", "QScriptContextInfo doesn't pick the correct meta-index for overloaded slots", Continue);
       
   253             QCOMPARE(info.functionParameterNames().size(), pnames.size());
       
   254             if (x == 0)
       
   255                 QEXPECT_FAIL("", "QScriptContextInfo doesn't pick the correct meta-index for overloaded slots", Continue);
       
   256             QCOMPARE(info.functionParameterNames(), pnames);
       
   257             if (x == 0)
       
   258                 QEXPECT_FAIL("", "QScriptContextInfo doesn't pick the correct meta-index for overloaded slots", Continue);
       
   259             QCOMPARE(info.functionMetaIndex(), metaObject()->indexOfMethod(sig));
       
   260         }
       
   261 
       
   262         // evaluate()
       
   263         QCOMPARE(lst.at(0).functionType(), QScriptContextInfo::NativeFunction);
       
   264     }
       
   265 }
       
   266 
       
   267 void tst_QScriptContextInfo::qtPropertyFunction()
       
   268 {
       
   269     QScriptEngine eng;
       
   270     eng.globalObject().setProperty("getContextInfoList", eng.newFunction(getContextInfoList));
       
   271     eng.globalObject().setProperty("qobj", eng.newQObject(this));
       
   272 
       
   273     QScriptValue ret = eng.evaluate("qobj.testProperty");
       
   274     QList<QScriptContextInfo> lst = qscriptvalue_cast<QList<QScriptContextInfo> >(ret);
       
   275     QCOMPARE(lst.size(), 3);
       
   276 
       
   277     // getContextInfoList()
       
   278     QCOMPARE(lst.at(0).functionType(), QScriptContextInfo::NativeFunction);
       
   279 
       
   280     {
       
   281         // testProperty()
       
   282         QScriptContextInfo info = lst.at(1);
       
   283         QCOMPARE(info.functionType(), QScriptContextInfo::QtPropertyFunction);
       
   284         QCOMPARE(info.scriptId(), (qint64)-1);
       
   285         QCOMPARE(info.fileName(), QString());
       
   286         QCOMPARE(info.lineNumber(), -1);
       
   287         QCOMPARE(info.columnNumber(), -1);
       
   288         QCOMPARE(info.functionName(), QString::fromLatin1("testProperty"));
       
   289         QCOMPARE(info.functionEndLineNumber(), -1);
       
   290         QCOMPARE(info.functionStartLineNumber(), -1);
       
   291         QCOMPARE(info.functionParameterNames().size(), 0);
       
   292         QCOMPARE(info.functionMetaIndex(), metaObject()->indexOfProperty("testProperty"));
       
   293     }
       
   294 
       
   295     // evaluate()
       
   296     QCOMPARE(lst.at(0).functionType(), QScriptContextInfo::NativeFunction);
       
   297 }
       
   298 
       
   299 void tst_QScriptContextInfo::nullContext()
       
   300 {
       
   301     QScriptContextInfo info((QScriptContext*)0);
       
   302     QVERIFY(info.isNull());
       
   303     QCOMPARE(info.columnNumber(), -1);
       
   304     QCOMPARE(info.scriptId(), (qint64)-1);
       
   305     QCOMPARE(info.fileName(), QString());
       
   306     QCOMPARE(info.functionEndLineNumber(), -1);
       
   307     QCOMPARE(info.functionMetaIndex(), -1);
       
   308     QCOMPARE(info.functionName(), QString());
       
   309     QCOMPARE(info.functionParameterNames().size(), 0);
       
   310     QCOMPARE(info.functionStartLineNumber(), -1);
       
   311     QCOMPARE(info.functionType(), QScriptContextInfo::NativeFunction);
       
   312 }
       
   313 
       
   314 void tst_QScriptContextInfo::streaming()
       
   315 {
       
   316     {
       
   317         QScriptContextInfo info((QScriptContext*)0);
       
   318         QByteArray ba;
       
   319         QDataStream stream(&ba, QIODevice::ReadWrite);
       
   320         stream << info;
       
   321         stream.device()->seek(0);
       
   322         QScriptContextInfo info2;
       
   323         stream >> info2;
       
   324         QVERIFY(stream.device()->atEnd());
       
   325         QCOMPARE(info.functionType(), info2.functionType());
       
   326         QCOMPARE(info.scriptId(), info2.scriptId());
       
   327         QCOMPARE(info.fileName(), info2.fileName());
       
   328         QCOMPARE(info.lineNumber(), info2.lineNumber());
       
   329         QCOMPARE(info.columnNumber(), info2.columnNumber());
       
   330         QCOMPARE(info.functionName(), info2.functionName());
       
   331         QCOMPARE(info.functionEndLineNumber(), info2.functionEndLineNumber());
       
   332         QCOMPARE(info.functionStartLineNumber(), info2.functionStartLineNumber());
       
   333         QCOMPARE(info.functionParameterNames(), info2.functionParameterNames());
       
   334         QCOMPARE(info.functionMetaIndex(), info2.functionMetaIndex());
       
   335     }
       
   336     {
       
   337         QScriptEngine eng;
       
   338         eng.globalObject().setProperty("getContextInfoList", eng.newFunction(getContextInfoList));
       
   339 
       
   340         QString fileName = "ciao.qs";
       
   341         int lineNumber = 456;
       
   342         QScriptValue ret = eng.evaluate("function bar(a, b, c) {\n return getContextInfoList();\n}\nbar()",
       
   343                                         fileName, lineNumber);
       
   344         QList<QScriptContextInfo> lst = qscriptvalue_cast<QList<QScriptContextInfo> >(ret);
       
   345         QCOMPARE(lst.size(), 3);
       
   346         for (int i = 0; i < lst.size(); ++i) {
       
   347             const QScriptContextInfo &info = lst.at(i);
       
   348             QByteArray ba;
       
   349             QDataStream stream(&ba, QIODevice::ReadWrite);
       
   350             stream << info;
       
   351             stream.device()->seek(0);
       
   352             QScriptContextInfo info2;
       
   353             stream >> info2;
       
   354             QVERIFY(stream.device()->atEnd());
       
   355             QCOMPARE(info.functionType(), info2.functionType());
       
   356             QCOMPARE(info.scriptId(), info2.scriptId());
       
   357             QCOMPARE(info.fileName(), info2.fileName());
       
   358             QCOMPARE(info.lineNumber(), info2.lineNumber());
       
   359             QCOMPARE(info.columnNumber(), info2.columnNumber());
       
   360             QCOMPARE(info.functionName(), info2.functionName());
       
   361             QCOMPARE(info.functionEndLineNumber(), info2.functionEndLineNumber());
       
   362             QCOMPARE(info.functionStartLineNumber(), info2.functionStartLineNumber());
       
   363             QCOMPARE(info.functionParameterNames(), info2.functionParameterNames());
       
   364             QCOMPARE(info.functionMetaIndex(), info2.functionMetaIndex());
       
   365         }
       
   366     }
       
   367 }
       
   368 
       
   369 void tst_QScriptContextInfo::assignmentAndComparison()
       
   370 {
       
   371     QScriptEngine eng;
       
   372     eng.globalObject().setProperty("getContextInfoList", eng.newFunction(getContextInfoList));
       
   373     QString fileName = "ciao.qs";
       
   374     int lineNumber = 456;
       
   375     QScriptValue ret = eng.evaluate("function bar(a, b, c) {\n return getContextInfoList();\n}\nbar()",
       
   376                                     fileName, lineNumber);
       
   377     QList<QScriptContextInfo> lst = qscriptvalue_cast<QList<QScriptContextInfo> >(ret);
       
   378     QCOMPARE(lst.size(), 3);
       
   379     QScriptContextInfo ci = lst.at(0);
       
   380     QScriptContextInfo same = ci;
       
   381     QVERIFY(ci == same);
       
   382     QVERIFY(!(ci != same));
       
   383     QScriptContextInfo other = lst.at(1);
       
   384     QVERIFY(!(ci == other));
       
   385     QVERIFY(ci != other);
       
   386 }
       
   387 
       
   388 QTEST_MAIN(tst_QScriptContextInfo)
       
   389 #include "tst_qscriptcontextinfo.moc"