| 1 |
from pypy.interpreter.astcompiler import ast |
| 2 |
class TestAstToObject: |
| 3 |
def test_types(self, space): |
| 4 |
assert space.issubtype_w( |
| 5 |
ast.get(space).w_Module, ast.get(space).w_mod) |
| 6 |
|
| 7 |
def test_constant_num(self, space): |
| 8 |
value = space.wrap(42) |
| 9 |
node = ast.Constant(value, space.w_None, lineno=1, col_offset=1, end_lineno=-1, end_col_offset=-1) |
| 10 |
w_node = node.to_object(space) |
| 11 |
assert space.is_w(space.getattr(w_node, space.wrap("value")), value) |
| 12 |
|
| 13 |
def test_expr(self, space): |
| 14 |
value = space.wrap(42) |
| 15 |
node = ast.Constant(value, space.w_None, lineno=1, col_offset=1, end_lineno=-1, end_col_offset=-1) |
| 16 |
expr = ast.Expr(node, lineno=1, col_offset=1, end_lineno=-1, end_col_offset=-1) |
| 17 |
w_node = expr.to_object(space) |
| 18 |
|
| 19 |
assert space.is_w(space.getattr(space.getattr(w_node, space.wrap("value")), |
| 20 |
space.wrap("value")), value) |
| 21 |
|
| 22 |
def test_operation(self, space): |
| 23 |
val1 = ast.Constant(space.wrap(1), space.w_None, lineno=1, col_offset=1, end_lineno=-1, end_col_offset=-1) |
| 24 |
val2 = ast.Constant(space.wrap(2), space.w_None, lineno=1, col_offset=1, end_lineno=-1, end_col_offset=-1) |
| 25 |
node = ast.BinOp(left=val1, right=val2, op=ast.Add, |
| 26 |
lineno=1, col_offset=1, end_lineno=-1, end_col_offset=-1) |
| 27 |
w_node = node.to_object(space) |
| 28 |
w_op = space.getattr(w_node, space.wrap("op")) |
| 29 |
assert space.isinstance_w(w_op, ast.get(space).w_operator) |
| 30 |
|
| 31 |
def test_to_object_does_not_add_optional_attributes(self, space): |
| 32 |
val1 = ast.Constant(space.wrap(1), None, lineno=1, col_offset=1, end_lineno=-1, end_col_offset=-1) |
| 33 |
w_const = val1.to_object(space) |
| 34 |
w_dict = w_const.getdict(space) |
| 35 |
space.raises_w(space.w_KeyError, space.getitem, w_dict, space.newtext("kind")) |
| 36 |
space.is_w(space.getattr(w_const, space.newtext("kind")), space.w_None) |
| 37 |
|
| 38 |
def test_from_object(self, space): |
| 39 |
value = space.wrap(42) |
| 40 |
w_node = space.call_function(ast.get(space).w_Constant) |
| 41 |
space.setattr(w_node, space.wrap('value'), value) |
| 42 |
space.setattr(w_node, space.wrap('kind'), space.w_None) |
| 43 |
space.setattr(w_node, space.wrap('lineno'), space.wrap(1)) |
| 44 |
space.setattr(w_node, space.wrap('col_offset'), space.wrap(1)) |
| 45 |
node = ast.Constant.from_object(space, w_node) |
| 46 |
assert node.value is value |
| 47 |
|
| 48 |
def test_from_object_error(self, space): |
| 49 |
w_node = space.call_function(ast.get(space).w_Module) |
| 50 |
excinfo = space.raises_w(space.w_TypeError, ast.Module.from_object, space, w_node) |
| 51 |
error = space.text_w(excinfo.value.get_w_value(space)) |
| 52 |
assert error == "required field 'body' missing from Module" |
| 53 |
w_node = space.call_function(ast.get(space).w_Expression, space.w_None) |
| 54 |
excinfo = space.raises_w(space.w_ValueError, ast.Expression.from_object, space, w_node) |
| 55 |
error = space.text_w(excinfo.value.get_w_value(space)) |
| 56 |
assert error == "field 'body' is required for Expression" |
| 57 |
|
| 58 |
def test_docstring(self, space): |
| 59 |
doc = space.text_w(space.getattr(ast.get(space).w_arguments, space.newtext("__doc__"))) |
| 60 |
assert doc == "arguments(arg* posonlyargs, arg* args, arg? vararg, arg* kwonlyargs, expr* kw_defaults, arg? kwarg, expr* defaults)" |
| 61 |
|
| 62 |
def test_fields(self, space): |
| 63 |
w_fields = space.getattr(ast.get(space).w_FunctionDef, |
| 64 |
space.wrap("_fields")) |
| 65 |
assert space.eq_w(w_fields, space.wrap( |
| 66 |
('name', 'args', 'body', 'decorator_list', 'returns', 'type_comment'))) |
| 67 |
w_fields = space.getattr(ast.get(space).w_arguments, |
| 68 |
space.wrap("_fields")) |
| 69 |
assert space.eq_w(w_fields, space.wrap( |
| 70 |
('posonlyargs', 'args', 'vararg', 'kwonlyargs', 'kw_defaults', |
| 71 |
'kwarg', 'defaults'))) |
| 72 |
|
| 73 |
def test_attributes(self, space): |
| 74 |
w_attrs = space.getattr(ast.get(space).w_FunctionDef, |
| 75 |
space.wrap("_attributes")) |
| 76 |
assert space.eq_w(w_attrs, space.wrap(('lineno', 'col_offset', 'end_lineno', 'end_col_offset'))) |
| 77 |
|
| 78 |
def test_end_lineno_end_col_offset_None_default(self): |
| 79 |
space = self.space |
| 80 |
w_node = space.call_function(ast.get(space).w_Constant) |
| 81 |
assert space.is_w(space.w_None, space.getattr(w_node, space.newtext("end_lineno"))) |
| 82 |
assert space.is_w(space.w_None, space.getattr(w_node, space.newtext("end_col_offset"))) |
| 83 |
|
| 84 |
def test_from_object_required_identifier_set_to_none_raises_valueerror(self, space): |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
for w_cls, field in [ |
| 90 |
(ast.get(space).w_alias, 'name'), |
| 91 |
(ast.get(space).w_arg, 'arg'), |
| 92 |
]: |
| 93 |
w_node = space.call_function(w_cls) |
| 94 |
space.setattr(w_node, space.newtext(field), space.w_None) |
| 95 |
space.setattr(w_node, space.newtext('lineno'), space.newint(1)) |
| 96 |
space.setattr(w_node, space.newtext('col_offset'), space.newint(0)) |
| 97 |
excinfo = space.raises_w( |
| 98 |
space.w_ValueError, |
| 99 |
ast.alias.from_object if field == 'name' else ast.arg.from_object, |
| 100 |
space, w_node) |
| 101 |
error = space.text_w(excinfo.value.get_w_value(space)) |
| 102 |
assert ("field '%s' is required" % field) in error |