Skip to content

Commit

Permalink
feat: Manually test imports
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake committed Jan 19, 2025
1 parent 445bacf commit ac65ee8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 24 deletions.
29 changes: 9 additions & 20 deletions compiler/test/runner.re
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ let stdlibfile = name =>
Filepath.to_string(Fp.At.(test_stdlib_dir / (name ++ ".gr")));
let runtimefile = name =>
Filepath.to_string(Fp.At.(test_runtime_dir / (name ++ ".gr")));
let watfile = name =>
Filepath.to_string(Fp.At.(test_output_dir / (name ++ ".wat")));
let wasmfile = name =>
Filepath.to_string(Fp.At.(test_output_dir / (name ++ ".wasm")));
let mashfile = name =>
Expand Down Expand Up @@ -220,27 +218,18 @@ let lsp = stdin_input => {
let module_header = "module Test; ";

let makeSnapshotRunner =
(
~config_fn=?,
~wasm=false,
test,
~module_header=module_header,
name,
prog,
) => {
let hook = if (wasm) {stop_after_assembled} else {stop_after_object_emitted};
let out_name =
if (wasm) {
watfile(name);
} else {
mashfile(name);
};
(~config_fn=?, test, ~module_header=module_header, name, prog) => {
test(name, ({expect}) => {
Config.preserve_all_configs(() => {
Config.sexp_locs_enabled := false;
Config.wat := wasm;
ignore @@ compile(~hook, ~config_fn?, name, module_header ++ prog);
expect.file(out_name).toMatchSnapshot();
ignore @@
compile(
~hook=stop_after_object_emitted,
~config_fn?,
name,
module_header ++ prog,
);
expect.file(mashfile(name)).toMatchSnapshot();
})
});
};
Expand Down
37 changes: 33 additions & 4 deletions compiler/test/suites/includes.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe("includes", ({test, testSkip}) => {
Sys.backend_type == Other("js_of_ocaml") ? testSkip : test;

let assertSnapshot = makeSnapshotRunner(test);
let assertWasmSnapshot = makeSnapshotRunner(test, ~wasm=true);
let assertCompileError = makeCompileErrorRunner(test);
let assertRun = makeRunner(test_or_skip);
let assertFileRun = makeFileRunner(test_or_skip);
Expand Down Expand Up @@ -209,7 +208,37 @@ describe("includes", ({test, testSkip}) => {
"{\n x: 1\n}\n",
);
/* Duplicate imports */
assertWasmSnapshot("duplicate_imports", {|
print("test")
|});
test("dedupe_includes", ({expect}) => {
let name = "dedupe_includes";
let outfile = wasmfile(name);
ignore @@
compile(
~hook=Grain.Compile.stop_after_assembled,
name,
{|
module DeDupeIncludes
// Ensures fd_write is only included once
print("test")
|},
);
let ic = open_in_bin(outfile);
let sections = Grain_utils.Wasm_utils.get_wasm_sections(ic);
close_in(ic);
let import_section =
List.find_map(
(sec: Grain_utils.Wasm_utils.wasm_bin_section) =>
switch (sec) {
| {sec_type: Import(imports)} => Some(imports)
| _ => None
},
sections,
);
expect.option(import_section).toBeSome();
expect.int(List.length(Option.get(import_section))).toBe(1);
expect.list(Option.get(import_section)).toContainEqual((
WasmFunction,
"wasi_snapshot_preview1",
"fd_write",
));
});
});

0 comments on commit ac65ee8

Please sign in to comment.