Пример #1
0
def test_multi_word_func_one_multi_word_arg() -> None:
    tokens = "function read file one argument file location".split()

    engine = RuleEngine(tokens)

    code = Code()

    assert engine.parse_function(code) == "def read_file(file_location):"
    assert engine.tokens == "".split()
Пример #2
0
def test_main_func() -> None:
    tokens = ["function", "main", "zero", "arguments"]

    code = Code()
    engine = RuleEngine()
    engine.add_tokens(tokens)

    assert engine.parse_function(code) == "def main():"
    assert engine.tokens == "".split()
Пример #3
0
def test_basic_func() -> None:
    tokens = "function f two arguments a and b".split()

    engine = RuleEngine(tokens)

    code = Code()

    assert engine.parse_function(code) == "def f(a, b):"
    assert engine.tokens == "".split()
Пример #4
0
def test_no_defined_func_call() -> None:
    tokens = "call f a b".split()

    engine = RuleEngine(tokens)

    code = Code()

    assert engine.parse_call(code) == "f(a, b)"
    assert engine.tokens == "".split()
Пример #5
0
def test_base() -> None:
    tokens = "function main zero arguments".split()

    engine = RuleEngine(tokens)

    code = Code()

    assert engine.parse_function(code) == "def main():"
    assert engine.tokens == "".split()
Пример #6
0
def test_call_print() -> None:
    tokens = "call print hello".split()

    engine = RuleEngine(tokens)

    code = Code()

    assert engine.parse_call(code) == "print(hello)"
    assert engine.tokens == "".split()
Пример #7
0
def test_func_zero_args() -> None:
    tokens = "function f zero arguments".split()

    engine = RuleEngine(tokens)

    code = Code()

    assert engine.parse_function(code) == "def f():"
    assert engine.tokens == "".split()
Пример #8
0
def test_if_with_statement() -> None:
    tokens = "if x less than three then set x to three".split()

    engine = RuleEngine(tokens)

    code = Code()
    code.symbols.add_variable_symbol("x")

    assert engine.parse_if(code) == "if x < 3:"
    assert engine.tokens == "set x to three".split()
Пример #9
0
def test_no_defined_func_call() -> None:
    tokens = "return a plus b".split()

    engine = RuleEngine(tokens)

    code = Code()
    code.symbols.add_variable_symbol("a")
    code.symbols.add_variable_symbol("b")

    assert engine.parse_return(code) == "return a + b"
    assert engine.tokens == "".split()
Пример #10
0
def test_set_dot_notation() -> None:
    tokens = "set a to x dot b plus five".split()

    engine = RuleEngine(tokens)

    code = Code()
    code.symbols.add_variable_symbol("a")
    code.symbols.add_variable_symbol("x")

    assert engine.parse_set(code) == "a = x.b + 5"
    assert engine.tokens == "".split()
Пример #11
0
def test_if_basic() -> None:
    tokens = "if x less than y then".split()

    engine = RuleEngine(tokens)

    code = Code()
    code.symbols.add_variable_symbol("x")
    code.symbols.add_variable_symbol("y")

    assert engine.parse_if(code) == "if x < y:"
    assert engine.tokens == []
Пример #12
0
def test_if_with_else() -> None:
    tokens = "if x greater than or equal to three then set x to three else set x to zero".split(
    )

    engine = RuleEngine(tokens)

    code = Code()
    code.symbols.add_variable_symbol("x")

    assert engine.parse_if(code) == "if x >= 3:"
    assert engine.tokens == "set x to three else set x to zero".split()
Пример #13
0
def test_set_basic() -> None:
    tokens = "set a to b".split()

    engine = RuleEngine(tokens)

    code = Code()
    code.symbols.add_variable_symbol("a")
    code.symbols.add_variable_symbol("b")

    assert engine.parse_set(code) == "a = b"
    assert engine.tokens == "".split()
Пример #14
0
def test_hard_call() -> None:
    func_tokens = "function f one argument file name return one".split()

    engine = RuleEngine(func_tokens)

    code = Code()
    code = engine.parse(code)
    tokens = "call f file path".split()
    engine.add_tokens(tokens)

    assert engine.parse_call(code) == "f(file_path)"
    assert engine.tokens == "".split()
Пример #15
0
def test_call_with_multi_words4() -> None:
    func_tokens = "function read file one argument file name return one".split()

    engine = RuleEngine(func_tokens)

    code = Code()
    code = engine.parse(code)

    tokens = "call read file file path".split()
    engine.add_tokens(tokens)

    assert engine.parse_call(code) == "read_file(file_path)"
    assert engine.tokens == "".split()
Пример #16
0
def test_goto_three() -> None:
    tokens = "function y two argument x and y".split()
    rule_engine = RuleEngine(tokens)

    code = Code([])

    for i in range(20, 30):
        tokens = f"set x to {i}".split()

        rule_engine.add_tokens(tokens)

    tokens = "goto line three".split()
    rule_engine.add_tokens(tokens)
    code = rule_engine.parse(code)
    print(code.print_lines())
    assert len(code.lines) == 11
    assert code.cursor_position == 3
Пример #17
0
def test_goto_beginning() -> None:
    tokens = "function y two argument x and y".split()
    rule_engine = RuleEngine(tokens)

    code = Code([])

    for i in range(10, 20):
        tokens = f"set x to {i}".split()

        rule_engine.add_tokens(tokens)

    tokens = "goto line beginning".split()
    rule_engine.add_tokens(tokens)
    code = rule_engine.parse(code)

    assert len(code.lines) == 11
    assert code.cursor_position == 1
Пример #18
0
def test_delete_line() -> None:
    tokens = "function y two argument x and y".split()
    rule_engine = RuleEngine(tokens)

    code = Code([])

    for i in range(10):
        tokens = f"set x to {i}".split()

        rule_engine.add_tokens(tokens)

    tokens = "delete line three".split()
    rule_engine.add_tokens(tokens)
    code = rule_engine.parse(code)

    assert len(code.lines) == 10
    assert "1" not in code.print_lines()
Пример #19
0
def test_basic_call_with_digits() -> None:
    func_tokens = "function f two arguments a and b return a plus b".split()

    engine = RuleEngine(func_tokens)

    code = Code()
    code = engine.parse(code)
    code.symbols.add_variable_symbol("a")
    code.symbols.add_variable_symbol("b")
    tokens = "call f two four".split()
    engine.add_tokens(tokens)

    assert engine.parse_call(code) == "f(2, 4)"
    assert engine.tokens == "".split()
Пример #20
0
def test_call_with_multi_words2() -> None:
    func_tokens = (
        "function read file two arguments file name and length return length".split()
    )

    engine = RuleEngine(func_tokens)

    code = Code()
    code = engine.parse(code)
    code.symbols.add_variable_symbol("file_path")

    tokens = "call read file foo dot txt twelve".split()
    engine.add_tokens(tokens)

    assert engine.parse_call(code) == "read_file('foo.txt', 12)"
    assert engine.tokens == "".split()
Пример #21
0
def test_goto_end() -> None:
    tokens = "function y two argument x and y".split()
    rule_engine = RuleEngine(tokens)

    code = Code([])

    for i in range(10):
        tokens = f"set x to {i}".split()

        rule_engine.add_tokens(tokens)

    tokens = "goto line end".split()
    rule_engine.add_tokens(tokens)

    print(code.print_lines())
    new_code = rule_engine.parse(code)
    print(new_code.print_lines())

    print(rule_engine.tokens)
    assert len(new_code.lines) == 11
    assert new_code.cursor_position == 11