mirror of
https://github.com/ellmau/adf-obdd.git
synced 2025-12-19 09:29:36 +01:00
Implemented automated Test generation (#7)
Generates tests, based on the existence of test-instances in the `res/`-folder
This commit is contained in:
parent
9c9e85e107
commit
21fdb2cd76
@ -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"
|
||||
|
||||
66
build.rs
Normal file
66
build.rs
Normal file
@ -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();
|
||||
}
|
||||
@ -325,9 +325,6 @@ impl AdfParser<'_> {
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
||||
use clap::ErrorKind;
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
|
||||
@ -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()
|
||||
);
|
||||
}
|
||||
1
tests/build.rs
Normal file
1
tests/build.rs
Normal file
@ -0,0 +1 @@
|
||||
|
||||
2
tests/test_loader.rs
Normal file
2
tests/test_loader.rs
Normal file
@ -0,0 +1,2 @@
|
||||
// automatically include the autogenerated tests
|
||||
include!(concat!(env!("OUT_DIR"), "/tests.rs"));
|
||||
20
tests/test_template
Normal file
20
tests/test_template
Normal file
@ -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()
|
||||
);
|
||||
}}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user