Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions
test.test_ast.AST_Tests Class Reference

List of all members.

Public Member Functions

def test_AST_objects
def test_snippets
def test_slice
def test_from_import
def test_base_classes
def test_field_attr_existence
def test_arguments
def test_field_attr_writable
def test_classattrs
def test_module
def test_nodeclasses
def test_no_fields
def test_pickling
def test_invalid_sum
def test_invalid_identitifer
def test_invalid_string

Private Member Functions

def _assertTrueorder

Detailed Description

Definition at line 174 of file test_ast.py.


Member Function Documentation

def test.test_ast.AST_Tests._assertTrueorder (   self,
  ast_node,
  parent_pos 
) [private]

Definition at line 176 of file test_ast.py.

00176 
00177     def _assertTrueorder(self, ast_node, parent_pos):
00178         if not isinstance(ast_node, ast.AST) or ast_node._fields is None:
00179             return
00180         if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)):
00181             node_pos = (ast_node.lineno, ast_node.col_offset)
00182             self.assertTrue(node_pos >= parent_pos)
00183             parent_pos = (ast_node.lineno, ast_node.col_offset)
00184         for name in ast_node._fields:
00185             value = getattr(ast_node, name)
00186             if isinstance(value, list):
00187                 for child in value:
00188                     self._assertTrueorder(child, parent_pos)
00189             elif value is not None:
00190                 self._assertTrueorder(value, parent_pos)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 242 of file test_ast.py.

00242 
00243     def test_arguments(self):
00244         x = ast.arguments()
00245         self.assertEqual(x._fields, ('args', 'vararg', 'varargannotation',
00246                                       'kwonlyargs', 'kwarg', 'kwargannotation',
00247                                       'defaults', 'kw_defaults'))
00248 
00249         with self.assertRaises(AttributeError):
00250             x.vararg
00251 
00252         x = ast.arguments(*range(1, 9))
00253         self.assertEqual(x.vararg, 2)

Here is the call graph for this function:

Definition at line 191 of file test_ast.py.

00191 
00192     def test_AST_objects(self):
00193         x = ast.AST()
00194         self.assertEqual(x._fields, ())
00195 
00196         with self.assertRaises(AttributeError):
00197             x.vararg
00198 
00199         with self.assertRaises(AttributeError):
00200             x.foobar = 21
00201 
00202         with self.assertRaises(AttributeError):
00203             ast.AST(lineno=2)
00204 
00205         with self.assertRaises(TypeError):
00206             # "_ast.AST constructor takes 0 positional arguments"
00207             ast.AST(2)

Here is the call graph for this function:

Definition at line 227 of file test_ast.py.

00227 
00228     def test_base_classes(self):
00229         self.assertTrue(issubclass(ast.For, ast.stmt))
00230         self.assertTrue(issubclass(ast.Name, ast.expr))
00231         self.assertTrue(issubclass(ast.stmt, ast.AST))
00232         self.assertTrue(issubclass(ast.expr, ast.AST))
00233         self.assertTrue(issubclass(ast.comprehension, ast.AST))
00234         self.assertTrue(issubclass(ast.Gt, ast.AST))

Here is the call graph for this function:

Definition at line 260 of file test_ast.py.

00260 
00261     def test_classattrs(self):
00262         x = ast.Num()
00263         self.assertEqual(x._fields, ('n',))
00264 
00265         with self.assertRaises(AttributeError):
00266             x.n
00267 
00268         x = ast.Num(42)
00269         self.assertEqual(x.n, 42)
00270 
00271         with self.assertRaises(AttributeError):
00272             x.lineno
00273 
00274         with self.assertRaises(AttributeError):
00275             x.foobar
00276 
00277         x = ast.Num(lineno=2)
00278         self.assertEqual(x.lineno, 2)
00279 
00280         x = ast.Num(42, lineno=0)
00281         self.assertEqual(x.lineno, 0)
00282         self.assertEqual(x._fields, ('n',))
00283         self.assertEqual(x.n, 42)
00284 
00285         self.assertRaises(TypeError, ast.Num, 1, 2)
00286         self.assertRaises(TypeError, ast.Num, 1, 2, lineno=0)

Here is the call graph for this function:

Definition at line 235 of file test_ast.py.

00235 
00236     def test_field_attr_existence(self):
00237         for name, item in ast.__dict__.items():
00238             if isinstance(item, type) and name != 'AST' and name[0].isupper():
00239                 x = item()
00240                 if isinstance(x, ast.AST):
00241                     self.assertEqual(type(x._fields), tuple)

Here is the call graph for this function:

Definition at line 254 of file test_ast.py.

00254 
00255     def test_field_attr_writable(self):
00256         x = ast.Num()
00257         # We can assign to _fields
00258         x._fields = 666
00259         self.assertEqual(x._fields, 666)

Here is the call graph for this function:

Definition at line 223 of file test_ast.py.

00223 
00224     def test_from_import(self):
00225         im = ast.parse("from . import y").body[0]
00226         self.assertIsNone(im.module)

Here is the call graph for this function:

Definition at line 367 of file test_ast.py.

