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_str(self): self.assertEqual(str(NumberNode(20)), "20") self.assertEqual(str(power_node_for_num(20, 10)), "20^(10)") self.assertEqual(str(expr( 20, [sum_sub('+', 30), sum_sub('-', 15)])), "20+30-15") self.assertEqual( str( expr(mult( 1, [mult_sub('*', 2), mult_sub('/', 4)]), [ sum_sub('+', mult(5, [mult_sub('*', 6), mult_sub('/', 7)])), sum_sub('-', 15) ])), "(1*2/4)+(5*6/7)-15")
def test_visit_mult(self): node = MultNode(power_node_for_num(20)) traverse_pre(node, self.visitor) self.assertEqual(self.visitor.counts, ['mult', 'power', 'func_', 'number_20'])
def visit_mult(self, node): new_rest = list( node.rest_value) + [MultSub(SignNode('*'), power_node_for_num(2))] return MultNode(node.init_node, *new_rest)
def test_visit_mult(self): node = MultNode(power_node_for_num(20)) node.accept(self.visitor) self.assertEqual(self.visitor.counts, {'mult': 1})
def test_pow_value2(self): node = power_node_for_num(10, 3) self.assertEqual(1000, node.get_value())