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)
def test_init_array(): src = """ func main() { var a: array[100]; } """ run_source(src)
def test_invalid_type(): src = """ func test(a: number) { } func main() { test("String type"); } """ with pytest.raises(interpreter.InvalidTypeException): run_source(src)
def test_assign_void_var(): src = """ func main() { var a: void = 3; } """ with pytest.raises(interpreter.IllegalTypeException): run_source(src)
def test_string_literal_typing(): src = """ func main() { var a: number = "1\"; } """ with pytest.raises(interpreter.InvalidTypeException): run_source(src)
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
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)
def test_invalid_implicit_return(): src = """ func main() { var a = 1; a; return a; } """ with pytest.raises(interpreter.InvalidImplicitReturnException): run_source(src)
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)
def test_invalid_implicit_string_return(): src = """ func hello(): string { "Hello"; "World"; } func main() { println(hello()); } """ with pytest.raises(interpreter.InvalidImplicitReturnException): run_source(src)
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)
def test_var_assignment(): src = "func main() { var a = 1; }" run_source(src)
def test_comparison_statement(): src = """ // Or conditions. func main() { true == false; } """ run_source(src)
def test_undefined_variable(): src = "func main() { println(a); }" with pytest.raises(interpreter.UndefinedVariableException): run_source(src)
def test_main_void(): src = "func main(): void { }" run_source(src)
def test_main_number_no_return(): src = "func main(): number { }" with pytest.raises(interpreter.InvalidTypeException): run_source(src)
def test_return_type(): src = 'func main(): string { return "Hello"; }' run_source(src)
def test_invalid_return_type(): src = 'func main(): number { return "Hello"; }' with pytest.raises(interpreter.InvalidTypeException): run_source(src)
def test_arithmetic_statement(): src = """ // Or arithmetic statements. func main() { 2 + 4; } """ run_source(src)
def test_number_statement(): src = """ // The same is true for numbers. func main() { 1; } """ run_source(src)
def test_bool_statement(): src = """ // Or bools. func main() { true; } """ run_source(src)
def test_return_bool_var(): src = """ func main() { var a: bool = true; return a; } """ run_source(src)
def test_return_bool_literal(): src = """ func main() { return true; } """ run_source(src)
def test_exit_code(): src = "func main() { return 1; }" res = run_source(src) assert res == 1
def test_exit_value(): src = 'func main() { return "Hello"; }' res = run_source(src) assert res == "Hello"
def test_empty(): src = "" with pytest.raises(interpreter.EntrypointNotFoundException): run_source(src)
def test_var_assignment_arithmetic(): src = "func main() { var a = 1 + 4; }" run_source(src)