-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtest_statically.py
More file actions
141 lines (115 loc) · 4 KB
/
test_statically.py
File metadata and controls
141 lines (115 loc) · 4 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import unittest
import asyncio
import subprocess
import sys
import os.path
import cython
try:
import IPython
except ImportError:
ipython_installed = False
else:
ipython_installed = True
import statically
ONE_HUNDRED = 100
execute = asyncio.get_event_loop().run_until_complete
def is_cython_function(obj):
return 'cython_function_or_method' in str(type(obj))
def anext(agen):
gen = agen.asend(None)
try:
gen.send(None)
except StopIteration as error:
return error.args[0]
class CompilationSuite(unittest.TestCase):
def test_function(self):
@statically.typed
def identity(x: cython.int):
return x
self.assertEqual(identity(14), 14)
def test_is_compiled(self):
@statically.typed
def compiled(x: cython.int):
return x
self.assertTrue(is_cython_function(compiled))
def test_non_local_var_in_class(self):
one = 1
@statically.typed
class Class:
number = 100 + one
self.assertEqual(Class.number, 101)
def test_non_local_var_in_method(self):
two = 2
class Class:
@statically.typed
def add_two(self, x):
return x + two
obj = Class()
self.assertEqual(obj.add_two(100), 102)
def test_non_local_var_in_function(self):
tree = 3
@statically.typed
def add_tree(x):
return x + tree
self.assertEqual(add_tree(100), 103)
def test_non_local_var_in_generator_function(self):
four = 4
@statically.typed
def add_four(x):
yield x + four
self.assertEqual(next(add_four(100)), 104)
def test_non_local_var_in_coroutine_function(self):
five = 5
@statically.typed
async def add_five(x):
return x + five
self.assertEqual(execute(add_five(100)), 105)
def test_global_var_in_class(self):
@statically.typed
class Class_:
number = 1 + ONE_HUNDRED
self.assertEqual(Class_.number, 101)
def test_global_var_in_method(self):
class Class:
@statically.typed
def add_one_hundred(self, x):
return ONE_HUNDRED + x
obj = Class()
self.assertEqual(obj.add_one_hundred(2), 102)
def test_global_var_in_function(self):
@statically.typed
def add_one_hundred(x):
return ONE_HUNDRED + x
self.assertEqual(add_one_hundred(3), 103)
def test_global_var_in_generator_function(self):
@statically.typed
def add_one_hundred(x):
yield ONE_HUNDRED + x
self.assertEqual(next(add_one_hundred(4)), 104)
def test_global_var_in_coroutine_function(self):
@statically.typed
async def add_one_hundred(x):
return ONE_HUNDRED + x
self.assertEqual(execute(add_one_hundred(5)), 105)
@unittest.skipUnless(statically.has_async_gen_fun, "Test does not apply for this version of Python")
def test_async_generator(self):
message = r"Async generator funcions are not supported."
with self.assertRaisesRegex(TypeError, message):
from test_statically_async import generator
@unittest.skipUnless(ipython_installed, "IPython not installed")
class IPythonSuite(unittest.TestCase):
def test_ipython(self):
base_dir = os.path.dirname(sys.executable)
executable = os.path.join(base_dir, "ipython")
process = subprocess.Popen([executable], stdin=subprocess.PIPE,
stdout=subprocess.PIPE)
script = "import statically\n" \
"@statically.typed\n" \
"def add(a: int, b: int): return a + b\n\n" \
"'cython_function_or_method' in str(type(add))\n".encode()
stdout, _ = process.communicate(script)
lines = stdout.decode().split("\n")
process.terminate()
self.assertEqual(lines[-4], "In [3]: Out[3]: True")
if __name__ == '__main__':
unittest.main()