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_arguments(): parser = create_parser("bool a, list b, number c") var_1 = Variable("bool", Identifier("a"), None) var_2 = Variable("list", Identifier("b"), None) var_3 = Variable("number", Identifier("c"), None) assert parser.parse_arguments() == [var_1, var_2, var_3]
def test_parse_function_with_empty_body(): parser = create_parser("function fun(bool arg1, number arg2, list arg3){}") arg_1 = Variable("bool", Identifier("arg1"), None) arg_2 = Variable("number", Identifier("arg2"), None) arg_3 = Variable("list", Identifier("arg3"), None) function = Function(Identifier("fun"), [arg_1, arg_2, arg_3], None) assert parser.parse_function() == function
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_line_with_declaration(): parser = create_parser("bool abc = [1, 2, 3];") variable = Variable("bool", Identifier("abc"), List([Number(1), Number(2), Number(3)])) assert parser.parse_line() == variable
def test_parse_declaration_with_assign(): parser = create_parser("bool a = [i];") assert parser.parse_declaration() == Variable("bool", Identifier("a"), List([Identifier("i")]))
def test_parse_declaration_without_assign(): parser = create_parser("bool a;") assert parser.parse_declaration() == Variable("bool", Identifier("a"))
def test_parse_content_with_return(): parser = create_parser("xyz = 3; bool dfdf = 4; return 1") line_1 = Expression(Identifier("xyz"), "=", Number(3)) line_2 = Variable("bool", Identifier("dfdf"), Number(4)) assert parser.parse_content() == [line_1, line_2]
def test_parse_content_with_bracket(): parser = create_parser("x = 3; bool y = 4;}") line_1 = Expression(Identifier("x"), "=", Number(3)) line_2 = Variable("bool", Identifier("y"), Number(4)) assert parser.parse_content() == [line_1, line_2]