def test_parse_function_call(): parser = create_parser("(83475, shadowrun, [1231, sdf, [1]])") arg_1 = Number(83475) arg_2 = Identifier("shadowrun") arg_3 = List([Number(1231), Identifier("sdf"), List([Number(1)])]) function_call = FunctionCall("fun", [arg_1, arg_2, arg_3]) assert parser.parse_function_call("fun") == function_call
def test_parse_line_with_expression(): parser = create_parser("xyz = [654, 9900, 3123] * 823;") identifier = Identifier("xyz") tmp_list = List([Number(654), Number(9900), Number(3123)]) expression = Expression(tmp_list, "*", Number(823)) result = Expression(Identifier("xyz"), "=", expression) assert parser.parse_line() == result
def test_parse_list_operation_filter_with_different_conditions(): parser = create_parser("filter(x > 1 & x < 999 & x >= abc & x <= xyz)") tmp_list = [1, 2, 3] conditions = [] conditions.append(FilterCondition("==", Number(1))) conditions.append(FilterCondition(">", Number(999))) conditions.append(FilterCondition("<", Identifier("abc"))) conditions.append(FilterCondition("<=", Identifier("xyz"))) assert parser.parse_list_operation_filter(tmp_list) != Filter(tmp_list, conditions)
def test_parse_function_without_arguments(): parser = create_parser( "function fun(){" " a = 1 + (2 * 3);" " return a;" "}" ) line_1 = Expression(Identifier("a"), "=", Expression(Number(1), "+", Expression(Number(2), "*", Number(3)))) function = Function(Identifier("fun"), [], FunctionBody(Identifier("a"), [line_1])) assert parser.parse_function() == function
def test_parse_list_operation_filter_with_different_list(): parser = create_parser("filter(x > 1 & x < 999 & x >= abc & x <= xyz)") list_1 = [1, 2, 3] list_2 = [4, 5, 6] conditions = [] conditions.append(FilterCondition(">", Number(1))) conditions.append(FilterCondition("<", Number(999))) conditions.append(FilterCondition(">=", Identifier("abc"))) conditions.append(FilterCondition("<=", Identifier("xyz"))) assert parser.parse_list_operation_filter(list_1) != Filter(list_2, conditions)
def test_parse_list_component(): parser = create_parser(".filter(x > xyz).each(-, abc).delete(12).get(3).length()") tmp_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] condition = FilterCondition(">", Identifier("xyz")) conditions = [condition] result_1 = Filter(tmp_list, conditions) result_2 = Each(result_1, "-", Identifier("abc")) result_3 = Delete(result_2, Number(12)) result_4 = Get(result_3, Number(3)) result_5 = Length(result_4) assert parser.parse_list_component(tmp_list) == result_5
def test_parse_list_operation_with_filter(): parser = create_parser(".filter(x >= 123 & x == abc)") tmp_list = [1, 2, 3] conditions = [] conditions.append(FilterCondition(">=", Number(123))) conditions.append(FilterCondition("==", Identifier("abc"))) assert parser.parse_list_operation(tmp_list) == Filter(tmp_list, conditions)
def test_parse_function_with_arguments_and_body(): parser = create_parser( "function fun(number arg1, number arg2) {" " a = 99 * (2 - 3);" " number x = [g, h, i].get(1);" " print jkl;" " return a;" "}" ) arg_1 = Variable("number", Identifier("arg1"), None) arg_2 = Variable("number", Identifier("arg2"), None) line_1 = Expression(Identifier("a"), "=", Expression(Number(99), "*", Expression(Number(2), "-", Number(3)))) line_2 = Variable("number", Identifier("x"), Get(List([Identifier("g"), Identifier("h"), Identifier("i")]), Number(1))) line_3 = PrintFunction(Identifier("jkl")) function = Function(Identifier("fun"), [arg_1, arg_2], FunctionBody(Identifier("a"), [line_1, line_2, line_3])) assert parser.parse_function() == function
def test_parse(): parser = create_parser( "function name1(bool a, number b) {" " return [abc, 123, [345, xyz]];" "}" "function name2(list abc) {" " number a = abc.get(2);" " print abc;" " abc.each(*, 564).length();" " a = 2 + 2;" " return 0;" " # comment \n" "}" ) arguments_1 = [Variable("bool", Identifier("a")), Variable("number", Identifier("b"))] function_body_1 = FunctionBody(List([Identifier("abc"), Number(123), List([Number(345), Identifier("xyz")])]), []) fun_1 = Function(Identifier("name1"), arguments_1, function_body_1) arguments_2 = [Variable("list", Identifier("abc"))] line_1 = Variable("number", Identifier("a"), Get(Identifier("abc"), Number(2))) line_2 = PrintFunction(Identifier("abc")) line_3 = Length(Each(Identifier("abc"), "*", Number(564))) line_4 = Expression(Identifier("a"), "=", Expression(Number(2), "+", Number(2))) function_body_2 = FunctionBody(Number(0), [line_1, line_2, line_3, line_4]) fun_2 = Function(Identifier("name2"), arguments_2, function_body_2) assert parser.parse() == [fun_1, fun_2]
def test_parse_return_with_number(): parser = create_parser("return 123") assert parser.parse_return() == Number(123)
def test_parse_return_with_list(): parser = create_parser("return [1, 2, 3]") elements = [Number(1), Number(2), Number(3)] assert parser.parse_return() == List(elements)
def test_parse_multiplication_with_letter(): parser = create_parser("1 a 2") assert parser.parse_multiplication() == Number(1)
def test_parse_multiplication_with_complex_example(): parser = create_parser("abc * def / 123 + 1") first_expression = Expression(Identifier("abc"), "*", Identifier("def")) assert parser.parse_multiplication() == Expression(first_expression, "/", Number(123))
def test_parse_multiplication_with_divide(): parser = create_parser("1 / 2") assert parser.parse_multiplication() == Expression(Number(1), "/", Number(2))
def test_parse_multiplication_with_minus(): parser = create_parser("1 - 2") assert parser.parse_multiplication() == Number(1)
def test_parse_list_operation_get_with_different_possition(): parser = create_parser("get(1)") tmp_list = [1, 2, 3] assert parser.parse_list_operation_get(tmp_list) != Get(tmp_list, Number(2))
def test_parse_list_operation_each(): parser = create_parser("each(+, 1)") tmp_list = [1, 2, 3] assert parser.parse_list_operation_each(tmp_list) == Each(tmp_list, "+", Number(1))
def test_parse_component_with_number(): parser = create_parser("987654321") assert parser.parse_component() == Number(987654321)
def test_parse_list_of_lists(): parser = create_parser("[[1, 2, 3], [[4]], [a, b]]") list_1 = List([Number(1), Number(2), Number(3)]) list_2 = List([List(Number(4))]) list_3 = List([Identifier("a"), Identifier("b")]) assert parser.parse_list() == List([list_1, list_2, list_3])
def test_parse_single_condition_with_LESS_OR_EQUAL_TO(): parser = create_parser("x <= 1") assert parser.parse_single_condition() == FilterCondition("<=", Number(1))
def test_parse_list_operation_with_each(): parser = create_parser(".each(+, 123)") tmp_list = [1, 2, 3] assert parser.parse_list_operation(tmp_list) == Each(tmp_list, "+", Number(123))
def test_parse_list_operation_each_with_complex_expression(): parser = create_parser("each(+, 1 + 2 * abc)") tmp_list = [1, 2, 3] expression = Expression(Number(1), "+", Expression(Number(2), "*", Identifier("abc"))) assert parser.parse_list_operation_each(tmp_list) == Each(tmp_list, "+", expression)
def test_parse_single_condition_with_GREATER_THAN(): parser = create_parser("x > 1") assert parser.parse_single_condition() == FilterCondition(">", Number(1))
def test_parse_multiplication_with_star(): parser = create_parser("1 * 2") assert parser.parse_multiplication() == Expression(Number(1), "*", Number(2))
def test_parse_single_condition_with_LESS_THAN(): parser = create_parser("x < 1") assert parser.parse_single_condition() == FilterCondition("<", Number(1))
def test_parse_list_operation_delete_with_number(): parser = create_parser("delete(123)") tmp_list = [1, 2, 3] assert parser.parse_list_operation_delete(tmp_list) == Delete(tmp_list, Number(123))
def test_parse_single_condition_with_NOT_EQUAL_TO(): parser = create_parser("x != 1") assert parser.parse_single_condition() == FilterCondition("!=", Number(1))
def test_parse_list_operation_get_with_number(): parser = create_parser("get(1)") tmp_list = [1, 2, 3] assert parser.parse_list_operation_get(tmp_list) == Get(tmp_list, Number(1))
def test_parse_component_with_list(): parser = create_parser("[987, abc, [321, xyz]]") assert parser.parse_component() == List([Number(987), Identifier("abc"), List([Number(321), Identifier("xyz")])])
def test_parse_list_operation_get_with_lists_with_different_length(): parser = create_parser("get(1)") list_1 = [1, 2, 3] list_2 = [4, 5, 6] assert parser.parse_list_operation_get(list_1) != Get(list_2, Number(2))