Back to index

python3.2  3.2.2
Public Member Functions
test.test_ast.ASTHelpers_Test Class Reference

List of all members.

Public Member Functions

def test_parse
def test_dump
def test_copy_location
def test_fix_missing_locations
def test_increment_lineno
def test_iter_fields
def test_iter_child_nodes
def test_get_docstring
def test_literal_eval
def test_literal_eval_issue4907

Detailed Description

Definition at line 382 of file test_ast.py.


Member Function Documentation

Definition at line 408 of file test_ast.py.

00408 
00409     def test_copy_location(self):
00410         src = ast.parse('1 + 1', mode='eval')
00411         src.body.right = ast.copy_location(ast.Num(2), src.body.right)
00412         self.assertEqual(ast.dump(src, include_attributes=True),
00413             'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
00414             'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
00415             'col_offset=0))'
00416         )

Here is the call graph for this function:

Definition at line 389 of file test_ast.py.

00389 
00390     def test_dump(self):
00391         node = ast.parse('spam(eggs, "and cheese")')
00392         self.assertEqual(ast.dump(node),
00393             "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
00394             "args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
00395             "keywords=[], starargs=None, kwargs=None))])"
00396         )
00397         self.assertEqual(ast.dump(node, annotate_fields=False),
00398             "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
00399             "Str('and cheese')], [], None, None))])"
00400         )
00401         self.assertEqual(ast.dump(node, include_attributes=True),
00402             "Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
00403             "lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
00404             "lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
00405             "col_offset=11)], keywords=[], starargs=None, kwargs=None, "
00406             "lineno=1, col_offset=0), lineno=1, col_offset=0)])"
00407         )

Here is the call graph for this function:

Definition at line 417 of file test_ast.py.

00417 
00418     def test_fix_missing_locations(self):
00419         src = ast.parse('write("spam")')
00420         src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
00421                                           [ast.Str('eggs')], [], None, None)))
00422         self.assertEqual(src, ast.fix_missing_locations(src))
00423         self.assertEqual(ast.dump(src, include_attributes=True),
00424             "Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
00425             "lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
00426             "col_offset=6)], keywords=[], starargs=None, kwargs=None, "
00427             "lineno=1, col_offset=0), lineno=1, col_offset=0), "
00428             "Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
00429             "col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
00430             "keywords=[], starargs=None, kwargs=None, lineno=1, "
00431             "col_offset=0), lineno=1, col_offset=0)])"
00432         )

Here is the call graph for this function:

Definition at line 468 of file test_ast.py.

00468 
00469     def test_get_docstring(self):
00470         node = ast.parse('def foo():\n  """line one\n  line two"""')
00471         self.assertEqual(ast.get_docstring(node.body[0]),
00472                          'line one\nline two')

Here is the call graph for this function:

Definition at line 433 of file test_ast.py.

00433 
00434     def test_increment_lineno(self):
00435         src = ast.parse('1 + 1', mode='eval')
00436         self.assertEqual(ast.increment_lineno(src, n=3), src)
00437         self.assertEqual(ast.dump(src, include_attributes=True),
00438             'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
00439             'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
00440             'col_offset=0))'
00441         )
00442         # issue10869: do not increment lineno of root twice
00443         src = ast.parse('1 + 1', mode='eval')
00444         self.assertEqual(ast.increment_lineno(src.body, n=3), src.body)
00445         self.assertEqual(ast.dump(src, include_attributes=True),
00446             'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), '
00447             'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, '
00448             'col_offset=0))'
00449         )

Here is the call graph for this function:

Definition at line 457 of file test_ast.py.

00457 
00458     def test_iter_child_nodes(self):
00459         node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
00460         self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
00461         iterator = ast.iter_child_nodes(node.body)
00462         self.assertEqual(next(iterator).id, 'spam')
00463         self.assertEqual(next(iterator).n, 23)
00464         self.assertEqual(next(iterator).n, 42)
00465         self.assertEqual(ast.dump(next(iterator)),
00466             "keyword(arg='eggs', value=Str(s='leek'))"
00467         )

Here is the call graph for this function:

Definition at line 450 of file test_ast.py.

00450 
00451     def test_iter_fields(self):
00452         node = ast.parse('foo()', mode='eval')
00453         d = dict(ast.iter_fields(node.body))
00454         self.assertEqual(d.pop('func').id, 'foo')
00455         self.assertEqual(d, {'keywords': [], 'kwargs': None,
00456                              'args': [], 'starargs': None})

Here is the call graph for this function:

Definition at line 473 of file test_ast.py.

00473 
00474     def test_literal_eval(self):
00475         self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])
00476         self.assertEqual(ast.literal_eval('{"foo": 42}'), {"foo": 42})
00477         self.assertEqual(ast.literal_eval('(True, False, None)'), (True, False, None))
00478         self.assertEqual(ast.literal_eval('{1, 2, 3}'), {1, 2, 3})
00479         self.assertEqual(ast.literal_eval('b"hi"'), b"hi")
00480         self.assertRaises(ValueError, ast.literal_eval, 'foo()')
00481         self.assertEqual(ast.literal_eval('-6'), -6)
00482         self.assertEqual(ast.literal_eval('-6j+3'), 3-6j)
00483         self.assertEqual(ast.literal_eval('3.25'), 3.25)

Here is the call graph for this function:

Definition at line 484 of file test_ast.py.

00484 
00485     def test_literal_eval_issue4907(self):
00486         self.assertEqual(ast.literal_eval('2j'), 2j)
00487         self.assertEqual(ast.literal_eval('10 + 2j'), 10 + 2j)
00488         self.assertEqual(ast.literal_eval('1.5 - 2j'), 1.5 - 2j)
00489 

Here is the call graph for this function:

Definition at line 384 of file test_ast.py.

00384 
00385     def test_parse(self):
00386         a = ast.parse('foo(1 + 1)')
00387         b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
00388         self.assertEqual(ast.dump(a), ast.dump(b))

Here is the call graph for this function:


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