|
6 | 6 |
|
7 | 7 | class TestSpecifics(unittest.TestCase): |
8 | 8 |
|
| 9 | + def compile_single(self, source): |
| 10 | + compile(source, "<single>", "single") |
| 11 | + |
| 12 | + def assertInvalidSingle(self, source): |
| 13 | + self.assertRaises(SyntaxError, self.compile_single, source) |
| 14 | + |
9 | 15 | def test_no_ending_newline(self): |
10 | 16 | compile("hi", "<test>", "exec") |
11 | 17 | compile("hi\r", "<test>", "exec") |
@@ -442,6 +448,28 @@ def test_same_filename_used(self): |
442 | 448 | if isinstance(obj, types.CodeType): |
443 | 449 | self.assertIs(obj.co_filename, c.co_filename) |
444 | 450 |
|
| 451 | + def test_single_statement(self): |
| 452 | + self.compile_single("1 + 2") |
| 453 | + self.compile_single("\n1 + 2") |
| 454 | + self.compile_single("1 + 2\n") |
| 455 | + self.compile_single("1 + 2\n\n") |
| 456 | + self.compile_single("1 + 2\t\t\n") |
| 457 | + self.compile_single("1 + 2\t\t\n ") |
| 458 | + self.compile_single("1 + 2 # one plus two") |
| 459 | + self.compile_single("1; 2") |
| 460 | + self.compile_single("import sys; sys") |
| 461 | + self.compile_single("def f():\n pass") |
| 462 | + self.compile_single("while False:\n pass") |
| 463 | + self.compile_single("if x:\n f(x)") |
| 464 | + self.compile_single("if x:\n f(x)\nelse:\n g(x)") |
| 465 | + self.compile_single("class T:\n pass") |
| 466 | + |
| 467 | + def test_bad_single_statement(self): |
| 468 | + self.assertInvalidSingle('1\n2') |
| 469 | + self.assertInvalidSingle('def f(): pass') |
| 470 | + self.assertInvalidSingle('a = 13\nb = 187') |
| 471 | + self.assertInvalidSingle('del x\ndel y') |
| 472 | + self.assertInvalidSingle('f()\ng()') |
445 | 473 |
|
446 | 474 | def test_main(): |
447 | 475 | support.run_unittest(TestSpecifics) |
|
0 commit comments