-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from lixitrixi/integration-tests
Here I create an integration testing framework to allow testing the full application workflow against various example problems. This will highlight issues like insufficient reduction rules.
- Loading branch information
Showing
11 changed files
with
226 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use convert_case::{Case, Casing}; | ||
use std::{env::var, error::Error, fs::read_dir, fs::write, fs::File, io::Write}; | ||
use walkdir::WalkDir; | ||
|
||
fn main() -> Result<(), Box<dyn Error>> { | ||
let out_dir = var("OUT_DIR")?; | ||
let dest_path = format!("{}/gen_tests.rs", out_dir); | ||
let mut file = File::create(&dest_path)?; | ||
|
||
let test_dir = "tests/integration"; | ||
|
||
for entry in WalkDir::new(test_dir) { | ||
let entry = entry?; | ||
if entry.file_type().is_dir() { | ||
let input_bases = read_dir(entry.path())? | ||
.filter_map(Result::ok) | ||
.filter(|ent| { | ||
ent.file_name() | ||
.into_string() | ||
.unwrap() | ||
.ends_with(".input.json") | ||
}) | ||
.filter_map(|ent| { | ||
ent.path() | ||
.file_name() | ||
.and_then(|stem| stem.to_str()) | ||
.map(|s| s.to_owned().split('.').next().unwrap().to_owned()) | ||
}) | ||
.collect(); | ||
write_tests(&mut file, entry.path().to_str().unwrap(), input_bases); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn write_tests(file: &mut File, dir_path: &str, input_bases: Vec<String>) { | ||
for base in input_bases { | ||
let test_name = format!("{}_{}", dir_path, base); | ||
let test_name = string_to_fn_name(&test_name); | ||
write!( | ||
file, | ||
include_str!("./tests/gen_test_template"), | ||
test_name = test_name, | ||
dir_path = dir_path, | ||
input_base = base | ||
) | ||
.unwrap(); | ||
} | ||
} | ||
|
||
fn string_to_fn_name(s: &str) -> String { | ||
s.to_case(Case::Snake) | ||
.chars() | ||
.map(|c| if c.is_alphanumeric() { c } else { '_' }) | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use antwort::{macros::register_rule, rule::RuleApplicationError, Expr}; | ||
|
||
#[register_rule] | ||
fn example_rule(_expr: &Expr) -> Result<Expr, RuleApplicationError> { | ||
Err(RuleApplicationError::RuleNotApplicable) | ||
} | ||
|
||
#[register_rule] | ||
fn de_morgans1(expr: &Expr) -> Result<Expr, RuleApplicationError> { | ||
if let Expr::Negation(b) = expr { | ||
if let Expr::Disjunction(exprs) = b.as_ref() { | ||
return Ok(Expr::Conjunction( | ||
exprs | ||
.iter() | ||
.map(|e| Expr::Negation(Box::new(e.clone()))) | ||
.collect(), | ||
)); | ||
} | ||
} | ||
Err(RuleApplicationError::RuleNotApplicable) | ||
} | ||
|
||
#[register_rule] | ||
fn de_morgans2(expr: &Expr) -> Result<Expr, RuleApplicationError> { | ||
if let Expr::Negation(b) = expr { | ||
if let Expr::Conjunction(exprs) = b.as_ref() { | ||
return Ok(Expr::Disjunction( | ||
exprs | ||
.iter() | ||
.map(|e| Expr::Negation(Box::new(e.clone()))) | ||
.collect(), | ||
)); | ||
} | ||
} | ||
Err(RuleApplicationError::RuleNotApplicable) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#[test] | ||
fn {test_name}() -> Result<(), Box<dyn Error>> {{ | ||
integration_test("{dir_path}", "{input_base}") | ||
}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use antwort::rule_engine::rewrite; | ||
use serde_json::from_str; | ||
use std::error::Error; | ||
use std::fs::read_to_string; | ||
|
||
include!("../src/_rules/index.rs"); | ||
|
||
fn integration_test(dir_path: &str, input_base: &str) -> Result<(), Box<dyn Error>> { | ||
use antwort::Expr; | ||
|
||
let input_json_path = format!("{}/{}.input.json", dir_path, input_base); | ||
let input_str = read_to_string(&input_json_path)?; | ||
let input_expr: Expr = from_str(&input_str)?; | ||
|
||
assert!( | ||
rewrite(&input_expr).is_cnf(), | ||
"The input expression does not reduce to CNF: {}", | ||
input_json_path, | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
include!(concat!(env!("OUT_DIR"), "/gen_tests.rs")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"Negation": { | ||
"Disjunction": [ | ||
{ | ||
"Variable": "a" | ||
}, | ||
{ | ||
"Variable": "b" | ||
} | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ version = "0.1.0" | |
edition = "2021" | ||
|
||
[dependencies] | ||
serde = { version = "1.0.193", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters