-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_print_statement.py
179 lines (134 loc) · 6.46 KB
/
test_print_statement.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import io
import os
import unittest
import sys
import print_statement
from print_statement import refactor
print_statement.install()
class TestRefactor(unittest.TestCase):
def test_simple(self):
self.assertEqual(refactor('print 1'), 'print(1)')
def test_multiple(self):
self.assertEqual(refactor('print 1, 2, 3'), 'print(1, 2, 3)')
def test_file(self):
self.assertEqual(refactor('print >>f, 1'), 'print(1, file=f)')
def test_end(self):
self.assertEqual(refactor('print 1,'), "print(1, end=' ')")
def test_newline(self):
self.assertEqual(refactor('print 1\n'), 'print(1)\n')
def test_continued(self):
self.assertEqual(refactor('print 1,\\\n2'), 'print(1,\\\n2)')
def test_multiline(self):
self.assertEqual(refactor('print (\n1)'), 'print((\n1))')
def test_multiline_string(self):
self.assertEqual(refactor('print """\n1"""'), 'print("""\n1""")')
def test_condition(self):
self.assertEqual(refactor('if True:\n print 1'), 'if True:\n print(1)')
def test_function(self):
self.assertEqual(refactor('def f():\n print 1'), 'def f():\n print(1)')
def test_class(self):
self.assertEqual(refactor('class F:\n print 1'), 'class F:\n print(1)')
class TestPrinterpreter(unittest.TestCase):
def setUp(self):
self.printerpreter = print_statement._Printerpreter()
self.refactor = self.printerpreter.refactor
self.printerpreter.refactor('from __past__ import print_statement\n')
def test_simple(self):
self.assertEqual(self.refactor('print 1\n'), 'print(1)\n')
def test_multiple(self):
self.assertEqual(self.refactor('print 1, 2, 3\n'), 'print(1, 2, 3)\n')
def test_file(self):
self.assertEqual(self.refactor('print >>f, 1\n'), 'print(1, file=f)\n')
def test_end(self):
self.assertEqual(self.refactor('print 1,\n'), "print(1, end=' ')\n")
def test_continued(self):
self.assertEqual(self.refactor('print 1,\\\n'), '#\n')
self.assertEqual(self.refactor('2\n'), 'print(1,\\\n2)\n')
def test_multiline(self):
self.assertEqual(self.refactor('print (\n'), '#\n')
self.assertEqual(self.refactor('1)\n'), 'print((\n1))\n')
def test_multiline_string(self):
self.assertEqual(self.refactor('print """\n'), '#\n')
self.assertEqual(self.refactor('1"""\n'), 'print("""\n1""")\n')
def test_condition(self):
self.assertEqual(self.refactor('if True:\n'), '#\n')
self.assertEqual(self.refactor(' print 1\n'), 'if True:\n print(1)\n')
def test_function(self):
self.assertEqual(self.refactor('def f():\n'), '#\n')
self.assertEqual(self.refactor(' print 1\n'), 'def f():\n print(1)\n')
def test_class(self):
self.assertEqual(self.refactor('class F:\n'), '#\n')
self.assertEqual(self.refactor(' print 1\n'), 'class F:\n print(1)\n')
def test_with(self):
self.assertEqual(self.refactor('with x:\n'), '#\n')
self.assertEqual(self.refactor(' print 1\n'), 'with x:\n print(1)\n')
def test_try(self):
self.assertEqual(self.refactor('try:\n'), '#\n')
self.assertEqual(self.refactor(' print 1\n'), '#\n')
self.assertEqual(self.refactor('except:\n'), '#\n')
self.assertEqual(self.refactor(' pass\n'), 'try:\n print(1)\nexcept:\n pass\n')
def test_eof(self):
self.assertEqual(self.refactor(''), '')
def test_no_print_function(self):
self.assertEqual(self.refactor('print print\n'), 'print ?print\n')
def test_no_print_kwargs(self):
self.assertEqual(self.refactor('print(1, file=f)\n'), 'print(1, file?=f)\n')
def test_no_print_assignment(self):
self.assertEqual(self.refactor('print = 1\n'), 'print ?= 1\n')
def test_error(self):
self.assertEqual(self.refactor('return return\n'), 'return ?return\n')
def test_empty_block(self):
"""Test an empty block is allowed to end."""
self.assertEqual(self.refactor('if True:\n'), '#\n')
self.assertEqual(self.refactor('\n'), 'if True:\n\n')
def test_bad_try(self):
"""Test a try with no except is allowed to end."""
self.assertEqual(self.refactor('try:\n'), '#\n')
self.assertEqual(self.refactor(' pass\n'), '#\n')
self.assertEqual(self.refactor('\n'), 'try:\n pass\n\n')
class TestImport(unittest.TestCase):
@classmethod
def setUpClass(cls):
assert '_test_package' not in sys.modules
def test_import_package(self):
import _test_package
buffer = io.StringIO()
_test_package.test(buffer)
buffer.seek(0)
self.assertEqual(buffer.read(), '_test_package\n')
self.assertEqual(_test_package.test.__module__, '_test_package')
def test_import_module(self):
from _test_package import test_module
buffer = io.StringIO()
test_module.test(buffer)
buffer.seek(0)
self.assertEqual(buffer.read(), '_test_package.test_module\n')
self.assertEqual(test_module.test.__module__, '_test_package.test_module')
def test_import_module_plain(self):
from _test_package import plain_module
buffer = io.StringIO()
plain_module.test(buffer)
buffer.seek(0)
self.assertEqual(buffer.read(), '_test_package.plain_module\n')
self.assertEqual(plain_module.test.__module__, '_test_package.plain_module')
def test_token_error(self):
with self.assertRaises(SyntaxError) as err:
from _test_package import token_error
filename = os.path.basename(err.exception.filename)
self.assertEqual(filename, 'token_error.py')
def test_syntax_error(self):
with self.assertRaises(SyntaxError) as err:
from _test_package import parse_error
filename = os.path.basename(err.exception.filename)
self.assertEqual(filename, 'parse_error.py')
class TestPastImport(unittest.TestCase):
def test_printerpreter(self):
printer = print_statement._Printerpreter()
printer.refactor('if True:\n', '>>> ')
printer.refactor('from __past__ import print_statement\n', '... ')
printer.refactor('"""\n', '>>> ')
printer.refactor('from __past__ import print_statement\n', '... ')
printer.refactor('"""\n', '... ')
self.assertEqual(printer.refactor('print 1\n', '>>> '), 'print 1\n')
printer.refactor('from __past__ import print_statement\n', '>>> ')
self.assertEqual(printer.refactor('print 1\n', '>>> '), 'print(1)\n')