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