mirror of
https://github.com/HigherOrderCO/Bend.git
synced 2024-08-16 15:20:40 +03:00
Implement a golden testing model for tests
This commit is contained in:
parent
7731d2487c
commit
1a1d331c35
29
Cargo.lock
generated
29
Cargo.lock
generated
@ -206,6 +206,7 @@ dependencies = [
|
||||
"logos",
|
||||
"pretty_assertions",
|
||||
"shrinkwraprs",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -313,6 +314,15 @@ version = "0.6.29"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
|
||||
|
||||
[[package]]
|
||||
name = "same-file"
|
||||
version = "1.0.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
|
||||
dependencies = [
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shrinkwraprs"
|
||||
version = "0.3.0"
|
||||
@ -391,6 +401,16 @@ version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||
|
||||
[[package]]
|
||||
name = "walkdir"
|
||||
version = "2.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "36df944cda56c7d8d8b7496af378e6b16de9284591917d307c9b4d313c44e698"
|
||||
dependencies = [
|
||||
"same-file",
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
@ -407,6 +427,15 @@ version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-util"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
|
@ -14,3 +14,4 @@ shrinkwraprs = "0.3.0"
|
||||
|
||||
[dev-dependencies]
|
||||
pretty_assertions = "1.4.0"
|
||||
walkdir = "2.3.3"
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::{ast::DefinitionBook, parser::parse_definition_book};
|
||||
use ariadne::{Color, Label, Report, ReportKind, Source};
|
||||
use chumsky::prelude::Rich;
|
||||
use std::path::Path;
|
||||
|
||||
/// Reads a file and parses to a definition book.
|
||||
@ -8,18 +9,23 @@ pub fn load_file_to_book(path: &Path) -> anyhow::Result<DefinitionBook> {
|
||||
match parse_definition_book(&code) {
|
||||
Ok(book) => Ok(book),
|
||||
Err(errs) => {
|
||||
for err in errs {
|
||||
Report::build(ReportKind::Error, (), err.span().start)
|
||||
.with_code(3)
|
||||
.with_message(err.to_string())
|
||||
.with_label(
|
||||
Label::new(err.span().into_range()).with_message(err.reason().to_string()).with_color(Color::Red),
|
||||
)
|
||||
.finish()
|
||||
.eprint(Source::from(code.clone()))
|
||||
.unwrap();
|
||||
}
|
||||
print_err_reports(&path.to_string_lossy(), &code, errs);
|
||||
Err(anyhow::anyhow!("Parsing error"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_err_reports<'a, T: std::fmt::Display>(path: &str, code: &str, errs: Vec<Rich<T>>) {
|
||||
errs.into_iter().for_each(|err| {
|
||||
Report::build(ReportKind::Error, path, err.span().start)
|
||||
.with_message(err.to_string())
|
||||
.with_label(
|
||||
Label::new((path, err.span().into_range()))
|
||||
.with_message(err.reason().to_string())
|
||||
.with_color(Color::Red),
|
||||
)
|
||||
.finish()
|
||||
.eprint((path, Source::from(code.to_string())))
|
||||
.unwrap()
|
||||
})
|
||||
}
|
||||
|
42
tests/golden_tests.rs
Normal file
42
tests/golden_tests.rs
Normal file
@ -0,0 +1,42 @@
|
||||
use hvm_lang::{loader::print_err_reports, parser::parse_term, to_core::term_to_hvm_core};
|
||||
use pretty_assertions::assert_eq;
|
||||
use std::{collections::HashMap, fs, io::Write, path::Path};
|
||||
use walkdir::WalkDir;
|
||||
|
||||
fn run_single_golden_test(path: &Path, run: &dyn Fn(&Path, &str) -> anyhow::Result<String>) -> anyhow::Result<()> {
|
||||
let code = fs::read_to_string(path)?;
|
||||
let result = run(path, &code)?;
|
||||
let golden_path = path.with_extension("golden");
|
||||
if let Ok(to_check) = fs::read_to_string(&golden_path) {
|
||||
assert_eq!(result, to_check, "Testing file '{}'", path.display());
|
||||
Ok(())
|
||||
} else {
|
||||
let mut file = fs::File::create(golden_path)?;
|
||||
file.write_all(result.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn run_golden_test_dir(root: &Path, run: &dyn Fn(&Path, &str) -> anyhow::Result<String>) {
|
||||
for entry in WalkDir::new(root).follow_links(true) {
|
||||
let entry = entry.unwrap();
|
||||
let path = entry.path();
|
||||
if path.is_file() && path.extension().map(|x| x == "hvm").unwrap_or(false) {
|
||||
eprintln!("running {}", path.display());
|
||||
run_single_golden_test(path, run).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_terms() {
|
||||
let root = format!("{}/tests/golden_tests/single_terms", env!("CARGO_MANIFEST_DIR"));
|
||||
run_golden_test_dir(Path::new(&root), &|path, code| {
|
||||
let term = parse_term(code).map_err(|errs| {
|
||||
print_err_reports(&path.to_string_lossy(), code, errs);
|
||||
anyhow::anyhow!("Parsing error")
|
||||
})?;
|
||||
let net = term_to_hvm_core(&term, &HashMap::new())?;
|
||||
Ok(net.to_string())
|
||||
})
|
||||
}
|
1
tests/golden_tests/single_terms/church_one.hvm
Normal file
1
tests/golden_tests/single_terms/church_one.hvm
Normal file
@ -0,0 +1 @@
|
||||
(λk λs λz dup s0 s1 = s; (s0 ((k s1) z))) (λq λw w)
|
1
tests/golden_tests/single_terms/church_zero.golden
Normal file
1
tests/golden_tests/single_terms/church_zero.golden
Normal file
@ -0,0 +1 @@
|
||||
$ (0 * (0 x0 x0))
|
1
tests/golden_tests/single_terms/church_zero.hvm
Normal file
1
tests/golden_tests/single_terms/church_zero.hvm
Normal file
@ -0,0 +1 @@
|
||||
λs λz z
|
1
tests/golden_tests/single_terms/dup_apply.golden
Normal file
1
tests/golden_tests/single_terms/dup_apply.golden
Normal file
@ -0,0 +1 @@
|
||||
$ (0 (1 (0 x0 x1) x0) x1)
|
1
tests/golden_tests/single_terms/dup_apply.hvm
Normal file
1
tests/golden_tests/single_terms/dup_apply.hvm
Normal file
@ -0,0 +1 @@
|
||||
λa dup a1 a2 = a; (a1 a2)
|
1
tests/golden_tests/single_terms/erased_dup.golden
Normal file
1
tests/golden_tests/single_terms/erased_dup.golden
Normal file
@ -0,0 +1 @@
|
||||
$ (0 (1 * x0) x0)
|
1
tests/golden_tests/single_terms/erased_dup.hvm
Normal file
1
tests/golden_tests/single_terms/erased_dup.hvm
Normal file
@ -0,0 +1 @@
|
||||
λa dup a1 a2 = a; a2
|
1
tests/golden_tests/single_terms/id.golden
Normal file
1
tests/golden_tests/single_terms/id.golden
Normal file
@ -0,0 +1 @@
|
||||
$ (0 x0 x0)
|
1
tests/golden_tests/single_terms/id.hvm
Normal file
1
tests/golden_tests/single_terms/id.hvm
Normal file
@ -0,0 +1 @@
|
||||
λa a
|
Loading…
Reference in New Issue
Block a user