Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
result = output(root)
assert result == """\
bar = a({x0, y0}
* {x1,{x2, y2} / {x3,{x4, y4} / {x5, y5}}})"""
output = printer.IndentedStream()
output.test_child_z_is_expression = False
output.test_child_z_list_sep = '//'
result = output(root)
assert result == """\
bar = a({x0, y0}
* {x1, {x2, y2}
// {x3, {x4, y4}
// {x5, y5}}})"""
finally:
printer.NODE_PRINTERS.pop('TestRoot', None)
printer.NODE_PRINTERS.pop('TestChild', None)
printer.NODE_PRINTERS.pop('TestNiece', None)
assert result == """\
bar = a({x0, y0}
* {x1,{x2, y2} / {x3,{x4, y4} / {x5, y5}}})"""
output = printer.IndentedStream()
output.test_child_z_is_expression = False
output.test_child_z_list_sep = '//'
result = output(root)
assert result == """\
bar = a({x0, y0}
* {x1, {x2, y2}
// {x3, {x4, y4}
// {x5, y5}}})"""
finally:
printer.NODE_PRINTERS.pop('TestRoot', None)
printer.NODE_PRINTERS.pop('TestChild', None)
printer.NODE_PRINTERS.pop('TestNiece', None)
def test_prettification(src, lineno, case):
parts = case.split('\n=\n')
original = parts[0].strip()
parts = parts[1].split('\n:\n')
expected = parts[0].strip().replace('\\n\\\n', '\n')
if len(parts) == 2:
options = literal_eval(parts[1])
else:
options = {}
prettified = IndentedStream(**options)(original)
assert expected == prettified, "%s:%d:%r != %r" % (src, lineno, expected, prettified)
def test_prettification(sample):
parts = sample.split('\n=\n')
original = parts[0].strip()
parts = parts[1].split('\n:\n')
expected = parts[0].strip()
if len(parts) == 2:
options = literal_eval(parts[1])
else:
options = {}
prettified = IndentedStream(**options)(original)
assert expected == prettified, "%r != %r" % (expected, prettified)
@printer.special_function('foo.test_function')
def test(node, output):
pass
@printer.node_printer('test_tag1')
def tag1(node, output):
pass
def roundtrip(sql):
orig_ast = parse_sql(sql)
_remove_stmt_len_and_location(orig_ast)
serialized = RawStream()(Node(orig_ast))
try:
serialized_ast = parse_sql(serialized)
except: # noqa
raise RuntimeError("Could not reparse %r" % serialized)
_remove_stmt_len_and_location(serialized_ast)
assert orig_ast == serialized_ast, "%r != %r" % (sql, serialized)
indented = IndentedStream()(Node(orig_ast))
try:
indented_ast = parse_sql(indented)
except: # noqa
raise RuntimeError("Could not reparse %r" % indented)
_remove_stmt_len_and_location(indented_ast)
assert orig_ast == indented_ast, "%r != %r" % (sql, indented)
# Run ``pytest -s tests/`` to see the following output
output.write('y')
output.print_node(node.y)
else:
if output.test_child_z_is_expression:
with output.push_indent(2, relative=False):
output.print_list(node.z, '+')
else:
output.print_list(node.z, '/', standalone_items=False)
output.write('}')
output = printer.RawStream()
output.test_child_z_is_expression = True
result = output(root)
assert result == 'bar = a({x0, y0} * {x1,{x2, y2} + {x3,{x4, y4} + {x5, y5}}})'
output = printer.RawStream()
output.test_child_z_is_expression = False
result = output(root)
assert result == 'bar = a({x0, y0} * {x1,{x2, y2} / {x3,{x4, y4} / {x5, y5}}})'
finally:
printer.NODE_PRINTERS.pop('TestRoot', None)
printer.NODE_PRINTERS.pop('TestChild', None)
printer.NODE_PRINTERS.pop('TestNiece', None)
def do(meth, node):
output = printer.RawStream()
getattr(output, meth)(node)
return output.getvalue()
def test_basic():
ptree = [{'Foo': {'bar': {'Bar': {'a': 1, 'b': 'b', 'c': None, 'd': 0}}}},
{'Foo': {'bar': {'Bar': {'a': 0, 'f': False, 't': True, 'c': [
{'C': {'x': 0, 'y': 0}},
{'C': {'x': 0, 'y': 0}}
]}}}}]
root = Node(ptree)
assert root.parent_node is None
assert root.parent_attribute is None
assert isinstance(root, List)
assert len(root) == 2
assert repr(root) == '[2*{Foo}]'
assert str(root) == 'None=[2*{Foo}]'
with pytest.raises(AttributeError):
root.not_there
foo1 = root[0]
assert foo1 != root
assert foo1.node_tag == 'Foo'
assert foo1.parse_tree == {'bar': {'Bar': {'a': 1, 'b': 'b', 'c': None, 'd': 0}}}
assert foo1.parent_node is None
assert foo1.parent_attribute == (None, 0)
assert repr(foo1) == '{Foo}'