|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 #include "tst_qscriptvalue.h" |
|
43 #include <QtGui/QPushButton> |
|
44 |
|
45 //TESTED_CLASS= |
|
46 //TESTED_FILES= |
|
47 |
|
48 QT_BEGIN_NAMESPACE |
|
49 extern bool qt_script_isJITEnabled(); |
|
50 QT_END_NAMESPACE |
|
51 |
|
52 tst_QScriptValue::tst_QScriptValue() |
|
53 : engine(0) |
|
54 { |
|
55 } |
|
56 |
|
57 tst_QScriptValue::~tst_QScriptValue() |
|
58 { |
|
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) |
|
115 |
|
116 void tst_QScriptValue::ctor() |
|
117 { |
|
118 QScriptEngine eng; |
|
119 { |
|
120 QScriptValue v; |
|
121 QCOMPARE(v.isValid(), false); |
|
122 QCOMPARE(v.engine(), (QScriptEngine *)0); |
|
123 } |
|
124 { |
|
125 QScriptValue v(&eng, QScriptValue::UndefinedValue); |
|
126 QCOMPARE(v.isValid(), true); |
|
127 QCOMPARE(v.isUndefined(), true); |
|
128 QCOMPARE(v.isObject(), false); |
|
129 QCOMPARE(v.engine(), &eng); |
|
130 } |
|
131 { |
|
132 QScriptValue v(&eng, QScriptValue::NullValue); |
|
133 QCOMPARE(v.isValid(), true); |
|
134 QCOMPARE(v.isNull(), true); |
|
135 QCOMPARE(v.isObject(), false); |
|
136 QCOMPARE(v.engine(), &eng); |
|
137 } |
|
138 { |
|
139 QScriptValue v(&eng, false); |
|
140 QCOMPARE(v.isValid(), true); |
|
141 QCOMPARE(v.isBoolean(), true); |
|
142 QCOMPARE(v.isBool(), true); |
|
143 QCOMPARE(v.isObject(), false); |
|
144 QCOMPARE(v.toBoolean(), false); |
|
145 QCOMPARE(v.engine(), &eng); |
|
146 } |
|
147 { |
|
148 QScriptValue v(&eng, int(1)); |
|
149 QCOMPARE(v.isValid(), true); |
|
150 QCOMPARE(v.isNumber(), true); |
|
151 QCOMPARE(v.isObject(), false); |
|
152 QCOMPARE(v.toNumber(), 1.0); |
|
153 QCOMPARE(v.engine(), &eng); |
|
154 } |
|
155 { |
|
156 QScriptValue v(int(0x43211234)); |
|
157 QVERIFY(v.isNumber()); |
|
158 QCOMPARE(v.toInt32(), 0x43211234); |
|
159 } |
|
160 { |
|
161 QScriptValue v(&eng, uint(1)); |
|
162 QCOMPARE(v.isValid(), true); |
|
163 QCOMPARE(v.isNumber(), true); |
|
164 QCOMPARE(v.isObject(), false); |
|
165 QCOMPARE(v.toNumber(), 1.0); |
|
166 QCOMPARE(v.engine(), &eng); |
|
167 } |
|
168 { |
|
169 QScriptValue v(uint(0x43211234)); |
|
170 QVERIFY(v.isNumber()); |
|
171 QCOMPARE(v.toUInt32(), uint(0x43211234)); |
|
172 } |
|
173 { |
|
174 QScriptValue v(&eng, 1.0); |
|
175 QCOMPARE(v.isValid(), true); |
|
176 QCOMPARE(v.isNumber(), true); |
|
177 QCOMPARE(v.isObject(), false); |
|
178 QCOMPARE(v.toNumber(), 1.0); |
|
179 QCOMPARE(v.engine(), &eng); |
|
180 } |
|
181 { |
|
182 QScriptValue v(12345678910.5); |
|
183 QVERIFY(v.isNumber()); |
|
184 QCOMPARE(v.toNumber(), 12345678910.5); |
|
185 } |
|
186 { |
|
187 QScriptValue v(&eng, "ciao"); |
|
188 QCOMPARE(v.isValid(), true); |
|
189 QCOMPARE(v.isString(), true); |
|
190 QCOMPARE(v.isObject(), false); |
|
191 QCOMPARE(v.toString(), QLatin1String("ciao")); |
|
192 QCOMPARE(v.engine(), &eng); |
|
193 } |
|
194 { |
|
195 QScriptValue v(&eng, QString("ciao")); |
|
196 QCOMPARE(v.isValid(), true); |
|
197 QCOMPARE(v.isString(), true); |
|
198 QCOMPARE(v.isObject(), false); |
|
199 QCOMPARE(v.toString(), QLatin1String("ciao")); |
|
200 QCOMPARE(v.engine(), &eng); |
|
201 } |
|
202 // copy constructor, operator= |
|
203 { |
|
204 QScriptValue v(&eng, 1.0); |
|
205 QScriptValue v2(v); |
|
206 QCOMPARE(v2.strictlyEquals(v), true); |
|
207 QCOMPARE(v2.engine(), &eng); |
|
208 |
|
209 QScriptValue v3(v); |
|
210 QCOMPARE(v3.strictlyEquals(v), true); |
|
211 QCOMPARE(v3.strictlyEquals(v2), true); |
|
212 QCOMPARE(v3.engine(), &eng); |
|
213 |
|
214 QScriptValue v4(&eng, 2.0); |
|
215 QCOMPARE(v4.strictlyEquals(v), false); |
|
216 v3 = v4; |
|
217 QCOMPARE(v3.strictlyEquals(v), false); |
|
218 QCOMPARE(v3.strictlyEquals(v4), true); |
|
219 |
|
220 v2 = QScriptValue(); |
|
221 QCOMPARE(v2.strictlyEquals(v), false); |
|
222 QCOMPARE(v.toNumber(), 1.0); |
|
223 |
|
224 QScriptValue v5(v); |
|
225 QCOMPARE(v5.strictlyEquals(v), true); |
|
226 v = QScriptValue(); |
|
227 QCOMPARE(v5.strictlyEquals(v), false); |
|
228 QCOMPARE(v5.toNumber(), 1.0); |
|
229 } |
|
230 |
|
231 // constructors that take no engine argument |
|
232 { |
|
233 QScriptValue v(QScriptValue::UndefinedValue); |
|
234 QCOMPARE(v.isValid(), true); |
|
235 QCOMPARE(v.isUndefined(), true); |
|
236 QCOMPARE(v.isObject(), false); |
|
237 QCOMPARE(v.engine(), (QScriptEngine *)0); |
|
238 } |
|
239 { |
|
240 QScriptValue v(QScriptValue::NullValue); |
|
241 QCOMPARE(v.isValid(), true); |
|
242 QCOMPARE(v.isNull(), true); |
|
243 QCOMPARE(v.isObject(), false); |
|
244 QCOMPARE(v.engine(), (QScriptEngine *)0); |
|
245 } |
|
246 { |
|
247 QScriptValue v(false); |
|
248 QCOMPARE(v.isValid(), true); |
|
249 QCOMPARE(v.isBoolean(), true); |
|
250 QCOMPARE(v.isBool(), true); |
|
251 QCOMPARE(v.isObject(), false); |
|
252 QCOMPARE(v.toBoolean(), false); |
|
253 QCOMPARE(v.engine(), (QScriptEngine *)0); |
|
254 } |
|
255 { |
|
256 QScriptValue v(int(1)); |
|
257 QCOMPARE(v.isValid(), true); |
|
258 QCOMPARE(v.isNumber(), true); |
|
259 QCOMPARE(v.isObject(), false); |
|
260 QCOMPARE(v.toNumber(), 1.0); |
|
261 QCOMPARE(v.engine(), (QScriptEngine *)0); |
|
262 } |
|
263 { |
|
264 QScriptValue v(uint(1)); |
|
265 QCOMPARE(v.isValid(), true); |
|
266 QCOMPARE(v.isNumber(), true); |
|
267 QCOMPARE(v.isObject(), false); |
|
268 QCOMPARE(v.toNumber(), 1.0); |
|
269 QCOMPARE(v.engine(), (QScriptEngine *)0); |
|
270 } |
|
271 { |
|
272 QScriptValue v(1.0); |
|
273 QCOMPARE(v.isValid(), true); |
|
274 QCOMPARE(v.isNumber(), true); |
|
275 QCOMPARE(v.isObject(), false); |
|
276 QCOMPARE(v.toNumber(), 1.0); |
|
277 QCOMPARE(v.engine(), (QScriptEngine *)0); |
|
278 } |
|
279 { |
|
280 QScriptValue v("ciao"); |
|
281 QCOMPARE(v.isValid(), true); |
|
282 QCOMPARE(v.isString(), true); |
|
283 QCOMPARE(v.isObject(), false); |
|
284 QCOMPARE(v.toString(), QLatin1String("ciao")); |
|
285 QCOMPARE(v.engine(), (QScriptEngine *)0); |
|
286 } |
|
287 { |
|
288 QScriptValue v(QString("ciao")); |
|
289 QCOMPARE(v.isValid(), true); |
|
290 QCOMPARE(v.isString(), true); |
|
291 QCOMPARE(v.isObject(), false); |
|
292 QCOMPARE(v.toString(), QLatin1String("ciao")); |
|
293 QCOMPARE(v.engine(), (QScriptEngine *)0); |
|
294 } |
|
295 // copy constructor, operator= |
|
296 { |
|
297 QScriptValue v(1.0); |
|
298 QScriptValue v2(v); |
|
299 QCOMPARE(v2.strictlyEquals(v), true); |
|
300 QCOMPARE(v2.engine(), (QScriptEngine *)0); |
|
301 |
|
302 QScriptValue v3(v); |
|
303 QCOMPARE(v3.strictlyEquals(v), true); |
|
304 QCOMPARE(v3.strictlyEquals(v2), true); |
|
305 QCOMPARE(v3.engine(), (QScriptEngine *)0); |
|
306 |
|
307 QScriptValue v4(2.0); |
|
308 QCOMPARE(v4.strictlyEquals(v), false); |
|
309 v3 = v4; |
|
310 QCOMPARE(v3.strictlyEquals(v), false); |
|
311 QCOMPARE(v3.strictlyEquals(v4), true); |
|
312 |
|
313 v2 = QScriptValue(); |
|
314 QCOMPARE(v2.strictlyEquals(v), false); |
|
315 QCOMPARE(v.toNumber(), 1.0); |
|
316 |
|
317 QScriptValue v5(v); |
|
318 QCOMPARE(v5.strictlyEquals(v), true); |
|
319 v = QScriptValue(); |
|
320 QCOMPARE(v5.strictlyEquals(v), false); |
|
321 QCOMPARE(v5.toNumber(), 1.0); |
|
322 } |
|
323 |
|
324 // 0 engine |
|
325 QVERIFY(QScriptValue(0, QScriptValue::UndefinedValue).isUndefined()); |
|
326 QVERIFY(QScriptValue(0, QScriptValue::NullValue).isNull()); |
|
327 QVERIFY(QScriptValue(0, false).isBool()); |
|
328 QVERIFY(QScriptValue(0, int(1)).isNumber()); |
|
329 QVERIFY(QScriptValue(0, uint(1)).isNumber()); |
|
330 QVERIFY(QScriptValue(0, 1.0).isNumber()); |
|
331 QVERIFY(QScriptValue(0, "ciao").isString()); |
|
332 QVERIFY(QScriptValue(0, QString("ciao")).isString()); |
|
333 } |
|
334 |
|
335 static QScriptValue myFunction(QScriptContext *, QScriptEngine *eng) |
|
336 { |
|
337 return eng->undefinedValue(); |
|
338 } |
|
339 |
|
340 void tst_QScriptValue::toString_old() |
|
341 { |
|
342 QScriptEngine eng; |
|
343 |
|
344 QScriptValue undefined = eng.undefinedValue(); |
|
345 QCOMPARE(undefined.toString(), QString("undefined")); |
|
346 QCOMPARE(qscriptvalue_cast<QString>(undefined), QString()); |
|
347 |
|
348 QScriptValue null = eng.nullValue(); |
|
349 QCOMPARE(null.toString(), QString("null")); |
|
350 QCOMPARE(qscriptvalue_cast<QString>(null), QString()); |
|
351 |
|
352 { |
|
353 QScriptValue falskt = QScriptValue(&eng, false); |
|
354 QCOMPARE(falskt.toString(), QString("false")); |
|
355 QCOMPARE(qscriptvalue_cast<QString>(falskt), QString("false")); |
|
356 |
|
357 QScriptValue sant = QScriptValue(&eng, true); |
|
358 QCOMPARE(sant.toString(), QString("true")); |
|
359 QCOMPARE(qscriptvalue_cast<QString>(sant), QString("true")); |
|
360 } |
|
361 { |
|
362 QScriptValue number = QScriptValue(&eng, 123); |
|
363 QCOMPARE(number.toString(), QString("123")); |
|
364 QCOMPARE(qscriptvalue_cast<QString>(number), QString("123")); |
|
365 } |
|
366 { |
|
367 QScriptValue number = QScriptValue(&eng, 6.37e-8); |
|
368 QCOMPARE(number.toString(), QString("6.37e-8")); |
|
369 } |
|
370 { |
|
371 QScriptValue number = QScriptValue(&eng, -6.37e-8); |
|
372 QCOMPARE(number.toString(), QString("-6.37e-8")); |
|
373 |
|
374 QScriptValue str = QScriptValue(&eng, QString("ciao")); |
|
375 QCOMPARE(str.toString(), QString("ciao")); |
|
376 QCOMPARE(qscriptvalue_cast<QString>(str), QString("ciao")); |
|
377 } |
|
378 |
|
379 QScriptValue object = eng.newObject(); |
|
380 QCOMPARE(object.toString(), QString("[object Object]")); |
|
381 QCOMPARE(qscriptvalue_cast<QString>(object), QString("[object Object]")); |
|
382 |
|
383 QScriptValue fun = eng.newFunction(myFunction); |
|
384 QCOMPARE(fun.toString(), QString("function () {\n [native code]\n}")); |
|
385 QCOMPARE(qscriptvalue_cast<QString>(fun), QString("function () {\n [native code]\n}")); |
|
386 |
|
387 // toString() that throws exception |
|
388 { |
|
389 QScriptValue objectObject = eng.evaluate( |
|
390 "(function(){" |
|
391 " o = { };" |
|
392 " o.toString = function() { throw new Error('toString'); };" |
|
393 " return o;" |
|
394 "})()"); |
|
395 QCOMPARE(objectObject.toString(), QLatin1String("Error: toString")); |
|
396 QVERIFY(eng.hasUncaughtException()); |
|
397 QCOMPARE(eng.uncaughtException().toString(), QLatin1String("Error: toString")); |
|
398 } |
|
399 { |
|
400 eng.clearExceptions(); |
|
401 QScriptValue objectObject = eng.evaluate( |
|
402 "(function(){" |
|
403 " var f = function() {};" |
|
404 " f.prototype = Date;" |
|
405 " return new f;" |
|
406 "})()"); |
|
407 QVERIFY(!eng.hasUncaughtException()); |
|
408 QVERIFY(objectObject.isObject()); |
|
409 QCOMPARE(objectObject.toString(), QString::fromLatin1("TypeError: Function.prototype.toString called on incompatible object")); |
|
410 QVERIFY(eng.hasUncaughtException()); |
|
411 eng.clearExceptions(); |
|
412 } |
|
413 |
|
414 QScriptValue inv = QScriptValue(); |
|
415 QCOMPARE(inv.toString(), QString()); |
|
416 |
|
417 // V2 constructors |
|
418 { |
|
419 QScriptValue falskt = QScriptValue(false); |
|
420 QCOMPARE(falskt.toString(), QString("false")); |
|
421 QCOMPARE(qscriptvalue_cast<QString>(falskt), QString("false")); |
|
422 |
|
423 QScriptValue sant = QScriptValue(true); |
|
424 QCOMPARE(sant.toString(), QString("true")); |
|
425 QCOMPARE(qscriptvalue_cast<QString>(sant), QString("true")); |
|
426 |
|
427 QScriptValue number = QScriptValue(123); |
|
428 QCOMPARE(number.toString(), QString("123")); |
|
429 QCOMPARE(qscriptvalue_cast<QString>(number), QString("123")); |
|
430 |
|
431 QScriptValue number2(int(0x43211234)); |
|
432 QCOMPARE(number2.toString(), QString("1126240820")); |
|
433 |
|
434 QScriptValue str = QScriptValue(QString("ciao")); |
|
435 QCOMPARE(str.toString(), QString("ciao")); |
|
436 QCOMPARE(qscriptvalue_cast<QString>(str), QString("ciao")); |
|
437 } |
|
438 |
|
439 // variant should use internal valueOf(), then fall back to QVariant::toString(), |
|
440 // then fall back to "QVariant(typename)" |
|
441 QScriptValue variant = eng.newVariant(123); |
|
442 QVERIFY(variant.isVariant()); |
|
443 QCOMPARE(variant.toString(), QString::fromLatin1("123")); |
|
444 variant = eng.newVariant(QByteArray("hello")); |
|
445 QVERIFY(variant.isVariant()); |
|
446 QCOMPARE(variant.toString(), QString::fromLatin1("hello")); |
|
447 variant = eng.newVariant(QVariant(QPoint(10, 20))); |
|
448 QVERIFY(variant.isVariant()); |
|
449 QCOMPARE(variant.toString(), QString::fromLatin1("QVariant(QPoint)")); |
|
450 variant = eng.newVariant(QUrl()); |
|
451 QVERIFY(variant.toString().isEmpty()); |
|
452 } |
|
453 |
|
454 void tst_QScriptValue::toNumber_old() |
|
455 { |
|
456 QScriptEngine eng; |
|
457 |
|
458 QScriptValue undefined = eng.undefinedValue(); |
|
459 QCOMPARE(qIsNaN(undefined.toNumber()), true); |
|
460 QCOMPARE(qIsNaN(qscriptvalue_cast<qsreal>(undefined)), true); |
|
461 |
|
462 QScriptValue null = eng.nullValue(); |
|
463 QCOMPARE(null.toNumber(), 0.0); |
|
464 QCOMPARE(qscriptvalue_cast<qsreal>(null), 0.0); |
|
465 |
|
466 { |
|
467 QScriptValue falskt = QScriptValue(&eng, false); |
|
468 QCOMPARE(falskt.toNumber(), 0.0); |
|
469 QCOMPARE(qscriptvalue_cast<qsreal>(falskt), 0.0); |
|
470 |
|
471 QScriptValue sant = QScriptValue(&eng, true); |
|
472 QCOMPARE(sant.toNumber(), 1.0); |
|
473 QCOMPARE(qscriptvalue_cast<qsreal>(sant), 1.0); |
|
474 |
|
475 QScriptValue number = QScriptValue(&eng, 123.0); |
|
476 QCOMPARE(number.toNumber(), 123.0); |
|
477 QCOMPARE(qscriptvalue_cast<qsreal>(number), 123.0); |
|
478 |
|
479 QScriptValue str = QScriptValue(&eng, QString("ciao")); |
|
480 QCOMPARE(qIsNaN(str.toNumber()), true); |
|
481 QCOMPARE(qIsNaN(qscriptvalue_cast<qsreal>(str)), true); |
|
482 |
|
483 QScriptValue str2 = QScriptValue(&eng, QString("123")); |
|
484 QCOMPARE(str2.toNumber(), 123.0); |
|
485 QCOMPARE(qscriptvalue_cast<qsreal>(str2), 123.0); |
|
486 } |
|
487 |
|
488 QScriptValue object = eng.newObject(); |
|
489 QCOMPARE(qIsNaN(object.toNumber()), true); |
|
490 QCOMPARE(qIsNaN(qscriptvalue_cast<qsreal>(object)), true); |
|
491 |
|
492 QScriptValue fun = eng.newFunction(myFunction); |
|
493 QCOMPARE(qIsNaN(fun.toNumber()), true); |
|
494 QCOMPARE(qIsNaN(qscriptvalue_cast<qsreal>(fun)), true); |
|
495 |
|
496 QScriptValue inv = QScriptValue(); |
|
497 QCOMPARE(inv.toNumber(), 0.0); |
|
498 QCOMPARE(qscriptvalue_cast<qsreal>(inv), 0.0); |
|
499 |
|
500 // V2 constructors |
|
501 { |
|
502 QScriptValue falskt = QScriptValue(false); |
|
503 QCOMPARE(falskt.toNumber(), 0.0); |
|
504 QCOMPARE(qscriptvalue_cast<qsreal>(falskt), 0.0); |
|
505 |
|
506 QScriptValue sant = QScriptValue(true); |
|
507 QCOMPARE(sant.toNumber(), 1.0); |
|
508 QCOMPARE(qscriptvalue_cast<qsreal>(sant), 1.0); |
|
509 |
|
510 QScriptValue number = QScriptValue(123.0); |
|
511 QCOMPARE(number.toNumber(), 123.0); |
|
512 QCOMPARE(qscriptvalue_cast<qsreal>(number), 123.0); |
|
513 |
|
514 QScriptValue number2(int(0x43211234)); |
|
515 QCOMPARE(number2.toNumber(), 1126240820.0); |
|
516 |
|
517 QScriptValue str = QScriptValue(QString("ciao")); |
|
518 QCOMPARE(qIsNaN(str.toNumber()), true); |
|
519 QCOMPARE(qIsNaN(qscriptvalue_cast<qsreal>(str)), true); |
|
520 |
|
521 QScriptValue str2 = QScriptValue(QString("123")); |
|
522 QCOMPARE(str2.toNumber(), 123.0); |
|
523 QCOMPARE(qscriptvalue_cast<qsreal>(str2), 123.0); |
|
524 } |
|
525 } |
|
526 |
|
527 void tst_QScriptValue::toBoolean_old() // deprecated |
|
528 { |
|
529 QScriptEngine eng; |
|
530 |
|
531 QScriptValue undefined = eng.undefinedValue(); |
|
532 QCOMPARE(undefined.toBoolean(), false); |
|
533 QCOMPARE(qscriptvalue_cast<bool>(undefined), false); |
|
534 |
|
535 QScriptValue null = eng.nullValue(); |
|
536 QCOMPARE(null.toBoolean(), false); |
|
537 QCOMPARE(qscriptvalue_cast<bool>(null), false); |
|
538 |
|
539 { |
|
540 QScriptValue falskt = QScriptValue(&eng, false); |
|
541 QCOMPARE(falskt.toBoolean(), false); |
|
542 QCOMPARE(qscriptvalue_cast<bool>(falskt), false); |
|
543 |
|
544 QScriptValue sant = QScriptValue(&eng, true); |
|
545 QCOMPARE(sant.toBoolean(), true); |
|
546 QCOMPARE(qscriptvalue_cast<bool>(sant), true); |
|
547 |
|
548 QScriptValue number = QScriptValue(&eng, 0.0); |
|
549 QCOMPARE(number.toBoolean(), false); |
|
550 QCOMPARE(qscriptvalue_cast<bool>(number), false); |
|
551 |
|
552 QScriptValue number2 = QScriptValue(&eng, qSNaN()); |
|
553 QCOMPARE(number2.toBoolean(), false); |
|
554 QCOMPARE(qscriptvalue_cast<bool>(number2), false); |
|
555 |
|
556 QScriptValue number3 = QScriptValue(&eng, 123.0); |
|
557 QCOMPARE(number3.toBoolean(), true); |
|
558 QCOMPARE(qscriptvalue_cast<bool>(number3), true); |
|
559 |
|
560 QScriptValue number4 = QScriptValue(&eng, -456.0); |
|
561 QCOMPARE(number4.toBoolean(), true); |
|
562 QCOMPARE(qscriptvalue_cast<bool>(number4), true); |
|
563 |
|
564 QScriptValue str = QScriptValue(&eng, QString("")); |
|
565 QCOMPARE(str.toBoolean(), false); |
|
566 QCOMPARE(qscriptvalue_cast<bool>(str), false); |
|
567 |
|
568 QScriptValue str2 = QScriptValue(&eng, QString("123")); |
|
569 QCOMPARE(str2.toBoolean(), true); |
|
570 QCOMPARE(qscriptvalue_cast<bool>(str2), true); |
|
571 } |
|
572 |
|
573 QScriptValue object = eng.newObject(); |
|
574 QCOMPARE(object.toBoolean(), true); |
|
575 QCOMPARE(qscriptvalue_cast<bool>(object), true); |
|
576 |
|
577 QScriptValue fun = eng.newFunction(myFunction); |
|
578 QCOMPARE(fun.toBoolean(), true); |
|
579 QCOMPARE(qscriptvalue_cast<bool>(fun), true); |
|
580 |
|
581 QScriptValue inv = QScriptValue(); |
|
582 QCOMPARE(inv.toBoolean(), false); |
|
583 QCOMPARE(qscriptvalue_cast<bool>(inv), false); |
|
584 |
|
585 // V2 constructors |
|
586 { |
|
587 QScriptValue falskt = QScriptValue(false); |
|
588 QCOMPARE(falskt.toBoolean(), false); |
|
589 QCOMPARE(qscriptvalue_cast<bool>(falskt), false); |
|
590 |
|
591 QScriptValue sant = QScriptValue(true); |
|
592 QCOMPARE(sant.toBoolean(), true); |
|
593 QCOMPARE(qscriptvalue_cast<bool>(sant), true); |
|
594 |
|
595 QScriptValue number = QScriptValue(0.0); |
|
596 QCOMPARE(number.toBoolean(), false); |
|
597 QCOMPARE(qscriptvalue_cast<bool>(number), false); |
|
598 |
|
599 QScriptValue number2 = QScriptValue(qSNaN()); |
|
600 QCOMPARE(number2.toBoolean(), false); |
|
601 QCOMPARE(qscriptvalue_cast<bool>(number2), false); |
|
602 |
|
603 QScriptValue number3 = QScriptValue(123.0); |
|
604 QCOMPARE(number3.toBoolean(), true); |
|
605 QCOMPARE(qscriptvalue_cast<bool>(number3), true); |
|
606 |
|
607 QScriptValue number4 = QScriptValue(-456.0); |
|
608 QCOMPARE(number4.toBoolean(), true); |
|
609 QCOMPARE(qscriptvalue_cast<bool>(number4), true); |
|
610 |
|
611 QScriptValue number5 = QScriptValue(0x43211234); |
|
612 QCOMPARE(number5.toBoolean(), true); |
|
613 |
|
614 QScriptValue str = QScriptValue(QString("")); |
|
615 QCOMPARE(str.toBoolean(), false); |
|
616 QCOMPARE(qscriptvalue_cast<bool>(str), false); |
|
617 |
|
618 QScriptValue str2 = QScriptValue(QString("123")); |
|
619 QCOMPARE(str2.toBoolean(), true); |
|
620 QCOMPARE(qscriptvalue_cast<bool>(str2), true); |
|
621 } |
|
622 } |
|
623 |
|
624 void tst_QScriptValue::toBool_old() |
|
625 { |
|
626 QScriptEngine eng; |
|
627 |
|
628 QScriptValue undefined = eng.undefinedValue(); |
|
629 QCOMPARE(undefined.toBool(), false); |
|
630 QCOMPARE(qscriptvalue_cast<bool>(undefined), false); |
|
631 |
|
632 QScriptValue null = eng.nullValue(); |
|
633 QCOMPARE(null.toBool(), false); |
|
634 QCOMPARE(qscriptvalue_cast<bool>(null), false); |
|
635 |
|
636 { |
|
637 QScriptValue falskt = QScriptValue(&eng, false); |
|
638 QCOMPARE(falskt.toBool(), false); |
|
639 QCOMPARE(qscriptvalue_cast<bool>(falskt), false); |
|
640 |
|
641 QScriptValue sant = QScriptValue(&eng, true); |
|
642 QCOMPARE(sant.toBool(), true); |
|
643 QCOMPARE(qscriptvalue_cast<bool>(sant), true); |
|
644 |
|
645 QScriptValue number = QScriptValue(&eng, 0.0); |
|
646 QCOMPARE(number.toBool(), false); |
|
647 QCOMPARE(qscriptvalue_cast<bool>(number), false); |
|
648 |
|
649 QScriptValue number2 = QScriptValue(&eng, qSNaN()); |
|
650 QCOMPARE(number2.toBool(), false); |
|
651 QCOMPARE(qscriptvalue_cast<bool>(number2), false); |
|
652 |
|
653 QScriptValue number3 = QScriptValue(&eng, 123.0); |
|
654 QCOMPARE(number3.toBool(), true); |
|
655 QCOMPARE(qscriptvalue_cast<bool>(number3), true); |
|
656 |
|
657 QScriptValue number4 = QScriptValue(&eng, -456.0); |
|
658 QCOMPARE(number4.toBool(), true); |
|
659 QCOMPARE(qscriptvalue_cast<bool>(number4), true); |
|
660 |
|
661 QScriptValue str = QScriptValue(&eng, QString("")); |
|
662 QCOMPARE(str.toBool(), false); |
|
663 QCOMPARE(qscriptvalue_cast<bool>(str), false); |
|
664 |
|
665 QScriptValue str2 = QScriptValue(&eng, QString("123")); |
|
666 QCOMPARE(str2.toBool(), true); |
|
667 QCOMPARE(qscriptvalue_cast<bool>(str2), true); |
|
668 } |
|
669 |
|
670 QScriptValue object = eng.newObject(); |
|
671 QCOMPARE(object.toBool(), true); |
|
672 QCOMPARE(qscriptvalue_cast<bool>(object), true); |
|
673 |
|
674 QScriptValue fun = eng.newFunction(myFunction); |
|
675 QCOMPARE(fun.toBool(), true); |
|
676 QCOMPARE(qscriptvalue_cast<bool>(fun), true); |
|
677 |
|
678 QScriptValue inv = QScriptValue(); |
|
679 QCOMPARE(inv.toBool(), false); |
|
680 QCOMPARE(qscriptvalue_cast<bool>(inv), false); |
|
681 |
|
682 // V2 constructors |
|
683 { |
|
684 QScriptValue falskt = QScriptValue(false); |
|
685 QCOMPARE(falskt.toBool(), false); |
|
686 QCOMPARE(qscriptvalue_cast<bool>(falskt), false); |
|
687 |
|
688 QScriptValue sant = QScriptValue(true); |
|
689 QCOMPARE(sant.toBool(), true); |
|
690 QCOMPARE(qscriptvalue_cast<bool>(sant), true); |
|
691 |
|
692 QScriptValue number = QScriptValue(0.0); |
|
693 QCOMPARE(number.toBool(), false); |
|
694 QCOMPARE(qscriptvalue_cast<bool>(number), false); |
|
695 |
|
696 QScriptValue number2 = QScriptValue(qSNaN()); |
|
697 QCOMPARE(number2.toBool(), false); |
|
698 QCOMPARE(qscriptvalue_cast<bool>(number2), false); |
|
699 |
|
700 QScriptValue number3 = QScriptValue(123.0); |
|
701 QCOMPARE(number3.toBool(), true); |
|
702 QCOMPARE(qscriptvalue_cast<bool>(number3), true); |
|
703 |
|
704 QScriptValue number4 = QScriptValue(-456.0); |
|
705 QCOMPARE(number4.toBool(), true); |
|
706 QCOMPARE(qscriptvalue_cast<bool>(number4), true); |
|
707 |
|
708 QScriptValue number5 = QScriptValue(0x43211234); |
|
709 QCOMPARE(number5.toBool(), true); |
|
710 |
|
711 QScriptValue str = QScriptValue(QString("")); |
|
712 QCOMPARE(str.toBool(), false); |
|
713 QCOMPARE(qscriptvalue_cast<bool>(str), false); |
|
714 |
|
715 QScriptValue str2 = QScriptValue(QString("123")); |
|
716 QCOMPARE(str2.toBool(), true); |
|
717 QCOMPARE(qscriptvalue_cast<bool>(str2), true); |
|
718 } |
|
719 } |
|
720 |
|
721 void tst_QScriptValue::toInteger_old() |
|
722 { |
|
723 QScriptEngine eng; |
|
724 |
|
725 { |
|
726 QScriptValue number = QScriptValue(&eng, 123.0); |
|
727 QCOMPARE(number.toInteger(), 123.0); |
|
728 |
|
729 QScriptValue number2 = QScriptValue(&eng, qSNaN()); |
|
730 QCOMPARE(number2.toInteger(), 0.0); |
|
731 |
|
732 QScriptValue number3 = QScriptValue(&eng, qInf()); |
|
733 QCOMPARE(qIsInf(number3.toInteger()), true); |
|
734 |
|
735 QScriptValue number4 = QScriptValue(&eng, 0.5); |
|
736 QCOMPARE(number4.toInteger(), 0.0); |
|
737 |
|
738 QScriptValue number5 = QScriptValue(&eng, 123.5); |
|
739 QCOMPARE(number5.toInteger(), 123.0); |
|
740 |
|
741 QScriptValue number6 = QScriptValue(&eng, -456.5); |
|
742 QCOMPARE(number6.toInteger(), -456.0); |
|
743 |
|
744 QScriptValue str = QScriptValue(&eng, "123.0"); |
|
745 QCOMPARE(str.toInteger(), 123.0); |
|
746 |
|
747 QScriptValue str2 = QScriptValue(&eng, "NaN"); |
|
748 QCOMPARE(str2.toInteger(), 0.0); |
|
749 |
|
750 QScriptValue str3 = QScriptValue(&eng, "Infinity"); |
|
751 QCOMPARE(qIsInf(str3.toInteger()), true); |
|
752 |
|
753 QScriptValue str4 = QScriptValue(&eng, "0.5"); |
|
754 QCOMPARE(str4.toInteger(), 0.0); |
|
755 |
|
756 QScriptValue str5 = QScriptValue(&eng, "123.5"); |
|
757 QCOMPARE(str5.toInteger(), 123.0); |
|
758 |
|
759 QScriptValue str6 = QScriptValue(&eng, "-456.5"); |
|
760 QCOMPARE(str6.toInteger(), -456.0); |
|
761 } |
|
762 // V2 constructors |
|
763 { |
|
764 QScriptValue number = QScriptValue(123.0); |
|
765 QCOMPARE(number.toInteger(), 123.0); |
|
766 |
|
767 QScriptValue number2 = QScriptValue(qSNaN()); |
|
768 QCOMPARE(number2.toInteger(), 0.0); |
|
769 |
|
770 QScriptValue number3 = QScriptValue(qInf()); |
|
771 QCOMPARE(qIsInf(number3.toInteger()), true); |
|
772 |
|
773 QScriptValue number4 = QScriptValue(0.5); |
|
774 QCOMPARE(number4.toInteger(), 0.0); |
|
775 |
|
776 QScriptValue number5 = QScriptValue(123.5); |
|
777 QCOMPARE(number5.toInteger(), 123.0); |
|
778 |
|
779 QScriptValue number6 = QScriptValue(-456.5); |
|
780 QCOMPARE(number6.toInteger(), -456.0); |
|
781 |
|
782 QScriptValue number7 = QScriptValue(0x43211234); |
|
783 QCOMPARE(number7.toInteger(), qsreal(0x43211234)); |
|
784 |
|
785 QScriptValue str = QScriptValue("123.0"); |
|
786 QCOMPARE(str.toInteger(), 123.0); |
|
787 |
|
788 QScriptValue str2 = QScriptValue("NaN"); |
|
789 QCOMPARE(str2.toInteger(), 0.0); |
|
790 |
|
791 QScriptValue str3 = QScriptValue("Infinity"); |
|
792 QCOMPARE(qIsInf(str3.toInteger()), true); |
|
793 |
|
794 QScriptValue str4 = QScriptValue("0.5"); |
|
795 QCOMPARE(str4.toInteger(), 0.0); |
|
796 |
|
797 QScriptValue str5 = QScriptValue("123.5"); |
|
798 QCOMPARE(str5.toInteger(), 123.0); |
|
799 |
|
800 QScriptValue str6 = QScriptValue("-456.5"); |
|
801 QCOMPARE(str6.toInteger(), -456.0); |
|
802 } |
|
803 |
|
804 QScriptValue inv; |
|
805 QCOMPARE(inv.toInteger(), 0.0); |
|
806 } |
|
807 |
|
808 void tst_QScriptValue::toInt32_old() |
|
809 { |
|
810 QScriptEngine eng; |
|
811 |
|
812 { |
|
813 QScriptValue zer0 = QScriptValue(&eng, 0.0); |
|
814 QCOMPARE(zer0.toInt32(), 0); |
|
815 QCOMPARE(qscriptvalue_cast<qint32>(zer0), 0); |
|
816 |
|
817 QScriptValue number = QScriptValue(&eng, 123.0); |
|
818 QCOMPARE(number.toInt32(), 123); |
|
819 QCOMPARE(qscriptvalue_cast<qint32>(number), 123); |
|
820 |
|
821 QScriptValue number2 = QScriptValue(&eng, qSNaN()); |
|
822 QCOMPARE(number2.toInt32(), 0); |
|
823 QCOMPARE(qscriptvalue_cast<qint32>(number2), 0); |
|
824 |
|
825 QScriptValue number3 = QScriptValue(&eng, +qInf()); |
|
826 QCOMPARE(number3.toInt32(), 0); |
|
827 QCOMPARE(qscriptvalue_cast<qint32>(number3), 0); |
|
828 |
|
829 QScriptValue number3_2 = QScriptValue(&eng, -qInf()); |
|
830 QCOMPARE(number3_2.toInt32(), 0); |
|
831 QCOMPARE(qscriptvalue_cast<qint32>(number3_2), 0); |
|
832 |
|
833 QScriptValue number4 = QScriptValue(&eng, 0.5); |
|
834 QCOMPARE(number4.toInt32(), 0); |
|
835 QCOMPARE(qscriptvalue_cast<qint32>(number4), 0); |
|
836 |
|
837 QScriptValue number5 = QScriptValue(&eng, 123.5); |
|
838 QCOMPARE(number5.toInt32(), 123); |
|
839 QCOMPARE(qscriptvalue_cast<qint32>(number5), 123); |
|
840 |
|
841 QScriptValue number6 = QScriptValue(&eng, -456.5); |
|
842 QCOMPARE(number6.toInt32(), -456); |
|
843 QCOMPARE(qscriptvalue_cast<qint32>(number6), -456); |
|
844 |
|
845 QScriptValue str = QScriptValue(&eng, "123.0"); |
|
846 QCOMPARE(str.toInt32(), 123); |
|
847 QCOMPARE(qscriptvalue_cast<qint32>(str), 123); |
|
848 |
|
849 QScriptValue str2 = QScriptValue(&eng, "NaN"); |
|
850 QCOMPARE(str2.toInt32(), 0); |
|
851 QCOMPARE(qscriptvalue_cast<qint32>(str2), 0); |
|
852 |
|
853 QScriptValue str3 = QScriptValue(&eng, "Infinity"); |
|
854 QCOMPARE(str3.toInt32(), 0); |
|
855 QCOMPARE(qscriptvalue_cast<qint32>(str3), 0); |
|
856 |
|
857 QScriptValue str3_2 = QScriptValue(&eng, "-Infinity"); |
|
858 QCOMPARE(str3_2.toInt32(), 0); |
|
859 QCOMPARE(qscriptvalue_cast<qint32>(str3_2), 0); |
|
860 |
|
861 QScriptValue str4 = QScriptValue(&eng, "0.5"); |
|
862 QCOMPARE(str4.toInt32(), 0); |
|
863 QCOMPARE(qscriptvalue_cast<qint32>(str4), 0); |
|
864 |
|
865 QScriptValue str5 = QScriptValue(&eng, "123.5"); |
|
866 QCOMPARE(str5.toInt32(), 123); |
|
867 QCOMPARE(qscriptvalue_cast<qint32>(str5), 123); |
|
868 |
|
869 QScriptValue str6 = QScriptValue(&eng, "-456.5"); |
|
870 QCOMPARE(str6.toInt32(), -456); |
|
871 QCOMPARE(qscriptvalue_cast<qint32>(str6), -456); |
|
872 } |
|
873 // V2 constructors |
|
874 { |
|
875 QScriptValue zer0 = QScriptValue(0.0); |
|
876 QCOMPARE(zer0.toInt32(), 0); |
|
877 QCOMPARE(qscriptvalue_cast<qint32>(zer0), 0); |
|
878 |
|
879 QScriptValue number = QScriptValue(123.0); |
|
880 QCOMPARE(number.toInt32(), 123); |
|
881 QCOMPARE(qscriptvalue_cast<qint32>(number), 123); |
|
882 |
|
883 QScriptValue number2 = QScriptValue(qSNaN()); |
|
884 QCOMPARE(number2.toInt32(), 0); |
|
885 QCOMPARE(qscriptvalue_cast<qint32>(number2), 0); |
|
886 |
|
887 QScriptValue number3 = QScriptValue(+qInf()); |
|
888 QCOMPARE(number3.toInt32(), 0); |
|
889 QCOMPARE(qscriptvalue_cast<qint32>(number3), 0); |
|
890 |
|
891 QScriptValue number3_2 = QScriptValue(-qInf()); |
|
892 QCOMPARE(number3_2.toInt32(), 0); |
|
893 QCOMPARE(qscriptvalue_cast<qint32>(number3_2), 0); |
|
894 |
|
895 QScriptValue number4 = QScriptValue(0.5); |
|
896 QCOMPARE(number4.toInt32(), 0); |
|
897 QCOMPARE(qscriptvalue_cast<qint32>(number4), 0); |
|
898 |
|
899 QScriptValue number5 = QScriptValue(123.5); |
|
900 QCOMPARE(number5.toInt32(), 123); |
|
901 QCOMPARE(qscriptvalue_cast<qint32>(number5), 123); |
|
902 |
|
903 QScriptValue number6 = QScriptValue(-456.5); |
|
904 QCOMPARE(number6.toInt32(), -456); |
|
905 QCOMPARE(qscriptvalue_cast<qint32>(number6), -456); |
|
906 |
|
907 QScriptValue number7 = QScriptValue(0x43211234); |
|
908 QCOMPARE(number7.toInt32(), 0x43211234); |
|
909 |
|
910 QScriptValue str = QScriptValue("123.0"); |
|
911 QCOMPARE(str.toInt32(), 123); |
|
912 QCOMPARE(qscriptvalue_cast<qint32>(str), 123); |
|
913 |
|
914 QScriptValue str2 = QScriptValue("NaN"); |
|
915 QCOMPARE(str2.toInt32(), 0); |
|
916 QCOMPARE(qscriptvalue_cast<qint32>(str2), 0); |
|
917 |
|
918 QScriptValue str3 = QScriptValue("Infinity"); |
|
919 QCOMPARE(str3.toInt32(), 0); |
|
920 QCOMPARE(qscriptvalue_cast<qint32>(str3), 0); |
|
921 |
|
922 QScriptValue str3_2 = QScriptValue("-Infinity"); |
|
923 QCOMPARE(str3_2.toInt32(), 0); |
|
924 QCOMPARE(qscriptvalue_cast<qint32>(str3_2), 0); |
|
925 |
|
926 QScriptValue str4 = QScriptValue("0.5"); |
|
927 QCOMPARE(str4.toInt32(), 0); |
|
928 QCOMPARE(qscriptvalue_cast<qint32>(str4), 0); |
|
929 |
|
930 QScriptValue str5 = QScriptValue("123.5"); |
|
931 QCOMPARE(str5.toInt32(), 123); |
|
932 QCOMPARE(qscriptvalue_cast<qint32>(str5), 123); |
|
933 |
|
934 QScriptValue str6 = QScriptValue("-456.5"); |
|
935 QCOMPARE(str6.toInt32(), -456); |
|
936 QCOMPARE(qscriptvalue_cast<qint32>(str6), -456); |
|
937 } |
|
938 |
|
939 QScriptValue inv; |
|
940 QCOMPARE(inv.toInt32(), 0); |
|
941 QCOMPARE(qscriptvalue_cast<qint32>(inv), 0); |
|
942 } |
|
943 |
|
944 void tst_QScriptValue::toUInt32_old() |
|
945 { |
|
946 QScriptEngine eng; |
|
947 |
|
948 { |
|
949 QScriptValue zer0 = QScriptValue(&eng, 0.0); |
|
950 QCOMPARE(zer0.toUInt32(), quint32(0)); |
|
951 QCOMPARE(qscriptvalue_cast<quint32>(zer0), quint32(0)); |
|
952 |
|
953 QScriptValue number = QScriptValue(&eng, 123.0); |
|
954 QCOMPARE(number.toUInt32(), quint32(123)); |
|
955 QCOMPARE(qscriptvalue_cast<quint32>(number), quint32(123)); |
|
956 |
|
957 QScriptValue number2 = QScriptValue(&eng, qSNaN()); |
|
958 QCOMPARE(number2.toUInt32(), quint32(0)); |
|
959 QCOMPARE(qscriptvalue_cast<quint32>(number2), quint32(0)); |
|
960 |
|
961 QScriptValue number3 = QScriptValue(&eng, +qInf()); |
|
962 QCOMPARE(number3.toUInt32(), quint32(0)); |
|
963 QCOMPARE(qscriptvalue_cast<quint32>(number3), quint32(0)); |
|
964 |
|
965 QScriptValue number3_2 = QScriptValue(&eng, -qInf()); |
|
966 QCOMPARE(number3_2.toUInt32(), quint32(0)); |
|
967 QCOMPARE(qscriptvalue_cast<quint32>(number3_2), quint32(0)); |
|
968 |
|
969 QScriptValue number4 = QScriptValue(&eng, 0.5); |
|
970 QCOMPARE(number4.toUInt32(), quint32(0)); |
|
971 |
|
972 QScriptValue number5 = QScriptValue(&eng, 123.5); |
|
973 QCOMPARE(number5.toUInt32(), quint32(123)); |
|
974 |
|
975 QScriptValue number6 = QScriptValue(&eng, -456.5); |
|
976 QCOMPARE(number6.toUInt32(), quint32(-456)); |
|
977 QCOMPARE(qscriptvalue_cast<quint32>(number6), quint32(-456)); |
|
978 |
|
979 QScriptValue str = QScriptValue(&eng, "123.0"); |
|
980 QCOMPARE(str.toUInt32(), quint32(123)); |
|
981 QCOMPARE(qscriptvalue_cast<quint32>(str), quint32(123)); |
|
982 |
|
983 QScriptValue str2 = QScriptValue(&eng, "NaN"); |
|
984 QCOMPARE(str2.toUInt32(), quint32(0)); |
|
985 QCOMPARE(qscriptvalue_cast<quint32>(str2), quint32(0)); |
|
986 |
|
987 QScriptValue str3 = QScriptValue(&eng, "Infinity"); |
|
988 QCOMPARE(str3.toUInt32(), quint32(0)); |
|
989 QCOMPARE(qscriptvalue_cast<quint32>(str3), quint32(0)); |
|
990 |
|
991 QScriptValue str3_2 = QScriptValue(&eng, "-Infinity"); |
|
992 QCOMPARE(str3_2.toUInt32(), quint32(0)); |
|
993 QCOMPARE(qscriptvalue_cast<quint32>(str3_2), quint32(0)); |
|
994 |
|
995 QScriptValue str4 = QScriptValue(&eng, "0.5"); |
|
996 QCOMPARE(str4.toUInt32(), quint32(0)); |
|
997 QCOMPARE(qscriptvalue_cast<quint32>(str4), quint32(0)); |
|
998 |
|
999 QScriptValue str5 = QScriptValue(&eng, "123.5"); |
|
1000 QCOMPARE(str5.toUInt32(), quint32(123)); |
|
1001 QCOMPARE(qscriptvalue_cast<quint32>(str5), quint32(123)); |
|
1002 |
|
1003 QScriptValue str6 = QScriptValue(&eng, "-456.5"); |
|
1004 QCOMPARE(str6.toUInt32(), quint32(-456)); |
|
1005 QCOMPARE(qscriptvalue_cast<quint32>(str6), quint32(-456)); |
|
1006 } |
|
1007 // V2 constructors |
|
1008 { |
|
1009 QScriptValue zer0 = QScriptValue(0.0); |
|
1010 QCOMPARE(zer0.toUInt32(), quint32(0)); |
|
1011 QCOMPARE(qscriptvalue_cast<quint32>(zer0), quint32(0)); |
|
1012 |
|
1013 QScriptValue number = QScriptValue(123.0); |
|
1014 QCOMPARE(number.toUInt32(), quint32(123)); |
|
1015 QCOMPARE(qscriptvalue_cast<quint32>(number), quint32(123)); |
|
1016 |
|
1017 QScriptValue number2 = QScriptValue(qSNaN()); |
|
1018 QCOMPARE(number2.toUInt32(), quint32(0)); |
|
1019 QCOMPARE(qscriptvalue_cast<quint32>(number2), quint32(0)); |
|
1020 |
|
1021 QScriptValue number3 = QScriptValue(+qInf()); |
|
1022 QCOMPARE(number3.toUInt32(), quint32(0)); |
|
1023 QCOMPARE(qscriptvalue_cast<quint32>(number3), quint32(0)); |
|
1024 |
|
1025 QScriptValue number3_2 = QScriptValue(-qInf()); |
|
1026 QCOMPARE(number3_2.toUInt32(), quint32(0)); |
|
1027 QCOMPARE(qscriptvalue_cast<quint32>(number3_2), quint32(0)); |
|
1028 |
|
1029 QScriptValue number4 = QScriptValue(0.5); |
|
1030 QCOMPARE(number4.toUInt32(), quint32(0)); |
|
1031 |
|
1032 QScriptValue number5 = QScriptValue(123.5); |
|
1033 QCOMPARE(number5.toUInt32(), quint32(123)); |
|
1034 |
|
1035 QScriptValue number6 = QScriptValue(-456.5); |
|
1036 QCOMPARE(number6.toUInt32(), quint32(-456)); |
|
1037 QCOMPARE(qscriptvalue_cast<quint32>(number6), quint32(-456)); |
|
1038 |
|
1039 QScriptValue number7 = QScriptValue(0x43211234); |
|
1040 QCOMPARE(number7.toUInt32(), quint32(0x43211234)); |
|
1041 |
|
1042 QScriptValue str = QScriptValue("123.0"); |
|
1043 QCOMPARE(str.toUInt32(), quint32(123)); |
|
1044 QCOMPARE(qscriptvalue_cast<quint32>(str), quint32(123)); |
|
1045 |
|
1046 QScriptValue str2 = QScriptValue("NaN"); |
|
1047 QCOMPARE(str2.toUInt32(), quint32(0)); |
|
1048 QCOMPARE(qscriptvalue_cast<quint32>(str2), quint32(0)); |
|
1049 |
|
1050 QScriptValue str3 = QScriptValue("Infinity"); |
|
1051 QCOMPARE(str3.toUInt32(), quint32(0)); |
|
1052 QCOMPARE(qscriptvalue_cast<quint32>(str3), quint32(0)); |
|
1053 |
|
1054 QScriptValue str3_2 = QScriptValue("-Infinity"); |
|
1055 QCOMPARE(str3_2.toUInt32(), quint32(0)); |
|
1056 QCOMPARE(qscriptvalue_cast<quint32>(str3_2), quint32(0)); |
|
1057 |
|
1058 QScriptValue str4 = QScriptValue("0.5"); |
|
1059 QCOMPARE(str4.toUInt32(), quint32(0)); |
|
1060 QCOMPARE(qscriptvalue_cast<quint32>(str4), quint32(0)); |
|
1061 |
|
1062 QScriptValue str5 = QScriptValue("123.5"); |
|
1063 QCOMPARE(str5.toUInt32(), quint32(123)); |
|
1064 QCOMPARE(qscriptvalue_cast<quint32>(str5), quint32(123)); |
|
1065 |
|
1066 QScriptValue str6 = QScriptValue("-456.5"); |
|
1067 QCOMPARE(str6.toUInt32(), quint32(-456)); |
|
1068 QCOMPARE(qscriptvalue_cast<quint32>(str6), quint32(-456)); |
|
1069 } |
|
1070 |
|
1071 QScriptValue inv; |
|
1072 QCOMPARE(inv.toUInt32(), quint32(0)); |
|
1073 QCOMPARE(qscriptvalue_cast<quint32>(inv), quint32(0)); |
|
1074 } |
|
1075 |
|
1076 void tst_QScriptValue::toUInt16_old() |
|
1077 { |
|
1078 QScriptEngine eng; |
|
1079 |
|
1080 { |
|
1081 QScriptValue zer0 = QScriptValue(&eng, 0.0); |
|
1082 QCOMPARE(zer0.toUInt16(), quint16(0)); |
|
1083 QCOMPARE(qscriptvalue_cast<quint16>(zer0), quint16(0)); |
|
1084 |
|
1085 QScriptValue number = QScriptValue(&eng, 123.0); |
|
1086 QCOMPARE(number.toUInt16(), quint16(123)); |
|
1087 QCOMPARE(qscriptvalue_cast<quint16>(number), quint16(123)); |
|
1088 |
|
1089 QScriptValue number2 = QScriptValue(&eng, qSNaN()); |
|
1090 QCOMPARE(number2.toUInt16(), quint16(0)); |
|
1091 QCOMPARE(qscriptvalue_cast<quint16>(number2), quint16(0)); |
|
1092 |
|
1093 QScriptValue number3 = QScriptValue(&eng, +qInf()); |
|
1094 QCOMPARE(number3.toUInt16(), quint16(0)); |
|
1095 QCOMPARE(qscriptvalue_cast<quint16>(number3), quint16(0)); |
|
1096 |
|
1097 QScriptValue number3_2 = QScriptValue(&eng, -qInf()); |
|
1098 QCOMPARE(number3_2.toUInt16(), quint16(0)); |
|
1099 QCOMPARE(qscriptvalue_cast<quint16>(number3_2), quint16(0)); |
|
1100 |
|
1101 QScriptValue number4 = QScriptValue(&eng, 0.5); |
|
1102 QCOMPARE(number4.toUInt16(), quint16(0)); |
|
1103 |
|
1104 QScriptValue number5 = QScriptValue(&eng, 123.5); |
|
1105 QCOMPARE(number5.toUInt16(), quint16(123)); |
|
1106 |
|
1107 QScriptValue number6 = QScriptValue(&eng, -456.5); |
|
1108 QCOMPARE(number6.toUInt16(), quint16(-456)); |
|
1109 QCOMPARE(qscriptvalue_cast<quint16>(number6), quint16(-456)); |
|
1110 |
|
1111 QScriptValue number7 = QScriptValue(&eng, 0x10000); |
|
1112 QCOMPARE(number7.toUInt16(), quint16(0)); |
|
1113 QCOMPARE(qscriptvalue_cast<quint16>(number7), quint16(0)); |
|
1114 |
|
1115 QScriptValue number8 = QScriptValue(&eng, 0x10001); |
|
1116 QCOMPARE(number8.toUInt16(), quint16(1)); |
|
1117 QCOMPARE(qscriptvalue_cast<quint16>(number8), quint16(1)); |
|
1118 |
|
1119 QScriptValue str = QScriptValue(&eng, "123.0"); |
|
1120 QCOMPARE(str.toUInt16(), quint16(123)); |
|
1121 QCOMPARE(qscriptvalue_cast<quint16>(str), quint16(123)); |
|
1122 |
|
1123 QScriptValue str2 = QScriptValue(&eng, "NaN"); |
|
1124 QCOMPARE(str2.toUInt16(), quint16(0)); |
|
1125 QCOMPARE(qscriptvalue_cast<quint16>(str2), quint16(0)); |
|
1126 |
|
1127 QScriptValue str3 = QScriptValue(&eng, "Infinity"); |
|
1128 QCOMPARE(str3.toUInt16(), quint16(0)); |
|
1129 QCOMPARE(qscriptvalue_cast<quint16>(str3), quint16(0)); |
|
1130 |
|
1131 QScriptValue str3_2 = QScriptValue(&eng, "-Infinity"); |
|
1132 QCOMPARE(str3_2.toUInt16(), quint16(0)); |
|
1133 QCOMPARE(qscriptvalue_cast<quint16>(str3_2), quint16(0)); |
|
1134 |
|
1135 QScriptValue str4 = QScriptValue(&eng, "0.5"); |
|
1136 QCOMPARE(str4.toUInt16(), quint16(0)); |
|
1137 |
|
1138 QScriptValue str5 = QScriptValue(&eng, "123.5"); |
|
1139 QCOMPARE(str5.toUInt16(), quint16(123)); |
|
1140 |
|
1141 QScriptValue str6 = QScriptValue(&eng, "-456.5"); |
|
1142 QCOMPARE(str6.toUInt16(), quint16(-456)); |
|
1143 QCOMPARE(qscriptvalue_cast<quint16>(str6), quint16(-456)); |
|
1144 |
|
1145 QScriptValue str7 = QScriptValue(&eng, "0x10000"); |
|
1146 QCOMPARE(str7.toUInt16(), quint16(0)); |
|
1147 QCOMPARE(qscriptvalue_cast<quint16>(str7), quint16(0)); |
|
1148 |
|
1149 QScriptValue str8 = QScriptValue(&eng, "0x10001"); |
|
1150 QCOMPARE(str8.toUInt16(), quint16(1)); |
|
1151 QCOMPARE(qscriptvalue_cast<quint16>(str8), quint16(1)); |
|
1152 } |
|
1153 // V2 constructors |
|
1154 { |
|
1155 QScriptValue zer0 = QScriptValue(0.0); |
|
1156 QCOMPARE(zer0.toUInt16(), quint16(0)); |
|
1157 QCOMPARE(qscriptvalue_cast<quint16>(zer0), quint16(0)); |
|
1158 |
|
1159 QScriptValue number = QScriptValue(123.0); |
|
1160 QCOMPARE(number.toUInt16(), quint16(123)); |
|
1161 QCOMPARE(qscriptvalue_cast<quint16>(number), quint16(123)); |
|
1162 |
|
1163 QScriptValue number2 = QScriptValue(qSNaN()); |
|
1164 QCOMPARE(number2.toUInt16(), quint16(0)); |
|
1165 QCOMPARE(qscriptvalue_cast<quint16>(number2), quint16(0)); |
|
1166 |
|
1167 QScriptValue number3 = QScriptValue(+qInf()); |
|
1168 QCOMPARE(number3.toUInt16(), quint16(0)); |
|
1169 QCOMPARE(qscriptvalue_cast<quint16>(number3), quint16(0)); |
|
1170 |
|
1171 QScriptValue number3_2 = QScriptValue(-qInf()); |
|
1172 QCOMPARE(number3_2.toUInt16(), quint16(0)); |
|
1173 QCOMPARE(qscriptvalue_cast<quint16>(number3_2), quint16(0)); |
|
1174 |
|
1175 QScriptValue number4 = QScriptValue(0.5); |
|
1176 QCOMPARE(number4.toUInt16(), quint16(0)); |
|
1177 |
|
1178 QScriptValue number5 = QScriptValue(123.5); |
|
1179 QCOMPARE(number5.toUInt16(), quint16(123)); |
|
1180 |
|
1181 QScriptValue number6 = QScriptValue(-456.5); |
|
1182 QCOMPARE(number6.toUInt16(), quint16(-456)); |
|
1183 QCOMPARE(qscriptvalue_cast<quint16>(number6), quint16(-456)); |
|
1184 |
|
1185 QScriptValue number7 = QScriptValue(0x10000); |
|
1186 QCOMPARE(number7.toUInt16(), quint16(0)); |
|
1187 QCOMPARE(qscriptvalue_cast<quint16>(number7), quint16(0)); |
|
1188 |
|
1189 QScriptValue number8 = QScriptValue(0x10001); |
|
1190 QCOMPARE(number8.toUInt16(), quint16(1)); |
|
1191 QCOMPARE(qscriptvalue_cast<quint16>(number8), quint16(1)); |
|
1192 |
|
1193 QScriptValue str = QScriptValue("123.0"); |
|
1194 QCOMPARE(str.toUInt16(), quint16(123)); |
|
1195 QCOMPARE(qscriptvalue_cast<quint16>(str), quint16(123)); |
|
1196 |
|
1197 QScriptValue str2 = QScriptValue("NaN"); |
|
1198 QCOMPARE(str2.toUInt16(), quint16(0)); |
|
1199 QCOMPARE(qscriptvalue_cast<quint16>(str2), quint16(0)); |
|
1200 |
|
1201 QScriptValue str3 = QScriptValue("Infinity"); |
|
1202 QCOMPARE(str3.toUInt16(), quint16(0)); |
|
1203 QCOMPARE(qscriptvalue_cast<quint16>(str3), quint16(0)); |
|
1204 |
|
1205 QScriptValue str3_2 = QScriptValue("-Infinity"); |
|
1206 QCOMPARE(str3_2.toUInt16(), quint16(0)); |
|
1207 QCOMPARE(qscriptvalue_cast<quint16>(str3_2), quint16(0)); |
|
1208 |
|
1209 QScriptValue str4 = QScriptValue("0.5"); |
|
1210 QCOMPARE(str4.toUInt16(), quint16(0)); |
|
1211 |
|
1212 QScriptValue str5 = QScriptValue("123.5"); |
|
1213 QCOMPARE(str5.toUInt16(), quint16(123)); |
|
1214 |
|
1215 QScriptValue str6 = QScriptValue("-456.5"); |
|
1216 QCOMPARE(str6.toUInt16(), quint16(-456)); |
|
1217 QCOMPARE(qscriptvalue_cast<quint16>(str6), quint16(-456)); |
|
1218 |
|
1219 QScriptValue str7 = QScriptValue("0x10000"); |
|
1220 QCOMPARE(str7.toUInt16(), quint16(0)); |
|
1221 QCOMPARE(qscriptvalue_cast<quint16>(str7), quint16(0)); |
|
1222 |
|
1223 QScriptValue str8 = QScriptValue("0x10001"); |
|
1224 QCOMPARE(str8.toUInt16(), quint16(1)); |
|
1225 QCOMPARE(qscriptvalue_cast<quint16>(str8), quint16(1)); |
|
1226 } |
|
1227 |
|
1228 QScriptValue inv; |
|
1229 QCOMPARE(inv.toUInt16(), quint16(0)); |
|
1230 QCOMPARE(qscriptvalue_cast<quint16>(inv), quint16(0)); |
|
1231 } |
|
1232 |
|
1233 #if defined Q_CC_MSVC && _MSC_VER < 1300 |
|
1234 Q_DECLARE_METATYPE(QVariant) |
|
1235 #endif |
|
1236 |
|
1237 void tst_QScriptValue::toVariant_old() |
|
1238 { |
|
1239 QScriptEngine eng; |
|
1240 |
|
1241 QScriptValue undefined = eng.undefinedValue(); |
|
1242 QCOMPARE(undefined.toVariant(), QVariant()); |
|
1243 QCOMPARE(qscriptvalue_cast<QVariant>(undefined), QVariant()); |
|
1244 |
|
1245 QScriptValue null = eng.nullValue(); |
|
1246 QCOMPARE(null.toVariant(), QVariant()); |
|
1247 QCOMPARE(qscriptvalue_cast<QVariant>(null), QVariant()); |
|
1248 |
|
1249 { |
|
1250 QScriptValue number = QScriptValue(&eng, 123.0); |
|
1251 QCOMPARE(number.toVariant(), QVariant(123.0)); |
|
1252 QCOMPARE(qscriptvalue_cast<QVariant>(number), QVariant(123.0)); |
|
1253 |
|
1254 QScriptValue falskt = QScriptValue(&eng, false); |
|
1255 QCOMPARE(falskt.toVariant(), QVariant(false)); |
|
1256 QCOMPARE(qscriptvalue_cast<QVariant>(falskt), QVariant(false)); |
|
1257 |
|
1258 QScriptValue sant = QScriptValue(&eng, true); |
|
1259 QCOMPARE(sant.toVariant(), QVariant(true)); |
|
1260 QCOMPARE(qscriptvalue_cast<QVariant>(sant), QVariant(true)); |
|
1261 |
|
1262 QScriptValue str = QScriptValue(&eng, QString("ciao")); |
|
1263 QCOMPARE(str.toVariant(), QVariant(QString("ciao"))); |
|
1264 QCOMPARE(qscriptvalue_cast<QVariant>(str), QVariant(QString("ciao"))); |
|
1265 } |
|
1266 |
|
1267 QVariant var(QChar(0x007A)); |
|
1268 QScriptValue opaque = eng.newVariant(var); |
|
1269 QVERIFY(opaque.isVariant()); |
|
1270 QCOMPARE(opaque.toVariant(), var); |
|
1271 |
|
1272 QScriptValue object = eng.newObject(); |
|
1273 QCOMPARE(object.toVariant(), QVariant(QString("[object Object]"))); |
|
1274 |
|
1275 QScriptValue qobject = eng.newQObject(this); |
|
1276 { |
|
1277 QVariant var = qobject.toVariant(); |
|
1278 QCOMPARE(var.userType(), int(QMetaType::QObjectStar)); |
|
1279 QCOMPARE(qVariantValue<QObject*>(var), (QObject *)this); |
|
1280 } |
|
1281 |
|
1282 { |
|
1283 QDateTime dateTime = QDateTime(QDate(1980, 10, 4)); |
|
1284 QScriptValue dateObject = eng.newDate(dateTime); |
|
1285 QVariant var = dateObject.toVariant(); |
|
1286 QCOMPARE(var, QVariant(dateTime)); |
|
1287 } |
|
1288 |
|
1289 { |
|
1290 QRegExp rx = QRegExp("[0-9a-z]+", Qt::CaseSensitive, QRegExp::RegExp2); |
|
1291 QScriptValue rxObject = eng.newRegExp(rx); |
|
1292 QVariant var = rxObject.toVariant(); |
|
1293 QCOMPARE(var, QVariant(rx)); |
|
1294 } |
|
1295 |
|
1296 QScriptValue inv; |
|
1297 QCOMPARE(inv.toVariant(), QVariant()); |
|
1298 QCOMPARE(qscriptvalue_cast<QVariant>(inv), QVariant()); |
|
1299 |
|
1300 // V2 constructors |
|
1301 { |
|
1302 QScriptValue number = QScriptValue(123.0); |
|
1303 QCOMPARE(number.toVariant(), QVariant(123.0)); |
|
1304 QCOMPARE(qscriptvalue_cast<QVariant>(number), QVariant(123.0)); |
|
1305 |
|
1306 QScriptValue falskt = QScriptValue(false); |
|
1307 QCOMPARE(falskt.toVariant(), QVariant(false)); |
|
1308 QCOMPARE(qscriptvalue_cast<QVariant>(falskt), QVariant(false)); |
|
1309 |
|
1310 QScriptValue sant = QScriptValue(true); |
|
1311 QCOMPARE(sant.toVariant(), QVariant(true)); |
|
1312 QCOMPARE(qscriptvalue_cast<QVariant>(sant), QVariant(true)); |
|
1313 |
|
1314 QScriptValue str = QScriptValue(QString("ciao")); |
|
1315 QCOMPARE(str.toVariant(), QVariant(QString("ciao"))); |
|
1316 QCOMPARE(qscriptvalue_cast<QVariant>(str), QVariant(QString("ciao"))); |
|
1317 } |
|
1318 |
|
1319 // array |
|
1320 { |
|
1321 QVariantList listIn; |
|
1322 listIn << 123 << "hello"; |
|
1323 QScriptValue array = qScriptValueFromValue(&eng, listIn); |
|
1324 QVERIFY(array.isArray()); |
|
1325 QCOMPARE(array.property("length").toInt32(), 2); |
|
1326 QVariant ret = array.toVariant(); |
|
1327 QCOMPARE(ret.type(), QVariant::List); |
|
1328 QVariantList listOut = ret.toList(); |
|
1329 QCOMPARE(listOut.size(), listIn.size()); |
|
1330 for (int i = 0; i < listIn.size(); ++i) |
|
1331 QVERIFY(listOut.at(i) == listIn.at(i)); |
|
1332 // round-trip conversion |
|
1333 QScriptValue array2 = qScriptValueFromValue(&eng, ret); |
|
1334 QVERIFY(array2.isArray()); |
|
1335 QCOMPARE(array2.property("length").toInt32(), array.property("length").toInt32()); |
|
1336 for (int i = 0; i < array.property("length").toInt32(); ++i) |
|
1337 QVERIFY(array2.property(i).strictlyEquals(array.property(i))); |
|
1338 } |
|
1339 } |
|
1340 |
|
1341 // unfortunately, this is necessary in order to do qscriptvalue_cast<QPushButton*>(...) |
|
1342 Q_DECLARE_METATYPE(QPushButton*) |
|
1343 |
|
1344 void tst_QScriptValue::toQObject_old() |
|
1345 { |
|
1346 QScriptEngine eng; |
|
1347 |
|
1348 QScriptValue undefined = eng.undefinedValue(); |
|
1349 QCOMPARE(undefined.toQObject(), (QObject *)0); |
|
1350 QCOMPARE(qscriptvalue_cast<QObject*>(undefined), (QObject *)0); |
|
1351 |
|
1352 QScriptValue null = eng.nullValue(); |
|
1353 QCOMPARE(null.toQObject(), (QObject *)0); |
|
1354 QCOMPARE(qscriptvalue_cast<QObject*>(null), (QObject *)0); |
|
1355 |
|
1356 { |
|
1357 QScriptValue falskt = QScriptValue(&eng, false); |
|
1358 QCOMPARE(falskt.toQObject(), (QObject *)0); |
|
1359 QCOMPARE(qscriptvalue_cast<QObject*>(falskt), (QObject *)0); |
|
1360 |
|
1361 QScriptValue sant = QScriptValue(&eng, true); |
|
1362 QCOMPARE(sant.toQObject(), (QObject *)0); |
|
1363 QCOMPARE(qscriptvalue_cast<QObject*>(sant), (QObject *)0); |
|
1364 |
|
1365 QScriptValue number = QScriptValue(&eng, 123.0); |
|
1366 QCOMPARE(number.toQObject(), (QObject *)0); |
|
1367 QCOMPARE(qscriptvalue_cast<QObject*>(number), (QObject *)0); |
|
1368 |
|
1369 QScriptValue str = QScriptValue(&eng, QString("ciao")); |
|
1370 QCOMPARE(str.toQObject(), (QObject *)0); |
|
1371 QCOMPARE(qscriptvalue_cast<QObject*>(str), (QObject *)0); |
|
1372 } |
|
1373 |
|
1374 QScriptValue object = eng.newObject(); |
|
1375 QCOMPARE(object.toQObject(), (QObject *)0); |
|
1376 QCOMPARE(qscriptvalue_cast<QObject*>(object), (QObject *)0); |
|
1377 |
|
1378 QScriptValue qobject = eng.newQObject(this); |
|
1379 QCOMPARE(qobject.toQObject(), (QObject *)this); |
|
1380 QCOMPARE(qscriptvalue_cast<QObject*>(qobject), (QObject *)this); |
|
1381 QCOMPARE(qscriptvalue_cast<QWidget*>(qobject), (QWidget *)0); |
|
1382 |
|
1383 QScriptValue qobject2 = eng.newQObject(0); |
|
1384 QCOMPARE(qobject2.toQObject(), (QObject *)0); |
|
1385 QCOMPARE(qscriptvalue_cast<QObject*>(qobject2), (QObject *)0); |
|
1386 |
|
1387 QWidget widget; |
|
1388 QScriptValue qwidget = eng.newQObject(&widget); |
|
1389 QCOMPARE(qwidget.toQObject(), (QObject *)&widget); |
|
1390 QCOMPARE(qscriptvalue_cast<QObject*>(qwidget), (QObject *)&widget); |
|
1391 QCOMPARE(qscriptvalue_cast<QWidget*>(qwidget), &widget); |
|
1392 |
|
1393 QPushButton button; |
|
1394 QScriptValue qbutton = eng.newQObject(&button); |
|
1395 QCOMPARE(qbutton.toQObject(), (QObject *)&button); |
|
1396 QCOMPARE(qscriptvalue_cast<QObject*>(qbutton), (QObject *)&button); |
|
1397 QCOMPARE(qscriptvalue_cast<QWidget*>(qbutton), (QWidget *)&button); |
|
1398 QCOMPARE(qscriptvalue_cast<QPushButton*>(qbutton), &button); |
|
1399 |
|
1400 // V2 constructors |
|
1401 { |
|
1402 QScriptValue falskt = QScriptValue(false); |
|
1403 QCOMPARE(falskt.toQObject(), (QObject *)0); |
|
1404 QCOMPARE(qscriptvalue_cast<QObject*>(falskt), (QObject *)0); |
|
1405 |
|
1406 QScriptValue sant = QScriptValue(true); |
|
1407 QCOMPARE(sant.toQObject(), (QObject *)0); |
|
1408 QCOMPARE(qscriptvalue_cast<QObject*>(sant), (QObject *)0); |
|
1409 |
|
1410 QScriptValue number = QScriptValue(123.0); |
|
1411 QCOMPARE(number.toQObject(), (QObject *)0); |
|
1412 QCOMPARE(qscriptvalue_cast<QObject*>(number), (QObject *)0); |
|
1413 |
|
1414 QScriptValue str = QScriptValue(QString("ciao")); |
|
1415 QCOMPARE(str.toQObject(), (QObject *)0); |
|
1416 QCOMPARE(qscriptvalue_cast<QObject*>(str), (QObject *)0); |
|
1417 } |
|
1418 |
|
1419 // wrapping a QObject* as variant |
|
1420 QScriptValue variant = eng.newVariant(qVariantFromValue((QObject*)&button)); |
|
1421 QCOMPARE(variant.toQObject(), (QObject*)&button); |
|
1422 QCOMPARE(qscriptvalue_cast<QObject*>(variant), (QObject*)&button); |
|
1423 QCOMPARE(qscriptvalue_cast<QWidget*>(variant), (QWidget*)&button); |
|
1424 QCOMPARE(qscriptvalue_cast<QPushButton*>(variant), &button); |
|
1425 |
|
1426 QScriptValue variant2 = eng.newVariant(qVariantFromValue((QWidget*)&button)); |
|
1427 QCOMPARE(variant2.toQObject(), (QObject*)&button); |
|
1428 QCOMPARE(qscriptvalue_cast<QObject*>(variant2), (QObject*)&button); |
|
1429 QCOMPARE(qscriptvalue_cast<QWidget*>(variant2), (QWidget*)&button); |
|
1430 QCOMPARE(qscriptvalue_cast<QPushButton*>(variant2), &button); |
|
1431 |
|
1432 QScriptValue variant3 = eng.newVariant(qVariantFromValue(&button)); |
|
1433 QCOMPARE(variant3.toQObject(), (QObject*)0); |
|
1434 QCOMPARE(qscriptvalue_cast<QObject*>(variant3), (QObject*)0); |
|
1435 QCOMPARE(qscriptvalue_cast<QWidget*>(variant3), (QWidget*)0); |
|
1436 QCOMPARE(qscriptvalue_cast<QPushButton*>(variant3), &button); |
|
1437 |
|
1438 QScriptValue inv; |
|
1439 QCOMPARE(inv.toQObject(), (QObject *)0); |
|
1440 QCOMPARE(qscriptvalue_cast<QObject*>(inv), (QObject *)0); |
|
1441 } |
|
1442 |
|
1443 void tst_QScriptValue::toObject() |
|
1444 { |
|
1445 QScriptEngine eng; |
|
1446 |
|
1447 QScriptValue undefined = eng.undefinedValue(); |
|
1448 QCOMPARE(undefined.toObject().isValid(), false); |
|
1449 |
|
1450 QScriptValue null = eng.nullValue(); |
|
1451 QCOMPARE(null.toObject().isValid(), false); |
|
1452 |
|
1453 { |
|
1454 QScriptValue falskt = QScriptValue(&eng, false); |
|
1455 { |
|
1456 QScriptValue tmp = falskt.toObject(); |
|
1457 QCOMPARE(tmp.isObject(), true); |
|
1458 QCOMPARE(tmp.toNumber(), falskt.toNumber()); |
|
1459 } |
|
1460 |
|
1461 QScriptValue sant = QScriptValue(&eng, true); |
|
1462 { |
|
1463 QScriptValue tmp = sant.toObject(); |
|
1464 QCOMPARE(tmp.isObject(), true); |
|
1465 QCOMPARE(tmp.toNumber(), sant.toNumber()); |
|
1466 } |
|
1467 |
|
1468 QScriptValue number = QScriptValue(&eng, 123.0); |
|
1469 { |
|
1470 QScriptValue tmp = number.toObject(); |
|
1471 QCOMPARE(tmp.isObject(), true); |
|
1472 QCOMPARE(tmp.toNumber(), number.toNumber()); |
|
1473 } |
|
1474 |
|
1475 QScriptValue str = QScriptValue(&eng, QString("ciao")); |
|
1476 { |
|
1477 QScriptValue tmp = str.toObject(); |
|
1478 QCOMPARE(tmp.isObject(), true); |
|
1479 QCOMPARE(tmp.toString(), str.toString()); |
|
1480 } |
|
1481 } |
|
1482 |
|
1483 QScriptValue object = eng.newObject(); |
|
1484 { |
|
1485 QScriptValue tmp = object.toObject(); |
|
1486 QCOMPARE(tmp.isObject(), true); |
|
1487 } |
|
1488 |
|
1489 QScriptValue qobject = eng.newQObject(this); |
|
1490 QCOMPARE(qobject.toObject().isValid(), true); |
|
1491 |
|
1492 QScriptValue inv; |
|
1493 QCOMPARE(inv.toObject().isValid(), false); |
|
1494 |
|
1495 // V2 constructors: in this case, you have to use QScriptEngine::toObject() |
|
1496 { |
|
1497 QScriptValue undefined = QScriptValue(QScriptValue::UndefinedValue); |
|
1498 QVERIFY(!undefined.toObject().isValid()); |
|
1499 QVERIFY(!eng.toObject(undefined).isValid()); |
|
1500 |
|
1501 QScriptValue null = QScriptValue(QScriptValue::NullValue); |
|
1502 QVERIFY(!null.toObject().isValid()); |
|
1503 QVERIFY(!eng.toObject(null).isValid()); |
|
1504 |
|
1505 QScriptValue falskt = QScriptValue(false); |
|
1506 QVERIFY(!falskt.toObject().isValid()); |
|
1507 { |
|
1508 QScriptValue tmp = eng.toObject(falskt); |
|
1509 QVERIFY(tmp.isObject()); |
|
1510 QVERIFY(tmp.toBool()); |
|
1511 } |
|
1512 |
|
1513 QScriptValue sant = QScriptValue(true); |
|
1514 QVERIFY(!sant.toObject().isValid()); |
|
1515 { |
|
1516 QScriptValue tmp = eng.toObject(sant); |
|
1517 QVERIFY(tmp.isObject()); |
|
1518 QVERIFY(tmp.toBool()); |
|
1519 } |
|
1520 |
|
1521 QScriptValue number = QScriptValue(123.0); |
|
1522 QVERIFY(!number.toObject().isValid()); |
|
1523 { |
|
1524 QScriptValue tmp = eng.toObject(number); |
|
1525 QVERIFY(tmp.isObject()); |
|
1526 QCOMPARE(tmp.toInt32(), number.toInt32()); |
|
1527 } |
|
1528 |
|
1529 QScriptValue str = QScriptValue(QString::fromLatin1("ciao")); |
|
1530 QVERIFY(!str.toObject().isValid()); |
|
1531 { |
|
1532 QScriptValue tmp = eng.toObject(str); |
|
1533 QVERIFY(tmp.isObject()); |
|
1534 QCOMPARE(tmp.toString(), QString::fromLatin1("ciao")); |
|
1535 } |
|
1536 } |
|
1537 } |
|
1538 |
|
1539 void tst_QScriptValue::toDateTime_old() |
|
1540 { |
|
1541 QScriptEngine eng; |
|
1542 QDateTime dt = eng.evaluate("new Date(0)").toDateTime(); |
|
1543 QVERIFY(dt.isValid()); |
|
1544 QCOMPARE(dt.timeSpec(), Qt::LocalTime); |
|
1545 QCOMPARE(dt.toUTC(), QDateTime(QDate(1970, 1, 1), QTime(0, 0, 0), Qt::UTC)); |
|
1546 |
|
1547 QVERIFY(!eng.evaluate("[]").toDateTime().isValid()); |
|
1548 QVERIFY(!eng.evaluate("{}").toDateTime().isValid()); |
|
1549 QVERIFY(!eng.globalObject().toDateTime().isValid()); |
|
1550 QVERIFY(!QScriptValue().toDateTime().isValid()); |
|
1551 QVERIFY(!QScriptValue(123).toDateTime().isValid()); |
|
1552 QVERIFY(!QScriptValue(false).toDateTime().isValid()); |
|
1553 QVERIFY(!eng.nullValue().toDateTime().isValid()); |
|
1554 QVERIFY(!eng.undefinedValue().toDateTime().isValid()); |
|
1555 } |
|
1556 |
|
1557 void tst_QScriptValue::toRegExp_old() |
|
1558 { |
|
1559 QScriptEngine eng; |
|
1560 { |
|
1561 QRegExp rx = eng.evaluate("/foo/").toRegExp(); |
|
1562 QVERIFY(rx.isValid()); |
|
1563 QCOMPARE(rx.patternSyntax(), QRegExp::RegExp2); |
|
1564 QCOMPARE(rx.pattern(), QString::fromLatin1("foo")); |
|
1565 QCOMPARE(rx.caseSensitivity(), Qt::CaseSensitive); |
|
1566 QVERIFY(!rx.isMinimal()); |
|
1567 } |
|
1568 { |
|
1569 QRegExp rx = eng.evaluate("/bar/gi").toRegExp(); |
|
1570 QVERIFY(rx.isValid()); |
|
1571 QCOMPARE(rx.patternSyntax(), QRegExp::RegExp2); |
|
1572 QCOMPARE(rx.pattern(), QString::fromLatin1("bar")); |
|
1573 QCOMPARE(rx.caseSensitivity(), Qt::CaseInsensitive); |
|
1574 QVERIFY(!rx.isMinimal()); |
|
1575 } |
|
1576 |
|
1577 QVERIFY(eng.evaluate("[]").toRegExp().isEmpty()); |
|
1578 QVERIFY(eng.evaluate("{}").toRegExp().isEmpty()); |
|
1579 QVERIFY(eng.globalObject().toRegExp().isEmpty()); |
|
1580 QVERIFY(QScriptValue().toRegExp().isEmpty()); |
|
1581 QVERIFY(QScriptValue(123).toRegExp().isEmpty()); |
|
1582 QVERIFY(QScriptValue(false).toRegExp().isEmpty()); |
|
1583 QVERIFY(eng.nullValue().toRegExp().isEmpty()); |
|
1584 QVERIFY(eng.undefinedValue().toRegExp().isEmpty()); |
|
1585 } |
|
1586 |
|
1587 void tst_QScriptValue::instanceOf_old() |
|
1588 { |
|
1589 QScriptEngine eng; |
|
1590 QScriptValue obj = eng.newObject(); |
|
1591 QCOMPARE(obj.instanceOf(eng.evaluate("Object.prototype")), false); |
|
1592 QCOMPARE(obj.instanceOf(eng.evaluate("Array.prototype")), false); |
|
1593 QCOMPARE(obj.instanceOf(eng.evaluate("Function.prototype")), false); |
|
1594 QCOMPARE(obj.instanceOf(eng.evaluate("QObject.prototype")), false); |
|
1595 QCOMPARE(obj.instanceOf(QScriptValue(&eng, 123)), false); |
|
1596 QCOMPARE(obj.instanceOf(eng.undefinedValue()), false); |
|
1597 QCOMPARE(obj.instanceOf(eng.nullValue()), false); |
|
1598 QCOMPARE(obj.instanceOf(QScriptValue()), false); |
|
1599 |
|
1600 QCOMPARE(obj.instanceOf(eng.evaluate("Object")), true); |
|
1601 QCOMPARE(obj.instanceOf(eng.evaluate("Array")), false); |
|
1602 QCOMPARE(obj.instanceOf(eng.evaluate("Function")), false); |
|
1603 QCOMPARE(obj.instanceOf(eng.evaluate("QObject")), false); |
|
1604 |
|
1605 QScriptValue arr = eng.newArray(); |
|
1606 QVERIFY(arr.isArray()); |
|
1607 QCOMPARE(arr.instanceOf(eng.evaluate("Object.prototype")), false); |
|
1608 QCOMPARE(arr.instanceOf(eng.evaluate("Array.prototype")), false); |
|
1609 QCOMPARE(arr.instanceOf(eng.evaluate("Function.prototype")), false); |
|
1610 QCOMPARE(arr.instanceOf(eng.evaluate("QObject.prototype")), false); |
|
1611 QCOMPARE(arr.instanceOf(eng.evaluate("Object")), true); |
|
1612 QCOMPARE(arr.instanceOf(eng.evaluate("Array")), true); |
|
1613 QCOMPARE(arr.instanceOf(eng.evaluate("Function")), false); |
|
1614 QCOMPARE(arr.instanceOf(eng.evaluate("QObject")), false); |
|
1615 |
|
1616 QCOMPARE(QScriptValue().instanceOf(arr), false); |
|
1617 |
|
1618 QScriptEngine otherEngine; |
|
1619 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::instanceof: cannot perform operation on a value created in a different engine"); |
|
1620 QCOMPARE(obj.instanceOf(otherEngine.globalObject().property("Object")), false); |
|
1621 } |
|
1622 |
|
1623 void tst_QScriptValue::isArray_old() |
|
1624 { |
|
1625 QScriptEngine eng; |
|
1626 QVERIFY(eng.evaluate("[]").isArray()); |
|
1627 QVERIFY(!eng.evaluate("{}").isArray()); |
|
1628 QVERIFY(!eng.globalObject().isArray()); |
|
1629 QVERIFY(!QScriptValue().isArray()); |
|
1630 QVERIFY(!QScriptValue(123).isArray()); |
|
1631 QVERIFY(!QScriptValue(false).isArray()); |
|
1632 QVERIFY(!eng.nullValue().isArray()); |
|
1633 QVERIFY(!eng.undefinedValue().isArray()); |
|
1634 } |
|
1635 |
|
1636 void tst_QScriptValue::isDate_old() |
|
1637 { |
|
1638 QScriptEngine eng; |
|
1639 QVERIFY(eng.evaluate("new Date()").isDate()); |
|
1640 QVERIFY(!eng.evaluate("[]").isDate()); |
|
1641 QVERIFY(!eng.evaluate("{}").isDate()); |
|
1642 QVERIFY(!eng.globalObject().isDate()); |
|
1643 QVERIFY(!QScriptValue().isDate()); |
|
1644 QVERIFY(!QScriptValue(123).isDate()); |
|
1645 QVERIFY(!QScriptValue(false).isDate()); |
|
1646 QVERIFY(!eng.nullValue().isDate()); |
|
1647 QVERIFY(!eng.undefinedValue().isDate()); |
|
1648 } |
|
1649 |
|
1650 void tst_QScriptValue::isError_old() |
|
1651 { |
|
1652 QStringList errors; |
|
1653 errors << "Error" |
|
1654 << "EvalError" |
|
1655 << "RangeError" |
|
1656 << "ReferenceError" |
|
1657 << "SyntaxError" |
|
1658 << "TypeError" |
|
1659 << "URIError"; |
|
1660 QScriptEngine eng; |
|
1661 for (int i = 0; i < errors.size(); ++i) { |
|
1662 QScriptValue ctor = eng.globalObject().property(errors.at(i)); |
|
1663 QVERIFY(ctor.isFunction()); |
|
1664 QVERIFY(ctor.property("prototype").isError()); |
|
1665 } |
|
1666 QVERIFY(!eng.globalObject().isError()); |
|
1667 QVERIFY(!QScriptValue().isError()); |
|
1668 QVERIFY(!QScriptValue(123).isError()); |
|
1669 QVERIFY(!QScriptValue(false).isError()); |
|
1670 QVERIFY(!eng.nullValue().isError()); |
|
1671 QVERIFY(!eng.undefinedValue().isError()); |
|
1672 QVERIFY(!eng.evaluate("new Object()").isError()); |
|
1673 } |
|
1674 |
|
1675 void tst_QScriptValue::isRegExp_old() |
|
1676 { |
|
1677 QScriptEngine eng; |
|
1678 QVERIFY(eng.evaluate("/foo/").isRegExp()); |
|
1679 QVERIFY(!eng.evaluate("[]").isRegExp()); |
|
1680 QVERIFY(!eng.evaluate("{}").isRegExp()); |
|
1681 QVERIFY(!eng.globalObject().isRegExp()); |
|
1682 QVERIFY(!QScriptValue().isRegExp()); |
|
1683 QVERIFY(!QScriptValue(123).isRegExp()); |
|
1684 QVERIFY(!QScriptValue(false).isRegExp()); |
|
1685 QVERIFY(!eng.nullValue().isRegExp()); |
|
1686 QVERIFY(!eng.undefinedValue().isRegExp()); |
|
1687 } |
|
1688 |
|
1689 static QScriptValue getter(QScriptContext *ctx, QScriptEngine *) |
|
1690 { |
|
1691 return ctx->thisObject().property("x"); |
|
1692 } |
|
1693 |
|
1694 static QScriptValue setter(QScriptContext *ctx, QScriptEngine *) |
|
1695 { |
|
1696 ctx->thisObject().setProperty("x", ctx->argument(0)); |
|
1697 return ctx->argument(0); |
|
1698 } |
|
1699 |
|
1700 static QScriptValue getterSetter(QScriptContext *ctx, QScriptEngine *) |
|
1701 { |
|
1702 if (ctx->argumentCount() > 0) |
|
1703 ctx->thisObject().setProperty("x", ctx->argument(0)); |
|
1704 return ctx->thisObject().property("x"); |
|
1705 } |
|
1706 |
|
1707 static QScriptValue getterSetterThrowingError(QScriptContext *ctx, QScriptEngine *) |
|
1708 { |
|
1709 if (ctx->argumentCount() > 0) |
|
1710 return ctx->throwError("set foo"); |
|
1711 else |
|
1712 return ctx->throwError("get foo"); |
|
1713 } |
|
1714 |
|
1715 static QScriptValue getSet__proto__(QScriptContext *ctx, QScriptEngine *) |
|
1716 { |
|
1717 if (ctx->argumentCount() > 0) |
|
1718 ctx->callee().setProperty("value", ctx->argument(0)); |
|
1719 return ctx->callee().property("value"); |
|
1720 } |
|
1721 |
|
1722 void tst_QScriptValue::getSetProperty() |
|
1723 { |
|
1724 QScriptEngine eng; |
|
1725 |
|
1726 QScriptValue object = eng.newObject(); |
|
1727 |
|
1728 QScriptValue str = QScriptValue(&eng, "bar"); |
|
1729 object.setProperty("foo", str); |
|
1730 QCOMPARE(object.property("foo").toString(), str.toString()); |
|
1731 |
|
1732 QScriptValue num = QScriptValue(&eng, 123.0); |
|
1733 object.setProperty("baz", num); |
|
1734 QCOMPARE(object.property("baz").toNumber(), num.toNumber()); |
|
1735 |
|
1736 QScriptValue strstr = QScriptValue("bar"); |
|
1737 QCOMPARE(strstr.engine(), (QScriptEngine *)0); |
|
1738 object.setProperty("foo", strstr); |
|
1739 QCOMPARE(object.property("foo").toString(), strstr.toString()); |
|
1740 QCOMPARE(strstr.engine(), &eng); // the value has been bound to the engine |
|
1741 |
|
1742 QScriptValue numnum = QScriptValue(123.0); |
|
1743 object.setProperty("baz", numnum); |
|
1744 QCOMPARE(object.property("baz").toNumber(), numnum.toNumber()); |
|
1745 |
|
1746 QScriptValue inv; |
|
1747 inv.setProperty("foo", num); |
|
1748 QCOMPARE(inv.property("foo").isValid(), false); |
|
1749 |
|
1750 QScriptValue array = eng.newArray(); |
|
1751 QVERIFY(array.isArray()); |
|
1752 array.setProperty(0, num); |
|
1753 QCOMPARE(array.property(0).toNumber(), num.toNumber()); |
|
1754 QCOMPARE(array.property("0").toNumber(), num.toNumber()); |
|
1755 QCOMPARE(array.property("length").toUInt32(), quint32(1)); |
|
1756 array.setProperty(1, str); |
|
1757 QCOMPARE(array.property(1).toString(), str.toString()); |
|
1758 QCOMPARE(array.property("1").toString(), str.toString()); |
|
1759 QCOMPARE(array.property("length").toUInt32(), quint32(2)); |
|
1760 array.setProperty("length", QScriptValue(&eng, 1)); |
|
1761 QCOMPARE(array.property("length").toUInt32(), quint32(1)); |
|
1762 QCOMPARE(array.property(1).isValid(), false); |
|
1763 |
|
1764 // task 162051 -- detecting whether the property is an array index or not |
|
1765 QVERIFY(eng.evaluate("a = []; a['00'] = 123; a['00']").strictlyEquals(QScriptValue(&eng, 123))); |
|
1766 QVERIFY(eng.evaluate("a.length").strictlyEquals(QScriptValue(&eng, 0))); |
|
1767 QVERIFY(eng.evaluate("a.hasOwnProperty('00')").strictlyEquals(QScriptValue(&eng, true))); |
|
1768 QVERIFY(eng.evaluate("a.hasOwnProperty('0')").strictlyEquals(QScriptValue(&eng, false))); |
|
1769 QVERIFY(eng.evaluate("a[0]").isUndefined()); |
|
1770 QVERIFY(eng.evaluate("a[0.5] = 456; a[0.5]").strictlyEquals(QScriptValue(&eng, 456))); |
|
1771 QVERIFY(eng.evaluate("a.length").strictlyEquals(QScriptValue(&eng, 0))); |
|
1772 QVERIFY(eng.evaluate("a.hasOwnProperty('0.5')").strictlyEquals(QScriptValue(&eng, true))); |
|
1773 QVERIFY(eng.evaluate("a[0]").isUndefined()); |
|
1774 QVERIFY(eng.evaluate("a[0] = 789; a[0]").strictlyEquals(QScriptValue(&eng, 789))); |
|
1775 QVERIFY(eng.evaluate("a.length").strictlyEquals(QScriptValue(&eng, 1))); |
|
1776 |
|
1777 // task 183072 -- 0x800000000 is not an array index |
|
1778 eng.evaluate("a = []; a[0x800000000] = 123"); |
|
1779 QVERIFY(eng.evaluate("a.length").strictlyEquals(QScriptValue(&eng, 0))); |
|
1780 QVERIFY(eng.evaluate("a[0]").isUndefined()); |
|
1781 QVERIFY(eng.evaluate("a[0x800000000]").strictlyEquals(QScriptValue(&eng, 123))); |
|
1782 |
|
1783 QScriptEngine otherEngine; |
|
1784 QScriptValue otherNum = QScriptValue(&otherEngine, 123); |
|
1785 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setProperty(oof) failed: cannot set value created in a different engine"); |
|
1786 object.setProperty("oof", otherNum); |
|
1787 QCOMPARE(object.property("oof").isValid(), false); |
|
1788 |
|
1789 // test ResolveMode |
|
1790 QScriptValue object2 = eng.newObject(); |
|
1791 object.setPrototype(object2); |
|
1792 QScriptValue num2 = QScriptValue(&eng, 456.0); |
|
1793 object2.setProperty("propertyInPrototype", num2); |
|
1794 // default is ResolvePrototype |
|
1795 QCOMPARE(object.property("propertyInPrototype") |
|
1796 .strictlyEquals(num2), true); |
|
1797 QCOMPARE(object.property("propertyInPrototype", QScriptValue::ResolvePrototype) |
|
1798 .strictlyEquals(num2), true); |
|
1799 QCOMPARE(object.property("propertyInPrototype", QScriptValue::ResolveLocal) |
|
1800 .isValid(), false); |
|
1801 QCOMPARE(object.property("propertyInPrototype", QScriptValue::ResolveScope) |
|
1802 .strictlyEquals(num2), false); |
|
1803 QCOMPARE(object.property("propertyInPrototype", QScriptValue::ResolveFull) |
|
1804 .strictlyEquals(num2), true); |
|
1805 |
|
1806 // test property removal (setProperty(QScriptValue())) |
|
1807 QScriptValue object3 = eng.newObject(); |
|
1808 object3.setProperty("foo", num); |
|
1809 QCOMPARE(object3.property("foo").strictlyEquals(num), true); |
|
1810 object3.setProperty("bar", str); |
|
1811 QCOMPARE(object3.property("bar").strictlyEquals(str), true); |
|
1812 object3.setProperty("foo", QScriptValue()); |
|
1813 QCOMPARE(object3.property("foo").isValid(), false); |
|
1814 QCOMPARE(object3.property("bar").strictlyEquals(str), true); |
|
1815 object3.setProperty("foo", num); |
|
1816 QCOMPARE(object3.property("foo").strictlyEquals(num), true); |
|
1817 QCOMPARE(object3.property("bar").strictlyEquals(str), true); |
|
1818 object3.setProperty("bar", QScriptValue()); |
|
1819 QCOMPARE(object3.property("bar").isValid(), false); |
|
1820 QCOMPARE(object3.property("foo").strictlyEquals(num), true); |
|
1821 object3.setProperty("foo", QScriptValue()); |
|
1822 object3.setProperty("foo", QScriptValue()); |
|
1823 |
|
1824 eng.globalObject().setProperty("object3", object3); |
|
1825 QCOMPARE(eng.evaluate("object3.hasOwnProperty('foo')") |
|
1826 .strictlyEquals(QScriptValue(&eng, false)), true); |
|
1827 object3.setProperty("foo", num); |
|
1828 QCOMPARE(eng.evaluate("object3.hasOwnProperty('foo')") |
|
1829 .strictlyEquals(QScriptValue(&eng, true)), true); |
|
1830 eng.globalObject().setProperty("object3", QScriptValue()); |
|
1831 QCOMPARE(eng.evaluate("this.hasOwnProperty('object3')") |
|
1832 .strictlyEquals(QScriptValue(&eng, false)), true); |
|
1833 |
|
1834 // getters and setters |
|
1835 { |
|
1836 QScriptValue object4 = eng.newObject(); |
|
1837 for (int x = 0; x < 2; ++x) { |
|
1838 object4.setProperty("foo", QScriptValue()); |
|
1839 // getter() returns this.x |
|
1840 object4.setProperty("foo", eng.newFunction(getter), |
|
1841 QScriptValue::PropertyGetter | QScriptValue::UserRange); |
|
1842 QCOMPARE(object4.propertyFlags("foo") & ~QScriptValue::UserRange, |
|
1843 QScriptValue::PropertyGetter ); |
|
1844 |
|
1845 QEXPECT_FAIL("", "User-range flags are not retained for getter/setter properties", Continue); |
|
1846 QCOMPARE(object4.propertyFlags("foo"), |
|
1847 QScriptValue::PropertyGetter | QScriptValue::UserRange); |
|
1848 object4.setProperty("x", num); |
|
1849 QCOMPARE(object4.property("foo").strictlyEquals(num), true); |
|
1850 |
|
1851 // setter() sets this.x |
|
1852 object4.setProperty("foo", eng.newFunction(setter), |
|
1853 QScriptValue::PropertySetter); |
|
1854 QCOMPARE(object4.propertyFlags("foo") & ~QScriptValue::UserRange, |
|
1855 QScriptValue::PropertySetter | QScriptValue::PropertyGetter); |
|
1856 |
|
1857 QCOMPARE(object4.propertyFlags("foo"), |
|
1858 QScriptValue::PropertySetter | QScriptValue::PropertyGetter); |
|
1859 object4.setProperty("foo", str); |
|
1860 QCOMPARE(object4.property("x").strictlyEquals(str), true); |
|
1861 QCOMPARE(object4.property("foo").strictlyEquals(str), true); |
|
1862 |
|
1863 // kill the getter |
|
1864 object4.setProperty("foo", QScriptValue(), QScriptValue::PropertyGetter); |
|
1865 QVERIFY(!(object4.propertyFlags("foo") & QScriptValue::PropertyGetter)); |
|
1866 QVERIFY(object4.propertyFlags("foo") & QScriptValue::PropertySetter); |
|
1867 QCOMPARE(object4.property("foo").isUndefined(), true); |
|
1868 |
|
1869 // setter should still work |
|
1870 object4.setProperty("foo", num); |
|
1871 QCOMPARE(object4.property("x").strictlyEquals(num), true); |
|
1872 |
|
1873 // kill the setter too |
|
1874 object4.setProperty("foo", QScriptValue(), QScriptValue::PropertySetter); |
|
1875 QVERIFY(!(object4.propertyFlags("foo") & QScriptValue::PropertySetter)); |
|
1876 // now foo is just a regular property |
|
1877 object4.setProperty("foo", str); |
|
1878 QCOMPARE(object4.property("x").strictlyEquals(num), true); |
|
1879 QCOMPARE(object4.property("foo").strictlyEquals(str), true); |
|
1880 } |
|
1881 |
|
1882 for (int x = 0; x < 2; ++x) { |
|
1883 object4.setProperty("foo", QScriptValue()); |
|
1884 // setter() sets this.x |
|
1885 object4.setProperty("foo", eng.newFunction(setter), QScriptValue::PropertySetter); |
|
1886 object4.setProperty("foo", str); |
|
1887 QCOMPARE(object4.property("x").strictlyEquals(str), true); |
|
1888 QCOMPARE(object4.property("foo").isUndefined(), true); |
|
1889 |
|
1890 // getter() returns this.x |
|
1891 object4.setProperty("foo", eng.newFunction(getter), QScriptValue::PropertyGetter); |
|
1892 object4.setProperty("x", num); |
|
1893 QCOMPARE(object4.property("foo").strictlyEquals(num), true); |
|
1894 |
|
1895 // kill the setter |
|
1896 object4.setProperty("foo", QScriptValue(), QScriptValue::PropertySetter); |
|
1897 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setProperty() failed: property 'foo' has a getter but no setter"); |
|
1898 object4.setProperty("foo", str); |
|
1899 |
|
1900 // getter should still work |
|
1901 QCOMPARE(object4.property("foo").strictlyEquals(num), true); |
|
1902 |
|
1903 // kill the getter too |
|
1904 object4.setProperty("foo", QScriptValue(), QScriptValue::PropertyGetter); |
|
1905 // now foo is just a regular property |
|
1906 object4.setProperty("foo", str); |
|
1907 QCOMPARE(object4.property("x").strictlyEquals(num), true); |
|
1908 QCOMPARE(object4.property("foo").strictlyEquals(str), true); |
|
1909 } |
|
1910 |
|
1911 // use a single function as both getter and setter |
|
1912 object4.setProperty("foo", QScriptValue()); |
|
1913 object4.setProperty("foo", eng.newFunction(getterSetter), |
|
1914 QScriptValue::PropertyGetter | QScriptValue::PropertySetter); |
|
1915 QCOMPARE(object4.propertyFlags("foo"), |
|
1916 QScriptValue::PropertyGetter | QScriptValue::PropertySetter); |
|
1917 object4.setProperty("x", num); |
|
1918 QCOMPARE(object4.property("foo").strictlyEquals(num), true); |
|
1919 |
|
1920 // killing the getter will preserve the setter, even though they are the same function |
|
1921 object4.setProperty("foo", QScriptValue(), QScriptValue::PropertyGetter); |
|
1922 QVERIFY(object4.propertyFlags("foo") & QScriptValue::PropertySetter); |
|
1923 QCOMPARE(object4.property("foo").isUndefined(), true); |
|
1924 |
|
1925 // getter/setter that throws an error |
|
1926 { |
|
1927 QScriptValue object5 = eng.newObject(); |
|
1928 object5.setProperty("foo", eng.newFunction(getterSetterThrowingError), |
|
1929 QScriptValue::PropertyGetter | QScriptValue::PropertySetter); |
|
1930 QVERIFY(!eng.hasUncaughtException()); |
|
1931 QScriptValue ret = object5.property("foo"); |
|
1932 QVERIFY(ret.isError()); |
|
1933 QVERIFY(eng.hasUncaughtException()); |
|
1934 QVERIFY(ret.strictlyEquals(eng.uncaughtException())); |
|
1935 eng.evaluate("Object"); // clear exception state... |
|
1936 QVERIFY(!eng.hasUncaughtException()); |
|
1937 object5.setProperty("foo", str); |
|
1938 QVERIFY(eng.hasUncaughtException()); |
|
1939 QCOMPARE(eng.uncaughtException().toString(), QLatin1String("Error: set foo")); |
|
1940 } |
|
1941 |
|
1942 // attempt to install getter+setter on built-in (native) property |
|
1943 { |
|
1944 QScriptValue object6 = eng.newObject(); |
|
1945 QVERIFY(object6.property("__proto__").strictlyEquals(object6.prototype())); |
|
1946 |
|
1947 QScriptValue fun = eng.newFunction(getSet__proto__); |
|
1948 fun.setProperty("value", QScriptValue(&eng, "boo")); |
|
1949 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setProperty() failed: " |
|
1950 "cannot set getter or setter of native property " |
|
1951 "`__proto__'"); |
|
1952 object6.setProperty("__proto__", fun, |
|
1953 QScriptValue::PropertyGetter | QScriptValue::PropertySetter |
|
1954 | QScriptValue::UserRange); |
|
1955 QVERIFY(object6.property("__proto__").strictlyEquals(object6.prototype())); |
|
1956 |
|
1957 object6.setProperty("__proto__", QScriptValue(), |
|
1958 QScriptValue::PropertyGetter | QScriptValue::PropertySetter); |
|
1959 QVERIFY(object6.property("__proto__").strictlyEquals(object6.prototype())); |
|
1960 } |
|
1961 |
|
1962 // global property that's a getter+setter |
|
1963 { |
|
1964 eng.globalObject().setProperty("globalGetterSetterProperty", eng.newFunction(getterSetter), |
|
1965 QScriptValue::PropertyGetter | QScriptValue::PropertySetter); |
|
1966 eng.evaluate("globalGetterSetterProperty = 123"); |
|
1967 { |
|
1968 QScriptValue ret = eng.evaluate("globalGetterSetterProperty"); |
|
1969 QVERIFY(ret.isNumber()); |
|
1970 QVERIFY(ret.strictlyEquals(QScriptValue(&eng, 123))); |
|
1971 } |
|
1972 QCOMPARE(eng.evaluate("typeof globalGetterSetterProperty").toString(), |
|
1973 QString::fromLatin1("number")); |
|
1974 { |
|
1975 QScriptValue ret = eng.evaluate("this.globalGetterSetterProperty()"); |
|
1976 QVERIFY(ret.isError()); |
|
1977 QCOMPARE(ret.toString(), QString::fromLatin1("TypeError: Result of expression 'this.globalGetterSetterProperty' [123] is not a function.")); |
|
1978 } |
|
1979 { |
|
1980 QScriptValue ret = eng.evaluate("new this.globalGetterSetterProperty()"); |
|
1981 QVERIFY(ret.isError()); |
|
1982 QCOMPARE(ret.toString(), QString::fromLatin1("TypeError: Result of expression 'this.globalGetterSetterProperty' [123] is not a constructor.")); |
|
1983 } |
|
1984 } |
|
1985 |
|
1986 // "upgrading" an existing property to become a getter+setter |
|
1987 { |
|
1988 QScriptValue object7 = eng.newObject(); |
|
1989 QScriptValue num(&eng, 123); |
|
1990 object7.setProperty("foo", num); |
|
1991 object7.setProperty("foo", eng.newFunction(getterSetter), |
|
1992 QScriptValue::PropertyGetter | QScriptValue::PropertySetter); |
|
1993 QVERIFY(!object7.property("x").isValid()); |
|
1994 object7.setProperty("foo", num); |
|
1995 QVERIFY(object7.property("x").equals(num)); |
|
1996 } |
|
1997 } |
|
1998 |
|
1999 eng.globalObject().setProperty("object", object); |
|
2000 |
|
2001 // ReadOnly |
|
2002 object.setProperty("readOnlyProperty", num, QScriptValue::ReadOnly); |
|
2003 QCOMPARE(object.propertyFlags("readOnlyProperty"), QScriptValue::ReadOnly); |
|
2004 QCOMPARE(object.property("readOnlyProperty").strictlyEquals(num), true); |
|
2005 eng.evaluate("object.readOnlyProperty = !object.readOnlyProperty"); |
|
2006 QCOMPARE(object.property("readOnlyProperty").strictlyEquals(num), true); |
|
2007 // should still be part of enumeration |
|
2008 { |
|
2009 QScriptValue ret = eng.evaluate( |
|
2010 "found = false;" |
|
2011 "for (var p in object) {" |
|
2012 " if (p == 'readOnlyProperty') {" |
|
2013 " found = true; break;" |
|
2014 " }" |
|
2015 "} found"); |
|
2016 QCOMPARE(ret.strictlyEquals(QScriptValue(&eng, true)), true); |
|
2017 } |
|
2018 // should still be deletable |
|
2019 { |
|
2020 QScriptValue ret = eng.evaluate("delete object.readOnlyProperty"); |
|
2021 QCOMPARE(ret.strictlyEquals(QScriptValue(&eng, true)), true); |
|
2022 QCOMPARE(object.property("readOnlyProperty").isValid(), false); |
|
2023 } |
|
2024 |
|
2025 // Undeletable |
|
2026 object.setProperty("undeletableProperty", num, QScriptValue::Undeletable); |
|
2027 QCOMPARE(object.propertyFlags("undeletableProperty"), QScriptValue::Undeletable); |
|
2028 QCOMPARE(object.property("undeletableProperty").strictlyEquals(num), true); |
|
2029 { |
|
2030 QScriptValue ret = eng.evaluate("delete object.undeletableProperty"); |
|
2031 QCOMPARE(ret.strictlyEquals(QScriptValue(&eng, true)), false); |
|
2032 QCOMPARE(object.property("undeletableProperty").strictlyEquals(num), true); |
|
2033 } |
|
2034 // should still be writable |
|
2035 eng.evaluate("object.undeletableProperty = object.undeletableProperty + 1"); |
|
2036 QCOMPARE(object.property("undeletableProperty").toNumber(), num.toNumber() + 1); |
|
2037 // should still be part of enumeration |
|
2038 { |
|
2039 QScriptValue ret = eng.evaluate( |
|
2040 "found = false;" |
|
2041 "for (var p in object) {" |
|
2042 " if (p == 'undeletableProperty') {" |
|
2043 " found = true; break;" |
|
2044 " }" |
|
2045 "} found"); |
|
2046 QCOMPARE(ret.strictlyEquals(QScriptValue(&eng, true)), true); |
|
2047 } |
|
2048 // should still be deletable from C++ |
|
2049 object.setProperty("undeletableProperty", QScriptValue()); |
|
2050 QVERIFY(!object.property("undeletableProperty").isValid()); |
|
2051 QCOMPARE(object.propertyFlags("undeletableProperty"), 0); |
|
2052 |
|
2053 // SkipInEnumeration |
|
2054 object.setProperty("dontEnumProperty", num, QScriptValue::SkipInEnumeration); |
|
2055 QCOMPARE(object.propertyFlags("dontEnumProperty"), QScriptValue::SkipInEnumeration); |
|
2056 QCOMPARE(object.property("dontEnumProperty").strictlyEquals(num), true); |
|
2057 // should not be part of enumeration |
|
2058 { |
|
2059 QScriptValue ret = eng.evaluate( |
|
2060 "found = false;" |
|
2061 "for (var p in object) {" |
|
2062 " if (p == 'dontEnumProperty') {" |
|
2063 " found = true; break;" |
|
2064 " }" |
|
2065 "} found"); |
|
2066 QCOMPARE(ret.strictlyEquals(QScriptValue(&eng, false)), true); |
|
2067 } |
|
2068 // should still be writable |
|
2069 eng.evaluate("object.dontEnumProperty = object.dontEnumProperty + 1"); |
|
2070 QCOMPARE(object.property("dontEnumProperty").toNumber(), num.toNumber() + 1); |
|
2071 // should still be deletable |
|
2072 { |
|
2073 QScriptValue ret = eng.evaluate("delete object.dontEnumProperty"); |
|
2074 QCOMPARE(ret.strictlyEquals(QScriptValue(&eng, true)), true); |
|
2075 QCOMPARE(object.property("dontEnumProperty").isValid(), false); |
|
2076 } |
|
2077 |
|
2078 // change flags |
|
2079 object.setProperty("flagProperty", str); |
|
2080 QCOMPARE(object.propertyFlags("flagProperty"), static_cast<QScriptValue::PropertyFlags>(0)); |
|
2081 |
|
2082 object.setProperty("flagProperty", str, QScriptValue::ReadOnly); |
|
2083 QCOMPARE(object.propertyFlags("flagProperty"), QScriptValue::ReadOnly); |
|
2084 |
|
2085 object.setProperty("flagProperty", str, object.propertyFlags("flagProperty") | QScriptValue::Undeletable); |
|
2086 QCOMPARE(object.propertyFlags("flagProperty"), QScriptValue::ReadOnly | QScriptValue::Undeletable); |
|
2087 |
|
2088 object.setProperty("flagProperty", str, QScriptValue::KeepExistingFlags); |
|
2089 QCOMPARE(object.propertyFlags("flagProperty"), QScriptValue::ReadOnly | QScriptValue::Undeletable); |
|
2090 |
|
2091 object.setProperty("flagProperty", str, QScriptValue::UserRange); |
|
2092 QCOMPARE(object.propertyFlags("flagProperty"), QScriptValue::UserRange); |
|
2093 |
|
2094 // flags of property in the prototype |
|
2095 { |
|
2096 QScriptValue object2 = eng.newObject(); |
|
2097 object2.setPrototype(object); |
|
2098 QCOMPARE(object2.propertyFlags("flagProperty", QScriptValue::ResolveLocal), 0); |
|
2099 QCOMPARE(object2.propertyFlags("flagProperty"), QScriptValue::UserRange); |
|
2100 } |
|
2101 |
|
2102 // using interned strings |
|
2103 QScriptString foo = eng.toStringHandle("foo"); |
|
2104 |
|
2105 object.setProperty(foo, QScriptValue()); |
|
2106 QVERIFY(!object.property(foo).isValid()); |
|
2107 |
|
2108 object.setProperty(foo, num); |
|
2109 QVERIFY(object.property(foo).strictlyEquals(num)); |
|
2110 QVERIFY(object.property("foo").strictlyEquals(num)); |
|
2111 QVERIFY(object.propertyFlags(foo) == 0); |
|
2112 } |
|
2113 |
|
2114 void tst_QScriptValue::arrayElementGetterSetter() |
|
2115 { |
|
2116 QScriptEngine eng; |
|
2117 QScriptValue obj = eng.newObject(); |
|
2118 obj.setProperty(1, eng.newFunction(getterSetter), QScriptValue::PropertyGetter|QScriptValue::PropertySetter); |
|
2119 { |
|
2120 QScriptValue num(123); |
|
2121 obj.setProperty("x", num); |
|
2122 QScriptValue ret = obj.property(1); |
|
2123 QVERIFY(ret.isValid()); |
|
2124 QVERIFY(ret.equals(num)); |
|
2125 } |
|
2126 { |
|
2127 QScriptValue num(456); |
|
2128 obj.setProperty(1, num); |
|
2129 QScriptValue ret = obj.property(1); |
|
2130 QVERIFY(ret.isValid()); |
|
2131 QVERIFY(ret.equals(num)); |
|
2132 QVERIFY(ret.equals(obj.property("1"))); |
|
2133 } |
|
2134 QCOMPARE(obj.propertyFlags("1"), QScriptValue::PropertyGetter|QScriptValue::PropertySetter); |
|
2135 |
|
2136 obj.setProperty(1, QScriptValue(), QScriptValue::PropertyGetter|QScriptValue::PropertySetter); |
|
2137 QVERIFY(obj.propertyFlags("1") == 0); |
|
2138 } |
|
2139 |
|
2140 void tst_QScriptValue::getSetPrototype() |
|
2141 { |
|
2142 QScriptEngine eng; |
|
2143 |
|
2144 QScriptValue object = eng.newObject(); |
|
2145 |
|
2146 QScriptValue object2 = eng.newObject(); |
|
2147 object2.setPrototype(object); |
|
2148 |
|
2149 QCOMPARE(object2.prototype().strictlyEquals(object), true); |
|
2150 |
|
2151 QScriptValue inv; |
|
2152 inv.setPrototype(object); |
|
2153 QCOMPARE(inv.prototype().isValid(), false); |
|
2154 |
|
2155 QScriptEngine otherEngine; |
|
2156 QScriptValue object3 = otherEngine.newObject(); |
|
2157 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setPrototype() failed: cannot set a prototype created in a different engine"); |
|
2158 object2.setPrototype(object3); |
|
2159 QCOMPARE(object2.prototype().strictlyEquals(object), true); |
|
2160 |
|
2161 // cyclic prototypes |
|
2162 QScriptValue old = object.prototype(); |
|
2163 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setPrototype() failed: cyclic prototype value"); |
|
2164 object.setPrototype(object); |
|
2165 QCOMPARE(object.prototype().strictlyEquals(old), true); |
|
2166 |
|
2167 object2.setPrototype(object); |
|
2168 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setPrototype() failed: cyclic prototype value"); |
|
2169 object.setPrototype(object2); |
|
2170 QCOMPARE(object.prototype().strictlyEquals(old), true); |
|
2171 |
|
2172 { |
|
2173 QScriptValue ret = eng.evaluate("o = { }; p = { }; o.__proto__ = p; p.__proto__ = o"); |
|
2174 QCOMPARE(eng.hasUncaughtException(), true); |
|
2175 QVERIFY(ret.strictlyEquals(eng.uncaughtException())); |
|
2176 QCOMPARE(ret.isError(), true); |
|
2177 QCOMPARE(ret.toString(), QLatin1String("Error: cyclic __proto__ value")); |
|
2178 } |
|
2179 { |
|
2180 QScriptValue ret = eng.evaluate("p.__proto__ = { }"); |
|
2181 QCOMPARE(eng.hasUncaughtException(), false); |
|
2182 QCOMPARE(ret.isError(), false); |
|
2183 } |
|
2184 } |
|
2185 |
|
2186 void tst_QScriptValue::getSetScope() |
|
2187 { |
|
2188 QScriptEngine eng; |
|
2189 |
|
2190 QScriptValue object = eng.newObject(); |
|
2191 QCOMPARE(object.scope().isValid(), false); |
|
2192 |
|
2193 QScriptValue object2 = eng.newObject(); |
|
2194 object2.setScope(object); |
|
2195 |
|
2196 QCOMPARE(object2.scope().strictlyEquals(object), true); |
|
2197 |
|
2198 object.setProperty("foo", 123); |
|
2199 QVERIFY(!object2.property("foo").isValid()); |
|
2200 { |
|
2201 QScriptValue ret = object2.property("foo", QScriptValue::ResolveScope); |
|
2202 QVERIFY(ret.isNumber()); |
|
2203 QCOMPARE(ret.toInt32(), 123); |
|
2204 } |
|
2205 |
|
2206 QScriptValue inv; |
|
2207 inv.setScope(object); |
|
2208 QCOMPARE(inv.scope().isValid(), false); |
|
2209 |
|
2210 QScriptEngine otherEngine; |
|
2211 QScriptValue object3 = otherEngine.newObject(); |
|
2212 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setScope() failed: cannot set a scope object created in a different engine"); |
|
2213 object2.setScope(object3); |
|
2214 QCOMPARE(object2.scope().strictlyEquals(object), true); |
|
2215 |
|
2216 object2.setScope(QScriptValue()); |
|
2217 QVERIFY(!object2.scope().isValid()); |
|
2218 } |
|
2219 |
|
2220 void tst_QScriptValue::getSetData() |
|
2221 { |
|
2222 QScriptEngine eng; |
|
2223 QScriptValue object = eng.newObject(); |
|
2224 QVERIFY(!object.data().isValid()); |
|
2225 QScriptValue v1(true); |
|
2226 object.setData(v1); |
|
2227 QVERIFY(object.data().strictlyEquals(v1)); |
|
2228 QScriptValue v2(123); |
|
2229 object.setData(v2); |
|
2230 QVERIFY(object.data().strictlyEquals(v2)); |
|
2231 QScriptValue v3 = eng.newObject(); |
|
2232 object.setData(v3); |
|
2233 QVERIFY(object.data().strictlyEquals(v3)); |
|
2234 object.setData(QScriptValue()); |
|
2235 QVERIFY(!object.data().isValid()); |
|
2236 } |
|
2237 |
|
2238 class TestScriptClass : public QScriptClass |
|
2239 { |
|
2240 public: |
|
2241 TestScriptClass(QScriptEngine *engine) : QScriptClass(engine) {} |
|
2242 }; |
|
2243 |
|
2244 void tst_QScriptValue::getSetScriptClass() |
|
2245 { |
|
2246 QScriptEngine eng; |
|
2247 QScriptValue inv; |
|
2248 QCOMPARE(inv.scriptClass(), (QScriptClass*)0); |
|
2249 QScriptValue num(123); |
|
2250 QCOMPARE(num.scriptClass(), (QScriptClass*)0); |
|
2251 |
|
2252 TestScriptClass testClass(&eng); |
|
2253 // object created in C++ (newObject()) |
|
2254 { |
|
2255 QScriptValue obj = eng.newObject(); |
|
2256 QCOMPARE(obj.scriptClass(), (QScriptClass*)0); |
|
2257 obj.setScriptClass(&testClass); |
|
2258 QCOMPARE(obj.scriptClass(), (QScriptClass*)&testClass); |
|
2259 obj.setScriptClass(0); |
|
2260 QCOMPARE(obj.scriptClass(), (QScriptClass*)0); |
|
2261 } |
|
2262 // object created in JS |
|
2263 { |
|
2264 QScriptValue obj = eng.evaluate("new Object"); |
|
2265 QVERIFY(!eng.hasUncaughtException()); |
|
2266 QVERIFY(obj.isObject()); |
|
2267 QCOMPARE(obj.scriptClass(), (QScriptClass*)0); |
|
2268 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setScriptClass() failed: cannot change class of non-QScriptObject"); |
|
2269 obj.setScriptClass(&testClass); |
|
2270 QEXPECT_FAIL("", "With JSC back-end, the class of a plain object created in JS can't be changed", Continue); |
|
2271 QCOMPARE(obj.scriptClass(), (QScriptClass*)&testClass); |
|
2272 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::setScriptClass() failed: cannot change class of non-QScriptObject"); |
|
2273 obj.setScriptClass(0); |
|
2274 QCOMPARE(obj.scriptClass(), (QScriptClass*)0); |
|
2275 } |
|
2276 // object that already has a(n internal) class |
|
2277 { |
|
2278 QScriptValue obj = eng.newVariant(QUrl("http://example.com")); |
|
2279 QVERIFY(obj.isVariant()); |
|
2280 QCOMPARE(obj.scriptClass(), (QScriptClass*)0); |
|
2281 obj.setScriptClass(&testClass); |
|
2282 QCOMPARE(obj.scriptClass(), (QScriptClass*)&testClass); |
|
2283 QVERIFY(obj.isObject()); |
|
2284 QVERIFY(!obj.isVariant()); |
|
2285 QVERIFY(!obj.toVariant().isValid()); |
|
2286 } |
|
2287 { |
|
2288 QScriptValue obj = eng.newQObject(this); |
|
2289 QVERIFY(obj.isQObject()); |
|
2290 QCOMPARE(obj.scriptClass(), (QScriptClass*)0); |
|
2291 obj.setScriptClass(&testClass); |
|
2292 QCOMPARE(obj.scriptClass(), (QScriptClass*)&testClass); |
|
2293 QVERIFY(obj.isObject()); |
|
2294 QVERIFY(!obj.isQObject()); |
|
2295 QVERIFY(obj.toQObject() == 0); |
|
2296 } |
|
2297 } |
|
2298 |
|
2299 static QScriptValue getArg(QScriptContext *ctx, QScriptEngine *) |
|
2300 { |
|
2301 return ctx->argument(0); |
|
2302 } |
|
2303 |
|
2304 static QScriptValue evaluateArg(QScriptContext *, QScriptEngine *eng) |
|
2305 { |
|
2306 return eng->evaluate("arguments[0]"); |
|
2307 } |
|
2308 |
|
2309 static QScriptValue addArgs(QScriptContext *, QScriptEngine *eng) |
|
2310 { |
|
2311 return eng->evaluate("arguments[0] + arguments[1]"); |
|
2312 } |
|
2313 |
|
2314 static QScriptValue returnInvalidValue(QScriptContext *, QScriptEngine *) |
|
2315 { |
|
2316 return QScriptValue(); |
|
2317 } |
|
2318 |
|
2319 void tst_QScriptValue::call() |
|
2320 { |
|
2321 QScriptEngine eng; |
|
2322 |
|
2323 { |
|
2324 QScriptValue fun = eng.evaluate("(function() { return 1; })"); |
|
2325 QVERIFY(fun.isFunction()); |
|
2326 QScriptValue result = fun.call(); |
|
2327 QVERIFY(result.isNumber()); |
|
2328 QCOMPARE(result.toInt32(), 1); |
|
2329 } |
|
2330 |
|
2331 QScriptValue Object = eng.evaluate("Object"); |
|
2332 QCOMPARE(Object.isFunction(), true); |
|
2333 { |
|
2334 QScriptValue result = Object.call(Object); |
|
2335 QCOMPARE(result.isObject(), true); |
|
2336 } |
|
2337 |
|
2338 // test that call() doesn't construct new objects |
|
2339 QScriptValue Number = eng.evaluate("Number"); |
|
2340 QCOMPARE(Object.isFunction(), true); |
|
2341 { |
|
2342 QScriptValueList args; |
|
2343 args << QScriptValue(&eng, 123); |
|
2344 QScriptValue result = Number.call(Object, args); |
|
2345 QCOMPARE(result.strictlyEquals(args.at(0)), true); |
|
2346 } |
|
2347 |
|
2348 // test that correct "this" object is used |
|
2349 { |
|
2350 QScriptValue fun = eng.evaluate("(function() { return this; })"); |
|
2351 QCOMPARE(fun.isFunction(), true); |
|
2352 |
|
2353 { |
|
2354 QScriptValue numberObject = QScriptValue(&eng, 123.0).toObject(); |
|
2355 QScriptValue result = fun.call(numberObject); |
|
2356 QCOMPARE(result.isObject(), true); |
|
2357 QCOMPARE(result.toNumber(), 123.0); |
|
2358 } |
|
2359 } |
|
2360 |
|
2361 // test that correct arguments are passed |
|
2362 { |
|
2363 QScriptValue fun = eng.evaluate("(function() { return arguments[0]; })"); |
|
2364 QCOMPARE(fun.isFunction(), true); |
|
2365 |
|
2366 { |
|
2367 QScriptValue result = fun.call(eng.undefinedValue()); |
|
2368 QCOMPARE(result.isUndefined(), true); |
|
2369 } |
|
2370 |
|
2371 { |
|
2372 QScriptValueList args; |
|
2373 args << QScriptValue(&eng, 123.0); |
|
2374 QScriptValue result = fun.call(eng.undefinedValue(), args); |
|
2375 QCOMPARE(result.isNumber(), true); |
|
2376 QCOMPARE(result.toNumber(), 123.0); |
|
2377 } |
|
2378 |
|
2379 // V2 constructors |
|
2380 { |
|
2381 QScriptValueList args; |
|
2382 args << QScriptValue(123.0); |
|
2383 QScriptValue result = fun.call(eng.undefinedValue(), args); |
|
2384 QCOMPARE(result.isNumber(), true); |
|
2385 QCOMPARE(result.toNumber(), 123.0); |
|
2386 } |
|
2387 { |
|
2388 QScriptValue args = eng.newArray(); |
|
2389 args.setProperty(0, 123); |
|
2390 QScriptValue result = fun.call(eng.undefinedValue(), args); |
|
2391 QVERIFY(result.isNumber()); |
|
2392 QCOMPARE(result.toNumber(), 123.0); |
|
2393 } |
|
2394 } |
|
2395 |
|
2396 { |
|
2397 QScriptValue fun = eng.evaluate("(function() { return arguments[1]; })"); |
|
2398 QCOMPARE(fun.isFunction(), true); |
|
2399 |
|
2400 { |
|
2401 QScriptValueList args; |
|
2402 args << QScriptValue(&eng, 123.0) << QScriptValue(&eng, 456.0); |
|
2403 QScriptValue result = fun.call(eng.undefinedValue(), args); |
|
2404 QCOMPARE(result.isNumber(), true); |
|
2405 QCOMPARE(result.toNumber(), 456.0); |
|
2406 } |
|
2407 { |
|
2408 QScriptValue args = eng.newArray(); |
|
2409 args.setProperty(0, 123); |
|
2410 args.setProperty(1, 456); |
|
2411 QScriptValue result = fun.call(eng.undefinedValue(), args); |
|
2412 QVERIFY(result.isNumber()); |
|
2413 QCOMPARE(result.toNumber(), 456.0); |
|
2414 } |
|
2415 } |
|
2416 |
|
2417 { |
|
2418 QScriptValue fun = eng.evaluate("(function() { throw new Error('foo'); })"); |
|
2419 QCOMPARE(fun.isFunction(), true); |
|
2420 QVERIFY(!eng.hasUncaughtException()); |
|
2421 |
|
2422 { |
|
2423 QScriptValue result = fun.call(); |
|
2424 QCOMPARE(result.isError(), true); |
|
2425 QCOMPARE(eng.hasUncaughtException(), true); |
|
2426 QVERIFY(result.strictlyEquals(eng.uncaughtException())); |
|
2427 } |
|
2428 } |
|
2429 |
|
2430 { |
|
2431 eng.clearExceptions(); |
|
2432 QScriptValue fun = eng.newFunction(getArg); |
|
2433 { |
|
2434 QScriptValueList args; |
|
2435 args << QScriptValue(&eng, 123.0); |
|
2436 QScriptValue result = fun.call(eng.undefinedValue(), args); |
|
2437 QVERIFY(!eng.hasUncaughtException()); |
|
2438 QCOMPARE(result.isNumber(), true); |
|
2439 QCOMPARE(result.toNumber(), 123.0); |
|
2440 } |
|
2441 // V2 constructors |
|
2442 { |
|
2443 QScriptValueList args; |
|
2444 args << QScriptValue(123.0); |
|
2445 QScriptValue result = fun.call(eng.undefinedValue(), args); |
|
2446 QCOMPARE(result.isNumber(), true); |
|
2447 QCOMPARE(result.toNumber(), 123.0); |
|
2448 } |
|
2449 { |
|
2450 QScriptValue args = eng.newArray(); |
|
2451 args.setProperty(0, 123); |
|
2452 QScriptValue result = fun.call(eng.undefinedValue(), args); |
|
2453 QVERIFY(result.isNumber()); |
|
2454 QCOMPARE(result.toNumber(), 123.0); |
|
2455 } |
|
2456 } |
|
2457 |
|
2458 { |
|
2459 QScriptValue fun = eng.newFunction(evaluateArg); |
|
2460 { |
|
2461 QScriptValueList args; |
|
2462 args << QScriptValue(&eng, 123.0); |
|
2463 QScriptValue result = fun.call(eng.undefinedValue(), args); |
|
2464 QVERIFY(!eng.hasUncaughtException()); |
|
2465 QCOMPARE(result.isNumber(), true); |
|
2466 QCOMPARE(result.toNumber(), 123.0); |
|
2467 } |
|
2468 } |
|
2469 |
|
2470 QScriptValue inv; |
|
2471 QCOMPARE(inv.call().isValid(), false); |
|
2472 |
|
2473 // test that invalid arguments are handled gracefully |
|
2474 { |
|
2475 QScriptValue fun = eng.newFunction(getArg); |
|
2476 { |
|
2477 QScriptValueList args; |
|
2478 args << QScriptValue(); |
|
2479 QScriptValue ret = fun.call(QScriptValue(), args); |
|
2480 QVERIFY(!eng.hasUncaughtException()); |
|
2481 QCOMPARE(ret.isValid(), true); |
|
2482 QCOMPARE(ret.isUndefined(), true); |
|
2483 } |
|
2484 } |
|
2485 { |
|
2486 QScriptValue fun = eng.newFunction(evaluateArg); |
|
2487 { |
|
2488 QScriptValueList args; |
|
2489 args << QScriptValue(); |
|
2490 QScriptValue ret = fun.call(QScriptValue(), args); |
|
2491 QCOMPARE(ret.isValid(), true); |
|
2492 QCOMPARE(ret.isUndefined(), true); |
|
2493 } |
|
2494 } |
|
2495 { |
|
2496 QScriptValue fun = eng.newFunction(addArgs); |
|
2497 { |
|
2498 QScriptValueList args; |
|
2499 args << QScriptValue() << QScriptValue(); |
|
2500 QScriptValue ret = fun.call(QScriptValue(), args); |
|
2501 QCOMPARE(ret.isValid(), true); |
|
2502 QCOMPARE(ret.isNumber(), true); |
|
2503 QCOMPARE(qIsNaN(ret.toNumber()), true); |
|
2504 } |
|
2505 } |
|
2506 { |
|
2507 QScriptValue fun = eng.evaluate("Object"); |
|
2508 QVERIFY(fun.isFunction()); |
|
2509 QScriptEngine eng2; |
|
2510 QScriptValue objectInDifferentEngine = eng2.newObject(); |
|
2511 QScriptValueList args; |
|
2512 args << objectInDifferentEngine; |
|
2513 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::call() failed: cannot call function with argument created in a different engine"); |
|
2514 fun.call(QScriptValue(), args); |
|
2515 } |
|
2516 |
|
2517 // test that invalid return value is handled gracefully |
|
2518 { |
|
2519 QScriptValue fun = eng.newFunction(returnInvalidValue); |
|
2520 eng.globalObject().setProperty("returnInvalidValue", fun); |
|
2521 QScriptValue ret = eng.evaluate("returnInvalidValue() + returnInvalidValue()"); |
|
2522 QCOMPARE(ret.isValid(), true); |
|
2523 QCOMPARE(ret.isNumber(), true); |
|
2524 QCOMPARE(qIsNaN(ret.toNumber()), true); |
|
2525 } |
|
2526 |
|
2527 { |
|
2528 QScriptEngine otherEngine; |
|
2529 QScriptValue fun = otherEngine.evaluate("(function() { return 1; })"); |
|
2530 QVERIFY(fun.isFunction()); |
|
2531 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::call() failed: " |
|
2532 "cannot call function with thisObject created in " |
|
2533 "a different engine"); |
|
2534 QCOMPARE(fun.call(Object).isValid(), false); |
|
2535 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::call() failed: " |
|
2536 "cannot call function with argument created in " |
|
2537 "a different engine"); |
|
2538 QCOMPARE(fun.call(QScriptValue(), QScriptValueList() << QScriptValue(&eng, 123)).isValid(), false); |
|
2539 } |
|
2540 |
|
2541 { |
|
2542 QScriptValue fun = eng.evaluate("(function() { return arguments; })"); |
|
2543 QVERIFY(fun.isFunction()); |
|
2544 QScriptValue array = eng.newArray(3); |
|
2545 array.setProperty(0, QScriptValue(&eng, 123.0)); |
|
2546 array.setProperty(1, QScriptValue(&eng, 456.0)); |
|
2547 array.setProperty(2, QScriptValue(&eng, 789.0)); |
|
2548 // call with single array object as arguments |
|
2549 QScriptValue ret = fun.call(QScriptValue(), array); |
|
2550 QVERIFY(!eng.hasUncaughtException()); |
|
2551 QCOMPARE(ret.isError(), false); |
|
2552 QCOMPARE(ret.property(0).strictlyEquals(array.property(0)), true); |
|
2553 QCOMPARE(ret.property(1).strictlyEquals(array.property(1)), true); |
|
2554 QCOMPARE(ret.property(2).strictlyEquals(array.property(2)), true); |
|
2555 // call with arguments object as arguments |
|
2556 QScriptValue ret2 = fun.call(QScriptValue(), ret); |
|
2557 QCOMPARE(ret2.isError(), false); |
|
2558 QCOMPARE(ret2.property(0).strictlyEquals(ret.property(0)), true); |
|
2559 QCOMPARE(ret2.property(1).strictlyEquals(ret.property(1)), true); |
|
2560 QCOMPARE(ret2.property(2).strictlyEquals(ret.property(2)), true); |
|
2561 // call with null as arguments |
|
2562 QScriptValue ret3 = fun.call(QScriptValue(), eng.nullValue()); |
|
2563 QCOMPARE(ret3.isError(), false); |
|
2564 QCOMPARE(ret3.property("length").isNumber(), true); |
|
2565 QCOMPARE(ret3.property("length").toNumber(), 0.0); |
|
2566 // call with undefined as arguments |
|
2567 QScriptValue ret4 = fun.call(QScriptValue(), eng.undefinedValue()); |
|
2568 QCOMPARE(ret4.isError(), false); |
|
2569 QCOMPARE(ret4.property("length").isNumber(), true); |
|
2570 QCOMPARE(ret4.property("length").toNumber(), 0.0); |
|
2571 // call with something else as arguments |
|
2572 QScriptValue ret5 = fun.call(QScriptValue(), QScriptValue(&eng, 123.0)); |
|
2573 QCOMPARE(ret5.isError(), true); |
|
2574 // call with a non-array object as arguments |
|
2575 QScriptValue ret6 = fun.call(QScriptValue(), eng.globalObject()); |
|
2576 QVERIFY(ret6.isError()); |
|
2577 QCOMPARE(ret6.toString(), QString::fromLatin1("TypeError: Arguments must be an array")); |
|
2578 } |
|
2579 |
|
2580 // calling things that are not functions |
|
2581 QVERIFY(!QScriptValue(false).call().isValid()); |
|
2582 QVERIFY(!QScriptValue(123).call().isValid()); |
|
2583 QVERIFY(!QScriptValue(QString::fromLatin1("ciao")).call().isValid()); |
|
2584 QVERIFY(!QScriptValue(QScriptValue::UndefinedValue).call().isValid()); |
|
2585 QVERIFY(!QScriptValue(QScriptValue::NullValue).call().isValid()); |
|
2586 } |
|
2587 |
|
2588 static QScriptValue ctorReturningUndefined(QScriptContext *ctx, QScriptEngine *) |
|
2589 { |
|
2590 ctx->thisObject().setProperty("foo", 123); |
|
2591 return QScriptValue(QScriptValue::UndefinedValue); |
|
2592 } |
|
2593 |
|
2594 static QScriptValue ctorReturningNewObject(QScriptContext *, QScriptEngine *eng) |
|
2595 { |
|
2596 QScriptValue result = eng->newObject(); |
|
2597 result.setProperty("bar", 456); |
|
2598 return result; |
|
2599 } |
|
2600 |
|
2601 void tst_QScriptValue::construct() |
|
2602 { |
|
2603 QScriptEngine eng; |
|
2604 |
|
2605 { |
|
2606 QScriptValue fun = eng.evaluate("(function () { this.foo = 123; })"); |
|
2607 QVERIFY(fun.isFunction()); |
|
2608 QScriptValue ret = fun.construct(); |
|
2609 QVERIFY(ret.isObject()); |
|
2610 QVERIFY(ret.instanceOf(fun)); |
|
2611 QCOMPARE(ret.property("foo").toInt32(), 123); |
|
2612 } |
|
2613 // returning a different object overrides the default-constructed one |
|
2614 { |
|
2615 QScriptValue fun = eng.evaluate("(function () { return { bar: 456 }; })"); |
|
2616 QVERIFY(fun.isFunction()); |
|
2617 QScriptValue ret = fun.construct(); |
|
2618 QVERIFY(ret.isObject()); |
|
2619 QVERIFY(!ret.instanceOf(fun)); |
|
2620 QCOMPARE(ret.property("bar").toInt32(), 456); |
|
2621 } |
|
2622 |
|
2623 { |
|
2624 QScriptValue fun = eng.newFunction(ctorReturningUndefined); |
|
2625 QScriptValue ret = fun.construct(); |
|
2626 QVERIFY(ret.isObject()); |
|
2627 QVERIFY(ret.instanceOf(fun)); |
|
2628 QCOMPARE(ret.property("foo").toInt32(), 123); |
|
2629 } |
|
2630 { |
|
2631 QScriptValue fun = eng.newFunction(ctorReturningNewObject); |
|
2632 QScriptValue ret = fun.construct(); |
|
2633 QVERIFY(ret.isObject()); |
|
2634 QVERIFY(!ret.instanceOf(fun)); |
|
2635 QCOMPARE(ret.property("bar").toInt32(), 456); |
|
2636 } |
|
2637 |
|
2638 QScriptValue Number = eng.evaluate("Number"); |
|
2639 QCOMPARE(Number.isFunction(), true); |
|
2640 { |
|
2641 QScriptValueList args; |
|
2642 args << QScriptValue(&eng, 123); |
|
2643 QScriptValue ret = Number.construct(args); |
|
2644 QCOMPARE(ret.isObject(), true); |
|
2645 QCOMPARE(ret.toNumber(), args.at(0).toNumber()); |
|
2646 } |
|
2647 |
|
2648 // test that internal prototype is set correctly |
|
2649 { |
|
2650 QScriptValue fun = eng.evaluate("(function() { return this.__proto__; })"); |
|
2651 QCOMPARE(fun.isFunction(), true); |
|
2652 QCOMPARE(fun.property("prototype").isObject(), true); |
|
2653 QScriptValue ret = fun.construct(); |
|
2654 QCOMPARE(fun.property("prototype").strictlyEquals(ret), true); |
|
2655 } |
|
2656 |
|
2657 // test that we return the new object even if a non-object value is returned from the function |
|
2658 { |
|
2659 QScriptValue fun = eng.evaluate("(function() { return 123; })"); |
|
2660 QCOMPARE(fun.isFunction(), true); |
|
2661 QScriptValue ret = fun.construct(); |
|
2662 QCOMPARE(ret.isObject(), true); |
|
2663 } |
|
2664 |
|
2665 { |
|
2666 QScriptValue fun = eng.evaluate("(function() { throw new Error('foo'); })"); |
|
2667 QCOMPARE(fun.isFunction(), true); |
|
2668 QScriptValue ret = fun.construct(); |
|
2669 QCOMPARE(ret.isError(), true); |
|
2670 QCOMPARE(eng.hasUncaughtException(), true); |
|
2671 QVERIFY(ret.strictlyEquals(eng.uncaughtException())); |
|
2672 } |
|
2673 |
|
2674 QScriptValue inv; |
|
2675 QCOMPARE(inv.construct().isValid(), false); |
|
2676 |
|
2677 { |
|
2678 QScriptValue fun = eng.evaluate("(function() { return arguments; })"); |
|
2679 QVERIFY(fun.isFunction()); |
|
2680 QScriptValue array = eng.newArray(3); |
|
2681 array.setProperty(0, QScriptValue(&eng, 123.0)); |
|
2682 array.setProperty(1, QScriptValue(&eng, 456.0)); |
|
2683 array.setProperty(2, QScriptValue(&eng, 789.0)); |
|
2684 // construct with single array object as arguments |
|
2685 QScriptValue ret = fun.construct(array); |
|
2686 QVERIFY(!eng.hasUncaughtException()); |
|
2687 QVERIFY(ret.isValid()); |
|
2688 QVERIFY(ret.isObject()); |
|
2689 QCOMPARE(ret.property(0).strictlyEquals(array.property(0)), true); |
|
2690 QCOMPARE(ret.property(1).strictlyEquals(array.property(1)), true); |
|
2691 QCOMPARE(ret.property(2).strictlyEquals(array.property(2)), true); |
|
2692 // construct with arguments object as arguments |
|
2693 QScriptValue ret2 = fun.construct(ret); |
|
2694 QCOMPARE(ret2.property(0).strictlyEquals(ret.property(0)), true); |
|
2695 QCOMPARE(ret2.property(1).strictlyEquals(ret.property(1)), true); |
|
2696 QCOMPARE(ret2.property(2).strictlyEquals(ret.property(2)), true); |
|
2697 // construct with null as arguments |
|
2698 QScriptValue ret3 = fun.construct(eng.nullValue()); |
|
2699 QCOMPARE(ret3.isError(), false); |
|
2700 QCOMPARE(ret3.property("length").isNumber(), true); |
|
2701 QCOMPARE(ret3.property("length").toNumber(), 0.0); |
|
2702 // construct with undefined as arguments |
|
2703 QScriptValue ret4 = fun.construct(eng.undefinedValue()); |
|
2704 QCOMPARE(ret4.isError(), false); |
|
2705 QCOMPARE(ret4.property("length").isNumber(), true); |
|
2706 QCOMPARE(ret4.property("length").toNumber(), 0.0); |
|
2707 // construct with something else as arguments |
|
2708 QScriptValue ret5 = fun.construct(QScriptValue(&eng, 123.0)); |
|
2709 QCOMPARE(ret5.isError(), true); |
|
2710 // construct with a non-array object as arguments |
|
2711 QScriptValue ret6 = fun.construct(eng.globalObject()); |
|
2712 QVERIFY(ret6.isError()); |
|
2713 QCOMPARE(ret6.toString(), QString::fromLatin1("TypeError: Arguments must be an array")); |
|
2714 } |
|
2715 |
|
2716 // construct on things that are not functions |
|
2717 QVERIFY(!QScriptValue(false).construct().isValid()); |
|
2718 QVERIFY(!QScriptValue(123).construct().isValid()); |
|
2719 QVERIFY(!QScriptValue(QString::fromLatin1("ciao")).construct().isValid()); |
|
2720 QVERIFY(!QScriptValue(QScriptValue::UndefinedValue).construct().isValid()); |
|
2721 QVERIFY(!QScriptValue(QScriptValue::NullValue).construct().isValid()); |
|
2722 } |
|
2723 |
|
2724 void tst_QScriptValue::lessThan_old() |
|
2725 { |
|
2726 QScriptEngine eng; |
|
2727 |
|
2728 QVERIFY(!QScriptValue().lessThan(QScriptValue())); |
|
2729 |
|
2730 QScriptValue num = QScriptValue(&eng, 123); |
|
2731 QCOMPARE(num.lessThan(QScriptValue(&eng, 124)), true); |
|
2732 QCOMPARE(num.lessThan(QScriptValue(&eng, 122)), false); |
|
2733 QCOMPARE(num.lessThan(QScriptValue(&eng, 123)), false); |
|
2734 QCOMPARE(num.lessThan(QScriptValue(&eng, "124")), true); |
|
2735 QCOMPARE(num.lessThan(QScriptValue(&eng, "122")), false); |
|
2736 QCOMPARE(num.lessThan(QScriptValue(&eng, "123")), false); |
|
2737 QCOMPARE(num.lessThan(QScriptValue(&eng, qSNaN())), false); |
|
2738 QCOMPARE(num.lessThan(QScriptValue(&eng, +qInf())), true); |
|
2739 QCOMPARE(num.lessThan(QScriptValue(&eng, -qInf())), false); |
|
2740 QCOMPARE(num.lessThan(num), false); |
|
2741 QCOMPARE(num.lessThan(QScriptValue(&eng, 124).toObject()), true); |
|
2742 QCOMPARE(num.lessThan(QScriptValue(&eng, 122).toObject()), false); |
|
2743 QCOMPARE(num.lessThan(QScriptValue(&eng, 123).toObject()), false); |
|
2744 QCOMPARE(num.lessThan(QScriptValue(&eng, "124").toObject()), true); |
|
2745 QCOMPARE(num.lessThan(QScriptValue(&eng, "122").toObject()), false); |
|
2746 QCOMPARE(num.lessThan(QScriptValue(&eng, "123").toObject()), false); |
|
2747 QCOMPARE(num.lessThan(QScriptValue(&eng, qSNaN()).toObject()), false); |
|
2748 QCOMPARE(num.lessThan(QScriptValue(&eng, +qInf()).toObject()), true); |
|
2749 QCOMPARE(num.lessThan(QScriptValue(&eng, -qInf()).toObject()), false); |
|
2750 QCOMPARE(num.lessThan(num.toObject()), false); |
|
2751 QCOMPARE(num.lessThan(QScriptValue()), false); |
|
2752 |
|
2753 QScriptValue str = QScriptValue(&eng, "123"); |
|
2754 QCOMPARE(str.lessThan(QScriptValue(&eng, "124")), true); |
|
2755 QCOMPARE(str.lessThan(QScriptValue(&eng, "122")), false); |
|
2756 QCOMPARE(str.lessThan(QScriptValue(&eng, "123")), false); |
|
2757 QCOMPARE(str.lessThan(QScriptValue(&eng, 124)), true); |
|
2758 QCOMPARE(str.lessThan(QScriptValue(&eng, 122)), false); |
|
2759 QCOMPARE(str.lessThan(QScriptValue(&eng, 123)), false); |
|
2760 QCOMPARE(str.lessThan(str), false); |
|
2761 QCOMPARE(str.lessThan(QScriptValue(&eng, "124").toObject()), true); |
|
2762 QCOMPARE(str.lessThan(QScriptValue(&eng, "122").toObject()), false); |
|
2763 QCOMPARE(str.lessThan(QScriptValue(&eng, "123").toObject()), false); |
|
2764 QCOMPARE(str.lessThan(QScriptValue(&eng, 124).toObject()), true); |
|
2765 QCOMPARE(str.lessThan(QScriptValue(&eng, 122).toObject()), false); |
|
2766 QCOMPARE(str.lessThan(QScriptValue(&eng, 123).toObject()), false); |
|
2767 QCOMPARE(str.lessThan(str.toObject()), false); |
|
2768 QCOMPARE(str.lessThan(QScriptValue()), false); |
|
2769 |
|
2770 // V2 constructors |
|
2771 QScriptValue num2 = QScriptValue(123); |
|
2772 QCOMPARE(num2.lessThan(QScriptValue(124)), true); |
|
2773 QCOMPARE(num2.lessThan(QScriptValue(122)), false); |
|
2774 QCOMPARE(num2.lessThan(QScriptValue(123)), false); |
|
2775 QCOMPARE(num2.lessThan(QScriptValue("124")), true); |
|
2776 QCOMPARE(num2.lessThan(QScriptValue("122")), false); |
|
2777 QCOMPARE(num2.lessThan(QScriptValue("123")), false); |
|
2778 QCOMPARE(num2.lessThan(QScriptValue(qSNaN())), false); |
|
2779 QCOMPARE(num2.lessThan(QScriptValue(+qInf())), true); |
|
2780 QCOMPARE(num2.lessThan(QScriptValue(-qInf())), false); |
|
2781 QCOMPARE(num2.lessThan(num), false); |
|
2782 QCOMPARE(num2.lessThan(QScriptValue()), false); |
|
2783 |
|
2784 QScriptValue str2 = QScriptValue("123"); |
|
2785 QCOMPARE(str2.lessThan(QScriptValue("124")), true); |
|
2786 QCOMPARE(str2.lessThan(QScriptValue("122")), false); |
|
2787 QCOMPARE(str2.lessThan(QScriptValue("123")), false); |
|
2788 QCOMPARE(str2.lessThan(QScriptValue(124)), true); |
|
2789 QCOMPARE(str2.lessThan(QScriptValue(122)), false); |
|
2790 QCOMPARE(str2.lessThan(QScriptValue(123)), false); |
|
2791 QCOMPARE(str2.lessThan(str), false); |
|
2792 QCOMPARE(str2.lessThan(QScriptValue()), false); |
|
2793 |
|
2794 QScriptValue obj1 = eng.newObject(); |
|
2795 QScriptValue obj2 = eng.newObject(); |
|
2796 QCOMPARE(obj1.lessThan(obj2), false); |
|
2797 QCOMPARE(obj2.lessThan(obj1), false); |
|
2798 QCOMPARE(obj1.lessThan(obj1), false); |
|
2799 QCOMPARE(obj2.lessThan(obj2), false); |
|
2800 |
|
2801 QScriptValue date1 = eng.newDate(QDateTime(QDate(2000, 1, 1))); |
|
2802 QScriptValue date2 = eng.newDate(QDateTime(QDate(1999, 1, 1))); |
|
2803 QCOMPARE(date1.lessThan(date2), false); |
|
2804 QCOMPARE(date2.lessThan(date1), true); |
|
2805 QCOMPARE(date1.lessThan(date1), false); |
|
2806 QCOMPARE(date2.lessThan(date2), false); |
|
2807 QCOMPARE(date1.lessThan(QScriptValue()), false); |
|
2808 |
|
2809 QCOMPARE(QScriptValue().lessThan(date2), false); |
|
2810 |
|
2811 QScriptEngine otherEngine; |
|
2812 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::lessThan: " |
|
2813 "cannot compare to a value created in " |
|
2814 "a different engine"); |
|
2815 QCOMPARE(date1.lessThan(QScriptValue(&otherEngine, 123)), false); |
|
2816 } |
|
2817 |
|
2818 void tst_QScriptValue::equals_old() |
|
2819 { |
|
2820 QScriptEngine eng; |
|
2821 |
|
2822 QVERIFY(QScriptValue().equals(QScriptValue())); |
|
2823 |
|
2824 QScriptValue num = QScriptValue(&eng, 123); |
|
2825 QCOMPARE(num.equals(QScriptValue(&eng, 123)), true); |
|
2826 QCOMPARE(num.equals(QScriptValue(&eng, 321)), false); |
|
2827 QCOMPARE(num.equals(QScriptValue(&eng, "123")), true); |
|
2828 QCOMPARE(num.equals(QScriptValue(&eng, "321")), false); |
|
2829 QCOMPARE(num.equals(QScriptValue(&eng, 123).toObject()), true); |
|
2830 QCOMPARE(num.equals(QScriptValue(&eng, 321).toObject()), false); |
|
2831 QCOMPARE(num.equals(QScriptValue(&eng, "123").toObject()), true); |
|
2832 QCOMPARE(num.equals(QScriptValue(&eng, "321").toObject()), false); |
|
2833 QVERIFY(num.toObject().equals(num)); |
|
2834 QCOMPARE(num.equals(QScriptValue()), false); |
|
2835 |
|
2836 QScriptValue str = QScriptValue(&eng, "123"); |
|
2837 QCOMPARE(str.equals(QScriptValue(&eng, "123")), true); |
|
2838 QCOMPARE(str.equals(QScriptValue(&eng, "321")), false); |
|
2839 QCOMPARE(str.equals(QScriptValue(&eng, 123)), true); |
|
2840 QCOMPARE(str.equals(QScriptValue(&eng, 321)), false); |
|
2841 QCOMPARE(str.equals(QScriptValue(&eng, "123").toObject()), true); |
|
2842 QCOMPARE(str.equals(QScriptValue(&eng, "321").toObject()), false); |
|
2843 QCOMPARE(str.equals(QScriptValue(&eng, 123).toObject()), true); |
|
2844 QCOMPARE(str.equals(QScriptValue(&eng, 321).toObject()), false); |
|
2845 QVERIFY(str.toObject().equals(str)); |
|
2846 QCOMPARE(str.equals(QScriptValue()), false); |
|
2847 |
|
2848 QScriptValue num2 = QScriptValue(123); |
|
2849 QCOMPARE(num2.equals(QScriptValue(123)), true); |
|
2850 QCOMPARE(num2.equals(QScriptValue(321)), false); |
|
2851 QCOMPARE(num2.equals(QScriptValue("123")), true); |
|
2852 QCOMPARE(num2.equals(QScriptValue("321")), false); |
|
2853 QCOMPARE(num2.equals(QScriptValue()), false); |
|
2854 |
|
2855 QScriptValue str2 = QScriptValue("123"); |
|
2856 QCOMPARE(str2.equals(QScriptValue("123")), true); |
|
2857 QCOMPARE(str2.equals(QScriptValue("321")), false); |
|
2858 QCOMPARE(str2.equals(QScriptValue(123)), true); |
|
2859 QCOMPARE(str2.equals(QScriptValue(321)), false); |
|
2860 QCOMPARE(str2.equals(QScriptValue()), false); |
|
2861 |
|
2862 QScriptValue date1 = eng.newDate(QDateTime(QDate(2000, 1, 1))); |
|
2863 QScriptValue date2 = eng.newDate(QDateTime(QDate(1999, 1, 1))); |
|
2864 QCOMPARE(date1.equals(date2), false); |
|
2865 QCOMPARE(date1.equals(date1), true); |
|
2866 QCOMPARE(date2.equals(date2), true); |
|
2867 |
|
2868 QScriptValue undefined = eng.undefinedValue(); |
|
2869 QScriptValue null = eng.nullValue(); |
|
2870 QCOMPARE(undefined.equals(undefined), true); |
|
2871 QCOMPARE(null.equals(null), true); |
|
2872 QCOMPARE(undefined.equals(null), true); |
|
2873 QCOMPARE(null.equals(undefined), true); |
|
2874 QCOMPARE(undefined.equals(QScriptValue()), false); |
|
2875 QCOMPARE(null.equals(QScriptValue()), false); |
|
2876 QVERIFY(!null.equals(num)); |
|
2877 QVERIFY(!undefined.equals(num)); |
|
2878 |
|
2879 QScriptValue sant = QScriptValue(&eng, true); |
|
2880 QVERIFY(sant.equals(QScriptValue(&eng, 1))); |
|
2881 QVERIFY(sant.equals(QScriptValue(&eng, "1"))); |
|
2882 QVERIFY(sant.equals(sant)); |
|
2883 QVERIFY(sant.equals(QScriptValue(&eng, 1).toObject())); |
|
2884 QVERIFY(sant.equals(QScriptValue(&eng, "1").toObject())); |
|
2885 QVERIFY(sant.equals(sant.toObject())); |
|
2886 QVERIFY(sant.toObject().equals(sant)); |
|
2887 QVERIFY(!sant.equals(QScriptValue(&eng, 0))); |
|
2888 QVERIFY(!sant.equals(undefined)); |
|
2889 QVERIFY(!sant.equals(null)); |
|
2890 |
|
2891 QScriptValue falskt = QScriptValue(&eng, false); |
|
2892 QVERIFY(falskt.equals(QScriptValue(&eng, 0))); |
|
2893 QVERIFY(falskt.equals(QScriptValue(&eng, "0"))); |
|
2894 QVERIFY(falskt.equals(falskt)); |
|
2895 QVERIFY(falskt.equals(QScriptValue(&eng, 0).toObject())); |
|
2896 QVERIFY(falskt.equals(QScriptValue(&eng, "0").toObject())); |
|
2897 QVERIFY(falskt.equals(falskt.toObject())); |
|
2898 QVERIFY(falskt.toObject().equals(falskt)); |
|
2899 QVERIFY(!falskt.equals(sant)); |
|
2900 QVERIFY(!falskt.equals(undefined)); |
|
2901 QVERIFY(!falskt.equals(null)); |
|
2902 |
|
2903 QScriptValue obj1 = eng.newObject(); |
|
2904 QScriptValue obj2 = eng.newObject(); |
|
2905 QCOMPARE(obj1.equals(obj2), false); |
|
2906 QCOMPARE(obj2.equals(obj1), false); |
|
2907 QCOMPARE(obj1.equals(obj1), true); |
|
2908 QCOMPARE(obj2.equals(obj2), true); |
|
2909 |
|
2910 QScriptValue qobj1 = eng.newQObject(this); |
|
2911 QScriptValue qobj2 = eng.newQObject(this); |
|
2912 QScriptValue qobj3 = eng.newQObject(0); |
|
2913 QScriptValue qobj4 = eng.newQObject(new QObject(), QScriptEngine::ScriptOwnership); |
|
2914 QVERIFY(qobj1.equals(qobj2)); // compares the QObject pointers |
|
2915 QVERIFY(!qobj2.equals(qobj4)); // compares the QObject pointers |
|
2916 QVERIFY(!qobj2.equals(obj2)); // compares the QObject pointers |
|
2917 |
|
2918 QScriptValue compareFun = eng.evaluate("(function(a, b) { return a == b; })"); |
|
2919 QVERIFY(compareFun.isFunction()); |
|
2920 { |
|
2921 QScriptValue ret = compareFun.call(QScriptValue(), QScriptValueList() << qobj1 << qobj2); |
|
2922 QVERIFY(ret.isBool()); |
|
2923 QVERIFY(ret.toBool()); |
|
2924 ret = compareFun.call(QScriptValue(), QScriptValueList() << qobj1 << qobj3); |
|
2925 QVERIFY(ret.isBool()); |
|
2926 QVERIFY(!ret.toBool()); |
|
2927 ret = compareFun.call(QScriptValue(), QScriptValueList() << qobj1 << qobj4); |
|
2928 QVERIFY(ret.isBool()); |
|
2929 QVERIFY(!ret.toBool()); |
|
2930 ret = compareFun.call(QScriptValue(), QScriptValueList() << qobj1 << obj1); |
|
2931 QVERIFY(ret.isBool()); |
|
2932 QVERIFY(!ret.toBool()); |
|
2933 } |
|
2934 |
|
2935 { |
|
2936 QScriptValue var1 = eng.newVariant(QVariant(false)); |
|
2937 QScriptValue var2 = eng.newVariant(QVariant(false)); |
|
2938 QVERIFY(var1.equals(var2)); |
|
2939 { |
|
2940 QScriptValue ret = compareFun.call(QScriptValue(), QScriptValueList() << var1 << var2); |
|
2941 QVERIFY(ret.isBool()); |
|
2942 QVERIFY(ret.toBool()); |
|
2943 } |
|
2944 } |
|
2945 { |
|
2946 QScriptValue var1 = eng.newVariant(QVariant(false)); |
|
2947 QScriptValue var2 = eng.newVariant(QVariant(0)); |
|
2948 // QVariant::operator==() performs type conversion |
|
2949 QVERIFY(var1.equals(var2)); |
|
2950 } |
|
2951 { |
|
2952 QScriptValue var1 = eng.newVariant(QVariant(QStringList() << "a")); |
|
2953 QScriptValue var2 = eng.newVariant(QVariant(QStringList() << "a")); |
|
2954 QVERIFY(var1.equals(var2)); |
|
2955 } |
|
2956 { |
|
2957 QScriptValue var1 = eng.newVariant(QVariant(QStringList() << "a")); |
|
2958 QScriptValue var2 = eng.newVariant(QVariant(QStringList() << "b")); |
|
2959 QVERIFY(!var1.equals(var2)); |
|
2960 } |
|
2961 { |
|
2962 QScriptValue var1 = eng.newVariant(QVariant(QPoint(1, 2))); |
|
2963 QScriptValue var2 = eng.newVariant(QVariant(QPoint(1, 2))); |
|
2964 QVERIFY(var1.equals(var2)); |
|
2965 } |
|
2966 { |
|
2967 QScriptValue var1 = eng.newVariant(QVariant(QPoint(1, 2))); |
|
2968 QScriptValue var2 = eng.newVariant(QVariant(QPoint(3, 4))); |
|
2969 QVERIFY(!var1.equals(var2)); |
|
2970 } |
|
2971 { |
|
2972 QScriptValue var1 = eng.newVariant(QVariant(int(1))); |
|
2973 QScriptValue var2 = eng.newVariant(QVariant(double(1))); |
|
2974 // QVariant::operator==() performs type conversion |
|
2975 QVERIFY(var1.equals(var2)); |
|
2976 } |
|
2977 { |
|
2978 QScriptValue var1 = eng.newVariant(QVariant(QString::fromLatin1("123"))); |
|
2979 QScriptValue var2 = eng.newVariant(QVariant(double(123))); |
|
2980 QScriptValue var3(QString::fromLatin1("123")); |
|
2981 QScriptValue var4(123); |
|
2982 |
|
2983 QVERIFY(var1.equals(var1)); |
|
2984 QVERIFY(var1.equals(var2)); |
|
2985 QVERIFY(var1.equals(var3)); |
|
2986 QVERIFY(var1.equals(var4)); |
|
2987 |
|
2988 QVERIFY(var2.equals(var1)); |
|
2989 QVERIFY(var2.equals(var2)); |
|
2990 QVERIFY(var2.equals(var3)); |
|
2991 QVERIFY(var2.equals(var4)); |
|
2992 |
|
2993 QVERIFY(var3.equals(var1)); |
|
2994 QVERIFY(var3.equals(var2)); |
|
2995 QVERIFY(var3.equals(var3)); |
|
2996 QVERIFY(var3.equals(var4)); |
|
2997 |
|
2998 QVERIFY(var4.equals(var1)); |
|
2999 QVERIFY(var4.equals(var2)); |
|
3000 QVERIFY(var4.equals(var3)); |
|
3001 QVERIFY(var4.equals(var4)); |
|
3002 } |
|
3003 |
|
3004 QScriptEngine otherEngine; |
|
3005 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::equals: " |
|
3006 "cannot compare to a value created in " |
|
3007 "a different engine"); |
|
3008 QCOMPARE(date1.equals(QScriptValue(&otherEngine, 123)), false); |
|
3009 } |
|
3010 |
|
3011 void tst_QScriptValue::strictlyEquals_old() |
|
3012 { |
|
3013 QScriptEngine eng; |
|
3014 |
|
3015 QVERIFY(QScriptValue().strictlyEquals(QScriptValue())); |
|
3016 |
|
3017 QScriptValue num = QScriptValue(&eng, 123); |
|
3018 QCOMPARE(num.strictlyEquals(QScriptValue(&eng, 123)), true); |
|
3019 QCOMPARE(num.strictlyEquals(QScriptValue(&eng, 321)), false); |
|
3020 QCOMPARE(num.strictlyEquals(QScriptValue(&eng, "123")), false); |
|
3021 QCOMPARE(num.strictlyEquals(QScriptValue(&eng, "321")), false); |
|
3022 QCOMPARE(num.strictlyEquals(QScriptValue(&eng, 123).toObject()), false); |
|
3023 QCOMPARE(num.strictlyEquals(QScriptValue(&eng, 321).toObject()), false); |
|
3024 QCOMPARE(num.strictlyEquals(QScriptValue(&eng, "123").toObject()), false); |
|
3025 QCOMPARE(num.strictlyEquals(QScriptValue(&eng, "321").toObject()), false); |
|
3026 QVERIFY(!num.toObject().strictlyEquals(num)); |
|
3027 QVERIFY(!num.strictlyEquals(QScriptValue())); |
|
3028 QVERIFY(!QScriptValue().strictlyEquals(num)); |
|
3029 |
|
3030 QScriptValue str = QScriptValue(&eng, "123"); |
|
3031 QCOMPARE(str.strictlyEquals(QScriptValue(&eng, "123")), true); |
|
3032 QCOMPARE(str.strictlyEquals(QScriptValue(&eng, "321")), false); |
|
3033 QCOMPARE(str.strictlyEquals(QScriptValue(&eng, 123)), false); |
|
3034 QCOMPARE(str.strictlyEquals(QScriptValue(&eng, 321)), false); |
|
3035 QCOMPARE(str.strictlyEquals(QScriptValue(&eng, "123").toObject()), false); |
|
3036 QCOMPARE(str.strictlyEquals(QScriptValue(&eng, "321").toObject()), false); |
|
3037 QCOMPARE(str.strictlyEquals(QScriptValue(&eng, 123).toObject()), false); |
|
3038 QCOMPARE(str.strictlyEquals(QScriptValue(&eng, 321).toObject()), false); |
|
3039 QVERIFY(!str.toObject().strictlyEquals(str)); |
|
3040 QVERIFY(!str.strictlyEquals(QScriptValue())); |
|
3041 |
|
3042 QScriptValue num2 = QScriptValue(123); |
|
3043 QCOMPARE(num2.strictlyEquals(QScriptValue(123)), true); |
|
3044 QCOMPARE(num2.strictlyEquals(QScriptValue(321)), false); |
|
3045 QCOMPARE(num2.strictlyEquals(QScriptValue("123")), false); |
|
3046 QCOMPARE(num2.strictlyEquals(QScriptValue("321")), false); |
|
3047 QVERIFY(!num2.strictlyEquals(QScriptValue())); |
|
3048 |
|
3049 QScriptValue str2 = QScriptValue("123"); |
|
3050 QCOMPARE(str2.strictlyEquals(QScriptValue("123")), true); |
|
3051 QCOMPARE(str2.strictlyEquals(QScriptValue("321")), false); |
|
3052 QCOMPARE(str2.strictlyEquals(QScriptValue(123)), false); |
|
3053 QCOMPARE(str2.strictlyEquals(QScriptValue(321)), false); |
|
3054 QVERIFY(!str2.strictlyEquals(QScriptValue())); |
|
3055 |
|
3056 QScriptValue date1 = eng.newDate(QDateTime(QDate(2000, 1, 1))); |
|
3057 QScriptValue date2 = eng.newDate(QDateTime(QDate(1999, 1, 1))); |
|
3058 QCOMPARE(date1.strictlyEquals(date2), false); |
|
3059 QCOMPARE(date1.strictlyEquals(date1), true); |
|
3060 QCOMPARE(date2.strictlyEquals(date2), true); |
|
3061 QVERIFY(!date1.strictlyEquals(QScriptValue())); |
|
3062 |
|
3063 QScriptValue undefined = eng.undefinedValue(); |
|
3064 QScriptValue null = eng.nullValue(); |
|
3065 QCOMPARE(undefined.strictlyEquals(undefined), true); |
|
3066 QCOMPARE(null.strictlyEquals(null), true); |
|
3067 QCOMPARE(undefined.strictlyEquals(null), false); |
|
3068 QCOMPARE(null.strictlyEquals(undefined), false); |
|
3069 QVERIFY(!null.strictlyEquals(QScriptValue())); |
|
3070 |
|
3071 QScriptValue sant = QScriptValue(&eng, true); |
|
3072 QVERIFY(!sant.strictlyEquals(QScriptValue(&eng, 1))); |
|
3073 QVERIFY(!sant.strictlyEquals(QScriptValue(&eng, "1"))); |
|
3074 QVERIFY(sant.strictlyEquals(sant)); |
|
3075 QVERIFY(!sant.strictlyEquals(QScriptValue(&eng, 1).toObject())); |
|
3076 QVERIFY(!sant.strictlyEquals(QScriptValue(&eng, "1").toObject())); |
|
3077 QVERIFY(!sant.strictlyEquals(sant.toObject())); |
|
3078 QVERIFY(!sant.toObject().strictlyEquals(sant)); |
|
3079 QVERIFY(!sant.strictlyEquals(QScriptValue(&eng, 0))); |
|
3080 QVERIFY(!sant.strictlyEquals(undefined)); |
|
3081 QVERIFY(!sant.strictlyEquals(null)); |
|
3082 QVERIFY(!sant.strictlyEquals(QScriptValue())); |
|
3083 |
|
3084 QScriptValue falskt = QScriptValue(&eng, false); |
|
3085 QVERIFY(!falskt.strictlyEquals(QScriptValue(&eng, 0))); |
|
3086 QVERIFY(!falskt.strictlyEquals(QScriptValue(&eng, "0"))); |
|
3087 QVERIFY(falskt.strictlyEquals(falskt)); |
|
3088 QVERIFY(!falskt.strictlyEquals(QScriptValue(&eng, 0).toObject())); |
|
3089 QVERIFY(!falskt.strictlyEquals(QScriptValue(&eng, "0").toObject())); |
|
3090 QVERIFY(!falskt.strictlyEquals(falskt.toObject())); |
|
3091 QVERIFY(!falskt.toObject().strictlyEquals(falskt)); |
|
3092 QVERIFY(!falskt.strictlyEquals(sant)); |
|
3093 QVERIFY(!falskt.strictlyEquals(undefined)); |
|
3094 QVERIFY(!falskt.strictlyEquals(null)); |
|
3095 QVERIFY(!falskt.strictlyEquals(QScriptValue())); |
|
3096 |
|
3097 QVERIFY(!QScriptValue(false).strictlyEquals(123)); |
|
3098 QVERIFY(!QScriptValue(QScriptValue::UndefinedValue).strictlyEquals(123)); |
|
3099 QVERIFY(!QScriptValue(QScriptValue::NullValue).strictlyEquals(123)); |
|
3100 QVERIFY(!QScriptValue(false).strictlyEquals("ciao")); |
|
3101 QVERIFY(!QScriptValue(QScriptValue::UndefinedValue).strictlyEquals("ciao")); |
|
3102 QVERIFY(!QScriptValue(QScriptValue::NullValue).strictlyEquals("ciao")); |
|
3103 QVERIFY(QScriptValue(&eng, "ciao").strictlyEquals("ciao")); |
|
3104 QVERIFY(QScriptValue("ciao").strictlyEquals(QScriptValue(&eng, "ciao"))); |
|
3105 QVERIFY(!QScriptValue("ciao").strictlyEquals(123)); |
|
3106 QVERIFY(!QScriptValue("ciao").strictlyEquals(QScriptValue(&eng, 123))); |
|
3107 QVERIFY(!QScriptValue(123).strictlyEquals("ciao")); |
|
3108 QVERIFY(!QScriptValue(123).strictlyEquals(QScriptValue(&eng, "ciao"))); |
|
3109 QVERIFY(!QScriptValue(&eng, 123).strictlyEquals("ciao")); |
|
3110 |
|
3111 QScriptValue obj1 = eng.newObject(); |
|
3112 QScriptValue obj2 = eng.newObject(); |
|
3113 QCOMPARE(obj1.strictlyEquals(obj2), false); |
|
3114 QCOMPARE(obj2.strictlyEquals(obj1), false); |
|
3115 QCOMPARE(obj1.strictlyEquals(obj1), true); |
|
3116 QCOMPARE(obj2.strictlyEquals(obj2), true); |
|
3117 QVERIFY(!obj1.strictlyEquals(QScriptValue())); |
|
3118 |
|
3119 QScriptValue qobj1 = eng.newQObject(this); |
|
3120 QScriptValue qobj2 = eng.newQObject(this); |
|
3121 QVERIFY(!qobj1.strictlyEquals(qobj2)); |
|
3122 |
|
3123 { |
|
3124 QScriptValue var1 = eng.newVariant(QVariant(false)); |
|
3125 QScriptValue var2 = eng.newVariant(QVariant(false)); |
|
3126 QVERIFY(!var1.strictlyEquals(var2)); |
|
3127 QVERIFY(!var1.strictlyEquals(QScriptValue())); |
|
3128 } |
|
3129 { |
|
3130 QScriptValue var1 = eng.newVariant(QVariant(false)); |
|
3131 QScriptValue var2 = eng.newVariant(QVariant(0)); |
|
3132 QVERIFY(!var1.strictlyEquals(var2)); |
|
3133 } |
|
3134 { |
|
3135 QScriptValue var1 = eng.newVariant(QVariant(QStringList() << "a")); |
|
3136 QScriptValue var2 = eng.newVariant(QVariant(QStringList() << "a")); |
|
3137 QVERIFY(!var1.strictlyEquals(var2)); |
|
3138 } |
|
3139 { |
|
3140 QScriptValue var1 = eng.newVariant(QVariant(QStringList() << "a")); |
|
3141 QScriptValue var2 = eng.newVariant(QVariant(QStringList() << "b")); |
|
3142 QVERIFY(!var1.strictlyEquals(var2)); |
|
3143 } |
|
3144 { |
|
3145 QScriptValue var1 = eng.newVariant(QVariant(QPoint(1, 2))); |
|
3146 QScriptValue var2 = eng.newVariant(QVariant(QPoint(1, 2))); |
|
3147 QVERIFY(!var1.strictlyEquals(var2)); |
|
3148 } |
|
3149 { |
|
3150 QScriptValue var1 = eng.newVariant(QVariant(QPoint(1, 2))); |
|
3151 QScriptValue var2 = eng.newVariant(QVariant(QPoint(3, 4))); |
|
3152 QVERIFY(!var1.strictlyEquals(var2)); |
|
3153 } |
|
3154 |
|
3155 QScriptEngine otherEngine; |
|
3156 QTest::ignoreMessage(QtWarningMsg, "QScriptValue::strictlyEquals: " |
|
3157 "cannot compare to a value created in " |
|
3158 "a different engine"); |
|
3159 QCOMPARE(date1.strictlyEquals(QScriptValue(&otherEngine, 123)), false); |
|
3160 } |
|
3161 |
|
3162 Q_DECLARE_METATYPE(int*) |
|
3163 Q_DECLARE_METATYPE(double*) |
|
3164 Q_DECLARE_METATYPE(QColor*) |
|
3165 Q_DECLARE_METATYPE(QBrush*) |
|
3166 |
|
3167 void tst_QScriptValue::castToPointer() |
|
3168 { |
|
3169 QScriptEngine eng; |
|
3170 { |
|
3171 QScriptValue v = eng.newVariant(int(123)); |
|
3172 int *ip = qscriptvalue_cast<int*>(v); |
|
3173 QVERIFY(ip != 0); |
|
3174 QCOMPARE(*ip, 123); |
|
3175 *ip = 456; |
|
3176 QCOMPARE(qscriptvalue_cast<int>(v), 456); |
|
3177 |
|
3178 double *dp = qscriptvalue_cast<double*>(v); |
|
3179 QVERIFY(dp == 0); |
|
3180 |
|
3181 QScriptValue v2 = eng.newVariant(qVariantFromValue(ip)); |
|
3182 QCOMPARE(qscriptvalue_cast<int*>(v2), ip); |
|
3183 } |
|
3184 { |
|
3185 QColor c(123, 210, 231); |
|
3186 QScriptValue v = eng.newVariant(c); |
|
3187 QColor *cp = qscriptvalue_cast<QColor*>(v); |
|
3188 QVERIFY(cp != 0); |
|
3189 QCOMPARE(*cp, c); |
|
3190 |
|
3191 QBrush *bp = qscriptvalue_cast<QBrush*>(v); |
|
3192 QVERIFY(bp == 0); |
|
3193 |
|
3194 QScriptValue v2 = eng.newVariant(qVariantFromValue(cp)); |
|
3195 QCOMPARE(qscriptvalue_cast<QColor*>(v2), cp); |
|
3196 } |
|
3197 } |
|
3198 |
|
3199 void tst_QScriptValue::prettyPrinter_data() |
|
3200 { |
|
3201 QTest::addColumn<QString>("function"); |
|
3202 QTest::addColumn<QString>("expected"); |
|
3203 QTest::newRow("function() { }") << QString("function() { }") << QString("function () { }"); |
|
3204 QTest::newRow("function foo() { }") << QString("(function foo() { })") << QString("function foo() { }"); |
|
3205 QTest::newRow("function foo(bar) { }") << QString("(function foo(bar) { })") << QString("function foo(bar) { }"); |
|
3206 QTest::newRow("function foo(bar, baz) { }") << QString("(function foo(bar, baz) { })") << QString("function foo(bar, baz) { }"); |
|
3207 QTest::newRow("this") << QString("function() { this; }") << QString("function () { this; }"); |
|
3208 QTest::newRow("identifier") << QString("function(a) { a; }") << QString("function (a) { a; }"); |
|
3209 QTest::newRow("null") << QString("function() { null; }") << QString("function () { null; }"); |
|
3210 QTest::newRow("true") << QString("function() { true; }") << QString("function () { true; }"); |
|
3211 QTest::newRow("false") << QString("function() { false; }") << QString("function () { false; }"); |
|
3212 QTest::newRow("string") << QString("function() { 'test'; }") << QString("function () { \'test\'; }"); |
|
3213 QTest::newRow("string") << QString("function() { \"test\"; }") << QString("function () { \"test\"; }"); |
|
3214 QTest::newRow("number") << QString("function() { 123; }") << QString("function () { 123; }"); |
|
3215 QTest::newRow("number") << QString("function() { 123.456; }") << QString("function () { 123.456; }"); |
|
3216 QTest::newRow("regexp") << QString("function() { /hello/; }") << QString("function () { /hello/; }"); |
|
3217 QTest::newRow("regexp") << QString("function() { /hello/gim; }") << QString("function () { /hello/gim; }"); |
|
3218 QTest::newRow("array") << QString("function() { []; }") << QString("function () { []; }"); |
|
3219 QTest::newRow("array") << QString("function() { [10]; }") << QString("function () { [10]; }"); |
|
3220 QTest::newRow("array") << QString("function() { [10, 20, 30]; }") << QString("function () { [10, 20, 30]; }"); |
|
3221 QTest::newRow("array") << QString("function() { [10, 20, , 40]; }") << QString("function () { [10, 20, , 40]; }"); |
|
3222 QTest::newRow("array") << QString("function() { [,]; }") << QString("function () { [,]; }"); |
|
3223 QTest::newRow("array") << QString("function() { [, 10]; }") << QString("function () { [, 10]; }"); |
|
3224 QTest::newRow("array") << QString("function() { [, 10, ]; }") << QString("function () { [, 10, ]; }"); |
|
3225 QTest::newRow("array") << QString("function() { [, 10, ,]; }") << QString("function () { [, 10, ,]; }"); |
|
3226 QTest::newRow("array") << QString("function() { [[10], [20]]; }") << QString("function () { [[10], [20]]; }"); |
|
3227 QTest::newRow("member") << QString("function() { a.b; }") << QString("function () { a.b; }"); |
|
3228 QTest::newRow("member") << QString("function() { a.b.c; }") << QString("function () { a.b.c; }"); |
|
3229 QTest::newRow("call") << QString("function() { f(); }") << QString("function () { f(); }"); |
|
3230 QTest::newRow("call") << QString("function() { f(a); }") << QString("function () { f(a); }"); |
|
3231 QTest::newRow("call") << QString("function() { f(a, b); }") << QString("function () { f(a, b); }"); |
|
3232 QTest::newRow("new") << QString("function() { new C(); }") << QString("function () { new C(); }"); |
|
3233 QTest::newRow("new") << QString("function() { new C(a); }") << QString("function () { new C(a); }"); |
|
3234 QTest::newRow("new") << QString("function() { new C(a, b); }") << QString("function () { new C(a, b); }"); |
|
3235 QTest::newRow("++") << QString("function() { a++; }") << QString("function () { a++; }"); |
|
3236 QTest::newRow("++") << QString("function() { ++a; }") << QString("function () { ++a; }"); |
|
3237 QTest::newRow("--") << QString("function() { a--; }") << QString("function () { a--; }"); |
|
3238 QTest::newRow("--") << QString("function() { --a; }") << QString("function () { --a; }"); |
|
3239 QTest::newRow("delete") << QString("function() { delete a; }") << QString("function () { delete a; }"); |
|
3240 QTest::newRow("void") << QString("function() { void a; }") << QString("function () { void a; }"); |
|
3241 QTest::newRow("typeof") << QString("function() { typeof a; }") << QString("function () { typeof a; }"); |
|
3242 QTest::newRow("+") << QString("function() { +a; }") << QString("function () { +a; }"); |
|
3243 QTest::newRow("-") << QString("function() { -a; }") << QString("function () { -a; }"); |
|
3244 QTest::newRow("~") << QString("function() { ~a; }") << QString("function () { ~a; }"); |
|
3245 QTest::newRow("!") << QString("function() { !a; }") << QString("function () { !a; }"); |
|
3246 QTest::newRow("+") << QString("function() { a + b; }") << QString("function () { a + b; }"); |
|
3247 QTest::newRow("&&") << QString("function() { a && b; }") << QString("function () { a && b; }"); |
|
3248 QTest::newRow("&=") << QString("function() { a &= b; }") << QString("function () { a &= b; }"); |
|
3249 QTest::newRow("=") << QString("function() { a = b; }") << QString("function () { a = b; }"); |
|
3250 QTest::newRow("&") << QString("function() { a & b; }") << QString("function () { a & b; }"); |
|
3251 QTest::newRow("|") << QString("function() { a | b; }") << QString("function () { a | b; }"); |
|
3252 QTest::newRow("^") << QString("function() { a ^ b; }") << QString("function () { a ^ b; }"); |
|
3253 QTest::newRow("-=") << QString("function() { a -= b; }") << QString("function () { a -= b; }"); |
|
3254 QTest::newRow("/") << QString("function() { a / b; }") << QString("function () { a / b; }"); |
|
3255 QTest::newRow("/=") << QString("function() { a /= b; }") << QString("function () { a /= b; }"); |
|
3256 QTest::newRow("==") << QString("function() { a == b; }") << QString("function () { a == b; }"); |
|
3257 QTest::newRow(">=") << QString("function() { a >= b; }") << QString("function () { a >= b; }"); |
|
3258 QTest::newRow(">") << QString("function() { a > b; }") << QString("function () { a > b; }"); |
|
3259 QTest::newRow("in") << QString("function() { a in b; }") << QString("function () { a in b; }"); |
|
3260 QTest::newRow("+=") << QString("function() { a += b; }") << QString("function () { a += b; }"); |
|
3261 QTest::newRow("instanceof") << QString("function() { a instanceof b; }") << QString("function () { a instanceof b; }"); |
|
3262 QTest::newRow("<=") << QString("function() { a <= b; }") << QString("function () { a <= b; }"); |
|
3263 QTest::newRow("<<") << QString("function() { a << b; }") << QString("function () { a << b; }"); |
|
3264 QTest::newRow("<<=") << QString("function() { a <<= b; }") << QString("function () { a <<= b; }"); |
|
3265 QTest::newRow("<") << QString("function() { a < b; }") << QString("function () { a < b; }"); |
|
3266 QTest::newRow("%") << QString("function() { a % b; }") << QString("function () { a % b; }"); |
|
3267 QTest::newRow("%=") << QString("function() { a %= b; }") << QString("function () { a %= b; }"); |
|
3268 QTest::newRow("*") << QString("function() { a * b; }") << QString("function () { a * b; }"); |
|
3269 QTest::newRow("*=") << QString("function() { a *= b; }") << QString("function () { a *= b; }"); |
|
3270 QTest::newRow("!=") << QString("function() { a != b; }") << QString("function () { a != b; }"); |
|
3271 QTest::newRow("||") << QString("function() { a || b; }") << QString("function () { a || b; }"); |
|
3272 QTest::newRow("|=") << QString("function() { a |= b; }") << QString("function () { a |= b; }"); |
|
3273 QTest::newRow(">>") << QString("function() { a >> b; }") << QString("function () { a >> b; }"); |
|
3274 QTest::newRow(">>=") << QString("function() { a >>= b; }") << QString("function () { a >>= b; }"); |
|
3275 QTest::newRow("===") << QString("function() { a === b; }") << QString("function () { a === b; }"); |
|
3276 QTest::newRow("!==") << QString("function() { a !== b; }") << QString("function () { a !== b; }"); |
|
3277 QTest::newRow("-") << QString("function() { a - b; }") << QString("function () { a - b; }"); |
|
3278 QTest::newRow(">>>") << QString("function() { a >>> b; }") << QString("function () { a >>> b; }"); |
|
3279 QTest::newRow(">>>=") << QString("function() { a >>>= b; }") << QString("function () { a >>>= b; }"); |
|
3280 QTest::newRow("^=") << QString("function() { a ^= b; }") << QString("function () { a ^= b; }"); |
|
3281 QTest::newRow("? :") << QString("function() { a ? b : c; }") << QString("function () { a ? b : c; }"); |
|
3282 QTest::newRow("a; b; c") << QString("function() { a; b; c; }") << QString("function () { a; b; c; }"); |
|
3283 QTest::newRow("var a;") << QString("function() { var a; }") << QString("function () { var a; }"); |
|
3284 QTest::newRow("var a, b;") << QString("function() { var a, b; }") << QString("function () { var a, b; }"); |
|
3285 QTest::newRow("var a = 10;") << QString("function() { var a = 10; }") << QString("function () { var a = 10; }"); |
|
3286 QTest::newRow("var a, b = 20;") << QString("function() { var a, b = 20; }") << QString("function () { var a, b = 20; }"); |
|
3287 QTest::newRow("var a = 10, b = 20;") << QString("function() { var a = 10, b = 20; }") << QString("function () { var a = 10, b = 20; }"); |
|
3288 QTest::newRow("if") << QString("function() { if (a) b; }") << QString("function () { if (a) b; }"); |
|
3289 QTest::newRow("if") << QString("function() { if (a) { b; c; } }") << QString("function () { if (a) { b; c; } }"); |
|
3290 QTest::newRow("if-else") << QString("function() { if (a) b; else c; }") << QString("function () { if (a) b; else c; }"); |
|
3291 QTest::newRow("if-else") << QString("function() { if (a) { b; c; } else { d; e; } }") << QString("function () { if (a) { b; c; } else { d; e; } }"); |
|
3292 QTest::newRow("do-while") << QString("function() { do { a; } while (b); }") << QString("function () { do { a; } while (b); }"); |
|
3293 QTest::newRow("do-while") << QString("function() { do { a; b; c; } while (d); }") << QString("function () { do { a; b; c; } while (d); }"); |
|
3294 QTest::newRow("while") << QString("function() { while (a) { b; } }") << QString("function () { while (a) { b; } }"); |
|
3295 QTest::newRow("while") << QString("function() { while (a) { b; c; } }") << QString("function () { while (a) { b; c; } }"); |
|
3296 QTest::newRow("for") << QString("function() { for (a; b; c) { } }") << QString("function () { for (a; b; c) { } }"); |
|
3297 QTest::newRow("for") << QString("function() { for (; a; b) { } }") << QString("function () { for (; a; b) { } }"); |
|
3298 QTest::newRow("for") << QString("function() { for (; ; a) { } }") << QString("function () { for (; ; a) { } }"); |
|
3299 QTest::newRow("for") << QString("function() { for (; ; ) { } }") << QString("function () { for (; ; ) { } }"); |
|
3300 QTest::newRow("for") << QString("function() { for (var a; b; c) { } }") << QString("function () { for (var a; b; c) { } }"); |
|
3301 QTest::newRow("for") << QString("function() { for (var a, b, c; d; e) { } }") << QString("function () { for (var a, b, c; d; e) { } }"); |
|
3302 QTest::newRow("continue") << QString("function() { for (; ; ) { continue; } }") << QString("function () { for (; ; ) { continue; } }"); |
|
3303 QTest::newRow("continue") << QString("function() { for (; ; ) { continue label; } }") << QString("function () { for (; ; ) { continue label; } }"); |
|
3304 QTest::newRow("break") << QString("function() { for (; ; ) { break; } }") << QString("function () { for (; ; ) { break; } }"); |
|
3305 QTest::newRow("break") << QString("function() { for (; ; ) { break label; } }") << QString("function () { for (; ; ) { break label; } }"); |
|
3306 QTest::newRow("return") << QString("function() { return; }") << QString("function () { return; }"); |
|
3307 QTest::newRow("return") << QString("function() { return 10; }") << QString("function () { return 10; }"); |
|
3308 QTest::newRow("with") << QString("function() { with (a) { b; } }") << QString("function () { with (a) { b; } }"); |
|
3309 QTest::newRow("with") << QString("function() { with (a) { b; c; } }") << QString("function () { with (a) { b; c; } }"); |
|
3310 QTest::newRow("switch") << QString("function() { switch (a) { } }") << QString("function () { switch (a) { } }"); |
|
3311 QTest::newRow("switch") << QString("function() { switch (a) { case 1: ; } }") << QString("function () { switch (a) { case 1: ; } }"); |
|
3312 QTest::newRow("switch") << QString("function() { switch (a) { case 1: b; break; } }") << QString("function () { switch (a) { case 1: b; break; } }"); |
|
3313 QTest::newRow("switch") << QString("function() { switch (a) { case 1: b; break; case 2: break; } }") << QString("function () { switch (a) { case 1: b; break; case 2: break; } }"); |
|
3314 QTest::newRow("switch") << QString("function() { switch (a) { case 1: case 2: ; } }") << QString("function () { switch (a) { case 1: case 2: ; } }"); |
|
3315 QTest::newRow("switch") << QString("function() { switch (a) { case 1: default: ; } }") << QString("function () { switch (a) { case 1: default: ; } }"); |
|
3316 QTest::newRow("switch") << QString("function() { switch (a) { case 1: default: ; case 3: ; } }") << QString("function () { switch (a) { case 1: default: ; case 3: ; } }"); |
|
3317 QTest::newRow("label") << QString("function() { a: b; }") << QString("function () { a: b; }"); |
|
3318 QTest::newRow("throw") << QString("function() { throw a; }") << QString("function () { throw a; }"); |
|
3319 QTest::newRow("try-catch") << QString("function() { try { a; } catch (e) { b; } }") << QString("function () { try { a; } catch (e) { b; } }"); |
|
3320 QTest::newRow("try-finally") << QString("function() { try { a; } finally { b; } }") << QString("function () { try { a; } finally { b; } }"); |
|
3321 QTest::newRow("try-catch-finally") << QString("function() { try { a; } catch (e) { b; } finally { c; } }") << QString("function () { try { a; } catch (e) { b; } finally { c; } }"); |
|
3322 QTest::newRow("a + b + c + d") << QString("function() { a + b + c + d; }") << QString("function () { a + b + c + d; }"); |
|
3323 QTest::newRow("a + b - c") << QString("function() { a + b - c; }") << QString("function () { a + b - c; }"); |
|
3324 QTest::newRow("a + -b") << QString("function() { a + -b; }") << QString("function () { a + -b; }"); |
|
3325 QTest::newRow("a + ~b") << QString("function() { a + ~b; }") << QString("function () { a + ~b; }"); |
|
3326 QTest::newRow("a + !b") << QString("function() { a + !b; }") << QString("function () { a + !b; }"); |
|
3327 QTest::newRow("a + +b") << QString("function() { a + +b; }") << QString("function () { a + +b; }"); |
|
3328 QTest::newRow("(a + b) - c") << QString("function() { (a + b) - c; }") << QString("function () { (a + b) - c; }"); |
|
3329 QTest::newRow("(a - b + c") << QString("function() { a - b + c; }") << QString("function () { a - b + c; }"); |
|
3330 QTest::newRow("(a - (b + c)") << QString("function() { a - (b + c); }") << QString("function () { a - (b + c); }"); |
|
3331 QTest::newRow("a + -(b + c)") << QString("function() { a + -(b + c); }") << QString("function () { a + -(b + c); }"); |
|
3332 QTest::newRow("a + ~(b + c)") << QString("function() { a + ~(b + c); }") << QString("function () { a + ~(b + c); }"); |
|
3333 QTest::newRow("a + !(b + c)") << QString("function() { a + !(b + c); }") << QString("function () { a + !(b + c); }"); |
|
3334 QTest::newRow("a + +(b + c)") << QString("function() { a + +(b + c); }") << QString("function () { a + +(b + c); }"); |
|
3335 QTest::newRow("a + b * c") << QString("function() { a + b * c; }") << QString("function () { a + b * c; }"); |
|
3336 QTest::newRow("(a + b) * c") << QString("function() { (a + b) * c; }") << QString("function () { (a + b) * c; }"); |
|
3337 QTest::newRow("(a + b) * (c + d)") << QString("function() { (a + b) * (c + d); }") << QString("function () { (a + b) * (c + d); }"); |
|
3338 QTest::newRow("a + (b * c)") << QString("function() { a + (b * c); }") << QString("function () { a + (b * c); }"); |
|
3339 QTest::newRow("a + (b / c)") << QString("function() { a + (b / c); }") << QString("function () { a + (b / c); }"); |
|
3340 QTest::newRow("(a / b) * c") << QString("function() { (a / b) * c; }") << QString("function () { (a / b) * c; }"); |
|
3341 QTest::newRow("a / (b * c)") << QString("function() { a / (b * c); }") << QString("function () { a / (b * c); }"); |
|
3342 QTest::newRow("a / (b % c)") << QString("function() { a / (b % c); }") << QString("function () { a / (b % c); }"); |
|
3343 QTest::newRow("a && b || c") << QString("function() { a && b || c; }") << QString("function () { a && b || c; }"); |
|
3344 QTest::newRow("a && (b || c)") << QString("function() { a && (b || c); }") << QString("function () { a && (b || c); }"); |
|
3345 QTest::newRow("a & b | c") << QString("function() { a & b | c; }") << QString("function () { a & b | c; }"); |
|
3346 QTest::newRow("a & (b | c)") << QString("function() { a & (b | c); }") << QString("function () { a & (b | c); }"); |
|
3347 QTest::newRow("a & b | c ^ d") << QString("function() { a & b | c ^ d; }") << QString("function () { a & b | c ^ d; }"); |
|
3348 QTest::newRow("a & (b | c ^ d)") << QString("function() { a & (b | c ^ d); }") << QString("function () { a & (b | c ^ d); }"); |
|
3349 QTest::newRow("(a & b | c) ^ d") << QString("function() { (a & b | c) ^ d; }") << QString("function () { (a & b | c) ^ d; }"); |
|
3350 QTest::newRow("a << b + c") << QString("function() { a << b + c; }") << QString("function () { a << b + c; }"); |
|
3351 QTest::newRow("(a << b) + c") << QString("function() { (a << b) + c; }") << QString("function () { (a << b) + c; }"); |
|
3352 QTest::newRow("a >> b + c") << QString("function() { a >> b + c; }") << QString("function () { a >> b + c; }"); |
|
3353 QTest::newRow("(a >> b) + c") << QString("function() { (a >> b) + c; }") << QString("function () { (a >> b) + c; }"); |
|
3354 QTest::newRow("a >>> b + c") << QString("function() { a >>> b + c; }") << QString("function () { a >>> b + c; }"); |
|
3355 QTest::newRow("(a >>> b) + c") << QString("function() { (a >>> b) + c; }") << QString("function () { (a >>> b) + c; }"); |
|
3356 QTest::newRow("a == b || c != d") << QString("function() { a == b || c != d; }") << QString("function () { a == b || c != d; }"); |
|
3357 QTest::newRow("a == (b || c != d)") << QString("function() { a == (b || c != d); }") << QString("function () { a == (b || c != d); }"); |
|
3358 QTest::newRow("a === b || c !== d") << QString("function() { a === b || c !== d; }") << QString("function () { a === b || c !== d; }"); |
|
3359 QTest::newRow("a === (b || c !== d)") << QString("function() { a === (b || c !== d); }") << QString("function () { a === (b || c !== d); }"); |
|
3360 QTest::newRow("a &= b + c") << QString("function() { a &= b + c; }") << QString("function () { a &= b + c; }"); |
|
3361 QTest::newRow("debugger") << QString("function() { debugger }") << QString("function () { debugger; }"); |
|
3362 } |
|
3363 |
|
3364 void tst_QScriptValue::prettyPrinter() |
|
3365 { |
|
3366 QFETCH(QString, function); |
|
3367 QFETCH(QString, expected); |
|
3368 QScriptEngine eng; |
|
3369 QScriptValue val = eng.evaluate("(" + function + ")"); |
|
3370 QVERIFY(val.isFunction()); |
|
3371 QString actual = val.toString(); |
|
3372 int count = qMin(actual.size(), expected.size()); |
|
3373 // qDebug() << actual << expected; |
|
3374 for (int i = 0; i < count; ++i) { |
|
3375 // qDebug() << i << actual.at(i) << expected.at(i); |
|
3376 QCOMPARE(actual.at(i), expected.at(i)); |
|
3377 } |
|
3378 QCOMPARE(actual.size(), expected.size()); |
|
3379 } |
|
3380 |
|
3381 void tst_QScriptValue::engineDeleted() |
|
3382 { |
|
3383 QScriptEngine *eng = new QScriptEngine; |
|
3384 QScriptValue v1(eng, 123); |
|
3385 QVERIFY(v1.isNumber()); |
|
3386 QScriptValue v2(eng, QString("ciao")); |
|
3387 QVERIFY(v2.isString()); |
|
3388 QScriptValue v3 = eng->newObject(); |
|
3389 QVERIFY(v3.isObject()); |
|
3390 QScriptValue v4 = eng->newQObject(this); |
|
3391 QVERIFY(v4.isQObject()); |
|
3392 QScriptValue v5 = "Hello"; |
|
3393 QVERIFY(v2.isString()); |
|
3394 |
|
3395 delete eng; |
|
3396 |
|
3397 QVERIFY(!v1.isValid()); |
|
3398 QVERIFY(v1.engine() == 0); |
|
3399 QVERIFY(!v2.isValid()); |
|
3400 QVERIFY(v2.engine() == 0); |
|
3401 QVERIFY(!v3.isValid()); |
|
3402 QVERIFY(v3.engine() == 0); |
|
3403 QVERIFY(!v4.isValid()); |
|
3404 QVERIFY(v4.engine() == 0); |
|
3405 QVERIFY(v5.isValid()); |
|
3406 QVERIFY(v5.engine() == 0); |
|
3407 |
|
3408 QVERIFY(!v3.property("foo").isValid()); |
|
3409 } |
|
3410 |
|
3411 void tst_QScriptValue::valueOfWithClosure() |
|
3412 { |
|
3413 QScriptEngine eng; |
|
3414 // valueOf() |
|
3415 { |
|
3416 QScriptValue obj = eng.evaluate("o = {}; (function(foo) { o.valueOf = function() { return foo; } })(123); o"); |
|
3417 QVERIFY(obj.isObject()); |
|
3418 QCOMPARE(obj.toInt32(), 123); |
|
3419 } |
|
3420 // toString() |
|
3421 { |
|
3422 QScriptValue obj = eng.evaluate("o = {}; (function(foo) { o.toString = function() { return foo; } })('ciao'); o"); |
|
3423 QVERIFY(obj.isObject()); |
|
3424 QCOMPARE(obj.toString(), QString::fromLatin1("ciao")); |
|
3425 } |
|
3426 } |
|
3427 |
|
3428 void tst_QScriptValue::objectId() |
|
3429 { |
|
3430 QCOMPARE(QScriptValue().objectId(), (qint64)-1); |
|
3431 QCOMPARE(QScriptValue(QScriptValue::UndefinedValue).objectId(), (qint64)-1); |
|
3432 QCOMPARE(QScriptValue(QScriptValue::NullValue).objectId(), (qint64)-1); |
|
3433 QCOMPARE(QScriptValue(false).objectId(), (qint64)-1); |
|
3434 QCOMPARE(QScriptValue(123).objectId(), (qint64)-1); |
|
3435 QCOMPARE(QScriptValue(uint(123)).objectId(), (qint64)-1); |
|
3436 QCOMPARE(QScriptValue(123.5).objectId(), (qint64)-1); |
|
3437 QCOMPARE(QScriptValue("ciao").objectId(), (qint64)-1); |
|
3438 |
|
3439 QScriptEngine eng; |
|
3440 QScriptValue o1 = eng.newObject(); |
|
3441 QVERIFY(o1.objectId() != -1); |
|
3442 QScriptValue o2 = eng.newObject(); |
|
3443 QVERIFY(o2.objectId() != -1); |
|
3444 QVERIFY(o1.objectId() != o2.objectId()); |
|
3445 |
|
3446 QVERIFY(eng.objectById(o1.objectId()).strictlyEquals(o1)); |
|
3447 QVERIFY(eng.objectById(o2.objectId()).strictlyEquals(o2)); |
|
3448 |
|
3449 qint64 globalObjectId = -1; |
|
3450 { |
|
3451 QScriptValue global = eng.globalObject(); |
|
3452 globalObjectId = global.objectId(); |
|
3453 QVERIFY(globalObjectId != -1); |
|
3454 QVERIFY(eng.objectById(globalObjectId).strictlyEquals(global)); |
|
3455 } |
|
3456 QScriptValue obj = eng.objectById(globalObjectId); |
|
3457 QVERIFY(obj.isObject()); |
|
3458 QVERIFY(obj.strictlyEquals(eng.globalObject())); |
|
3459 } |
|
3460 |
|
3461 QTEST_MAIN(tst_QScriptValue) |