00367 
00368     def test_invalid_identitifer(self):
00369         m = ast.Module([ast.Expr(ast.Name(42, ast.Load()))])
00370         ast.fix_missing_locations(m)
00371         with self.assertRaises(TypeError) as cm:
00372             compile(m, "<test>", "exec")
00373         self.assertIn("identifier must be of type str", str(cm.exception))

Here is the call graph for this function:

Definition at line 374 of file test_ast.py.

00374 
00375     def test_invalid_string(self):
00376         m = ast.Module([ast.Expr(ast.Str(42))])
00377         ast.fix_missing_locations(m)
00378         with self.assertRaises(TypeError) as cm:
00379             compile(m, "<test>", "exec")
00380         self.assertIn("string must be of type str", str(cm.exception))
00381 

Here is the call graph for this function:

Definition at line 360 of file test_ast.py.

00360 
00361     def test_invalid_sum(self):
00362         pos = dict(lineno=2, col_offset=3)
00363         m = ast.Module([ast.Expr(ast.expr(**pos), **pos)])
00364         with self.assertRaises(TypeError) as cm:
00365             compile(m, "<test>", "exec")
00366         self.assertIn("but got <_ast.expr", str(cm.exception))

Here is the call graph for this function:

Definition at line 287 of file test_ast.py.

00287 
00288     def test_module(self):
00289         body = [ast.Num(42)]
00290         x = ast.Module(body)
00291         self.assertEqual(x.body, body)

Here is the call graph for this function:

Definition at line 340 of file test_ast.py.

00340 
00341     def test_no_fields(self):
00342         # this used to fail because Sub._fields was None
00343         x = ast.Sub()
00344         self.assertEqual(x._fields, ())

Here is the call graph for this function:

Definition at line 292 of file test_ast.py.

00292 
00293     def test_nodeclasses(self):
00294         # Zero arguments constructor explicitely allowed
00295         x = ast.BinOp()
00296         self.assertEqual(x._fields, ('left', 'op', 'right'))
00297 
00298         # Random attribute allowed too
00299         x.foobarbaz = 5
00300         self.assertEqual(x.foobarbaz, 5)
00301 
00302         n1 = ast.Num(1)
00303         n3 = ast.Num(3)
00304         addop = ast.Add()
00305         x = ast.BinOp(n1, addop, n3)
00306         self.assertEqual(x.left, n1)
00307         self.assertEqual(x.op, addop)
00308         self.assertEqual(x.right, n3)
00309 
00310         x = ast.BinOp(1, 2, 3)
00311         self.assertEqual(x.left, 1)
00312         self.assertEqual(x.op, 2)
00313         self.assertEqual(x.right, 3)
00314 
00315         x = ast.BinOp(1, 2, 3, lineno=0)
00316         self.assertEqual(x.left, 1)
00317         self.assertEqual(x.op, 2)
00318         self.assertEqual(x.right, 3)
00319         self.assertEqual(x.lineno, 0)
00320 
00321         # node raises exception when not given enough arguments
00322         self.assertRaises(TypeError, ast.BinOp, 1, 2)
00323         # node raises exception when given too many arguments
00324         self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4)
00325         # node raises exception when not given enough arguments
00326         self.assertRaises(TypeError, ast.BinOp, 1, 2, lineno=0)
00327         # node raises exception when given too many arguments
00328         self.assertRaises(TypeError, ast.BinOp, 1, 2, 3, 4, lineno=0)
00329 
00330         # can set attributes through kwargs too
00331         x = ast.BinOp(left=1, op=2, right=3, lineno=0)
00332         self.assertEqual(x.left, 1)
00333         self.assertEqual(x.op, 2)
00334         self.assertEqual(x.right, 3)
00335         self.assertEqual(x.lineno, 0)
00336 
00337         # Random kwargs also allowed
00338         x = ast.BinOp(1, 2, 3, foobarbaz=42)
00339         self.assertEqual(x.foobarbaz, 42)

Here is the call graph for this function:

Definition at line 345 of file test_ast.py.

00345 
00346     def test_pickling(self):
00347         import pickle
00348         mods = [pickle]
00349         try:
00350             import cPickle
00351             mods.append(cPickle)
00352         except ImportError:
00353             pass
00354         protocols = [0, 1, 2]
00355         for mod in mods:
00356             for protocol in protocols:
00357                 for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
00358                     ast2 = mod.loads(mod.dumps(ast, protocol))
00359                     self.assertEqual(to_tuple(ast2), to_tuple(ast))

Here is the call graph for this function:

Definition at line 217 of file test_ast.py.

00217 
00218     def test_slice(self):
00219         slc = ast.parse("x[::]").body[0].value.slice
00220         self.assertIsNone(slc.upper)
00221         self.assertIsNone(slc.lower)
00222         self.assertIsNone(slc.step)

Here is the call graph for this function:

Definition at line 208 of file test_ast.py.

00208 
00209     def test_snippets(self):
00210         for input, output, kind in ((exec_tests, exec_results, "exec"),
00211                                     (single_tests, single_results, "single"),
00212                                     (eval_tests, eval_results, "eval")):
00213             for i, o in zip(input, output):
00214                 ast_tree = compile(i, "?", kind, ast.PyCF_ONLY_AST)
00215                 self.assertEqual(to_tuple(ast_tree), o)
00216                 self._assertTrueorder(ast_tree, (0, 0))

Here is the call graph for this function:


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