Add some imports tests

This commit is contained in:
LunaAmora 2024-06-11 10:34:24 -03:00
parent d1072da0a9
commit 7ae5886eed
24 changed files with 158 additions and 1 deletions

View File

@ -541,6 +541,12 @@ pub struct DefaultLoader {
pub loaded: HashSet<Name>,
}
impl DefaultLoader {
pub fn new(local_path: PathBuf) -> Self {
Self { local_path, loaded: HashSet::new() }
}
}
impl PackageLoader for DefaultLoader {
fn load(&mut self, name: Name, _import_type: &ImportType, pkgs: &mut Sources) -> Result<Vec<Name>, String> {
if !self.is_loaded(&name) {

View File

@ -5,6 +5,8 @@ use bend::{
load_book::do_parse_book_default, net_to_term::net_to_term, term_to_net::Labels, Book, Ctx, Name, Term,
},
hvm::hvm_book_show_pretty,
imports::DefaultLoader,
load_to_book,
net::hvm_to_net::hvm_to_net,
run_book, AdtEncoding, CompileOpts, RunOpts,
};
@ -69,7 +71,7 @@ fn run_golden_test_dir_multiple(test_name: &str, run: &[&RunFn]) {
test_name.rsplit_once(':').unwrap().1
));
let walker = WalkDir::new(&root).sort_by_file_name().max_depth(2).into_iter().filter_entry(|e| {
let walker = WalkDir::new(&root).sort_by_file_name().max_depth(1).into_iter().filter_entry(|e| {
let path = e.path();
path == root || path.is_dir() || (path.is_file() && path.extension().is_some_and(|x| x == "bend"))
});
@ -186,6 +188,30 @@ fn run_file() {
)
}
#[test]
fn import_system() {
run_golden_test_dir_multiple(
function_name!(),
&[(&|code, path| {
let _guard = RUN_MUTEX.lock().unwrap();
let diagnostics_cfg = DiagnosticsConfig {
unused_definition: Severity::Allow,
..DiagnosticsConfig::new(Severity::Error, true)
};
let book = load_to_book(path.display(), code, DefaultLoader::new(path.to_path_buf()), diagnostics_cfg)?;
let run_opts = RunOpts::default();
let mut res = String::new();
let compile_opts = CompileOpts::default();
let (term, _, diags) = run_book_simple(book, run_opts, compile_opts, diagnostics_cfg, None)?;
res.push_str(&format!("{diags}{term}\n\n"));
Ok(res)
})],
)
}
#[test]
#[ignore = "while lazy execution is not implemented for hvm32"]
fn run_lazy() {

View File

@ -0,0 +1,20 @@
use lib/types{Bool, MyTree}
use lib/bool_xor
def tree_xor(tree):
fold tree:
case MyTree/node:
return bool_xor(tree.lft, tree.rgt);
case MyTree/leaf:
return tree.val;
main =
let depth = 10
let tree = bend n = 0 {
when (< n depth):
(MyTree/node (fork (+ n 1)) (fork (+ n 1)))
else:
if (% n 2) { (MyTree/leaf Bool/True) } else { (MyTree/leaf Bool/False) }
}
(tree_xor tree)

View File

@ -0,0 +1,5 @@
use lib/nums{one, two}
use lib/defs{sum}
def main():
return sum(one, two)

View File

@ -0,0 +1,5 @@
use lib/nums{one as One, two as Two}
use lib/defs{sum as summation}
def main():
return summation(One, Two)

View File

@ -0,0 +1,4 @@
use lib/nums{one as A, two as A}
def main():
return A + A

View File

@ -0,0 +1,7 @@
use lib/a/b{C}
use lib/a{b/C}
lib/a/b/C = *
def main():
return *

View File

@ -0,0 +1,5 @@
use lib/folder/myFun
use lib/myFun
def main():
return myFun

View File

@ -0,0 +1,5 @@
use lib/myFun
use lib/folder/myFun
def main():
return myFun(lib/myFun/myFun) # use the full name to call a shadowed imported def

View File

@ -0,0 +1 @@
type b = C

View File

@ -0,0 +1 @@
C = *

View File

@ -0,0 +1,5 @@
use types{Bool}
(bool_xor Bool/True Bool/False) = Bool/True
(bool_xor Bool/False Bool/True) = Bool/True
(bool_xor * *) = Bool/False

View File

@ -0,0 +1,5 @@
def sum(a, b):
return a + b
def minus(a, b):
return a - b

View File

@ -0,0 +1,2 @@
def myFun(a):
return a

View File

@ -0,0 +1,8 @@
type Type = (A) | (B)
def myFun(a):
match a:
case Type/A:
return 1
case Type/B:
return 2

View File

@ -0,0 +1,7 @@
def one():
return 1
two = 2
def three():
return 3

View File

@ -0,0 +1,5 @@
type Bool:
True
False
type MyTree = (node ~lft ~rgt) | (leaf val)

View File

@ -0,0 +1,5 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/import_system/import_types.bend
---
lib/types/Bool/False

View File

@ -0,0 +1,5 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/import_system/imports.bend
---
3

View File

@ -0,0 +1,5 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/import_system/imports_alias.bend
---
3

View File

@ -0,0 +1,6 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/import_system/imports_alias_shadow.bend
---
Errors:
The import 'lib/nums/two' shadows the imported name 'lib/nums/one'

View File

@ -0,0 +1,7 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/import_system/imports_conflict.bend
---
Errors:
The imported definition 'lib/a/b/C' conflicts with the definition 'lib/a/b/C'.
The imported constructor 'lib/a/b/C' conflicts with the definition 'lib/a/b/C'.

View File

@ -0,0 +1,6 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/import_system/imports_shadow.bend
---
Errors:
The import 'lib/myFun/myFun' shadows the imported name 'lib/folder/myFun/myFun'

View File

@ -0,0 +1,6 @@
---
source: tests/golden_tests.rs
input_file: tests/golden_tests/import_system/imports_shadow2.bend
---
Errors:
The import 'lib/folder/myFun/myFun' shadows the imported name 'lib/myFun/myFun'