2 Test cases for codeop.py
6 from test
.test_support
import run_unittest
8 from codeop
import compile_command
, PyCF_DONT_IMPLY_DEDENT
10 class CodeopTests(unittest
.TestCase
):
12 def assertValid(self
, str, symbol
='single'):
13 '''succeed iff str is a valid piece of code'''
14 expected
= compile(str, "<input>", symbol
, PyCF_DONT_IMPLY_DEDENT
)
15 self
.assertEquals( compile_command(str, "<input>", symbol
), expected
)
18 def assertIncomplete(self
, str, symbol
='single'):
19 '''succeed iff str is the start of a valid piece of code'''
20 self
.assertEquals( compile_command(str, symbol
=symbol
), None)
22 def assertInvalid(self
, str, symbol
='single', is_syntax
=1):
23 '''succeed iff str is the start of an invalid piece of code'''
25 compile_command(str,symbol
=symbol
)
26 self
.fail("No exception thrown for invalid code")
28 self
.assert_(is_syntax
)
30 self
.assert_(not is_syntax
)
35 av("def x():\n pass\n")
38 av("if 9==3:\n pass\nelse:\n pass\n")
44 self
.assertEquals(compile_command(""),
45 compile("pass", "<input>", 'single',
46 PyCF_DONT_IMPLY_DEDENT
))
49 av("(lambda z: \n z**3)","eval")
50 av("#a\n#b\na**3","eval")
52 def test_incomplete(self
):
53 ai
= self
.assertIncomplete
59 ai("if 9==3:\n pass\nelse:\n")
60 ai("if 9==3:\n pass\nelse:\n pass")
68 ai("lambda z: \\","eval")
70 def test_invalid(self
):
71 ai
= self
.assertInvalid
82 ai("lambda z:","eval")
85 def test_filename(self
):
86 self
.assertEquals(compile_command("a = 1\n", "abc").co_filename
,
87 compile("a = 1\n", "abc", 'single').co_filename
)
88 self
.assertNotEquals(compile_command("a = 1\n", "abc").co_filename
,
89 compile("a = 1\n", "def", 'single').co_filename
)
93 run_unittest(CodeopTests
)
96 if __name__
== "__main__":