2001-07-30 09:30:08 -03:00
|
|
|
"""
|
|
|
|
Test cases for codeop.py
|
|
|
|
Nick Mathewson
|
|
|
|
"""
|
|
|
|
import unittest
|
2002-07-23 16:04:11 -03:00
|
|
|
from test.test_support import run_unittest
|
2001-07-30 09:30:08 -03:00
|
|
|
|
2003-02-13 18:07:59 -04:00
|
|
|
from codeop import compile_command, PyCF_DONT_IMPLY_DEDENT
|
2001-07-30 09:30:08 -03:00
|
|
|
|
|
|
|
class CodeopTests(unittest.TestCase):
|
|
|
|
|
|
|
|
def assertValid(self, str, symbol='single'):
|
|
|
|
'''succeed iff str is a valid piece of code'''
|
2003-02-13 18:07:59 -04:00
|
|
|
expected = compile(str, "<input>", symbol, PyCF_DONT_IMPLY_DEDENT)
|
2001-07-30 09:30:08 -03:00
|
|
|
self.assertEquals( compile_command(str, "<input>", symbol), expected)
|
2001-08-09 18:40:30 -03:00
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
|
|
|
|
def assertIncomplete(self, str, symbol='single'):
|
|
|
|
'''succeed iff str is the start of a valid piece of code'''
|
|
|
|
self.assertEquals( compile_command(str, symbol=symbol), None)
|
|
|
|
|
|
|
|
def assertInvalid(self, str, symbol='single', is_syntax=1):
|
|
|
|
'''succeed iff str is the start of an invalid piece of code'''
|
|
|
|
try:
|
|
|
|
compile_command(str,symbol=symbol)
|
|
|
|
self.fail("No exception thrown for invalid code")
|
|
|
|
except SyntaxError:
|
|
|
|
self.assert_(is_syntax)
|
|
|
|
except OverflowError:
|
|
|
|
self.assert_(not is_syntax)
|
|
|
|
|
|
|
|
def test_valid(self):
|
|
|
|
av = self.assertValid
|
|
|
|
av("a = 1\n")
|
|
|
|
av("def x():\n pass\n")
|
|
|
|
av("pass\n")
|
|
|
|
av("3**3\n")
|
|
|
|
av("if 9==3:\n pass\nelse:\n pass\n")
|
|
|
|
av("#a\n#b\na = 3\n")
|
|
|
|
av("#a\n\n \na=3\n")
|
|
|
|
av("a=3\n\n")
|
|
|
|
|
|
|
|
# special case
|
2001-08-09 18:40:30 -03:00
|
|
|
self.assertEquals(compile_command(""),
|
2003-02-13 18:07:59 -04:00
|
|
|
compile("pass", "<input>", 'single',
|
|
|
|
PyCF_DONT_IMPLY_DEDENT))
|
2001-07-30 09:30:08 -03:00
|
|
|
|
|
|
|
av("3**3","eval")
|
|
|
|
av("(lambda z: \n z**3)","eval")
|
|
|
|
av("#a\n#b\na**3","eval")
|
2001-08-09 18:40:30 -03:00
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
def test_incomplete(self):
|
|
|
|
ai = self.assertIncomplete
|
|
|
|
ai("(a **")
|
|
|
|
ai("def x():\n")
|
|
|
|
ai("(a,b,")
|
|
|
|
ai("(a,b,(")
|
|
|
|
ai("(a,b,(")
|
|
|
|
ai("if 9==3:\n pass\nelse:\n")
|
|
|
|
ai("if 9==3:\n pass\nelse:\n pass")
|
|
|
|
ai("a = (")
|
|
|
|
ai("a = 9+ \\")
|
2001-08-09 18:40:30 -03:00
|
|
|
|
2001-07-30 09:30:08 -03:00
|
|
|
ai("(","eval")
|
|
|
|
ai("(\n\n\n","eval")
|
|
|
|
ai("(9+","eval")
|
|
|
|
ai("9+ \\","eval")
|
|
|
|
ai("lambda z: \\","eval")
|
|
|
|
|
|
|
|
def test_invalid(self):
|
|
|
|
ai = self.assertInvalid
|
|
|
|
ai("a b")
|
|
|
|
ai("a = ")
|
|
|
|
ai("a = 9 +")
|
|
|
|
|
|
|
|
ai("a = 1","eval")
|
|
|
|
ai("a = (","eval")
|
|
|
|
ai("]","eval")
|
|
|
|
ai("())","eval")
|
|
|
|
ai("[}","eval")
|
|
|
|
ai("9+","eval")
|
|
|
|
ai("lambda z:","eval")
|
|
|
|
ai("a b","eval")
|
|
|
|
|
|
|
|
def test_filename(self):
|
|
|
|
self.assertEquals(compile_command("a = 1\n", "abc").co_filename,
|
|
|
|
compile("a = 1\n", "abc", 'single').co_filename)
|
|
|
|
self.assertNotEquals(compile_command("a = 1\n", "abc").co_filename,
|
|
|
|
compile("a = 1\n", "def", 'single').co_filename)
|
|
|
|
|
2001-09-20 18:33:42 -03:00
|
|
|
|
|
|
|
def test_main():
|
|
|
|
run_unittest(CodeopTests)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|