1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
43 #include <QtQml/qjsvalue.h>
44 #include <QtQml/qjsengine.h>
46 Q_DECLARE_METATYPE(QJSValue)
48 class tst_QJSValue : public QObject
54 virtual ~tst_QJSValue();
61 void boolConstructor();
62 void floatConstructor();
63 void numberConstructor();
64 void stringConstructor();
65 void nullConstructor();
66 void undefinedConstructor();
67 void boolConstructorWithEngine();
68 void floatConstructorWithEngine();
69 void intConstructorWithEngine();
70 void stringConstructorWithEngine();
71 void undefinedConstructorWithEngine();
72 void copyConstructor_data();
73 void copyConstructor();
76 void construct_data();
81 void data_noData_data();
84 void equalsSelf_data();
87 void lessThanSelf_data();
90 void strictlyEqualsSelf_data();
91 void strictlyEqualsSelf();
100 void isCallable_data();
104 void isNumber_data();
106 void isObject_data();
108 #if 0 // no qmetaobject
109 void isQMetaObject_data();
110 void isQMetaObject();
112 void isQObject_data();
114 void isRegExp_data();
116 void isString_data();
118 void isUndefined_data();
120 void isVariant_data();
124 void toDateTime_data();
128 void toNumber_data();
130 void toRegExp_data();
132 void toString_data();
136 #if 0 // no qmetaobject
137 void toQMetaObject_data();
138 void toQMetaObject();
140 void toQObject_data();
142 void toVariant_data();
144 void property_data();
146 #if 0 // no string handle
147 void propertyById_data();
150 void propertyByIndex();
151 void setProperty_data();
153 #if 0 // no string handle
154 void setPropertyById_data();
155 void setPropertyById();
157 void setPropertyByIndex();
158 #if 0 // no propertyFlags for now
159 void propertyFlags_data();
160 void propertyFlags();
161 void propertyFlagsById_data();
162 void propertyFlagsById();
164 void prototype_data();
167 #if 0 // no script class
168 void scriptClass_data();
170 void setScriptClass();
172 #if 0 // no string handle
173 void readMetaProperty();
174 void writeMetaProperty();
178 void defineStandardTestValues();
182 m_engine = new QJSEngine;
188 tst_QJSValue::tst_QJSValue()
193 tst_QJSValue::~tst_QJSValue()
198 void tst_QJSValue::init()
202 void tst_QJSValue::cleanup()
206 void tst_QJSValue::boolConstructor()
213 void tst_QJSValue::floatConstructor()
220 void tst_QJSValue::numberConstructor()
227 void tst_QJSValue::stringConstructor()
229 QString str = QString::fromLatin1("ciao");
235 void tst_QJSValue::nullConstructor()
238 QJSValue val(QJSValue::NullValue);
242 void tst_QJSValue::undefinedConstructor()
245 QJSValue val(QJSValue::UndefinedValue);
249 void tst_QJSValue::boolConstructorWithEngine()
253 m_engine->toScriptValue(true);
257 void tst_QJSValue::floatConstructorWithEngine()
261 m_engine->toScriptValue(123.0);
265 void tst_QJSValue::intConstructorWithEngine()
269 m_engine->toScriptValue(123);
273 void tst_QJSValue::stringConstructorWithEngine()
276 QString str = QString::fromLatin1("ciao");
278 m_engine->toScriptValue(str);
282 void tst_QJSValue::undefinedConstructorWithEngine()
287 m_engine->toScriptValue(var);
291 void tst_QJSValue::copyConstructor_data()
293 defineStandardTestValues();
296 void tst_QJSValue::copyConstructor()
298 QFETCH(QJSValue, val);
304 void tst_QJSValue::call_data()
307 QTest::addColumn<QString>("code");
308 QTest::newRow("empty function") << QString::fromLatin1("(function(){})");
309 QTest::newRow("function returning number") << QString::fromLatin1("(function(){ return 123; })");
310 QTest::newRow("closure") << QString::fromLatin1("(function(a, b){ return function() { return a + b; }; })(1, 2)");
313 void tst_QJSValue::call()
315 QFETCH(QString, code);
316 QJSValue fun = m_engine->evaluate(code);
317 QVERIFY(fun.isCallable());
323 void tst_QJSValue::construct_data()
326 QTest::addColumn<QString>("code");
327 QTest::newRow("empty function") << QString::fromLatin1("(function(){})");
328 QTest::newRow("simple constructor") << QString::fromLatin1("(function(){ this.x = 10; this.y = 20; })");
331 void tst_QJSValue::construct()
333 QFETCH(QString, code);
334 QJSValue fun = m_engine->evaluate(code);
335 QVERIFY(fun.isCallable());
337 (void)fun.callAsConstructor();
342 void tst_QJSValue::data()
345 QJSValue obj = m_engine->newObject();
346 obj.setData(QJSValue(m_engine, 123));
352 void tst_QJSValue::setData()
355 QJSValue obj = m_engine->newObject();
356 QJSValue val(m_engine, 123);
362 void tst_QJSValue::data_noData_data()
364 defineStandardTestValues();
367 void tst_QJSValue::data_noData()
369 QFETCH(QJSValue, val);
370 QVERIFY(!val.data().isValid());
377 void tst_QJSValue::equalsSelf_data()
379 defineStandardTestValues();
382 void tst_QJSValue::equalsSelf()
384 QFETCH(QJSValue, val);
391 void tst_QJSValue::lessThanSelf_data()
393 defineStandardTestValues();
396 void tst_QJSValue::lessThanSelf()
398 QFETCH(QJSValue, val);
405 void tst_QJSValue::strictlyEqualsSelf_data()
407 defineStandardTestValues();
410 void tst_QJSValue::strictlyEqualsSelf()
412 QFETCH(QJSValue, val);
414 val.strictlyEquals(val);
418 void tst_QJSValue::isArray_data()
420 defineStandardTestValues();
423 void tst_QJSValue::isArray()
425 QFETCH(QJSValue, val);
431 void tst_QJSValue::isBool_data()
433 defineStandardTestValues();
436 void tst_QJSValue::isBool()
438 QFETCH(QJSValue, val);
444 void tst_QJSValue::isDate_data()
446 defineStandardTestValues();
449 void tst_QJSValue::isDate()
451 QFETCH(QJSValue, val);
457 void tst_QJSValue::isError_data()
459 defineStandardTestValues();
462 void tst_QJSValue::isError()
464 QFETCH(QJSValue, val);
470 void tst_QJSValue::isCallable_data()
472 defineStandardTestValues();
475 void tst_QJSValue::isCallable()
477 QFETCH(QJSValue, val);
483 void tst_QJSValue::isNull_data()
485 defineStandardTestValues();
488 void tst_QJSValue::isNull()
490 QFETCH(QJSValue, val);
496 void tst_QJSValue::isNumber_data()
498 defineStandardTestValues();
501 void tst_QJSValue::isNumber()
503 QFETCH(QJSValue, val);
509 void tst_QJSValue::isObject_data()
511 defineStandardTestValues();
514 void tst_QJSValue::isObject()
516 QFETCH(QJSValue, val);
523 void tst_QJSValue::isQMetaObject_data()
525 defineStandardTestValues();
528 void tst_QJSValue::isQMetaObject()
530 QFETCH(QJSValue, val);
537 void tst_QJSValue::isQObject_data()
539 defineStandardTestValues();
542 void tst_QJSValue::isQObject()
544 QFETCH(QJSValue, val);
550 void tst_QJSValue::isRegExp_data()
552 defineStandardTestValues();
555 void tst_QJSValue::isRegExp()
557 QFETCH(QJSValue, val);
563 void tst_QJSValue::isString_data()
565 defineStandardTestValues();
568 void tst_QJSValue::isString()
570 QFETCH(QJSValue, val);
576 void tst_QJSValue::isUndefined_data()
578 defineStandardTestValues();
581 void tst_QJSValue::isUndefined()
583 QFETCH(QJSValue, val);
589 void tst_QJSValue::isVariant_data()
591 defineStandardTestValues();
594 void tst_QJSValue::isVariant()
596 QFETCH(QJSValue, val);
602 void tst_QJSValue::toBool_data()
604 defineStandardTestValues();
607 void tst_QJSValue::toBool()
609 QFETCH(QJSValue, val);
615 void tst_QJSValue::toDateTime_data()
617 defineStandardTestValues();
620 void tst_QJSValue::toDateTime()
622 QFETCH(QJSValue, val);
628 void tst_QJSValue::toInt_data()
630 defineStandardTestValues();
633 void tst_QJSValue::toInt()
635 QFETCH(QJSValue, val);
641 void tst_QJSValue::toNumber_data()
643 defineStandardTestValues();
646 void tst_QJSValue::toNumber()
648 QFETCH(QJSValue, val);
654 void tst_QJSValue::toRegExp_data()
656 defineStandardTestValues();
659 void tst_QJSValue::toRegExp()
661 QFETCH(QJSValue, val);
663 qjsvalue_cast<QRegExp>(val);
667 void tst_QJSValue::toString_data()
669 defineStandardTestValues();
672 void tst_QJSValue::toString()
674 QFETCH(QJSValue, val);
676 (void)val.toString();
681 void tst_QJSValue::toQMetaObject_data()
683 defineStandardTestValues();
686 void tst_QJSValue::toQMetaObject()
688 QFETCH(QJSValue, val);
695 void tst_QJSValue::toQObject_data()
697 defineStandardTestValues();
700 void tst_QJSValue::toQObject()
702 QFETCH(QJSValue, val);
704 (void)val.toQObject();
708 void tst_QJSValue::toUInt_data()
710 defineStandardTestValues();
713 void tst_QJSValue::toUInt()
715 QFETCH(QJSValue, val);
721 void tst_QJSValue::toVariant_data()
723 defineStandardTestValues();
726 void tst_QJSValue::toVariant()
728 QFETCH(QJSValue, val);
733 void tst_QJSValue::property_data()
735 QTest::addColumn<QString>("propertyName");
736 QTest::addColumn<bool>("create");
737 QTest::newRow("foo") << QString::fromLatin1("foo") << true;
738 QTest::newRow("hasOwnProperty") << QString::fromLatin1("hasOwnProperty") << false; // From Object.prototype.
739 QTest::newRow("noSuchProperty") << QString::fromLatin1("noSuchProperty") << false;
742 void tst_QJSValue::property()
744 QFETCH(QString, propertyName);
745 QFETCH(bool, create);
747 QJSValue obj = m_engine->newObject();
749 obj.setProperty(propertyName, 123);
751 (void)obj.property(propertyName);
756 void tst_QJSValue::propertyById_data()
761 void tst_QJSValue::propertyById()
763 QFETCH(QString, propertyName);
764 QFETCH(bool, create);
766 QJSValue obj = m_engine->newObject();
767 QJSString id = m_engine->toStringHandle(propertyName);
769 obj.setProperty(id, 123);
776 void tst_QJSValue::propertyByIndex()
779 QJSValue obj = m_engine->newObject();
780 obj.setProperty(123, 456);
786 void tst_QJSValue::setProperty_data()
789 QTest::addColumn<QString>("propertyName");
790 QTest::addColumn<QJSValue>("val");
791 QTest::newRow("foo") << QString::fromLatin1("foo") << QJSValue(123);
792 QTest::newRow("bar") << QString::fromLatin1("bar") << m_engine->toScriptValue(123);
793 QTest::newRow("baz") << QString::fromLatin1("baz") << QJSValue();
794 QTest::newRow("toString") << QString::fromLatin1("toString") << m_engine->toScriptValue(true);
797 void tst_QJSValue::setProperty()
799 QFETCH(QString, propertyName);
800 QFETCH(QJSValue, val);
801 QJSValue obj = m_engine->newObject();
803 obj.setProperty(propertyName, val);
808 void tst_QJSValue::setPropertyById_data()
813 void tst_QJSValue::setPropertyById()
815 QFETCH(QString, propertyName);
816 QFETCH(QJSValue, val);
817 QJSValue obj = m_engine->newObject();
818 QJSString id = m_engine->toStringHandle(propertyName);
820 obj.setProperty(id, val);
825 void tst_QJSValue::setPropertyByIndex()
828 QJSValue obj = m_engine->newObject();
831 obj.setProperty(123, 456);
836 void tst_QJSValue::propertyFlags_data()
841 void tst_QJSValue::propertyFlags()
843 QFETCH(QString, propertyName);
844 QFETCH(bool, create);
846 QJSValue obj = m_engine->newObject();
848 obj.setProperty(propertyName, 123, QJSValue::SkipInEnumeration | QJSValue::ReadOnly);
850 (void)obj.propertyFlags(propertyName);
854 void tst_QJSValue::propertyFlagsById_data()
856 propertyFlags_data();
859 void tst_QJSValue::propertyFlagsById()
861 QFETCH(QString, propertyName);
862 QFETCH(bool, create);
864 QJSValue obj = m_engine->newObject();
865 QJSString id = m_engine->toStringHandle(propertyName);
867 obj.setProperty(id, 123, QJSValue::SkipInEnumeration | QJSValue::ReadOnly);
869 obj.propertyFlags(id);
874 void tst_QJSValue::prototype_data()
876 defineStandardTestValues();
879 void tst_QJSValue::prototype()
881 QFETCH(QJSValue, val);
887 void tst_QJSValue::setPrototype()
890 QJSValue obj = m_engine->newObject();
891 QJSValue proto = m_engine->newObject();
893 obj.setPrototype(proto);
898 void tst_QJSValue::scriptClass_data()
900 defineStandardTestValues();
903 void tst_QJSValue::scriptClass()
905 QFETCH(QJSValue, val);
911 void tst_QJSValue::setScriptClass()
914 QJSValue obj = m_engine->newObject();
915 QJSClass cls(m_engine);
917 obj.setScriptClass(&cls);
921 void tst_QJSValue::readMetaProperty()
924 QJSValue object = m_engine->newQObject(QCoreApplication::instance());
925 QJSString propertyName = m_engine->toStringHandle("objectName");
927 for (int i = 0; i < 10000; ++i)
928 object.property(propertyName);
932 void tst_QJSValue::writeMetaProperty()
935 QJSValue object = m_engine->newQObject(QCoreApplication::instance());
936 QJSString propertyName = m_engine->toStringHandle("objectName");
937 QJSValue value(m_engine, "foo");
939 for (int i = 0; i < 10000; ++i)
940 object.setProperty(propertyName, value);
945 void tst_QJSValue::defineStandardTestValues()
948 QTest::addColumn<QJSValue>("val");
949 QTest::newRow("bool") << m_engine->evaluate("true");
950 QTest::newRow("number") << m_engine->evaluate("123");
951 QTest::newRow("string") << m_engine->evaluate("'ciao'");
952 QTest::newRow("null") << m_engine->evaluate("null");
953 QTest::newRow("undefined") << m_engine->evaluate("undefined");
954 QTest::newRow("object") << m_engine->evaluate("({foo:123})");
955 QTest::newRow("array") << m_engine->evaluate("[10,20,30]");
956 QTest::newRow("function") << m_engine->evaluate("(function foo(a, b, c) { return a + b + c; })");
957 QTest::newRow("date") << m_engine->evaluate("new Date");
958 QTest::newRow("regexp") << m_engine->evaluate("new RegExp('foo')");
959 QTest::newRow("error") << m_engine->evaluate("new Error");
961 QTest::newRow("qobject") << m_engine->newQObject(this);
962 #if 0 // no qmetaobject
963 QTest::newRow("qmetaobject") << m_engine->newQMetaObject(&QJSEngine::staticMetaObject);
965 QTest::newRow("variant") << m_engine->toScriptValue(QPoint(10, 20));
967 QTest::newRow("qscriptclassobject") << m_engine->newObject(new QJSClass(m_engine));
970 QTest::newRow("invalid") << QJSValue();
971 QTest::newRow("bool-no-engine") << QJSValue(true);
972 QTest::newRow("number-no-engine") << QJSValue(123.0);
973 QTest::newRow("string-no-engine") << QJSValue(QString::fromLatin1("hello"));
974 QTest::newRow("null-no-engine") << QJSValue(QJSValue::NullValue);
975 QTest::newRow("undefined-no-engine") << QJSValue(QJSValue::UndefinedValue);
978 QTEST_MAIN(tst_QJSValue)
979 #include "tst_qjsvalue.moc"