-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_scope_capture.py
38 lines (30 loc) · 935 Bytes
/
test_scope_capture.py
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
from scope_capture import capture
def test_capturing_in_a_lambda():
a = []
SOMETHING_GLOBAL = 123
for i in [1, 2]:
a.append(capture(lambda: (SOMETHING_GLOBAL, i)))
assert [f() for f in a] == [(123, 1), (123, 2)]
def test_accepting_arguments():
assert capture(lambda a: a + 2)(2) == 4
assert capture(lambda a=3: a + 2)() == 5
assert capture(lambda a: a + 2)(a=4) == 6
def test_capturing_in_a_function():
a = []
SOMETHING_GLOBAL = 256
for i in [3, 4]:
def f():
return (SOMETHING_GLOBAL, i)
a.append(capture(f))
assert [f() for f in a] == [(256, 3), (256, 4)]
def test_capturing_nested_scopes():
functions = []
for i in [1, 2]:
def a():
def b():
def c():
functions.append(capture(lambda: i))
c()
b()
a()
assert [f() for f in functions] == [1, 2]