Changeset 68651 in webkit for trunk/JavaScriptCore/qt/tests


Ignore:
Timestamp:
Sep 29, 2010, 9:31:22 AM (15 years ago)
Author:
[email protected]
Message:

2010-09-29 Caio Marcelo de Oliveira Filho <[email protected]>

Reviewed by Andreas Kling.

[Qt] QScriptEngine should have an API for creating Date objects
https://bugs.webkit.org/show_bug.cgi?id=41667

Implement newDate(), isDate() and toDateTime() functions. Use the
QDateTime::{to,set}MSecsSinceEpoch() functions to do the
calculations.

  • api/qscriptengine.cpp: (QScriptEngine::newDate):
  • api/qscriptengine.h:
  • api/qscriptengine_p.cpp: (QScriptEnginePrivate::newDate):
  • api/qscriptengine_p.h: (QScriptEnginePrivate::isDate):
  • api/qscriptoriginalglobalobject_p.h: (QScriptOriginalGlobalObject::QScriptOriginalGlobalObject): need to keep track of Date Constructor and Prototype. (QScriptOriginalGlobalObject::~QScriptOriginalGlobalObject): ditto. (QScriptOriginalGlobalObject::isDate): use the Date Constructor and Prototype to identify Date values.
  • api/qscriptvalue.cpp: (QScriptValue::isDate): (QScriptValue::toDateTime):
  • api/qscriptvalue.h:
  • api/qscriptvalue_p.h: (QScriptValuePrivate::isDate): (QScriptValuePrivate::toDateTime):
  • tests/qscriptengine/tst_qscriptengine.cpp: (tst_QScriptEngine::newDate):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/qt/tests/qscriptengine/tst_qscriptengine.cpp

    r64130 r68651  
    2222#include "qscriptsyntaxcheckresult.h"
    2323#include "qscriptvalue.h"
     24#include <QtCore/qnumeric.h>
    2425#include <QtTest/qtest.h>
    2526
     
    5152    void newArray();
    5253    void uncaughtException();
     54    void newDate();
    5355};
    5456
     
    682684}
    683685
     686void tst_QScriptEngine::newDate()
     687{
     688    QScriptEngine eng;
     689    {
     690        QScriptValue date = eng.newDate(0);
     691        QCOMPARE(date.isValid(), true);
     692        QCOMPARE(date.isDate(), true);
     693        QCOMPARE(date.isObject(), true);
     694        QVERIFY(!date.isFunction());
     695        // prototype should be Date.prototype
     696        QCOMPARE(date.prototype().isValid(), true);
     697        QCOMPARE(date.prototype().isDate(), true);
     698        QCOMPARE(date.prototype().strictlyEquals(eng.evaluate("Date.prototype")), true);
     699    }
     700    {
     701        QDateTime dt = QDateTime(QDate(1, 2, 3), QTime(4, 5, 6, 7), Qt::LocalTime);
     702        QScriptValue date = eng.newDate(dt);
     703        QCOMPARE(date.isValid(), true);
     704        QCOMPARE(date.isDate(), true);
     705        QCOMPARE(date.isObject(), true);
     706        // prototype should be Date.prototype
     707        QCOMPARE(date.prototype().isValid(), true);
     708        QCOMPARE(date.prototype().isDate(), true);
     709        QCOMPARE(date.prototype().strictlyEquals(eng.evaluate("Date.prototype")), true);
     710
     711        QCOMPARE(date.toDateTime(), dt);
     712    }
     713    {
     714        QDateTime dt = QDateTime(QDate(1, 2, 3), QTime(4, 5, 6, 7), Qt::UTC);
     715        QScriptValue date = eng.newDate(dt);
     716        // toDateTime() result should be in local time
     717        QCOMPARE(date.toDateTime(), dt.toLocalTime());
     718    }
     719    // Date.parse() should return NaN when it fails
     720    {
     721        QScriptValue ret = eng.evaluate("Date.parse()");
     722        QVERIFY(ret.isNumber());
     723        QVERIFY(qIsNaN(ret.toNumber()));
     724    }
     725    // Date.parse() should be able to parse the output of Date().toString()
     726    {
     727        QScriptValue ret = eng.evaluate("var x = new Date(); var s = x.toString(); s == new Date(Date.parse(s)).toString()");
     728        QVERIFY(ret.isBoolean());
     729        QCOMPARE(ret.toBoolean(), true);
     730    }
     731}
     732
    684733QTEST_MAIN(tst_QScriptEngine)
    685734#include "tst_qscriptengine.moc"
Note: See TracChangeset for help on using the changeset viewer.