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 |
|
|
45 |
#include <QtScript/qscriptengine.h>
|
|
46 |
#include <QtScript/qscriptvalueiterator.h>
|
|
47 |
|
|
48 |
//TESTED_CLASS=
|
|
49 |
//TESTED_FILES=
|
|
50 |
|
|
51 |
Q_DECLARE_METATYPE(QScriptValue);
|
|
52 |
|
|
53 |
class tst_QScriptValueIterator : public QObject
|
|
54 |
{
|
|
55 |
Q_OBJECT
|
|
56 |
|
|
57 |
public:
|
|
58 |
tst_QScriptValueIterator();
|
|
59 |
virtual ~tst_QScriptValueIterator();
|
|
60 |
|
|
61 |
private slots:
|
|
62 |
void iterateForward_data();
|
|
63 |
void iterateForward();
|
|
64 |
void iterateBackward_data();
|
|
65 |
void iterateBackward();
|
|
66 |
void iterateArray_data();
|
|
67 |
void iterateArray();
|
|
68 |
void iterateBackAndForth();
|
|
69 |
void setValue();
|
|
70 |
void remove();
|
|
71 |
void iterateString();
|
|
72 |
void iterateGetterSetter();
|
|
73 |
void assignObjectToIterator();
|
|
74 |
};
|
|
75 |
|
|
76 |
tst_QScriptValueIterator::tst_QScriptValueIterator()
|
|
77 |
{
|
|
78 |
}
|
|
79 |
|
|
80 |
tst_QScriptValueIterator::~tst_QScriptValueIterator()
|
|
81 |
{
|
|
82 |
}
|
|
83 |
|
|
84 |
void tst_QScriptValueIterator::iterateForward_data()
|
|
85 |
{
|
|
86 |
QTest::addColumn<QStringList>("propertyNames");
|
|
87 |
QTest::addColumn<QStringList>("propertyValues");
|
|
88 |
|
|
89 |
QTest::newRow("no properties")
|
|
90 |
<< QStringList() << QStringList();
|
|
91 |
QTest::newRow("foo=bar")
|
|
92 |
<< (QStringList() << "foo")
|
|
93 |
<< (QStringList() << "bar");
|
|
94 |
QTest::newRow("foo=bar, baz=123")
|
|
95 |
<< (QStringList() << "foo" << "baz")
|
|
96 |
<< (QStringList() << "bar" << "123");
|
|
97 |
QTest::newRow("foo=bar, baz=123, rab=oof")
|
|
98 |
<< (QStringList() << "foo" << "baz" << "rab")
|
|
99 |
<< (QStringList() << "bar" << "123" << "oof");
|
|
100 |
}
|
|
101 |
|
|
102 |
void tst_QScriptValueIterator::iterateForward()
|
|
103 |
{
|
|
104 |
QFETCH(QStringList, propertyNames);
|
|
105 |
QFETCH(QStringList, propertyValues);
|
|
106 |
QMap<QString, QString> pmap;
|
|
107 |
Q_ASSERT(propertyNames.size() == propertyValues.size());
|
|
108 |
|
|
109 |
QScriptEngine engine;
|
|
110 |
QScriptValue object = engine.newObject();
|
|
111 |
for (int i = 0; i < propertyNames.size(); ++i) {
|
|
112 |
QString name = propertyNames.at(i);
|
|
113 |
QString value = propertyValues.at(i);
|
|
114 |
pmap.insert(name, value);
|
|
115 |
object.setProperty(name, QScriptValue(&engine, value));
|
|
116 |
}
|
|
117 |
QScriptValue otherObject = engine.newObject();
|
|
118 |
otherObject.setProperty("foo", QScriptValue(&engine, 123456));
|
|
119 |
otherObject.setProperty("protoProperty", QScriptValue(&engine, 654321));
|
|
120 |
object.setPrototype(otherObject); // should not affect iterator
|
|
121 |
|
|
122 |
QStringList lst;
|
|
123 |
QScriptValueIterator it(object);
|
|
124 |
while (!pmap.isEmpty()) {
|
|
125 |
QCOMPARE(it.hasNext(), true);
|
|
126 |
QCOMPARE(it.hasNext(), true);
|
|
127 |
it.next();
|
|
128 |
QString name = it.name();
|
|
129 |
QCOMPARE(pmap.contains(name), true);
|
|
130 |
QCOMPARE(it.name(), name);
|
|
131 |
QCOMPARE(it.flags(), object.propertyFlags(name));
|
|
132 |
QCOMPARE(it.value().strictlyEquals(QScriptValue(&engine, pmap.value(name))), true);
|
|
133 |
QCOMPARE(it.scriptName(), engine.toStringHandle(name));
|
|
134 |
pmap.remove(name);
|
|
135 |
lst.append(name);
|
|
136 |
}
|
|
137 |
|
|
138 |
QCOMPARE(it.hasNext(), false);
|
|
139 |
QCOMPARE(it.hasNext(), false);
|
|
140 |
|
|
141 |
it.toFront();
|
|
142 |
for (int i = 0; i < lst.count(); ++i) {
|
|
143 |
QCOMPARE(it.hasNext(), true);
|
|
144 |
it.next();
|
|
145 |
QCOMPARE(it.name(), lst.at(i));
|
|
146 |
}
|
|
147 |
|
|
148 |
for (int i = 0; i < lst.count(); ++i) {
|
|
149 |
QCOMPARE(it.hasPrevious(), true);
|
|
150 |
it.previous();
|
|
151 |
QCOMPARE(it.name(), lst.at(lst.count()-1-i));
|
|
152 |
}
|
|
153 |
QCOMPARE(it.hasPrevious(), false);
|
|
154 |
}
|
|
155 |
|
|
156 |
void tst_QScriptValueIterator::iterateBackward_data()
|
|
157 |
{
|
|
158 |
iterateForward_data();
|
|
159 |
}
|
|
160 |
|
|
161 |
void tst_QScriptValueIterator::iterateBackward()
|
|
162 |
{
|
|
163 |
QFETCH(QStringList, propertyNames);
|
|
164 |
QFETCH(QStringList, propertyValues);
|
|
165 |
QMap<QString, QString> pmap;
|
|
166 |
Q_ASSERT(propertyNames.size() == propertyValues.size());
|
|
167 |
|
|
168 |
QScriptEngine engine;
|
|
169 |
QScriptValue object = engine.newObject();
|
|
170 |
for (int i = 0; i < propertyNames.size(); ++i) {
|
|
171 |
QString name = propertyNames.at(i);
|
|
172 |
QString value = propertyValues.at(i);
|
|
173 |
pmap.insert(name, value);
|
|
174 |
object.setProperty(name, QScriptValue(&engine, value));
|
|
175 |
}
|
|
176 |
|
|
177 |
QStringList lst;
|
|
178 |
QScriptValueIterator it(object);
|
|
179 |
it.toBack();
|
|
180 |
while (!pmap.isEmpty()) {
|
|
181 |
QCOMPARE(it.hasPrevious(), true);
|
|
182 |
QCOMPARE(it.hasPrevious(), true);
|
|
183 |
it.previous();
|
|
184 |
QString name = it.name();
|
|
185 |
QCOMPARE(pmap.contains(name), true);
|
|
186 |
QCOMPARE(it.name(), name);
|
|
187 |
QCOMPARE(it.flags(), object.propertyFlags(name));
|
|
188 |
QCOMPARE(it.value().strictlyEquals(QScriptValue(&engine, pmap.value(name))), true);
|
|
189 |
pmap.remove(name);
|
|
190 |
lst.append(name);
|
|
191 |
}
|
|
192 |
|
|
193 |
QCOMPARE(it.hasPrevious(), false);
|
|
194 |
QCOMPARE(it.hasPrevious(), false);
|
|
195 |
|
|
196 |
it.toBack();
|
|
197 |
for (int i = 0; i < lst.count(); ++i) {
|
|
198 |
QCOMPARE(it.hasPrevious(), true);
|
|
199 |
it.previous();
|
|
200 |
QCOMPARE(it.name(), lst.at(i));
|
|
201 |
}
|
|
202 |
|
|
203 |
for (int i = 0; i < lst.count(); ++i) {
|
|
204 |
QCOMPARE(it.hasNext(), true);
|
|
205 |
it.next();
|
|
206 |
QCOMPARE(it.name(), lst.at(lst.count()-1-i));
|
|
207 |
}
|
|
208 |
QCOMPARE(it.hasNext(), false);
|
|
209 |
}
|
|
210 |
|
|
211 |
void tst_QScriptValueIterator::iterateArray_data()
|
|
212 |
{
|
|
213 |
QTest::addColumn<QStringList>("inputPropertyNames");
|
|
214 |
QTest::addColumn<QStringList>("inputPropertyValues");
|
|
215 |
QTest::addColumn<QStringList>("propertyNames");
|
|
216 |
QTest::addColumn<QStringList>("propertyValues");
|
|
217 |
QTest::newRow("no elements") << QStringList() << QStringList() << QStringList() << QStringList();
|
|
218 |
|
|
219 |
|
|
220 |
QTest::newRow("0=foo, 1=barr")
|
|
221 |
<< (QStringList() << "0" << "1")
|
|
222 |
<< (QStringList() << "foo" << "bar")
|
|
223 |
<< (QStringList() << "0" << "1")
|
|
224 |
<< (QStringList() << "foo" << "bar");
|
|
225 |
|
|
226 |
|
|
227 |
QTest::newRow("0=foo, 3=barr")
|
|
228 |
<< (QStringList() << "0" << "1" << "2" << "3")
|
|
229 |
<< (QStringList() << "foo" << "" << "" << "bar")
|
|
230 |
<< (QStringList() << "0" << "1" << "2" << "3")
|
|
231 |
<< (QStringList() << "foo" << "" << "" << "bar");
|
|
232 |
}
|
|
233 |
|
|
234 |
void tst_QScriptValueIterator::iterateArray()
|
|
235 |
{
|
|
236 |
QFETCH(QStringList, inputPropertyNames);
|
|
237 |
QFETCH(QStringList, inputPropertyValues);
|
|
238 |
QFETCH(QStringList, propertyNames);
|
|
239 |
QFETCH(QStringList, propertyValues);
|
|
240 |
|
|
241 |
QScriptEngine engine;
|
|
242 |
QScriptValue array = engine.newArray();
|
|
243 |
for (int i = 0; i < inputPropertyNames.size(); ++i) {
|
|
244 |
array.setProperty(inputPropertyNames.at(i), inputPropertyValues.at(i));
|
|
245 |
}
|
|
246 |
|
|
247 |
int length = array.property("length").toInt32();
|
|
248 |
QCOMPARE(length, propertyNames.size());
|
|
249 |
QScriptValueIterator it(array);
|
|
250 |
for (int i = 0; i < length; ++i) {
|
|
251 |
QCOMPARE(it.hasNext(), true);
|
|
252 |
it.next();
|
|
253 |
QCOMPARE(it.name(), propertyNames.at(i));
|
|
254 |
QCOMPARE(it.flags(), array.propertyFlags(propertyNames.at(i)));
|
|
255 |
QVERIFY(it.value().strictlyEquals(array.property(propertyNames.at(i))));
|
|
256 |
QCOMPARE(it.value().toString(), propertyValues.at(i));
|
|
257 |
}
|
|
258 |
QCOMPARE(it.hasNext(), false);
|
|
259 |
|
|
260 |
QCOMPARE(it.hasPrevious(), length > 0);
|
|
261 |
for (int i = length - 1; i >= 0; --i) {
|
|
262 |
it.previous();
|
|
263 |
QCOMPARE(it.name(), propertyNames.at(i));
|
|
264 |
QCOMPARE(it.flags(), array.propertyFlags(propertyNames.at(i)));
|
|
265 |
QVERIFY(it.value().strictlyEquals(array.property(propertyNames.at(i))));
|
|
266 |
QCOMPARE(it.value().toString(), propertyValues.at(i));
|
|
267 |
QCOMPARE(it.hasPrevious(), i > 0);
|
|
268 |
}
|
|
269 |
QCOMPARE(it.hasPrevious(), false);
|
|
270 |
|
|
271 |
// hasNext() and hasPrevious() cache their result; verify that the result is in sync
|
|
272 |
if (length > 1) {
|
|
273 |
QVERIFY(it.hasNext());
|
|
274 |
it.next();
|
|
275 |
QCOMPARE(it.name(), QString::fromLatin1("0"));
|
|
276 |
QVERIFY(it.hasNext());
|
|
277 |
it.previous();
|
|
278 |
QCOMPARE(it.name(), QString::fromLatin1("0"));
|
|
279 |
QVERIFY(!it.hasPrevious());
|
|
280 |
it.next();
|
|
281 |
QCOMPARE(it.name(), QString::fromLatin1("0"));
|
|
282 |
QVERIFY(it.hasPrevious());
|
|
283 |
it.next();
|
|
284 |
QCOMPARE(it.name(), QString::fromLatin1("1"));
|
|
285 |
}
|
|
286 |
{
|
|
287 |
// same test as object:
|
|
288 |
QScriptValue originalArray = engine.newArray();
|
|
289 |
for (int i = 0; i < inputPropertyNames.size(); ++i) {
|
|
290 |
originalArray.setProperty(inputPropertyNames.at(i), inputPropertyValues.at(i));
|
|
291 |
}
|
|
292 |
QScriptValue array = originalArray.toObject();
|
|
293 |
int length = array.property("length").toInt32();
|
|
294 |
QCOMPARE(length, propertyNames.size());
|
|
295 |
QScriptValueIterator it(array);
|
|
296 |
for (int i = 0; i < length; ++i) {
|
|
297 |
QCOMPARE(it.hasNext(), true);
|
|
298 |
it.next();
|
|
299 |
QCOMPARE(it.name(), propertyNames.at(i));
|
|
300 |
QCOMPARE(it.flags(), array.propertyFlags(propertyNames.at(i)));
|
|
301 |
QVERIFY(it.value().strictlyEquals(array.property(propertyNames.at(i))));
|
|
302 |
QCOMPARE(it.value().toString(), propertyValues.at(i));
|
|
303 |
}
|
|
304 |
QCOMPARE(it.hasNext(), false);
|
|
305 |
}
|
|
306 |
}
|
|
307 |
|
|
308 |
void tst_QScriptValueIterator::iterateBackAndForth()
|
|
309 |
{
|
|
310 |
QScriptEngine engine;
|
|
311 |
{
|
|
312 |
QScriptValue object = engine.newObject();
|
|
313 |
object.setProperty("foo", QScriptValue(&engine, "bar"));
|
|
314 |
object.setProperty("rab", QScriptValue(&engine, "oof"),
|
|
315 |
QScriptValue::SkipInEnumeration); // should not affect iterator
|
|
316 |
QScriptValueIterator it(object);
|
|
317 |
QVERIFY(it.hasNext());
|
|
318 |
it.next();
|
|
319 |
QCOMPARE(it.name(), QLatin1String("foo"));
|
|
320 |
QVERIFY(it.hasPrevious());
|
|
321 |
it.previous();
|
|
322 |
QCOMPARE(it.name(), QLatin1String("foo"));
|
|
323 |
QVERIFY(it.hasNext());
|
|
324 |
it.next();
|
|
325 |
QCOMPARE(it.name(), QLatin1String("foo"));
|
|
326 |
QVERIFY(it.hasPrevious());
|
|
327 |
it.previous();
|
|
328 |
QCOMPARE(it.name(), QLatin1String("foo"));
|
|
329 |
QVERIFY(it.hasNext());
|
|
330 |
it.next();
|
|
331 |
QCOMPARE(it.name(), QLatin1String("foo"));
|
|
332 |
QVERIFY(it.hasNext());
|
|
333 |
it.next();
|
|
334 |
QCOMPARE(it.name(), QLatin1String("rab"));
|
|
335 |
QVERIFY(it.hasPrevious());
|
|
336 |
it.previous();
|
|
337 |
QCOMPARE(it.name(), QLatin1String("rab"));
|
|
338 |
QVERIFY(it.hasNext());
|
|
339 |
it.next();
|
|
340 |
QCOMPARE(it.name(), QLatin1String("rab"));
|
|
341 |
QVERIFY(it.hasPrevious());
|
|
342 |
it.previous();
|
|
343 |
QCOMPARE(it.name(), QLatin1String("rab"));
|
|
344 |
}
|
|
345 |
{
|
|
346 |
// hasNext() and hasPrevious() cache their result; verify that the result is in sync
|
|
347 |
QScriptValue object = engine.newObject();
|
|
348 |
object.setProperty("foo", QScriptValue(&engine, "bar"));
|
|
349 |
object.setProperty("rab", QScriptValue(&engine, "oof"));
|
|
350 |
QScriptValueIterator it(object);
|
|
351 |
QVERIFY(it.hasNext());
|
|
352 |
it.next();
|
|
353 |
QCOMPARE(it.name(), QString::fromLatin1("foo"));
|
|
354 |
QVERIFY(it.hasNext());
|
|
355 |
it.previous();
|
|
356 |
QCOMPARE(it.name(), QString::fromLatin1("foo"));
|
|
357 |
QVERIFY(!it.hasPrevious());
|
|
358 |
it.next();
|
|
359 |
QCOMPARE(it.name(), QString::fromLatin1("foo"));
|
|
360 |
QVERIFY(it.hasPrevious());
|
|
361 |
it.next();
|
|
362 |
QCOMPARE(it.name(), QString::fromLatin1("rab"));
|
|
363 |
}
|
|
364 |
}
|
|
365 |
|
|
366 |
void tst_QScriptValueIterator::setValue()
|
|
367 |
{
|
|
368 |
QScriptEngine engine;
|
|
369 |
QScriptValue object = engine.newObject();
|
|
370 |
object.setProperty("foo", QScriptValue(&engine, "bar"));
|
|
371 |
QScriptValueIterator it(object);
|
|
372 |
it.next();
|
|
373 |
QCOMPARE(it.name(), QLatin1String("foo"));
|
|
374 |
it.setValue(QScriptValue(&engine, "baz"));
|
|
375 |
QCOMPARE(it.value().strictlyEquals(QScriptValue(&engine, QLatin1String("baz"))), true);
|
|
376 |
QCOMPARE(object.property("foo").toString(), QLatin1String("baz"));
|
|
377 |
it.setValue(QScriptValue(&engine, "zab"));
|
|
378 |
QCOMPARE(it.value().strictlyEquals(QScriptValue(&engine, QLatin1String("zab"))), true);
|
|
379 |
QCOMPARE(object.property("foo").toString(), QLatin1String("zab"));
|
|
380 |
}
|
|
381 |
|
|
382 |
void tst_QScriptValueIterator::remove()
|
|
383 |
{
|
|
384 |
QScriptEngine engine;
|
|
385 |
QScriptValue object = engine.newObject();
|
|
386 |
object.setProperty("foo", QScriptValue(&engine, "bar"),
|
|
387 |
QScriptValue::SkipInEnumeration); // should not affect iterator
|
|
388 |
object.setProperty("rab", QScriptValue(&engine, "oof"));
|
|
389 |
QScriptValueIterator it(object);
|
|
390 |
it.next();
|
|
391 |
QCOMPARE(it.name(), QLatin1String("foo"));
|
|
392 |
it.remove();
|
|
393 |
QCOMPARE(it.hasPrevious(), false);
|
|
394 |
QCOMPARE(object.property("foo").isValid(), false);
|
|
395 |
QCOMPARE(object.property("rab").toString(), QLatin1String("oof"));
|
|
396 |
it.next();
|
|
397 |
QCOMPARE(it.name(), QLatin1String("rab"));
|
|
398 |
QCOMPARE(it.value().toString(), QLatin1String("oof"));
|
|
399 |
QCOMPARE(it.hasNext(), false);
|
|
400 |
it.remove();
|
|
401 |
QCOMPARE(object.property("rab").isValid(), false);
|
|
402 |
QCOMPARE(it.hasPrevious(), false);
|
|
403 |
QCOMPARE(it.hasNext(), false);
|
|
404 |
}
|
|
405 |
|
|
406 |
void tst_QScriptValueIterator::iterateString()
|
|
407 |
{
|
|
408 |
QScriptEngine engine;
|
|
409 |
QScriptValue str = QScriptValue(&engine, QString::fromLatin1("ciao"));
|
|
410 |
QVERIFY(str.isString());
|
|
411 |
QScriptValue obj = str.toObject();
|
|
412 |
int length = obj.property("length").toInt32();
|
|
413 |
QCOMPARE(length, 4);
|
|
414 |
QScriptValueIterator it(obj);
|
|
415 |
for (int i = 0; i < length; ++i) {
|
|
416 |
QCOMPARE(it.hasNext(), true);
|
|
417 |
QString indexStr = QScriptValue(&engine, i).toString();
|
|
418 |
it.next();
|
|
419 |
QCOMPARE(it.name(), indexStr);
|
|
420 |
QCOMPARE(it.flags(), obj.propertyFlags(indexStr));
|
|
421 |
QCOMPARE(it.value().strictlyEquals(obj.property(indexStr)), true);
|
|
422 |
}
|
|
423 |
QCOMPARE(it.hasNext(), false);
|
|
424 |
|
|
425 |
QVERIFY(it.hasPrevious());
|
|
426 |
for (int i = length - 1; i >= 0; --i) {
|
|
427 |
it.previous();
|
|
428 |
QString indexStr = QScriptValue(&engine, i).toString();
|
|
429 |
QCOMPARE(it.name(), indexStr);
|
|
430 |
QCOMPARE(it.flags(), obj.propertyFlags(indexStr));
|
|
431 |
QCOMPARE(it.value().strictlyEquals(obj.property(indexStr)), true);
|
|
432 |
QCOMPARE(it.hasPrevious(), i > 0);
|
|
433 |
}
|
|
434 |
QCOMPARE(it.hasPrevious(), false);
|
|
435 |
}
|
|
436 |
|
|
437 |
static QScriptValue myGetterSetter(QScriptContext *ctx, QScriptEngine *)
|
|
438 |
{
|
|
439 |
if (ctx->argumentCount() == 1)
|
|
440 |
ctx->thisObject().setProperty("bar", ctx->argument(0));
|
|
441 |
return ctx->thisObject().property("bar");
|
|
442 |
}
|
|
443 |
|
|
444 |
static QScriptValue myGetter(QScriptContext *ctx, QScriptEngine *)
|
|
445 |
{
|
|
446 |
return ctx->thisObject().property("bar");
|
|
447 |
}
|
|
448 |
|
|
449 |
static QScriptValue mySetter(QScriptContext *ctx, QScriptEngine *)
|
|
450 |
{
|
|
451 |
ctx->thisObject().setProperty("bar", ctx->argument(0));
|
|
452 |
return ctx->argument(0);
|
|
453 |
}
|
|
454 |
|
|
455 |
void tst_QScriptValueIterator::iterateGetterSetter()
|
|
456 |
{
|
|
457 |
// unified getter/setter function
|
|
458 |
{
|
|
459 |
QScriptEngine eng;
|
|
460 |
QScriptValue obj = eng.newObject();
|
|
461 |
obj.setProperty("foo", eng.newFunction(myGetterSetter),
|
|
462 |
QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
|
|
463 |
QScriptValue val(&eng, 123);
|
|
464 |
obj.setProperty("foo", val);
|
|
465 |
QVERIFY(obj.property("bar").strictlyEquals(val));
|
|
466 |
QVERIFY(obj.property("foo").strictlyEquals(val));
|
|
467 |
|
|
468 |
QScriptValueIterator it(obj);
|
|
469 |
QVERIFY(it.hasNext());
|
|
470 |
it.next();
|
|
471 |
QCOMPARE(it.name(), QString::fromLatin1("foo"));
|
|
472 |
QCOMPARE(it.flags(), QScriptValue::PropertyFlags(QScriptValue::PropertyGetter | QScriptValue::PropertySetter));
|
|
473 |
QVERIFY(it.value().strictlyEquals(val));
|
|
474 |
QScriptValue val2(&eng, 456);
|
|
475 |
it.setValue(val2);
|
|
476 |
QVERIFY(obj.property("bar").strictlyEquals(val2));
|
|
477 |
QVERIFY(obj.property("foo").strictlyEquals(val2));
|
|
478 |
|
|
479 |
QVERIFY(it.hasNext());
|
|
480 |
it.next();
|
|
481 |
QCOMPARE(it.name(), QString::fromLatin1("bar"));
|
|
482 |
QVERIFY(!it.hasNext());
|
|
483 |
|
|
484 |
QVERIFY(it.hasPrevious());
|
|
485 |
it.previous();
|
|
486 |
QCOMPARE(it.name(), QString::fromLatin1("bar"));
|
|
487 |
QVERIFY(it.hasPrevious());
|
|
488 |
it.previous();
|
|
489 |
QCOMPARE(it.name(), QString::fromLatin1("foo"));
|
|
490 |
QCOMPARE(it.flags(), QScriptValue::PropertyFlags(QScriptValue::PropertyGetter | QScriptValue::PropertySetter));
|
|
491 |
QVERIFY(it.value().strictlyEquals(val2));
|
|
492 |
it.setValue(val);
|
|
493 |
QVERIFY(obj.property("bar").strictlyEquals(val));
|
|
494 |
QVERIFY(obj.property("foo").strictlyEquals(val));
|
|
495 |
}
|
|
496 |
// separate getter/setter function
|
|
497 |
for (int x = 0; x < 2; ++x) {
|
|
498 |
QScriptEngine eng;
|
|
499 |
QScriptValue obj = eng.newObject();
|
|
500 |
if (x == 0) {
|
|
501 |
obj.setProperty("foo", eng.newFunction(myGetter), QScriptValue::PropertyGetter);
|
|
502 |
obj.setProperty("foo", eng.newFunction(mySetter), QScriptValue::PropertySetter);
|
|
503 |
} else {
|
|
504 |
obj.setProperty("foo", eng.newFunction(mySetter), QScriptValue::PropertySetter);
|
|
505 |
obj.setProperty("foo", eng.newFunction(myGetter), QScriptValue::PropertyGetter);
|
|
506 |
}
|
|
507 |
QScriptValue val(&eng, 123);
|
|
508 |
obj.setProperty("foo", val);
|
|
509 |
QVERIFY(obj.property("bar").strictlyEquals(val));
|
|
510 |
QVERIFY(obj.property("foo").strictlyEquals(val));
|
|
511 |
|
|
512 |
QScriptValueIterator it(obj);
|
|
513 |
QVERIFY(it.hasNext());
|
|
514 |
it.next();
|
|
515 |
QCOMPARE(it.name(), QString::fromLatin1("foo"));
|
|
516 |
QVERIFY(it.value().strictlyEquals(val));
|
|
517 |
QScriptValue val2(&eng, 456);
|
|
518 |
it.setValue(val2);
|
|
519 |
QVERIFY(obj.property("bar").strictlyEquals(val2));
|
|
520 |
QVERIFY(obj.property("foo").strictlyEquals(val2));
|
|
521 |
|
|
522 |
QVERIFY(it.hasNext());
|
|
523 |
it.next();
|
|
524 |
QCOMPARE(it.name(), QString::fromLatin1("bar"));
|
|
525 |
QVERIFY(!it.hasNext());
|
|
526 |
|
|
527 |
QVERIFY(it.hasPrevious());
|
|
528 |
it.previous();
|
|
529 |
QCOMPARE(it.name(), QString::fromLatin1("bar"));
|
|
530 |
QVERIFY(it.hasPrevious());
|
|
531 |
it.previous();
|
|
532 |
QCOMPARE(it.name(), QString::fromLatin1("foo"));
|
|
533 |
QVERIFY(it.value().strictlyEquals(val2));
|
|
534 |
it.setValue(val);
|
|
535 |
QVERIFY(obj.property("bar").strictlyEquals(val));
|
|
536 |
QVERIFY(obj.property("foo").strictlyEquals(val));
|
|
537 |
}
|
|
538 |
}
|
|
539 |
|
|
540 |
void tst_QScriptValueIterator::assignObjectToIterator()
|
|
541 |
{
|
|
542 |
QScriptEngine eng;
|
|
543 |
QScriptValue obj1 = eng.newObject();
|
|
544 |
obj1.setProperty("foo", 123);
|
|
545 |
QScriptValue obj2 = eng.newObject();
|
|
546 |
obj2.setProperty("bar", 456);
|
|
547 |
|
|
548 |
QScriptValueIterator it(obj1);
|
|
549 |
QVERIFY(it.hasNext());
|
|
550 |
it.next();
|
|
551 |
it = obj2;
|
|
552 |
QVERIFY(it.hasNext());
|
|
553 |
it.next();
|
|
554 |
QCOMPARE(it.name(), QString::fromLatin1("bar"));
|
|
555 |
|
|
556 |
it = obj1;
|
|
557 |
QVERIFY(it.hasNext());
|
|
558 |
it.next();
|
|
559 |
QCOMPARE(it.name(), QString::fromLatin1("foo"));
|
|
560 |
|
|
561 |
it = obj2;
|
|
562 |
QVERIFY(it.hasNext());
|
|
563 |
it.next();
|
|
564 |
QCOMPARE(it.name(), QString::fromLatin1("bar"));
|
|
565 |
|
|
566 |
it = obj2;
|
|
567 |
QVERIFY(it.hasNext());
|
|
568 |
it.next();
|
|
569 |
QCOMPARE(it.name(), QString::fromLatin1("bar"));
|
|
570 |
}
|
|
571 |
|
|
572 |
QTEST_MAIN(tst_QScriptValueIterator)
|
|
573 |
#include "tst_qscriptvalueiterator.moc"
|