Jeremy Hylton | 05ab2e6 | 2002-05-31 14:08:29 +0000 | [diff] [blame] | 1 | import re |
| 2 | import unittest |
| 3 | |
| 4 | import test_support |
| 5 | |
| 6 | class SyntaxTestCase(unittest.TestCase): |
| 7 | |
| 8 | def _check_error(self, code, errtext, |
| 9 | filename="<testcase>", mode="exec"): |
| 10 | """Check that compiling code raises SyntaxError with errtext. |
| 11 | |
| 12 | errtest is a regular expression that must be present in the |
| 13 | test of the exception raised. |
| 14 | """ |
| 15 | try: |
| 16 | compile(code, filename, mode) |
| 17 | except SyntaxError, err: |
| 18 | mo = re.search(errtext, str(err)) |
| 19 | if mo is None: |
| 20 | self.fail("SyntaxError did not contain '%s'" % `errtext`) |
| 21 | else: |
| 22 | self.fail("compile() did not raise SyntaxError") |
| 23 | |
| 24 | def test_assign_call(self): |
| 25 | self._check_error("f() = 1", "assign") |
| 26 | |
| 27 | def test_assign_del(self): |
| 28 | self._check_error("del f()", "delete") |
| 29 | |
| 30 | def test_main(): |
| 31 | test_support.run_unittest(SyntaxTestCase) |
| 32 | |
| 33 | if __name__ == "__main__": |
| 34 | test_main() |