diff --git a/Cargo.toml b/Cargo.toml index f93f2b5..ae64f9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ license = "GPL-3.0-only" exclude = ["res/", "./flake*", "*.nix", ".envrc", "_config.yml"] description = "Solver for ADFs grounded semantics by utilising OBDDs - ordered binary decision diagrams" +build = "build.rs" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -21,4 +22,3 @@ lexical-sort = "0.3.1" [dev-dependencies] test-log = "0.2.*" -test-generator = "0.3.0" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..7bc6994 --- /dev/null +++ b/build.rs @@ -0,0 +1,66 @@ +//! Buildscript to automatically generate test-cases +//! Idea found at https://blog.cyplo.dev/posts/2018/12/generate-rust-tests-from-data +use std::env; +use std::fs::read_dir; +use std::fs::DirEntry; +use std::fs::File; +use std::io::Write; +use std::path::Path; + +// build script's entry point +fn main() { + gen_tests(); +} + +fn gen_tests() { + let out_dir = env::var("OUT_DIR").unwrap(); + let destination = Path::new(&out_dir).join("tests.rs"); + let mut test_file = File::create(&destination).unwrap(); + + if let Ok(test_data_directory) = read_dir("./res/adf-instances/instances/") { + // write test file header, put `use`, `const` etc there + write_header(&mut test_file); + + for file in test_data_directory { + write_test(&mut test_file, &file.unwrap()); + } + } +} + +fn write_test(test_file: &mut File, file: &DirEntry) { + let file = file.path().canonicalize().unwrap(); + let path = file.display(); + let test_name = format!("{}", file.file_name().unwrap().to_string_lossy()) + .replace(".", "_") + .replace("-", "_") + .to_lowercase(); + let grounded_name = format!( + "{}-grounded.txt", + file.as_path().as_os_str().to_string_lossy() + ) + .replace( + "adf-instances/instances", + "adf-instances/grounded-interpretations", + ); + + write!( + test_file, + include_str!("./tests/test_template"), + name = test_name, + path = path, + grounded = grounded_name + ) + .unwrap(); +} + +fn write_header(test_file: &mut File) { + write!( + test_file, + r#" +use adf_bdd::adf::Adf; +use adf_bdd::parser::AdfParser; +use test_log::test; +"# + ) + .unwrap(); +} diff --git a/src/parser.rs b/src/parser.rs index ec84e1a..1f679a7 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -325,9 +325,6 @@ impl AdfParser<'_> { #[cfg(test)] mod test { - - use clap::ErrorKind; - use super::*; #[test] diff --git a/tests/automated.rs b/tests/automated.rs deleted file mode 100644 index c62092c..0000000 --- a/tests/automated.rs +++ /dev/null @@ -1,26 +0,0 @@ -use adf_bdd::{adf::Adf, parser::AdfParser}; -use test_generator::test_resources; -use test_log::test; - -//#[test_resources("res/adf-instances/instances/*.adf")] -fn compute_grounded(resource: &str) { - let grounded = &[ - "res/adf-instances/grounded-interpretations/", - &resource[28..resource.len() - 8], - ".apx.adf-grounded.txt", - ] - .concat(); - log::debug!("Grounded: {}", grounded); - let parser = AdfParser::default(); - let expected_result = std::fs::read_to_string(grounded); - assert!(expected_result.is_ok()); - let input = std::fs::read_to_string(resource).unwrap(); - parser.parse()(&input).unwrap(); - parser.varsort_alphanum(); - let mut adf = Adf::from_parser(&parser); - let grounded = adf.grounded(); - assert_eq!( - format!("{}", adf.print_interpretation(&grounded)), - expected_result.unwrap() - ); -} diff --git a/tests/build.rs b/tests/build.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/tests/build.rs @@ -0,0 +1 @@ + diff --git a/tests/test_loader.rs b/tests/test_loader.rs new file mode 100644 index 0000000..0cec68c --- /dev/null +++ b/tests/test_loader.rs @@ -0,0 +1,2 @@ +// automatically include the autogenerated tests +include!(concat!(env!("OUT_DIR"), "/tests.rs")); diff --git a/tests/test_template b/tests/test_template new file mode 100644 index 0000000..44efced --- /dev/null +++ b/tests/test_template @@ -0,0 +1,20 @@ +#[test] +fn {name}() {{ + let resource = "{path}"; + log::debug!("resource: {{}}", resource); + let grounded = "{grounded}"; + log::debug!("Grounded: {{}}", grounded); + let parser = AdfParser::default(); + let expected_result = std::fs::read_to_string(grounded); + assert!(expected_result.is_ok()); + let input = std::fs::read_to_string(resource).unwrap(); + parser.parse()(&input).unwrap(); + parser.varsort_alphanum(); + let mut adf = Adf::from_parser(&parser); + let grounded = adf.grounded(); + assert_eq!( + format!("{{}}", adf.print_interpretation(&grounded)), + expected_result.unwrap() + ); +}} +