Debian Codegraph @

pypy/module/_continuation/test/test_generator.py

raw symbols SCIP
1 from pypy.module._continuation.test.support import BaseAppTest
2
3
4 class AppTestGenerator(BaseAppTest):
5
6 def test_simple(self):
7 from _continuation import generator
8 #
9 @generator
10 def f(gen, n):
11 gen.switch(n+1)
12 f2(gen, n+2)
13 gen.switch(n+3)
14 #
15 def f2(gen, m):
16 gen.switch(m*2)
17 #
18 g = f(10)
19 res = next(g)
20 assert res == 11
21 res = next(g)
22 assert res == 24
23 res = next(g)
24 assert res == 13
25 raises(StopIteration, next, g)
26
27 def test_iterator(self):
28 from _continuation import generator
29 #
30 @generator
31 def f(gen, n):
32 gen.switch(n+1)
33 f2(gen, n+2)
34 gen.switch(n+3)
35 #
36 def f2(gen, m):
37 gen.switch(m*2)
38 #
39 res = list(f(10))
40 assert res == [11, 24, 13]
41 g = f(20)
42 assert iter(g) is g
43
44 def test_bound_method(self):
45 from _continuation import generator
46 #
47 class A(object):
48 def __init__(self, m):
49 self.m = m
50 #
51 @generator
52 def f(self, gen, n):
53 gen.switch(n - self.m)
54 #
55 a = A(10)
56 res = list(a.f(25))
57 assert res == [15]
58
59 def test_must_return_None(self):
60 from _continuation import generator
61 #
62 @generator
63 def f(gen, n):
64 gen.switch(n+1)
65 return "foo"
66 #
67 g = f(10)
68 res = next(g)
69 assert res == 11
70 raises(TypeError, next, g)