Exemple #1
0
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]
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
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
Exemple #5
0
def test_parse_multiplication_with_divide():
    parser = create_parser("1 / 2")
    assert parser.parse_multiplication() == Expression(Number(1), "/", Number(2))
Exemple #6
0
def test_parse_multiplication_with_star():
    parser = create_parser("1 * 2")
    assert parser.parse_multiplication() == Expression(Number(1), "*", Number(2))
Exemple #7
0
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)
Exemple #8
0
def test_parse_function_body_with_nonempty_return_and_content():
    parser = create_parser("a = 230 / hi; [9, 8, 7].get(3); return 0;}")
    line_1 = Expression(Identifier("a"), "=", Expression(Number(230), "/", Identifier("hi")))
    line_2 = Get(List([Number(9), Number(8), Number(7)]), Number(3))
    assert parser.parse_function_body() == FunctionBody(Number(0), [line_1, line_2])
Exemple #9
0
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]
Exemple #10
0
def test_parse_expression_with_complex_example():
    parser = create_parser("(1 - 9 * 2) / 8 - 6")
    expr = Expression(Expression(Expression(Number(1), "-", Expression(Number(9), "*", Number(2))), "/", Number(8)), "-", Number(6))
    assert parser.parse_expression() == expr
Exemple #11
0
def test_parse_factor_with_expression_within_brackets():
    parser = create_parser("(1 + 2)")
    assert parser.parse_factor() == Expression(Number(1), "+", Number(2))
Exemple #12
0
def test_parse_expression_with_brackets():
    parser = create_parser("9 / (2 + 3)")
    assert parser.parse_expression() == Expression(Number(9), "/", Expression(Number(2), "+", Number(3)))
Exemple #13
0
def test_parse_expression_with_star_and_divide():
    parser = create_parser("5 * 4 / 6")
    assert parser.parse_expression() == Expression(Expression(Number(5), "*", Number(4)), "/", Number(6))
Exemple #14
0
def test_parse_expression_with_plus_and_minus():
    parser = create_parser("2 - 3 + 9")
    assert parser.parse_expression() == Expression(Expression(Number(2), "-", Number(3)), "+", Number(9))
Exemple #15
0
def test_parse_multiplication_with_divide_and_identifiers():
    parser = create_parser("abc / def")
    assert parser.parse_multiplication() == Expression(Identifier("abc"), "/", Identifier("def"))
Exemple #16
0
def test_parse_function_body_with_empty_return():
    parser = create_parser("a = 123 / hi; xyz.delete(3);}")
    line_1 = Expression(Identifier("a"), "=", Expression(Number(123), "/", Identifier("hi")))
    line_2 = Delete(Identifier("xyz"), Number(3))
    assert parser.parse_function_body() == FunctionBody(None, [line_1, line_2])
Exemple #17
0
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))
Exemple #18
0
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]