summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_codeop.py
diff options
context:
space:
mode:
authorMartin v. Löwis <martin@v.loewis.de>2001-07-30 12:30:08 (GMT)
committerMartin v. Löwis <martin@v.loewis.de>2001-07-30 12:30:08 (GMT)
commit3bd071e3f76e0c131e63107828ac53783938258c (patch)
treed54d3556d3069495e5cd502b64079d8f265283a4 /Lib/test/test_codeop.py
parent27c430e54e624fa1ad943077b9001c280f91761f (diff)
downloadcpython-3bd071e3f76e0c131e63107828ac53783938258c.zip
cpython-3bd071e3f76e0c131e63107828ac53783938258c.tar.gz
cpython-3bd071e3f76e0c131e63107828ac53783938258c.tar.bz2
Patch #442866: Tests for codeop.py.
Diffstat (limited to 'Lib/test/test_codeop.py')
-rw-r--r--Lib/test/test_codeop.py90
1 files changed, 90 insertions, 0 deletions
diff --git a/Lib/test/test_codeop.py b/Lib/test/test_codeop.py
new file mode 100644
index 0000000..be9ca7b
--- /dev/null
+++ b/Lib/test/test_codeop.py
@@ -0,0 +1,90 @@
+"""
+ Test cases for codeop.py
+ Nick Mathewson
+"""
+import unittest
+from test_support import run_unittest
+
+from codeop import compile_command
+
+class CodeopTests(unittest.TestCase):
+
+ def assertValid(self, str, symbol='single'):
+ '''succeed iff str is a valid piece of code'''
+ expected = compile(str, "<input>", symbol)
+ self.assertEquals( compile_command(str, "<input>", symbol), expected)
+
+
+ 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
+ self.assertEquals(compile_command(""),
+ compile("pass", "<input>", 'single'))
+
+ av("3**3","eval")
+ av("(lambda z: \n z**3)","eval")
+ av("#a\n#b\na**3","eval")
+
+ 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+ \\")
+
+ 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)
+
+run_unittest(CodeopTests)