Kaydet (Commit) cf580c7e authored tarafından Fred Drake's avatar Fred Drake

Added tests for the new yield support in the parser module.

(Should be merged with descr branch.)
üst 02126f20
...@@ -26,6 +26,18 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase): ...@@ -26,6 +26,18 @@ class RoundtripLegalSyntaxTestCase(unittest.TestCase):
def check_suite(self, s): def check_suite(self, s):
self.roundtrip(parser.suite, 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): def test_expressions(self):
self.check_expr("foo(1)") self.check_expr("foo(1)")
self.check_expr("[1, 2, 3]") self.check_expr("[1, 2, 3]")
...@@ -138,8 +150,127 @@ class IllegalSyntaxTestCase(unittest.TestCase): ...@@ -138,8 +150,127 @@ class IllegalSyntaxTestCase(unittest.TestCase):
# not even remotely valid: # not even remotely valid:
self.check_bad_tree((1, 2, 3), "<junk>") 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): def test_print_chevron_comma(self):
"Illegal input: print >>fp,""" """Illegal input: print >>fp,"""
tree = \ tree = \
(257, (257,
(264, (264,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment