2 Test cases for codeop.py
6 from test
.test_support
import run_unittest
, is_jython
8 from codeop
import compile_command
, PyCF_DONT_IMPLY_DEDENT
14 def unify_callables(d
):
20 class CodeopTests(unittest
.TestCase
):
22 def assertValid(self
, str, symbol
='single'):
23 '''succeed iff str is a valid piece of code'''
25 code
= compile_command(str, "<input>", symbol
)
27 if symbol
== "single":
29 saved_stdout
= sys
.stdout
30 sys
.stdout
= cStringIO
.StringIO()
33 exec compile(str,"<input>","single") in r
35 sys
.stdout
= saved_stdout
36 elif symbol
== 'eval':
38 d
= { 'value': eval(code
,ctx
) }
39 r
= { 'value': eval(str,ctx
) }
40 self
.assertEquals(unify_callables(r
),unify_callables(d
))
42 expected
= compile(str, "<input>", symbol
, PyCF_DONT_IMPLY_DEDENT
)
43 self
.assertEquals( compile_command(str, "<input>", symbol
), expected
)
45 def assertIncomplete(self
, str, symbol
='single'):
46 '''succeed iff str is the start of a valid piece of code'''
47 self
.assertEquals( compile_command(str, symbol
=symbol
), None)
49 def assertInvalid(self
, str, symbol
='single', is_syntax
=1):
50 '''succeed iff str is the start of an invalid piece of code'''
52 compile_command(str,symbol
=symbol
)
53 self
.fail("No exception thrown for invalid code")
55 self
.assertTrue(is_syntax
)
57 self
.assertTrue(not is_syntax
)
64 self
.assertEquals(compile_command(""),
65 compile("pass", "<input>", 'single',
66 PyCF_DONT_IMPLY_DEDENT
))
67 self
.assertEquals(compile_command("\n"),
68 compile("pass", "<input>", 'single',
69 PyCF_DONT_IMPLY_DEDENT
))
80 av("def x():\n pass\n")
83 av("\n\nif 1: pass\n")
84 av("\n\nif 1: pass\n\n")
86 av("def x():\n\n pass\n")
87 av("def x():\n pass\n \n")
88 av("def x():\n pass\n \n")
93 av("if 9==3:\n pass\nelse:\n pass\n")
94 av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
102 av("(lambda z: \n z**3)","eval")
104 av("9+ \\\n3","eval")
105 av("9+ \\\n3\n","eval")
107 av("\n\na**3","eval")
108 av("\n \na**3","eval")
109 av("#a\n#b\na**3","eval")
112 av("\n\nif 1: a=1\n\n")
114 av("if 1:\n pass\n if 1:\n pass\n else:\n pass\n")
115 av("#a\n\n \na=3\n\n")
117 av("\n\na**3","eval")
118 av("\n \na**3","eval")
119 av("#a\n#b\na**3","eval")
121 av("def f():\n try: pass\n finally: [x for x in (1,2)]\n")
122 av("def f():\n pass\n#foo\n")
123 av("@a.b.c\ndef f():\n pass\n")
125 def test_incomplete(self
):
126 ai
= self
.assertIncomplete
136 ai("if 9==3:\n pass\nelse:")
137 ai("if 9==3:\n pass\nelse:\n")
138 ai("if 9==3:\n pass\nelse:\n pass")
141 ai("if 1:\n pass\n if 1:\n pass\n else:")
142 ai("if 1:\n pass\n if 1:\n pass\n else:\n")
143 ai("if 1:\n pass\n if 1:\n pass\n else:\n pass")
149 ai("def x():\n pass")
150 ai("def x():\n pass\n ")
151 ai("def x():\n pass\n ")
152 ai("\n\ndef x():\n pass")
164 ai("lambda z: \\","eval")
166 ai("if True:\n if True:\n if True: \n")
174 ai("from a import (")
175 ai("from a import (b")
176 ai("from a import (b,")
177 ai("from a import (b,c")
178 ai("from a import (b,c,")
222 ai("if a:\n pass\nelif b:")
223 ai("if a:\n pass\nelif b:\n pass\nelse:")
226 ai("while a:\n pass\nelse:")
229 ai("for a in b:\n pass\nelse:")
232 ai("try:\n pass\nexcept:")
233 ai("try:\n pass\nfinally:")
234 ai("try:\n pass\nexcept:\n pass\nfinally:")
253 def test_invalid(self
):
254 ai
= self
.assertInvalid
264 ai("def x():\n\npass\n")
266 ai("\n\n if 1: pass\n\npass")
278 ai("lambda z:","eval")
282 ai("if (a == 1 and b = 2): pass")
290 ai("[i for i in range(10)] = (1, 2, 3)")
292 def test_filename(self
):
293 self
.assertEquals(compile_command("a = 1\n", "abc").co_filename
,
294 compile("a = 1\n", "abc", 'single').co_filename
)
295 self
.assertNotEquals(compile_command("a = 1\n", "abc").co_filename
,
296 compile("a = 1\n", "def", 'single').co_filename
)
300 run_unittest(CodeopTests
)
303 if __name__
== "__main__":