mirror of
https://github.com/python/cpython.git
synced 2025-01-26 11:04:21 +08:00
Added tests for the new yield support in the parser module.
(Should be merged with descr branch.)
This commit is contained in:
parent
02126f20b6
commit
cf580c7ea1
@ -26,6 +26,18 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase):
|
||||
def check_suite(self, s):
|
||||
self.roundtrip(parser.suite, s)
|
||||
|
||||
def test_yield_statement(self):
|
||||
self.check_suite("from __future__ import generators\n"
|
||||
"def f(): yield 1")
|
||||
self.check_suite("from __future__ import generators\n"
|
||||
"def f(): return; yield 1")
|
||||
self.check_suite("from __future__ import generators\n"
|
||||
"def f(): yield 1; return")
|
||||
self.check_suite("from __future__ import generators\n"
|
||||
"def f():\n"
|
||||
" for x in range(30):\n"
|
||||
" yield x\n")
|
||||
|
||||
def test_expressions(self):
|
||||
self.check_expr("foo(1)")
|
||||
self.check_expr("[1, 2, 3]")
|
||||
@ -138,8 +150,127 @@ class IllegalSyntaxTestCase(unittest.TestCase):
|
||||
# not even remotely valid:
|
||||
self.check_bad_tree((1, 2, 3), "<junk>")
|
||||
|
||||
def test_illegal_yield_1(self):
|
||||
"""Illegal yield statement: def f(): return 1; yield 1"""
|
||||
tree = \
|
||||
(257,
|
||||
(264,
|
||||
(285,
|
||||
(259,
|
||||
(1, 'def'),
|
||||
(1, 'f'),
|
||||
(260, (7, '('), (8, ')')),
|
||||
(11, ':'),
|
||||
(291,
|
||||
(4, ''),
|
||||
(5, ''),
|
||||
(264,
|
||||
(265,
|
||||
(266,
|
||||
(272,
|
||||
(275,
|
||||
(1, 'return'),
|
||||
(313,
|
||||
(292,
|
||||
(293,
|
||||
(294,
|
||||
(295,
|
||||
(297,
|
||||
(298,
|
||||
(299,
|
||||
(300,
|
||||
(301,
|
||||
(302, (303, (304, (305, (2, '1')))))))))))))))))),
|
||||
(264,
|
||||
(265,
|
||||
(266,
|
||||
(272,
|
||||
(276,
|
||||
(1, 'yield'),
|
||||
(313,
|
||||
(292,
|
||||
(293,
|
||||
(294,
|
||||
(295,
|
||||
(297,
|
||||
(298,
|
||||
(299,
|
||||
(300,
|
||||
(301,
|
||||
(302,
|
||||
(303, (304, (305, (2, '1')))))))))))))))))),
|
||||
(4, ''))),
|
||||
(6, ''))))),
|
||||
(4, ''),
|
||||
(0, ''))))
|
||||
self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
|
||||
|
||||
def test_illegal_yield_2(self):
|
||||
"""Illegal return in generator: def f(): return 1; yield 1"""
|
||||
tree = \
|
||||
(257,
|
||||
(264,
|
||||
(265,
|
||||
(266,
|
||||
(278,
|
||||
(1, 'from'),
|
||||
(281, (1, '__future__')),
|
||||
(1, 'import'),
|
||||
(279, (1, 'generators')))),
|
||||
(4, ''))),
|
||||
(264,
|
||||
(285,
|
||||
(259,
|
||||
(1, 'def'),
|
||||
(1, 'f'),
|
||||
(260, (7, '('), (8, ')')),
|
||||
(11, ':'),
|
||||
(291,
|
||||
(4, ''),
|
||||
(5, ''),
|
||||
(264,
|
||||
(265,
|
||||
(266,
|
||||
(272,
|
||||
(275,
|
||||
(1, 'return'),
|
||||
(313,
|
||||
(292,
|
||||
(293,
|
||||
(294,
|
||||
(295,
|
||||
(297,
|
||||
(298,
|
||||
(299,
|
||||
(300,
|
||||
(301,
|
||||
(302, (303, (304, (305, (2, '1')))))))))))))))))),
|
||||
(264,
|
||||
(265,
|
||||
(266,
|
||||
(272,
|
||||
(276,
|
||||
(1, 'yield'),
|
||||
(313,
|
||||
(292,
|
||||
(293,
|
||||
(294,
|
||||
(295,
|
||||
(297,
|
||||
(298,
|
||||
(299,
|
||||
(300,
|
||||
(301,
|
||||
(302,
|
||||
(303, (304, (305, (2, '1')))))))))))))))))),
|
||||
(4, ''))),
|
||||
(6, ''))))),
|
||||
(4, ''),
|
||||
(0, ''))))
|
||||
self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
|
||||
|
||||
def test_print_chevron_comma(self):
|
||||
"Illegal input: print >>fp,"""
|
||||
"""Illegal input: print >>fp,"""
|
||||
tree = \
|
||||
(257,
|
||||
(264,
|
||||
|
Loading…
Reference in New Issue
Block a user