Back to index

python3.2  3.2.2
Public Member Functions
test.test_parser.RoundtripLegalSyntaxTestCase Class Reference

List of all members.

Public Member Functions

def roundtrip
def check_expr
def test_flags_passed
def check_suite
def test_yield_statement
def test_expressions
def test_simple_expression
def test_simple_assignments
def test_simple_augmented_assignments
def test_function_defs
def test_class_defs
def test_import_from_statement
def test_basic_import_statement
def test_relative_imports
def test_pep263
def test_assert
def test_with
def test_try_stmt
def test_position
def test_extended_unpacking

Detailed Description

Definition at line 13 of file test_parser.py.


Member Function Documentation

Definition at line 26 of file test_parser.py.

00026 
00027     def check_expr(self, s):
00028         self.roundtrip(parser.expr, s)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 38 of file test_parser.py.

00038 
00039     def check_suite(self, s):
00040         self.roundtrip(parser.suite, s)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 15 of file test_parser.py.

00015 
00016     def roundtrip(self, f, s):
00017         st1 = f(s)
00018         t = st1.totuple()
00019         try:
00020             st2 = parser.sequence2st(t)
00021         except parser.ParserError as why:
00022             self.fail("could not roundtrip %r: %s" % (s, why))
00023 
00024         self.assertEqual(t, st2.totuple(),
00025                          "could not re-generate syntax tree")

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 208 of file test_parser.py.

00208 
00209     def test_assert(self):
00210         self.check_suite("assert alo < ahi and blo < bhi\n")

Here is the call graph for this function:

Definition at line 185 of file test_parser.py.

00185 
00186     def test_basic_import_statement(self):
00187         self.check_suite("import sys")
00188         self.check_suite("import sys as system")
00189         self.check_suite("import sys, math")
00190         self.check_suite("import sys as system, math")
00191         self.check_suite("import sys, math as my_math")

Here is the call graph for this function:

Definition at line 149 of file test_parser.py.

00149 
00150     def test_class_defs(self):
00151         self.check_suite("class foo():pass")
00152         self.check_suite("class foo(object):pass")
00153         self.check_suite("@class_decorator\n"
00154                          "class foo():pass")
00155         self.check_suite("@class_decorator(arg)\n"
00156                          "class foo():pass")
00157         self.check_suite("@decorator1\n"
00158                          "@decorator2\n"
00159                          "class foo():pass")

Here is the call graph for this function:

Definition at line 60 of file test_parser.py.

00060 
00061     def test_expressions(self):
00062         self.check_expr("foo(1)")
00063         self.check_expr("[1, 2, 3]")
00064         self.check_expr("[x**3 for x in range(20)]")
00065         self.check_expr("[x**3 for x in range(20) if x % 3]")
00066         self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
00067         self.check_expr("list(x**3 for x in range(20))")
00068         self.check_expr("list(x**3 for x in range(20) if x % 3)")
00069         self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
00070         self.check_expr("foo(*args)")
00071         self.check_expr("foo(*args, **kw)")
00072         self.check_expr("foo(**kw)")
00073         self.check_expr("foo(key=value)")
00074         self.check_expr("foo(key=value, *args)")
00075         self.check_expr("foo(key=value, *args, **kw)")
00076         self.check_expr("foo(key=value, **kw)")
00077         self.check_expr("foo(a, b, c, *args)")
00078         self.check_expr("foo(a, b, c, *args, **kw)")
00079         self.check_expr("foo(a, b, c, **kw)")
00080         self.check_expr("foo(a, *args, keyword=23)")
00081         self.check_expr("foo + bar")
00082         self.check_expr("foo - bar")
00083         self.check_expr("foo * bar")
00084         self.check_expr("foo / bar")
00085         self.check_expr("foo // bar")
00086         self.check_expr("lambda: 0")
00087         self.check_expr("lambda x: 0")
00088         self.check_expr("lambda *y: 0")
00089         self.check_expr("lambda *y, **z: 0")
00090         self.check_expr("lambda **z: 0")
00091         self.check_expr("lambda x, y: 0")
00092         self.check_expr("lambda foo=bar: 0")
00093         self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
00094         self.check_expr("lambda foo=bar, **z: 0")
00095         self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
00096         self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
00097         self.check_expr("lambda x, *y, **z: 0")
00098         self.check_expr("(x for x in range(10))")
00099         self.check_expr("foo(x for x in range(10))")

