forked from andreif/codegen
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
70 lines (59 loc) · 2.19 KB
/
test.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
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
import unittest
import codegen
import ast
class TestCodegen(unittest.TestCase):
def assertPreserved(self, code):
result = codegen.to_source(ast.parse(code))
self.assertEqual(result.rstrip(), code.rstrip())
def test_BoolOp(self):
self.assertPreserved("(x and y)")
self.assertPreserved("(x or y)")
def test_BinOp(self):
self.assertPreserved("(x + y)")
self.assertPreserved("(x - y)")
self.assertPreserved("(x * y)")
self.assertPreserved("(x / y)")
self.assertPreserved("(x // y)")
self.assertPreserved("(x % y)")
self.assertPreserved("(x ** y)")
self.assertPreserved("(x << y)")
self.assertPreserved("(x >> y)")
self.assertPreserved("(x | y)")
self.assertPreserved("(x & y)")
self.assertPreserved("(x ^ y)")
def test_Compare(self):
self.assertPreserved("(x == y)")
self.assertPreserved("(x > y)")
self.assertPreserved("(x >= y)")
self.assertPreserved("(x in y)")
self.assertPreserved("(x is y)")
self.assertPreserved("(x is not y)")
self.assertPreserved("(x < y)")
self.assertPreserved("(x <= y)")
self.assertPreserved("(x != y)")
self.assertPreserved("(x not in y)")
def test_UnaryOp(self):
self.assertPreserved("(~x)")
self.assertPreserved("(not x)")
self.assertPreserved("(+x)")
self.assertPreserved("(-x)")
def test_del(self):
self.assertPreserved("del l[0]")
self.assertPreserved("del obj.x")
def test_try_expect(self):
source = ("try:\n"
" '#'[2]\n"
"except IndexError:\n"
" print 'What did you expect?!'")
self.assertPreserved(source)
source = ("try:\n"
" l = []\n"
" l[1]\n"
"except IndexError, index_error:\n"
" print index_error")
self.assertPreserved(source)
def test_import(self):
self.assertPreserved("import intertools as iterators")
self.assertPreserved("from math import floor as fl, ceil as cl")
if __name__ == '__main__':
unittest.main()