def test_add_mult_by_two(self): node = mult_node_for_num(20) new_node = map_post(node, MultByTwoSubstitutor()) self.assertEqual(node, mult_node_for_num(20)) self.assertEqual( new_node, MultNode(power_node_for_num(20), MultSub(SignNode('*'), power_node_for_num(2))))
def test_visit_sum(self): node = SumNode(mult_node_for_num(30), SumSub(SignNode('+'), mult_node_for_num(10))) traverse_post(node, self.visitor) print(self.visitor.counts) self.assertEqual(self.visitor.counts, [ 'number_30', 'func_', 'power', 'mult', 'sign', 'number_10', 'func_', 'power', 'mult', 'sum-sub', 'sum' ])
def test_substitute_number_with_expr(self): node = mult_node_for_num(10) new_node = map_post(node, PlusOneSubstitutor()) self.assertEqual(node, mult_node_for_num(10)) expected = MultNode( PowerNode( FuncallNode( None, SumNode(mult_node_for_num(10), SumSub(SignNode('+'), mult_node_for_num(1)))))) self.assertEqual(new_node, expected) self.assertEqual(new_node.get_value(), 11)
def test_visit_sum(self): node = SumNode(mult_node_for_num(30), SumSub(SignNode('+'), sum_node_for_num(50))) traverse_pre(node, self.visitor) self.assertEqual(self.visitor.counts, [ 'sum', 'mult', 'power', 'func_', 'number_30', 'sum-sub', 'sign', 'sum', 'mult', 'power', 'func_', 'number_50' ])
def test_stop_at_root(self): visitor = HeadSequenceVisitor() node = mult_node_for_num(30) traverse_pre(node, visitor) print(visitor.counts) self.assertEqual(visitor.counts, ['mult'])
def visit_number(self, node): current_num = node.val return SumNode(mult_node_for_num(current_num), SumSub(SignNode('+'), mult_node_for_num(1)))
def test_visit_sum(self): node = SumNode(mult_node_for_num(30), SumSub('+', sum_node_for_num(50))) node.accept(self.visitor) self.assertEqual(self.visitor.counts, {'sum': 1})
def test_sum_value2(self): node = SumNode(mult_node_for_num(20), SumSub(SignNode('+'), mult_node_for_num(20)), SumSub(SignNode('-'), mult_node_for_num(10))) self.assertEqual(30, node.get_value())
def test_mult_value(self): node = mult_node_for_num(30) self.assertEqual(30, node.get_value())