Here is the call graph for this function:

Definition at line 263 of file test_parser.py.

00263 
00264     def test_extended_unpacking(self):
00265         self.check_suite("*a = y")
00266         self.check_suite("x, *b, = m")
00267         self.check_suite("[*a, *b] = y")
00268         self.check_suite("for [*x, b] in x: pass")
00269 
00270 
00271 #
00272 #  Second, we take *invalid* trees and make sure we get ParserError
00273 #  rejections for them.
00274 #

Here is the call graph for this function:

Definition at line 29 of file test_parser.py.

00029 
00030     def test_flags_passed(self):
00031         # The unicode literals flags has to be passed from the paser to AST
00032         # generation.
00033         suite = parser.suite("from __future__ import unicode_literals; x = ''")
00034         code = suite.compile()
00035         scope = {}
00036         exec(code, {}, scope)
00037         self.assertIsInstance(scope["x"], str)

Here is the call graph for this function:

Definition at line 122 of file test_parser.py.

00122 
00123     def test_function_defs(self):
00124         self.check_suite("def f(): pass")
00125         self.check_suite("def f(*args): pass")
00126         self.check_suite("def f(*args, **kw): pass")
00127         self.check_suite("def f(**kw): pass")
00128         self.check_suite("def f(foo=bar): pass")
00129         self.check_suite("def f(foo=bar, *args): pass")
00130         self.check_suite("def f(foo=bar, *args, **kw): pass")
00131         self.check_suite("def f(foo=bar, **kw): pass")
00132 
00133         self.check_suite("def f(a, b): pass")
00134         self.check_suite("def f(a, b, *args): pass")
00135         self.check_suite("def f(a, b, *args, **kw): pass")
00136         self.check_suite("def f(a, b, **kw): pass")
00137         self.check_suite("def f(a, b, foo=bar): pass")
00138         self.check_suite("def f(a, b, foo=bar, *args): pass")
00139         self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
00140         self.check_suite("def f(a, b, foo=bar, **kw): pass")
00141 
00142         self.check_suite("@staticmethod\n"
00143                          "def f(): pass")
00144         self.check_suite("@staticmethod\n"
00145                          "@funcattrs(x, y)\n"
00146                          "def f(): pass")
00147         self.check_suite("@funcattrs()\n"
00148                          "def f(): pass")

Here is the call graph for this function:

Definition at line 160 of file test_parser.py.

00160 
00161     def test_import_from_statement(self):
00162         self.check_suite("from sys.path import *")
00163         self.check_suite("from sys.path import dirname")
00164         self.check_suite("from sys.path import (dirname)")
00165         self.check_suite("from sys.path import (dirname,)")
00166         self.check_suite("from sys.path import dirname as my_dirname")
00167         self.check_suite("from sys.path import (dirname as my_dirname)")
00168         self.check_suite("from sys.path import (dirname as my_dirname,)")
00169         self.check_suite("from sys.path import dirname, basename")
00170         self.check_suite("from sys.path import (dirname, basename)")
00171         self.check_suite("from sys.path import (dirname, basename,)")
00172         self.check_suite(
00173             "from sys.path import dirname as my_dirname, basename")
00174         self.check_suite(
00175             "from sys.path import (dirname as my_dirname, basename)")
00176         self.check_suite(
00177             "from sys.path import (dirname as my_dirname, basename,)")
00178         self.check_suite(
00179             "from sys.path import dirname, basename as my_basename")
00180         self.check_suite(
00181             "from sys.path import (dirname, basename as my_basename)")
00182         self.check_suite(
00183             "from sys.path import (dirname, basename as my_basename,)")
00184         self.check_suite("from .bogus import x")

Here is the call graph for this function:

Definition at line 204 of file test_parser.py.

00204 
00205     def test_pep263(self):
00206         self.check_suite("# -*- coding: iso-8859-1 -*-\n"
00207                          "pass\n")

Here is the call graph for this function:

Definition at line 226 of file test_parser.py.

00226 
00227     def test_position(self):
00228         # An absolutely minimal test of position information.  Better
00229         # tests would be a big project.
00230         code = "def f(x):\n    return x + 1"
00231         st1 = parser.suite(code)
00232         st2 = st1.totuple(line_info=1, col_info=1)
00233 
00234         def walk(tree):
00235             node_type = tree[0]
00236             next = tree[1]
00237             if isinstance(next, tuple):
00238                 for elt in tree[1:]:
00239                     for x in walk(elt):
00240                         yield x
00241             else:
00242                 yield tree
00243 
00244         terminals = list(walk(st2))
00245         self.assertEqual([
00246             (1, 'def', 1, 0),
00247             (1, 'f', 1, 4),
00248             (7, '(', 1, 5),
00249             (1, 'x', 1, 6),
00250             (8, ')', 1, 7),
00251             (11, ':', 1, 8),
00252             (4, '', 1, 9),
00253             (5, '', 2, -1),
00254             (1, 'return', 2, 4),
00255             (1, 'x', 2, 11),
00256             (14, '+', 2, 13),
00257             (2, '1', 2, 15),
00258             (4, '', 2, 16),
00259             (6, '', 2, -1),
00260             (4, '', 2, -1),
00261             (0, '', 2, -1)],
00262                          terminals)

Here is the call graph for this function:

Definition at line 192 of file test_parser.py.

00192 
00193     def test_relative_imports(self):
00194         self.check_suite("from . import name")
00195         self.check_suite("from .. import name")
00196         # check all the way up to '....', since '...' is tokenized
00197         # differently from '.' (it's an ellipsis token).
00198         self.check_suite("from ... import name")
00199         self.check_suite("from .... import name")
00200         self.check_suite("from .pkg import name")
00201         self.check_suite("from ..pkg import name")
00202         self.check_suite("from ...pkg import name")
00203         self.check_suite("from ....pkg import name")

Here is the call graph for this function:

Definition at line 104 of file test_parser.py.

00104 
00105     def test_simple_assignments(self):
00106         self.check_suite("a = b")
00107         self.check_suite("a = b = c = d = e")

Here is the call graph for this function:

Definition at line 108 of file test_parser.py.

00108 
00109     def test_simple_augmented_assignments(self):
00110         self.check_suite("a += b")
00111         self.check_suite("a -= b")
00112         self.check_suite("a *= b")
00113         self.check_suite("a /= b")
00114         self.check_suite("a //= b")
00115         self.check_suite("a %= b")
00116         self.check_suite("a &= b")
00117         self.check_suite("a |= b")
00118         self.check_suite("a ^= b")
00119         self.check_suite("a <<= b")
00120         self.check_suite("a >>= b")
00121         self.check_suite("a **= b")

Here is the call graph for this function:

Definition at line 100 of file test_parser.py.

00100 
00101     def test_simple_expression(self):
00102         # expr_stmt
00103         self.check_suite("a")

Here is the call graph for this function:

Definition at line 216 of file test_parser.py.

00216 
00217     def test_try_stmt(self):
00218         self.check_suite("try: pass\nexcept: pass\n")
00219         self.check_suite("try: pass\nfinally: pass\n")
00220         self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
00221         self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
00222                          "finally: pass\n")
00223         self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
00224         self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
00225                          "finally: pass\n")

Here is the call graph for this function:

Definition at line 211 of file test_parser.py.

00211 
00212     def test_with(self):
00213         self.check_suite("with open('x'): pass\n")
00214         self.check_suite("with open('x') as f: pass\n")
00215         self.check_suite("with open('x') as f, open('y') as g: pass\n")

Here is the call graph for this function:

Definition at line 41 of file test_parser.py.

00041 
00042     def test_yield_statement(self):
00043         self.check_suite("def f(): yield 1")
00044         self.check_suite("def f(): yield")
00045         self.check_suite("def f(): x += yield")
00046         self.check_suite("def f(): x = yield 1")
00047         self.check_suite("def f(): x = y = yield 1")
00048         self.check_suite("def f(): x = yield")
00049         self.check_suite("def f(): x = y = yield")
00050         self.check_suite("def f(): 1 + (yield)*2")
00051         self.check_suite("def f(): (yield 1)*2")
00052         self.check_suite("def f(): return; yield 1")
00053         self.check_suite("def f(): yield 1; return")
00054         self.check_suite("def f():\n"
00055                          "    for x in range(30):\n"
00056                          "        yield x\n")
00057         self.check_suite("def f():\n"
00058                          "    if (yield):\n"
00059                          "        yield x\n")

Here is the call graph for this function:


The documentation for this class was generated from the following file: