forked from andrewschaaf/codegen
-
Notifications
You must be signed in to change notification settings - Fork 5
/
test.py
82 lines (69 loc) · 2.52 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
71
72
73
74
75
76
77
78
79
80
81
82
import unittest
import codegen
import ast
import sys
PY3 = sys.version_info >= (3, 0)
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_OperatorPrecedence(self):
self.assertPreserved("x * y + z")
self.assertPreserved("x * (y + z)")
def test_del(self):
self.assertPreserved("del l[0]")
self.assertPreserved("del obj.x")
def test_try_expect(self):
open_bracket = '('
close_bracket = ')'
if not PY3:
open_bracket = ' '
close_bracket = ' '
source = ("try:\n"
" '#'[2]\n"
"except IndexError:\n"
" print{}'What did you expect?!'{}")
self.assertPreserved(source.format(open_bracket, close_bracket))
source = ("try:\n"
" l = []\n"
" l[1]\n"
"except IndexError as index_error:\n"
" print{}index_error{}")
self.assertPreserved(source.format(open_bracket, close_bracket))
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()