forked from davisp/python-spidermonkey
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-iterate.py
More file actions
40 lines (34 loc) · 1.03 KB
/
Copy pathtest-iterate.py
File metadata and controls
40 lines (34 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Copyright 2009 Paul J. Davis <paul.joseph.davis@gmail.com>
#
# This file is part of the python-spidermonkey package released
# under the MIT license.
import t
js_for_script = """
var ret = [];
for(var v in data) {ret.push(v);}
ret;
"""
js_for_each_script = """
var ret = [];
for each(var v in data) {ret.push(v);}
ret;
"""
@t.glbl("data", {"foo": "bar", "baz": "bam"})
def test_iter_py_map(cx, glbl):
t.eq(set(cx.execute(js_for_script)), {"foo", "baz"})
t.eq(set(cx.execute(js_for_each_script)), {"bar", "bam"})
@t.glbl("data", ["a", 2, "zing!"])
def test_iter_py_array(cx, glbl):
t.eq(cx.execute(js_for_script), [0, 1, 2])
t.eq(cx.execute(js_for_each_script), ["a", 2, "zing!"])
@t.cx()
def test_iter_js_object(cx):
ret = cx.execute('var f = {"foo": 1, "domino": "daily"}; f;')
items = set(["domino", "foo"])
for k in ret:
t.isin(k, items)
items.remove(k)
@t.cx()
def test_iter_js_array(cx):
ret = cx.execute('["foo", 1, "bing", [3, 6]]')
t.eq([k for k in ret], ["foo", 1, "bing", [3, 6]])