mirror of
https://github.com/HigherOrderCO/Bend.git
synced 2024-10-05 15:57:08 +03:00
Add some imports tests
This commit is contained in:
parent
d1072da0a9
commit
7ae5886eed
@ -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) {
|
||||
|
@ -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() {
|
||||
|
20
tests/golden_tests/import_system/import_types.bend
Normal file
20
tests/golden_tests/import_system/import_types.bend
Normal 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)
|
||||
|
5
tests/golden_tests/import_system/imports.bend
Normal file
5
tests/golden_tests/import_system/imports.bend
Normal file
@ -0,0 +1,5 @@
|
||||
use lib/nums{one, two}
|
||||
use lib/defs{sum}
|
||||
|
||||
def main():
|
||||
return sum(one, two)
|
5
tests/golden_tests/import_system/imports_alias.bend
Normal file
5
tests/golden_tests/import_system/imports_alias.bend
Normal 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)
|
@ -0,0 +1,4 @@
|
||||
use lib/nums{one as A, two as A}
|
||||
|
||||
def main():
|
||||
return A + A
|
7
tests/golden_tests/import_system/imports_conflict.bend
Normal file
7
tests/golden_tests/import_system/imports_conflict.bend
Normal file
@ -0,0 +1,7 @@
|
||||
use lib/a/b{C}
|
||||
use lib/a{b/C}
|
||||
|
||||
lib/a/b/C = *
|
||||
|
||||
def main():
|
||||
return *
|
5
tests/golden_tests/import_system/imports_shadow.bend
Normal file
5
tests/golden_tests/import_system/imports_shadow.bend
Normal file
@ -0,0 +1,5 @@
|
||||
use lib/folder/myFun
|
||||
use lib/myFun
|
||||
|
||||
def main():
|
||||
return myFun
|
5
tests/golden_tests/import_system/imports_shadow2.bend
Normal file
5
tests/golden_tests/import_system/imports_shadow2.bend
Normal 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
|
1
tests/golden_tests/import_system/lib/a.bend
Normal file
1
tests/golden_tests/import_system/lib/a.bend
Normal file
@ -0,0 +1 @@
|
||||
type b = C
|
1
tests/golden_tests/import_system/lib/a/b.bend
Normal file
1
tests/golden_tests/import_system/lib/a/b.bend
Normal file
@ -0,0 +1 @@
|
||||
C = *
|
5
tests/golden_tests/import_system/lib/bool_xor.bend
Normal file
5
tests/golden_tests/import_system/lib/bool_xor.bend
Normal 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
|
5
tests/golden_tests/import_system/lib/defs.bend
Normal file
5
tests/golden_tests/import_system/lib/defs.bend
Normal file
@ -0,0 +1,5 @@
|
||||
def sum(a, b):
|
||||
return a + b
|
||||
|
||||
def minus(a, b):
|
||||
return a - b
|
2
tests/golden_tests/import_system/lib/folder/myFun.bend
Normal file
2
tests/golden_tests/import_system/lib/folder/myFun.bend
Normal file
@ -0,0 +1,2 @@
|
||||
def myFun(a):
|
||||
return a
|
8
tests/golden_tests/import_system/lib/myFun.bend
Normal file
8
tests/golden_tests/import_system/lib/myFun.bend
Normal file
@ -0,0 +1,8 @@
|
||||
type Type = (A) | (B)
|
||||
|
||||
def myFun(a):
|
||||
match a:
|
||||
case Type/A:
|
||||
return 1
|
||||
case Type/B:
|
||||
return 2
|
7
tests/golden_tests/import_system/lib/nums.bend
Normal file
7
tests/golden_tests/import_system/lib/nums.bend
Normal file
@ -0,0 +1,7 @@
|
||||
def one():
|
||||
return 1
|
||||
|
||||
two = 2
|
||||
|
||||
def three():
|
||||
return 3
|
5
tests/golden_tests/import_system/lib/types.bend
Normal file
5
tests/golden_tests/import_system/lib/types.bend
Normal file
@ -0,0 +1,5 @@
|
||||
type Bool:
|
||||
True
|
||||
False
|
||||
|
||||
type MyTree = (node ~lft ~rgt) | (leaf val)
|
5
tests/snapshots/import_system__import_types.bend.snap
Normal file
5
tests/snapshots/import_system__import_types.bend.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/import_system/import_types.bend
|
||||
---
|
||||
lib/types/Bool/False
|
5
tests/snapshots/import_system__imports.bend.snap
Normal file
5
tests/snapshots/import_system__imports.bend.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/import_system/imports.bend
|
||||
---
|
||||
3
|
5
tests/snapshots/import_system__imports_alias.bend.snap
Normal file
5
tests/snapshots/import_system__imports_alias.bend.snap
Normal file
@ -0,0 +1,5 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/import_system/imports_alias.bend
|
||||
---
|
||||
3
|
@ -0,0 +1,6 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/import_system/imports_alias_shadow.bend
|
||||
---
|
||||
[4m[1m[31mErrors:[0m
|
||||
The import 'lib/nums/two' shadows the imported name 'lib/nums/one'
|
@ -0,0 +1,7 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/import_system/imports_conflict.bend
|
||||
---
|
||||
[4m[1m[31mErrors:[0m
|
||||
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'.
|
6
tests/snapshots/import_system__imports_shadow.bend.snap
Normal file
6
tests/snapshots/import_system__imports_shadow.bend.snap
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/import_system/imports_shadow.bend
|
||||
---
|
||||
[4m[1m[31mErrors:[0m
|
||||
The import 'lib/myFun/myFun' shadows the imported name 'lib/folder/myFun/myFun'
|
6
tests/snapshots/import_system__imports_shadow2.bend.snap
Normal file
6
tests/snapshots/import_system__imports_shadow2.bend.snap
Normal file
@ -0,0 +1,6 @@
|
||||
---
|
||||
source: tests/golden_tests.rs
|
||||
input_file: tests/golden_tests/import_system/imports_shadow2.bend
|
||||
---
|
||||
[4m[1m[31mErrors:[0m
|
||||
The import 'lib/folder/myFun/myFun' shadows the imported name 'lib/myFun/myFun'
|
Loading…
Reference in New Issue
Block a user