def test_binary_ops_level_4_and_below(op, left, right, op_str, left_str, right_str): ast = op(left, right) s = left_str + op_str + right_str parser = Parser(Tokenizer(s)) assert parser.expression() == ast
def test_gt_2(): ast = gt(div(add(c(1), c(2)), c(3)), add(c(1), c(2))) parser = Parser(Tokenizer("(1+2)/3 > 1 + 2")) assert parser.expression() == ast
def test_binary_ops_all_levels(op, left, right, op_str, left_str, right_str): ast = op(left, right) s = " ".join((left_str, op_str, right_str)) parser = Parser(Tokenizer(s)) assert parser.expression() == ast
def test_tailing_new_line(): ast = add(v("a"), v("b")) parser = Parser(Tokenizer("a+b\n")) assert parser.expression() == ast assert parser.cur_token == EOF
def test_lt_1(): ast = lt(div(add(c(1), c(2)), c(3)), add(c(4), c(5))) parser = Parser(Tokenizer("(1+2)/3 < 4 + 5")) assert parser.expression() == ast
def test_differing_priority_bins_2(): ast = add(div(c(1), v("b")), c(4)) parser = Parser(Tokenizer("1/b+4")) assert parser.expression() == ast
def test_parens(): ast = div(add(c(1), v("b")), c(4)) parser = Parser(Tokenizer("(1+b)/4")) assert parser.expression() == ast
def test_differing_priority_bins_1(): ast = add(c(1), div(v("b"), c(4))) parser = Parser(Tokenizer("1+b/4")) assert parser.expression() == ast
def test_uniary_ops(op, operand, op_str, operand_str): ast = op(operand) s = op_str + " " + operand_str parser = Parser(Tokenizer(s)) assert parser.expression() == ast
def test_multiple_same_priority_bins_4(): ast1 = sub(c(1), add(v("b"), sub(c(4), v("a")))) ast2 = sub(add(sub(c(1), v("b")), c(4)), v("a")) parser = Parser(Tokenizer("1-b+4-a")) expr = parser.expression() assert expr == ast1 or expr == ast2
def test_multiple_same_priority_bins_2(): ast1 = add(c(1), add(v("b"), add(c(4), v("a")))) ast2 = add(add(add(c(1), v("b")), c(4)), v("a")) parser = Parser(Tokenizer("1+b+4+a")) expr = parser.expression() assert expr == ast1 or expr == ast2
def test_const_bin_var(): ast = add(c(1), v("b")) parser = Parser(Tokenizer("1+b")) assert parser.expression() == ast
def test_var_bin_var(): ast = add(v("a"), v("b")) parser = Parser(Tokenizer("a+b")) assert parser.expression() == ast
def test_unary_var(): ast = _not(v("b")) parser = Parser(Tokenizer("not b")) assert parser.expression() == ast