home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_parser.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  11.2 KB  |  366 lines

  1. import parser
  2. import test_support
  3. import unittest
  4.  
  5. #
  6. #  First, we test that we can generate trees from valid source fragments,
  7. #  and that these valid trees are indeed allowed by the tree-loading side
  8. #  of the parser module.
  9. #
  10.  
  11. class RoundtripLegalSyntaxTestCase(unittest.TestCase):
  12.     def roundtrip(self, f, s):
  13.         st1 = f(s)
  14.         t = st1.totuple()
  15.         try:
  16.             st2 = parser.sequence2st(t)
  17.         except parser.ParserError:
  18.             self.fail("could not roundtrip %r" % s)
  19.  
  20.         self.assertEquals(t, st2.totuple(),
  21.                           "could not re-generate syntax tree")
  22.  
  23.     def check_expr(self, s):
  24.         self.roundtrip(parser.expr, s)
  25.  
  26.     def check_suite(self, s):
  27.         self.roundtrip(parser.suite, s)
  28.  
  29.     def test_yield_statement(self):
  30.         self.check_suite("from __future__ import generators\n"
  31.                          "def f(): yield 1")
  32.         self.check_suite("from __future__ import generators\n"
  33.                          "def f(): return; yield 1")
  34.         self.check_suite("from __future__ import generators\n"
  35.                          "def f(): yield 1; return")
  36.         self.check_suite("from __future__ import generators\n"
  37.                          "def f():\n"
  38.                          "    for x in range(30):\n"
  39.                          "        yield x\n")
  40.  
  41.     def test_expressions(self):
  42.         self.check_expr("foo(1)")
  43.         self.check_expr("[1, 2, 3]")
  44.         self.check_expr("[x**3 for x in range(20)]")
  45.         self.check_expr("[x**3 for x in range(20) if x % 3]")
  46.         self.check_expr("foo(*args)")
  47.         self.check_expr("foo(*args, **kw)")
  48.         self.check_expr("foo(**kw)")
  49.         self.check_expr("foo(key=value)")
  50.         self.check_expr("foo(key=value, *args)")
  51.         self.check_expr("foo(key=value, *args, **kw)")
  52.         self.check_expr("foo(key=value, **kw)")
  53.         self.check_expr("foo(a, b, c, *args)")
  54.         self.check_expr("foo(a, b, c, *args, **kw)")
  55.         self.check_expr("foo(a, b, c, **kw)")
  56.         self.check_expr("foo + bar")
  57.         self.check_expr("lambda: 0")
  58.         self.check_expr("lambda x: 0")
  59.         self.check_expr("lambda *y: 0")
  60.         self.check_expr("lambda *y, **z: 0")
  61.         self.check_expr("lambda **z: 0")
  62.         self.check_expr("lambda x, y: 0")
  63.         self.check_expr("lambda foo=bar: 0")
  64.         self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
  65.         self.check_expr("lambda foo=bar, **z: 0")
  66.         self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
  67.         self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
  68.         self.check_expr("lambda x, *y, **z: 0")
  69.  
  70.     def test_print(self):
  71.         self.check_suite("print")
  72.         self.check_suite("print 1")
  73.         self.check_suite("print 1,")
  74.         self.check_suite("print >>fp")
  75.         self.check_suite("print >>fp, 1")
  76.         self.check_suite("print >>fp, 1,")
  77.  
  78.     def test_simple_expression(self):
  79.         # expr_stmt
  80.         self.check_suite("a")
  81.  
  82.     def test_simple_assignments(self):
  83.         self.check_suite("a = b")
  84.         self.check_suite("a = b = c = d = e")
  85.  
  86.     def test_simple_augmented_assignments(self):
  87.         self.check_suite("a += b")
  88.         self.check_suite("a -= b")
  89.         self.check_suite("a *= b")
  90.         self.check_suite("a /= b")
  91.         self.check_suite("a %= b")
  92.         self.check_suite("a &= b")
  93.         self.check_suite("a |= b")
  94.         self.check_suite("a ^= b")
  95.         self.check_suite("a <<= b")
  96.         self.check_suite("a >>= b")
  97.         self.check_suite("a **= b")
  98.  
  99.     def test_function_defs(self):
  100.         self.check_suite("def f(): pass")
  101.         self.check_suite("def f(*args): pass")
  102.         self.check_suite("def f(*args, **kw): pass")
  103.         self.check_suite("def f(**kw): pass")
  104.         self.check_suite("def f(foo=bar): pass")
  105.         self.check_suite("def f(foo=bar, *args): pass")
  106.         self.check_suite("def f(foo=bar, *args, **kw): pass")
  107.         self.check_suite("def f(foo=bar, **kw): pass")
  108.  
  109.         self.check_suite("def f(a, b): pass")
  110.         self.check_suite("def f(a, b, *args): pass")
  111.         self.check_suite("def f(a, b, *args, **kw): pass")
  112.         self.check_suite("def f(a, b, **kw): pass")
  113.         self.check_suite("def f(a, b, foo=bar): pass")
  114.         self.check_suite("def f(a, b, foo=bar, *args): pass")
  115.         self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
  116.         self.check_suite("def f(a, b, foo=bar, **kw): pass")
  117.  
  118.     def test_import_from_statement(self):
  119.         self.check_suite("from sys.path import *")
  120.         self.check_suite("from sys.path import dirname")
  121.         self.check_suite("from sys.path import dirname as my_dirname")
  122.         self.check_suite("from sys.path import dirname, basename")
  123.         self.check_suite(
  124.             "from sys.path import dirname as my_dirname, basename")
  125.         self.check_suite(
  126.             "from sys.path import dirname, basename as my_basename")
  127.  
  128.     def test_basic_import_statement(self):
  129.         self.check_suite("import sys")
  130.         self.check_suite("import sys as system")
  131.         self.check_suite("import sys, math")
  132.         self.check_suite("import sys as system, math")
  133.         self.check_suite("import sys, math as my_math")
  134.  
  135. #
  136. #  Second, we take *invalid* trees and make sure we get ParserError
  137. #  rejections for them.
  138. #
  139.  
  140. class IllegalSyntaxTestCase(unittest.TestCase):
  141.     def check_bad_tree(self, tree, label):
  142.         try:
  143.             parser.sequence2st(tree)
  144.         except parser.ParserError:
  145.             pass
  146.         else:
  147.             self.fail("did not detect invalid tree for %r" % label)
  148.  
  149.     def test_junk(self):
  150.         # not even remotely valid:
  151.         self.check_bad_tree((1, 2, 3), "<junk>")
  152.  
  153.     def test_illegal_yield_1(self):
  154.         """Illegal yield statement: def f(): return 1; yield 1"""
  155.         tree = \
  156.         (257,
  157.          (264,
  158.           (285,
  159.            (259,
  160.             (1, 'def'),
  161.             (1, 'f'),
  162.             (260, (7, '('), (8, ')')),
  163.             (11, ':'),
  164.             (291,
  165.              (4, ''),
  166.              (5, ''),
  167.              (264,
  168.               (265,
  169.                (266,
  170.                 (272,
  171.                  (275,
  172.                   (1, 'return'),
  173.                   (313,
  174.                    (292,
  175.                     (293,
  176.                      (294,
  177.                       (295,
  178.                        (297,
  179.                         (298,
  180.                          (299,
  181.                           (300,
  182.                            (301,
  183.                             (302, (303, (304, (305, (2, '1')))))))))))))))))),
  184.                (264,
  185.                 (265,
  186.                  (266,
  187.                   (272,
  188.                    (276,
  189.                     (1, 'yield'),
  190.                     (313,
  191.                      (292,
  192.                       (293,
  193.                        (294,
  194.                         (295,
  195.                          (297,
  196.                           (298,
  197.                            (299,
  198.                             (300,
  199.                              (301,
  200.                               (302,
  201.                                (303, (304, (305, (2, '1')))))))))))))))))),
  202.                  (4, ''))),
  203.                (6, ''))))),
  204.            (4, ''),
  205.            (0, ''))))
  206.         self.check_bad_tree(tree, "def f():\n  return 1\n  yield 1")
  207.  
  208.     def test_illegal_yield_2(self):
  209.         """Illegal return in generator: def f(): return 1; yield 1"""
  210.         tree = \
  211.         (257,
  212.          (264,
  213.           (265,
  214.            (266,
  215.             (278,
  216.              (1, 'from'),
  217.              (281, (1, '__future__')),
  218.              (1, 'import'),
  219.              (279, (1, 'generators')))),
  220.            (4, ''))),
  221.          (264,
  222.           (285,
  223.            (259,
  224.             (1, 'def'),
  225.             (1, 'f'),
  226.             (260, (7, '('), (8, ')')),
  227.             (11, ':'),
  228.             (291,
  229.              (4, ''),
  230.              (5, ''),
  231.              (264,
  232.               (265,
  233.                (266,
  234.                 (272,
  235.                  (275,
  236.                   (1, 'return'),
  237.                   (313,
  238.                    (292,
  239.                     (293,
  240.                      (294,
  241.                       (295,
  242.                        (297,
  243.                         (298,
  244.                          (299,
  245.                           (300,
  246.                            (301,
  247.                             (302, (303, (304, (305, (2, '1')))))))))))))))))),
  248.                (264,
  249.                 (265,
  250.                  (266,
  251.                   (272,
  252.                    (276,
  253.                     (1, 'yield'),
  254.                     (313,
  255.                      (292,
  256.                       (293,
  257.                        (294,
  258.                         (295,
  259.                          (297,
  260.                           (298,
  261.                            (299,
  262.                             (300,
  263.                              (301,
  264.                               (302,
  265.                                (303, (304, (305, (2, '1')))))))))))))))))),
  266.                  (4, ''))),
  267.                (6, ''))))),
  268.            (4, ''),
  269.            (0, ''))))
  270.         self.check_bad_tree(tree, "def f():\n  return 1\n  yield 1")
  271.  
  272.     def test_print_chevron_comma(self):
  273.         """Illegal input: print >>fp,"""
  274.         tree = \
  275.         (257,
  276.          (264,
  277.           (265,
  278.            (266,
  279.             (268,
  280.              (1, 'print'),
  281.              (35, '>>'),
  282.              (290,
  283.               (291,
  284.                (292,
  285.                 (293,
  286.                  (295,
  287.                   (296,
  288.                    (297,
  289.                     (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
  290.              (12, ','))),
  291.            (4, ''))),
  292.          (0, ''))
  293.         self.check_bad_tree(tree, "print >>fp,")
  294.  
  295.     def test_a_comma_comma_c(self):
  296.         """Illegal input: a,,c"""
  297.         tree = \
  298.         (258,
  299.          (311,
  300.           (290,
  301.            (291,
  302.             (292,
  303.              (293,
  304.               (295,
  305.                (296,
  306.                 (297,
  307.                  (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
  308.           (12, ','),
  309.           (12, ','),
  310.           (290,
  311.            (291,
  312.             (292,
  313.              (293,
  314.               (295,
  315.                (296,
  316.                 (297,
  317.                  (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
  318.          (4, ''),
  319.          (0, ''))
  320.         self.check_bad_tree(tree, "a,,c")
  321.  
  322.     def test_illegal_operator(self):
  323.         """Illegal input: a $= b"""
  324.         tree = \
  325.         (257,
  326.          (264,
  327.           (265,
  328.            (266,
  329.             (267,
  330.              (312,
  331.               (291,
  332.                (292,
  333.                 (293,
  334.                  (294,
  335.                   (296,
  336.                    (297,
  337.                     (298,
  338.                      (299,
  339.                       (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
  340.              (268, (37, '$=')),
  341.              (312,
  342.               (291,
  343.                (292,
  344.                 (293,
  345.                  (294,
  346.                   (296,
  347.                    (297,
  348.                     (298,
  349.                      (299,
  350.                       (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
  351.            (4, ''))),
  352.          (0, ''))
  353.         self.check_bad_tree(tree, "a $= b")
  354.  
  355.  
  356. def test_main():
  357.     loader = unittest.TestLoader()
  358.     suite = unittest.TestSuite()
  359.     suite.addTest(loader.loadTestsFromTestCase(RoundtripLegalSyntaxTestCase))
  360.     suite.addTest(loader.loadTestsFromTestCase(IllegalSyntaxTestCase))
  361.     test_support.run_suite(suite)
  362.  
  363.  
  364. if __name__ == "__main__":
  365.     test_main()
  366.