spooky/test/test_spooky.ml

130 lines
2.8 KiB
OCaml
Raw Permalink Normal View History

2026-04-29 15:25:15 +00:00
let valid_program =
{|
struct Item {
int value;
};
2026-04-29 18:56:41 +00:00
fn fold(xs: int[]) -> int {
let mut total: int = 0;
2026-04-29 15:25:15 +00:00
foreach (int x in xs) {
total = total + x;
}
return total;
}
2026-04-29 18:56:41 +00:00
fn main() -> int {
let xs: int[];
let it: Item;
let mut y = 2 + 3 * 4;
2026-04-29 15:25:15 +00:00
it.value = y;
if (y >= 0) {
y = fold(xs);
} else {
y = 0;
}
return y;
}
|}
let invalid_program =
{|
2026-04-29 18:56:41 +00:00
fn main() -> int {
let flag = true;
let mut x = 1;
2026-04-29 15:25:15 +00:00
x = flag;
return x;
}
|}
2026-04-29 18:56:41 +00:00
let combo_valid_program =
{|
fn get_value() -> int#int {
let x = 1;
return x;
}
fn do_work(v: int#int) -> int {
return 0;
}
fn main() -> int {
let x = get_value();
return do_work(x);
}
|}
let combo_invalid_program =
{|
fn get_value() -> int {
return 1;
}
fn get_comptime() -> #int {
return 2;
}
fn do_work(v: int#int) -> int {
return 0;
}
fn main() -> int {
let x = get_value();
let y = get_comptime();
return do_work(x);
}
|}
2026-04-29 15:25:15 +00:00
let test_valid_program () =
match Spooky.parse_and_type_check valid_program with
| Ok _ -> ()
| Error msg -> failwith ("expected valid program, got: " ^ msg)
let test_invalid_program () =
match Spooky.parse_and_type_check invalid_program with
| Ok _ -> failwith "expected type error, but got success"
| Error _ -> ()
2026-04-29 18:56:41 +00:00
let test_combo_valid_program () =
match Spooky.parse_and_type_check combo_valid_program with
| Ok _ -> ()
| Error msg -> failwith ("expected valid combo program, got: " ^ msg)
let test_combo_invalid_program () =
match Spooky.parse_and_type_check combo_invalid_program with
| Ok _ -> failwith "expected combo type error, but got success"
| Error _ -> ()
2026-04-29 16:15:08 +00:00
let write_file path content =
let oc = open_out_bin path in
Fun.protect ~finally:(fun () -> close_out oc) (fun () -> output_string oc content)
let test_imports () =
let base = Filename.concat (Filename.get_temp_dir_name ()) "spooky_import_test" in
let modules_dir = Filename.concat base "modules" in
Unix.mkdir base 0o755;
Unix.mkdir modules_dir 0o755;
let cleanup () =
(try Sys.remove (Filename.concat modules_dir "math.spooky") with _ -> ());
(try Sys.remove (Filename.concat base "main.spooky") with _ -> ());
(try Unix.rmdir modules_dir with _ -> ());
(try Unix.rmdir base with _ -> ())
in
Fun.protect
~finally:cleanup
(fun () ->
write_file (Filename.concat modules_dir "math.spooky")
2026-04-29 18:56:41 +00:00
"fn add(a: int, b: int) -> int { return a + b; }\n";
2026-04-29 16:15:08 +00:00
write_file (Filename.concat base "main.spooky")
2026-04-29 18:56:41 +00:00
"import \"modules/math.spooky\";\nfn main() -> int { return add(1, 2); }\n";
2026-04-29 16:15:08 +00:00
match Spooky.parse_and_type_check_file (Filename.concat base "main.spooky") with
| Ok _ -> ()
| Error msg -> failwith ("expected valid import program, got: " ^ msg))
2026-04-29 15:25:15 +00:00
let () =
test_valid_program ();
test_invalid_program ();
2026-04-29 18:56:41 +00:00
test_combo_valid_program ();
test_combo_invalid_program ();
2026-04-29 16:15:08 +00:00
test_imports ();
2026-04-29 15:25:15 +00:00
print_endline "All parser/type-check tests passed."