Added quaternary and quinary test cases and code cleanup.

parent 1d05417a
# vim: set fileencoding=utf-8 : # vim: set fileencoding=utf-8 :
from node import Node, Leaf
def generate_graph(root, node_type, separator=' ', verbose=False): def generate_graph(root, node_type, separator=' ', verbose=False):
...@@ -107,8 +106,6 @@ def generate_graph(root, node_type, separator=' ', verbose=False): ...@@ -107,8 +106,6 @@ def generate_graph(root, node_type, separator=' ', verbose=False):
title_line = center_text(node.title(), line_width, middle) title_line = center_text(node.title(), line_width, middle)
node_mid = node_len / 2
pipe_sign = '│'.decode('utf-8') pipe_sign = '│'.decode('utf-8')
dash_sign = '─'.decode('utf-8') dash_sign = '─'.decode('utf-8')
cross_sign = '┼'.decode('utf-8') cross_sign = '┼'.decode('utf-8')
...@@ -158,11 +155,15 @@ def generate_graph(root, node_type, separator=' ', verbose=False): ...@@ -158,11 +155,15 @@ def generate_graph(root, node_type, separator=' ', verbose=False):
edge_line += dash_sign edge_line += dash_sign
if i < middle_node: if i < middle_node:
marker = (left_sign if i == 0 else tsplit_sign) marker = (left_sign if i == 0 else tsplit_dn_sign)
edge_line += center_text(marker, box_widths[i], edge_line += center_text(marker, box_widths[i],
middle=0, right=dash_sign) middle=0, right=dash_sign)
else: else:
marker = (right_sign if i == node_len - 1 else tsplit_sign) if i == node_len - 1:
marker = right_sign
else:
marker = tsplit_dn_sign
edge_line += center_text(marker, box_widths[i], edge_line += center_text(marker, box_widths[i],
middle=0, left=dash_sign) middle=0, left=dash_sign)
......
...@@ -92,9 +92,12 @@ def generate_line(root, node_type): ...@@ -92,9 +92,12 @@ def generate_line(root, node_type):
right = '(' + right + ')' right = '(' + right + ')'
s = left + ' ' + s + ' ' + right s = left + ' ' + s + ' ' + right
else:
raise ValueError('arity = %d is currently not supported.' \
% arity)
else: else:
# Function # Function
s += '(' + ', '.join(map(traverse, node.nodes)) + ')' s += '(' + ', '.join(map(traverse, node)) + ')'
return s return s
......
...@@ -77,6 +77,54 @@ class TestGraph(unittest.TestCase): ...@@ -77,6 +77,54 @@ class TestGraph(unittest.TestCase):
LL LL
""") """)
def test_ternary_multichar_middle_right(self):
exp = Leaf('x')
minus_99 = Node('-', Leaf('99'))
minus_inf = Node('-', Leaf('oo'))
integral = Node('int', exp, minus_inf, minus_99)
g = generate_graph(integral, Node, verbose=True)
self.assertEqualGraphs(g, """
int
╭─┼──╮
x - -
│ │
oo 99
""")
def test_ternary_multichar_right(self):
exp = Leaf('x')
minus_99 = Node('-', Leaf('99'))
ten = Leaf('10')
integral = Node('int', exp, ten, minus_99)
g = generate_graph(integral, Node, verbose=True)
self.assertEqualGraphs(g, """
int
╭─┼──╮
x 10 -
99
""")
def test_quaternary(self):
a, b, c, d = Leaf(0), Leaf(1), Leaf(2), Leaf(3)
sum_node = Node('sum', a, b, c, d)
g = generate_graph(sum_node, Node, verbose=True)
self.assertEqualGraphs(g, """
sum
╭─┬┴┬─╮
0 1 2 3
""")
def test_quinary(self):
a, b, c, d, e = Leaf(0), Leaf(1), Leaf(2), Leaf(3), Leaf(4)
sum_node = Node('sum', a, b, c, d, e)
g = generate_graph(sum_node, Node, verbose=True)
self.assertEqualGraphs(g, """
sum
╭─┬─┼─┬─╮
0 1 2 3 4
""")
def strip(self, str): def strip(self, str):
return str.replace('\n ', '\n')[1:-1] return str.replace('\n ', '\n')[1:-1]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment