def test_nested_parenthesis(): exp = '10+((1+2)*(3+4))+30' node, node_end = expressions.parse_expression(exp) expected = \ expnodes.BinaryOperatorNode( operator='+', a=expnodes.BinaryOperatorNode( operator='+', a=expnodes.ValueNode(10), b=expnodes.BinaryOperatorNode( operator='*', a=expnodes.BinaryOperatorNode( operator='+', a=expnodes.ValueNode(1), b=expnodes.ValueNode(2) ), b=expnodes.BinaryOperatorNode( operator='+', a=expnodes.ValueNode(3), b=expnodes.ValueNode(4) ) ) ), b=expnodes.ValueNode(30) ) assert node == expected assert node_end == len(exp)
def test_insert_sub_node_and_add(): exp = '10+20*30*40+40' node, node_end = expressions.parse_expression(exp) expected = \ expnodes.BinaryOperatorNode( operator='+', a=expnodes.BinaryOperatorNode( operator='+', a=expnodes.ValueNode(10), b=expnodes.BinaryOperatorNode( operator='*', a=expnodes.BinaryOperatorNode( operator='*', a=expnodes.ValueNode(20), b=expnodes.ValueNode(30) ), b=expnodes.ValueNode(40) ) ), b=expnodes.ValueNode(40) ) assert node == expected assert node_end == len(exp)
def test_mul_before_addition(): exp = '10+20*30' node, node_end = expressions.parse_expression(exp) expected = expnodes.BinaryOperatorNode( operator='+', a=expnodes.ValueNode(10), b=expnodes.BinaryOperatorNode( operator='*', a=expnodes.ValueNode(20), b=expnodes.ValueNode(30) ) ) assert node == expected assert node_end == len(exp)
def test_additions_without_spacing(): exp = '10+20+30' node, node_end = expressions.parse_expression(exp) expected = expnodes.BinaryOperatorNode( operator='+', a=expnodes.BinaryOperatorNode( operator='+', a=expnodes.ValueNode(10), b=expnodes.ValueNode(20) ), b=expnodes.ValueNode(30) ) assert node == expected assert node_end == len(exp)
def test_variable_exp(): exp = '10+$value*12' node, node_end = expressions.parse_expression(exp) expected = \ expnodes.BinaryOperatorNode( operator='+', b=expnodes.BinaryOperatorNode( operator='*', a=expnodes.VariableNode("$value"), b=expnodes.ValueNode(12) ), a=expnodes.ValueNode(10) ) assert node == expected assert node_end == len(exp)
def test_basic_parenthesis(): exp = '10+(200+300)' node, node_end = expressions.parse_expression(exp) expected = \ expnodes.BinaryOperatorNode( operator='+', b=expnodes.BinaryOperatorNode( operator='+', a=expnodes.ValueNode(200), b=expnodes.ValueNode(300) ), a=expnodes.ValueNode(10) ) assert node == expected assert node_end == len(exp)
def test_multi_char_operators(): exp = 'True <> False' node, node_end = expressions.parse_expression(exp) assert node == expnodes.BinaryOperatorNode( operator='<>', a=expnodes.ValueNode(True), b=expnodes.ValueNode(False) ) assert node_end == len(exp)
def test_boolean_node(): exp = 'False And True' node, node_end = expressions.parse_expression(exp) expected = \ expnodes.BinaryOperatorNode( operator='And', a=expnodes.ValueNode(False), b=expnodes.ValueNode(True) ) assert node == expected assert node_end == len(exp)
def test_string_nodes(): exp = '"Bye" & "\'Hello\'"' node, node_end = expressions.parse_expression(exp) expected = \ expnodes.BinaryOperatorNode( operator='&', a=expnodes.ValueNode("Bye"), b=expnodes.ValueNode("\'Hello\'") ) assert node == expected assert node_end == len(exp)
def read_values(exp, end_options=(), allow_more=False): if not exp: raise SyntaxError("Empty expression") end_options = list(end_options) is_error = True end_index = 0 while exp and exp[0] not in end_options: exp, space_num = strip_spaces(exp) end_index += space_num node, node_end_index = get_first_node(exp) if node_end_index == -1: break is_error = False yield node end_index += node_end_index exp = exp[node_end_index:] exp, space_num = strip_spaces(exp) end_index += space_num op, op_end_index = get_operator(exp) if op_end_index == -1: break is_error = True exp = exp[op_end_index:] op = expnodes.BinaryOperatorNode(operator=op) end_index += op_end_index yield op exp, spaces_cnt = strip_spaces(exp) if not allow_more and end_options and (not exp or exp[0] not in end_options): raise SyntaxError("End character ({}) not met: {}".format(end_options, exp)) if not allow_more and not end_options and exp: raise SyntaxError("Left chars in the expression: " + repr(exp)) if is_error: raise SyntaxError("Missing operand:" + exp) yield end_index+spaces_cnt
def test_not_call(): exp = 'Not (10 = 12)' node, node_end = expressions.parse_expression(exp) expected = \ expnodes.NotNode( value=expnodes.BinaryOperatorNode( operator='=', a=expnodes.ValueNode(10), b=expnodes.ValueNode(12) ) ) assert node == expected assert node_end == len(exp)
def test_array_node(): exp = '[1, 2, 4, 5, 20+30]' node, node_end = expressions.parse_expression(exp) expected = \ expnodes.ArrayNode( [ expnodes.ValueNode(1), expnodes.ValueNode(2), expnodes.ValueNode(4), expnodes.ValueNode(5), expnodes.BinaryOperatorNode(operator='+', a=expnodes.ValueNode(20), b=expnodes.ValueNode(30) ) ] ) assert node == expected assert node_end == len(exp)