Skip to content

Commit

Permalink
Merge pull request #251 from HigherOrderCO/feature/sc-512/add-tests-f…
Browse files Browse the repository at this point in the history
…or-file-builtin-functions

[sc-512] Add tests for file builtin functions
  • Loading branch information
imaqtkatt authored Apr 3, 2024
2 parents a8209d8 + 3635d6d commit 6e587fd
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use hvmc::{ast, host::Host, stdlib::LogDef};
use crate::{
readback_hvmc,
term::{
builtins::{SCONS, SNIL},
builtins::{RESULT_ERR, RESULT_OK, SCONS, SNIL},
term_to_net::Labels,
AdtEncoding, Book, Term,
},
Expand All @@ -27,7 +27,8 @@ pub mod util;
pub const CORE_BUILTINS: [&str; 7] =
["HVM.log", "HVM.black_box", "HVM.print", "HVM.query", "HVM.store", "HVM.load", "HVM.exit"];
/// List of definition names used by the core builtins
pub const CORE_BUILTINS_USES: [&[&str]; 7] = [&[], &[], &[], &[SCONS, SNIL], &[], &[], &[]];
pub const CORE_BUILTINS_USES: [&[&str]; 7] =
[&[], &[], &[], &[SCONS, SNIL], &[RESULT_OK, RESULT_ERR], &[RESULT_OK, RESULT_ERR], &[]];

/// Creates a host with the hvm-core primitive definitions built-in.
/// This needs the book as an Arc because the closure that logs
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(box_patterns)]
#![feature(let_chains)]

use builtins::create_host;
use builtins::{create_host, CORE_BUILTINS_USES};
use diagnostics::{DiagnosticOrigin, Diagnostics, DiagnosticsConfig, Severity};
use hvmc::{
ast::Net,
Expand Down Expand Up @@ -63,7 +63,10 @@ pub fn compile_book(
diagnostics.fatal(())?;
}
if opts.prune {
core_book.prune(&[book.hvmc_entrypoint().to_string()]);
let mut prune_entrypoints = vec![book.hvmc_entrypoint().to_string()];
let mut builtin_uses = CORE_BUILTINS_USES.concat().iter().map(|x| x.to_string()).collect::<Vec<_>>();
prune_entrypoints.append(&mut builtin_uses);
core_book.prune(&prune_entrypoints);
}
mutual_recursion::check_cycles(&core_book, &mut diagnostics)?;

Expand Down
24 changes: 24 additions & 0 deletions tests/golden_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,3 +357,27 @@ fn mutual_recursion() {
Ok(format!("{}{}", res.diagnostics, res.core_book))
})
}

#[test]
fn io() {
run_golden_test_dir_multiple(function_name!(), &[
(&|code, path| {
let book = do_parse_book(code, path)?;

let mut desugar_opts = CompileOpts::light();
desugar_opts.lazy_mode();

// 1 million nodes for the test runtime. Smaller doesn't seem to make it any faster
let (res, info) =
run_book(book, None, RunOpts::lazy(), desugar_opts, DiagnosticsConfig::default(), None)?;
Ok(format!("Lazy mode:\n{}{}", info.diagnostics, res))
}),
(&|code, path| {
let book = do_parse_book(code, path)?;

let (res, info) =
run_book(book, None, RunOpts::default(), CompileOpts::light(), DiagnosticsConfig::default(), None)?;
Ok(format!("Strict mode:\n{}{}", info.diagnostics, res))
}),
])
}
6 changes: 6 additions & 0 deletions tests/golden_tests/io/load.hvm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(Main) =
use path = "tests/golden_tests/io/load.txt"
(HVM.load path @result match result {
Result.ok: result.val;
Result.err: result.val;
})
1 change: 1 addition & 0 deletions tests/golden_tests/io/load.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Contents
6 changes: 6 additions & 0 deletions tests/golden_tests/io/load_fail.hvm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(Main) =
use path = "tests/golden_tests/io/load_fail.txt"
(HVM.load path @result match result {
Result.ok: result.val;
Result.err: result.val;
})
6 changes: 6 additions & 0 deletions tests/golden_tests/io/store.hvm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(Main) =
use path = "tests/golden_tests/io/store.txt"
(HVM.store path "(Main) = 0" @result match result {
Result.ok: result.val;
Result.err: result.val;
})
1 change: 1 addition & 0 deletions tests/golden_tests/io/store.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(Main) = 0
6 changes: 6 additions & 0 deletions tests/golden_tests/io/store_fail.hvm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(Main) =
use path = "tests/golden_tests/io/missing_dir/store_fail.txt"
(HVM.store path "(Main) = 0" @result match result {
Result.ok: result.val;
Result.err: result.val;
})
8 changes: 8 additions & 0 deletions tests/snapshots/io__load.hvm.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/io/load.hvm
---
Lazy mode:
"Contents\n"
Strict mode:
"Contents\n"
8 changes: 8 additions & 0 deletions tests/snapshots/io__load_fail.hvm.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/io/load_fail.hvm
---
Lazy mode:
"Filesystem error: No such file or directory (os error 2)"
Strict mode:
"Filesystem error: No such file or directory (os error 2)"
8 changes: 8 additions & 0 deletions tests/snapshots/io__store.hvm.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/io/store.hvm
---
Lazy mode:
*
Strict mode:
*
8 changes: 8 additions & 0 deletions tests/snapshots/io__store_fail.hvm.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/io/store_fail.hvm
---
Lazy mode:
"Filesystem error: No such file or directory (os error 2)"
Strict mode:
"Filesystem error: No such file or directory (os error 2)"

0 comments on commit 6e587fd

Please sign in to comment.