37 ** |
37 ** |
38 ** $QT_END_LICENSE$ |
38 ** $QT_END_LICENSE$ |
39 ** |
39 ** |
40 ****************************************************************************/ |
40 ****************************************************************************/ |
41 |
41 |
42 |
42 #include "tst_qscriptvalue.h" |
43 #include <QtTest/QtTest> |
|
44 #include <QtGui/QPushButton> |
43 #include <QtGui/QPushButton> |
45 #include <QtCore/qnumeric.h> |
|
46 |
|
47 #include <QtScript/qscriptclass.h> |
|
48 #include <QtScript/qscriptvalue.h> |
|
49 #include <QtScript/qscriptengine.h> |
|
50 |
44 |
51 //TESTED_CLASS= |
45 //TESTED_CLASS= |
52 //TESTED_FILES= |
46 //TESTED_FILES= |
53 |
47 |
54 QT_BEGIN_NAMESPACE |
48 QT_BEGIN_NAMESPACE |
55 extern bool qt_script_isJITEnabled(); |
49 extern bool qt_script_isJITEnabled(); |
56 QT_END_NAMESPACE |
50 QT_END_NAMESPACE |
57 |
51 |
58 class tst_QScriptValue : public QObject |
|
59 { |
|
60 Q_OBJECT |
|
61 |
|
62 public: |
|
63 tst_QScriptValue(); |
|
64 virtual ~tst_QScriptValue(); |
|
65 |
|
66 private slots: |
|
67 void ctor(); |
|
68 void engine(); |
|
69 void toString(); |
|
70 void toNumber(); |
|
71 void toBoolean(); |
|
72 void toBool(); |
|
73 void toInteger(); |
|
74 void toInt32(); |
|
75 void toUInt32(); |
|
76 void toUInt16(); |
|
77 void toVariant(); |
|
78 void toQObject(); |
|
79 void toObject(); |
|
80 void toDateTime(); |
|
81 void toRegExp(); |
|
82 void instanceOf(); |
|
83 void isArray(); |
|
84 void isDate(); |
|
85 void isError(); |
|
86 void isRegExp(); |
|
87 void getSetPrototype(); |
|
88 void getSetScope(); |
|
89 void getSetProperty(); |
|
90 void arrayElementGetterSetter(); |
|
91 void getSetData(); |
|
92 void getSetScriptClass(); |
|
93 void call(); |
|
94 void construct(); |
|
95 void lessThan(); |
|
96 void equals(); |
|
97 void strictlyEquals(); |
|
98 void castToPointer(); |
|
99 void prettyPrinter_data(); |
|
100 void prettyPrinter(); |
|
101 void engineDeleted(); |
|
102 void valueOfWithClosure(); |
|
103 void objectId(); |
|
104 }; |
|
105 |
|
106 tst_QScriptValue::tst_QScriptValue() |
52 tst_QScriptValue::tst_QScriptValue() |
|
53 : engine(0) |
107 { |
54 { |
108 } |
55 } |
109 |
56 |
110 tst_QScriptValue::~tst_QScriptValue() |
57 tst_QScriptValue::~tst_QScriptValue() |
111 { |
58 { |
112 } |
59 delete engine; |
|
60 } |
|
61 |
|
62 void tst_QScriptValue::dataHelper(InitDataFunction init, DefineDataFunction define) |
|
63 { |
|
64 QTest::addColumn<QString>("__expression__"); |
|
65 (this->*init)(); |
|
66 QHash<QString,QScriptValue>::const_iterator it; |
|
67 for (it = m_values.constBegin(); it != m_values.constEnd(); ++it) { |
|
68 m_currentExpression = it.key(); |
|
69 (this->*define)(it.key().toLatin1()); |
|
70 } |
|
71 m_currentExpression = QString(); |
|
72 } |
|
73 |
|
74 QTestData &tst_QScriptValue::newRow(const char *tag) |
|
75 { |
|
76 return QTest::newRow(tag) << m_currentExpression; |
|
77 } |
|
78 |
|
79 void tst_QScriptValue::testHelper(TestFunction fun) |
|
80 { |
|
81 QFETCH(QString, __expression__); |
|
82 QScriptValue value = m_values.value(__expression__); |
|
83 (this->*fun)(__expression__.toLatin1(), value); |
|
84 } |
|
85 |
|
86 void tst_QScriptValue::assignAndCopyConstruct_initData() |
|
87 { |
|
88 QTest::addColumn<int>("dummy"); |
|
89 initScriptValues(); |
|
90 } |
|
91 |
|
92 void tst_QScriptValue::assignAndCopyConstruct_makeData(const char *expr) |
|
93 { |
|
94 newRow(expr) << 0; |
|
95 } |
|
96 |
|
97 void tst_QScriptValue::assignAndCopyConstruct_test(const char *, const QScriptValue &value) |
|
98 { |
|
99 QScriptValue copy(value); |
|
100 QCOMPARE(copy.strictlyEquals(value), !value.isNumber() || !qIsNaN(value.toNumber())); |
|
101 QCOMPARE(copy.engine(), value.engine()); |
|
102 |
|
103 QScriptValue assigned = copy; |
|
104 QCOMPARE(assigned.strictlyEquals(value), !copy.isNumber() || !qIsNaN(copy.toNumber())); |
|
105 QCOMPARE(assigned.engine(), assigned.engine()); |
|
106 |
|
107 QScriptValue other(!value.toBool()); |
|
108 assigned = other; |
|
109 QVERIFY(!assigned.strictlyEquals(copy)); |
|
110 QVERIFY(assigned.strictlyEquals(other)); |
|
111 QCOMPARE(assigned.engine(), other.engine()); |
|
112 } |
|
113 |
|
114 DEFINE_TEST_FUNCTION(assignAndCopyConstruct) |
113 |
115 |
114 void tst_QScriptValue::ctor() |
116 void tst_QScriptValue::ctor() |
115 { |
117 { |
116 QScriptEngine eng; |
118 QScriptEngine eng; |
117 { |
119 { |
328 QVERIFY(QScriptValue(0, 1.0).isNumber()); |
330 QVERIFY(QScriptValue(0, 1.0).isNumber()); |
329 QVERIFY(QScriptValue(0, "ciao").isString()); |
331 QVERIFY(QScriptValue(0, "ciao").isString()); |
330 QVERIFY(QScriptValue(0, QString("ciao")).isString()); |
332 QVERIFY(QScriptValue(0, QString("ciao")).isString()); |
331 } |
333 } |
332 |
334 |
333 void tst_QScriptValue::engine() |
|
334 { |
|
335 QScriptEngine eng; |
|
336 QScriptValue object = eng.newObject(); |
|
337 QCOMPARE(object.engine(), &eng); |
|
338 } |
|
339 |
|
340 static QScriptValue myFunction(QScriptContext *, QScriptEngine *eng) |
335 static QScriptValue myFunction(QScriptContext *, QScriptEngine *eng) |
341 { |
336 { |
342 return eng->undefinedValue(); |
337 return eng->undefinedValue(); |
343 } |
338 } |
344 |
339 |
345 void tst_QScriptValue::toString() |
340 void tst_QScriptValue::toString_old() |
346 { |
341 { |
347 QScriptEngine eng; |
342 QScriptEngine eng; |
348 |
343 |
349 QScriptValue undefined = eng.undefinedValue(); |
344 QScriptValue undefined = eng.undefinedValue(); |
350 QCOMPARE(undefined.toString(), QString("undefined")); |
345 QCOMPARE(undefined.toString(), QString("undefined")); |
454 QCOMPARE(variant.toString(), QString::fromLatin1("QVariant(QPoint)")); |
449 QCOMPARE(variant.toString(), QString::fromLatin1("QVariant(QPoint)")); |
455 variant = eng.newVariant(QUrl()); |
450 variant = eng.newVariant(QUrl()); |
456 QVERIFY(variant.toString().isEmpty()); |
451 QVERIFY(variant.toString().isEmpty()); |
457 } |
452 } |
458 |
453 |
459 void tst_QScriptValue::toNumber() |
454 void tst_QScriptValue::toNumber_old() |
460 { |
455 { |
461 QScriptEngine eng; |
456 QScriptEngine eng; |
462 |
457 |
463 QScriptValue undefined = eng.undefinedValue(); |
458 QScriptValue undefined = eng.undefinedValue(); |
464 QCOMPARE(qIsNaN(undefined.toNumber()), true); |
459 QCOMPARE(qIsNaN(undefined.toNumber()), true); |
527 QCOMPARE(str2.toNumber(), 123.0); |
522 QCOMPARE(str2.toNumber(), 123.0); |
528 QCOMPARE(qscriptvalue_cast<qsreal>(str2), 123.0); |
523 QCOMPARE(qscriptvalue_cast<qsreal>(str2), 123.0); |
529 } |
524 } |
530 } |
525 } |
531 |
526 |
532 void tst_QScriptValue::toBoolean() // deprecated |
527 void tst_QScriptValue::toBoolean_old() // deprecated |
533 { |
528 { |
534 QScriptEngine eng; |
529 QScriptEngine eng; |
535 |
530 |
536 QScriptValue undefined = eng.undefinedValue(); |
531 QScriptValue undefined = eng.undefinedValue(); |
537 QCOMPARE(undefined.toBoolean(), false); |
532 QCOMPARE(undefined.toBoolean(), false); |
1587 QVERIFY(QScriptValue(false).toRegExp().isEmpty()); |
1582 QVERIFY(QScriptValue(false).toRegExp().isEmpty()); |
1588 QVERIFY(eng.nullValue().toRegExp().isEmpty()); |
1583 QVERIFY(eng.nullValue().toRegExp().isEmpty()); |
1589 QVERIFY(eng.undefinedValue().toRegExp().isEmpty()); |
1584 QVERIFY(eng.undefinedValue().toRegExp().isEmpty()); |
1590 } |
1585 } |
1591 |
1586 |
1592 void tst_QScriptValue::instanceOf() |
1587 void tst_QScriptValue::instanceOf_old() |
1593 { |
1588 { |
1594 QScriptEngine eng; |
1589 QScriptEngine eng; |
1595 QScriptValue obj = eng.newObject(); |
1590 QScriptValue obj = eng.newObject(); |
1596 QCOMPARE(obj.instanceOf(eng.evaluate("Object.prototype")), false); |
1591 QCOMPARE(obj.instanceOf(eng.evaluate("Object.prototype")), false); |
1597 QCOMPARE(obj.instanceOf(eng.evaluate("Array.prototype")), false); |
1592 QCOMPARE(obj.instanceOf(eng.evaluate("Array.prototype")), false); |
1623 QScriptEngine otherEngine; |
1618 QScriptEngine otherEngine; |
1624 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::instanceof: cannot perform operation on a value created in a different engine"); |
1619 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::instanceof: cannot perform operation on a value created in a different engine"); |
1625 QCOMPARE(obj.instanceOf(otherEngine.globalObject().property("Object")), false); |
1620 QCOMPARE(obj.instanceOf(otherEngine.globalObject().property("Object")), false); |
1626 } |
1621 } |
1627 |
1622 |
1628 void tst_QScriptValue::isArray() |
1623 void tst_QScriptValue::isArray_old() |
1629 { |
1624 { |
1630 QScriptEngine eng; |
1625 QScriptEngine eng; |
1631 QVERIFY(eng.evaluate("[]").isArray()); |
1626 QVERIFY(eng.evaluate("[]").isArray()); |
1632 QVERIFY(!eng.evaluate("{}").isArray()); |
1627 QVERIFY(!eng.evaluate("{}").isArray()); |
1633 QVERIFY(!eng.globalObject().isArray()); |
1628 QVERIFY(!eng.globalObject().isArray()); |
1636 QVERIFY(!QScriptValue(false).isArray()); |
1631 QVERIFY(!QScriptValue(false).isArray()); |
1637 QVERIFY(!eng.nullValue().isArray()); |
1632 QVERIFY(!eng.nullValue().isArray()); |
1638 QVERIFY(!eng.undefinedValue().isArray()); |
1633 QVERIFY(!eng.undefinedValue().isArray()); |
1639 } |
1634 } |
1640 |
1635 |
1641 void tst_QScriptValue::isDate() |
1636 void tst_QScriptValue::isDate_old() |
1642 { |
1637 { |
1643 QScriptEngine eng; |
1638 QScriptEngine eng; |
1644 QVERIFY(eng.evaluate("new Date()").isDate()); |
1639 QVERIFY(eng.evaluate("new Date()").isDate()); |
1645 QVERIFY(!eng.evaluate("[]").isDate()); |
1640 QVERIFY(!eng.evaluate("[]").isDate()); |
1646 QVERIFY(!eng.evaluate("{}").isDate()); |
1641 QVERIFY(!eng.evaluate("{}").isDate()); |
1675 QVERIFY(!eng.nullValue().isError()); |
1670 QVERIFY(!eng.nullValue().isError()); |
1676 QVERIFY(!eng.undefinedValue().isError()); |
1671 QVERIFY(!eng.undefinedValue().isError()); |
1677 QVERIFY(!eng.evaluate("new Object()").isError()); |
1672 QVERIFY(!eng.evaluate("new Object()").isError()); |
1678 } |
1673 } |
1679 |
1674 |
1680 void tst_QScriptValue::isRegExp() |
1675 void tst_QScriptValue::isRegExp_old() |
1681 { |
1676 { |
1682 QScriptEngine eng; |
1677 QScriptEngine eng; |
1683 QVERIFY(eng.evaluate("/foo/").isRegExp()); |
1678 QVERIFY(eng.evaluate("/foo/").isRegExp()); |
1684 QVERIFY(!eng.evaluate("[]").isRegExp()); |
1679 QVERIFY(!eng.evaluate("[]").isRegExp()); |
1685 QVERIFY(!eng.evaluate("{}").isRegExp()); |
1680 QVERIFY(!eng.evaluate("{}").isRegExp()); |
2905 QCOMPARE(obj2.equals(obj2), true); |
2900 QCOMPARE(obj2.equals(obj2), true); |
2906 |
2901 |
2907 QScriptValue qobj1 = eng.newQObject(this); |
2902 QScriptValue qobj1 = eng.newQObject(this); |
2908 QScriptValue qobj2 = eng.newQObject(this); |
2903 QScriptValue qobj2 = eng.newQObject(this); |
2909 QScriptValue qobj3 = eng.newQObject(0); |
2904 QScriptValue qobj3 = eng.newQObject(0); |
2910 QScriptValue qobj4 = eng.newQObject(new QObject()); |
2905 QScriptValue qobj4 = eng.newQObject(new QObject(), QScriptEngine::ScriptOwnership); |
2911 QVERIFY(qobj1.equals(qobj2)); // compares the QObject pointers |
2906 QVERIFY(qobj1.equals(qobj2)); // compares the QObject pointers |
2912 QVERIFY(!qobj2.equals(qobj4)); // compares the QObject pointers |
2907 QVERIFY(!qobj2.equals(qobj4)); // compares the QObject pointers |
2913 QVERIFY(!qobj2.equals(obj2)); // compares the QObject pointers |
2908 QVERIFY(!qobj2.equals(obj2)); // compares the QObject pointers |
2914 |
2909 |
2915 QScriptValue compareFun = eng.evaluate("(function(a, b) { return a == b; })"); |
2910 QScriptValue compareFun = eng.evaluate("(function(a, b) { return a == b; })"); |
3088 QVERIFY(!falskt.toObject().strictlyEquals(falskt)); |
3083 QVERIFY(!falskt.toObject().strictlyEquals(falskt)); |
3089 QVERIFY(!falskt.strictlyEquals(sant)); |
3084 QVERIFY(!falskt.strictlyEquals(sant)); |
3090 QVERIFY(!falskt.strictlyEquals(undefined)); |
3085 QVERIFY(!falskt.strictlyEquals(undefined)); |
3091 QVERIFY(!falskt.strictlyEquals(null)); |
3086 QVERIFY(!falskt.strictlyEquals(null)); |
3092 QVERIFY(!falskt.strictlyEquals(QScriptValue())); |
3087 QVERIFY(!falskt.strictlyEquals(QScriptValue())); |
|
3088 |
|
3089 QVERIFY(!QScriptValue(false).strictlyEquals(123)); |
|
3090 QVERIFY(!QScriptValue(QScriptValue::UndefinedValue).strictlyEquals(123)); |
|
3091 QVERIFY(!QScriptValue(QScriptValue::NullValue).strictlyEquals(123)); |
|
3092 QVERIFY(!QScriptValue(false).strictlyEquals("ciao")); |
|
3093 QVERIFY(!QScriptValue(QScriptValue::UndefinedValue).strictlyEquals("ciao")); |
|
3094 QVERIFY(!QScriptValue(QScriptValue::NullValue).strictlyEquals("ciao")); |
|
3095 QVERIFY(QScriptValue(&eng, "ciao").strictlyEquals("ciao")); |
|
3096 QVERIFY(QScriptValue("ciao").strictlyEquals(QScriptValue(&eng, "ciao"))); |
|
3097 QVERIFY(!QScriptValue("ciao").strictlyEquals(123)); |
|
3098 QVERIFY(!QScriptValue("ciao").strictlyEquals(QScriptValue(&eng, 123))); |
|
3099 QVERIFY(!QScriptValue(123).strictlyEquals("ciao")); |
|
3100 QVERIFY(!QScriptValue(123).strictlyEquals(QScriptValue(&eng, "ciao"))); |
|
3101 QVERIFY(!QScriptValue(&eng, 123).strictlyEquals("ciao")); |
3093 |
3102 |
3094 QScriptValue obj1 = eng.newObject(); |
3103 QScriptValue obj1 = eng.newObject(); |
3095 QScriptValue obj2 = eng.newObject(); |
3104 QScriptValue obj2 = eng.newObject(); |
3096 QCOMPARE(obj1.strictlyEquals(obj2), false); |
3105 QCOMPARE(obj1.strictlyEquals(obj2), false); |
3097 QCOMPARE(obj2.strictlyEquals(obj1), false); |
3106 QCOMPARE(obj2.strictlyEquals(obj1), false); |