Exemple #1
0
def test_string_statement():
    src = """
    // The string constant 'returns itself' when executed, 
    // when this is not printed or assigned to a variable nothing happens.
    func main() { "String type"; }
    """
    run_source(src)
Exemple #2
0
def test_init_array():
    src = """
    func main() {
        var a: array[100];
    }
    """
    run_source(src)
Exemple #3
0
def test_invalid_type():
    src = """
    func test(a: number) { }
    func main() { test("String type"); }
    """
    with pytest.raises(interpreter.InvalidTypeException):
        run_source(src)
Exemple #4
0
def test_assign_void_var():
    src = """
    func main() {
        var a: void = 3;
    }
    """
    with pytest.raises(interpreter.IllegalTypeException):
        run_source(src)
Exemple #5
0
def test_string_literal_typing():
    src = """
    func main() {
        var a: number = "1\";
    }
    """
    with pytest.raises(interpreter.InvalidTypeException):
        run_source(src)
Exemple #6
0
def run_capture_stdout(src: str):
    captured_output = ""

    def stdout_cap(x):
        nonlocal captured_output
        captured_output = captured_output + x

    run_source(src, stdout=stdout_cap)
    return captured_output
Exemple #7
0
def test_str_index_out_of_range():
    src = """
    func main() {
        var a: string = "Hello World";
        println(a[50]);
    }
    """
    with pytest.raises(interpreter.IndexOutOfBoundsException):
        run_source(src)
Exemple #8
0
def test_invalid_implicit_return():
    src = """
    func main() {
        var a = 1;
        a;
        return a;
    }
    """
    with pytest.raises(interpreter.InvalidImplicitReturnException):
        run_source(src)
Exemple #9
0
def test_duplicate_func():
    src = """
    func a(n: number) { }
    func a(n: number) { }

    func main() {
        a(4);
    }
    """
    with pytest.raises(interpreter.SmickelRuntimeException):
        run_source(src)
Exemple #10
0
def test_invalid_implicit_string_return():
    src = """
    func hello(): string {
        "Hello";
        "World";
    }

    func main() {
        println(hello());
    }
    """
    with pytest.raises(interpreter.InvalidImplicitReturnException):
        run_source(src)
Exemple #11
0
def test_invalid_implicit_func_return():
    src = """
    func odd(n: number): bool {
        if(n == 0) { false; }
        even(n - 1);
        even(n - 1);
    }

    func even(n: number): bool {
    if (n == 0) { true; }
        // We even support rust-like implicit returns.
        odd(n - 1)
    }

    func main() {
        odd(5);
    }
    """
    with pytest.raises(interpreter.InvalidImplicitReturnException):
        run_source(src)
Exemple #12
0
def test_var_assignment():
    src = "func main() { var a = 1; }"
    run_source(src)
Exemple #13
0
def test_comparison_statement():
    src = """
    // Or conditions.
    func main() { true == false; }
    """
    run_source(src)
Exemple #14
0
def test_undefined_variable():
    src = "func main() { println(a); }"
    with pytest.raises(interpreter.UndefinedVariableException):
        run_source(src)
Exemple #15
0
def test_main_void():
    src = "func main(): void { }"
    run_source(src)
Exemple #16
0
def test_main_number_no_return():
    src = "func main(): number { }"
    with pytest.raises(interpreter.InvalidTypeException):
        run_source(src)
Exemple #17
0
def test_return_type():
    src = 'func main(): string { return "Hello"; }'
    run_source(src)
Exemple #18
0
def test_invalid_return_type():
    src = 'func main(): number { return "Hello"; }'
    with pytest.raises(interpreter.InvalidTypeException):
        run_source(src)
Exemple #19
0
def test_arithmetic_statement():
    src = """
    // Or arithmetic statements.
    func main() { 2 + 4; }
    """
    run_source(src)
Exemple #20
0
def test_number_statement():
    src = """
    // The same is true for numbers.
    func main() { 1; }
    """
    run_source(src)
Exemple #21
0
def test_bool_statement():
    src = """
    // Or bools.
    func main() { true; }
    """
    run_source(src)
Exemple #22
0
def test_return_bool_var():
    src = """
    func main() { var a: bool = true; return a; }
    """
    run_source(src)
Exemple #23
0
def test_return_bool_literal():
    src = """
    func main() { return true; }
    """
    run_source(src)
Exemple #24
0
def test_exit_code():
    src = "func main() { return 1; }"
    res = run_source(src)
    assert res == 1
Exemple #25
0
def test_exit_value():
    src = 'func main() { return "Hello"; }'
    res = run_source(src)
    assert res == "Hello"
Exemple #26
0
def test_empty():
    src = ""
    with pytest.raises(interpreter.EntrypointNotFoundException):
        run_source(src)
Exemple #27
0
def test_var_assignment_arithmetic():
    src = "func main() { var a = 1 + 4; }"
    run_source(src)