Skip to content

Commit

Permalink
test: generate test functions (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
pinbraerts authored Feb 15, 2025
1 parent 10a3f87 commit 3f9f2ad
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
52 changes: 52 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::env;
use std::fs::{self, File};
use std::io::Write;
use std::path::Path;

fn main() {
let input_path = Path::new("tests/input");
let output_path = Path::new("tests/output");

let var = env::var("OUT_DIR").unwrap();
let out_dir = Path::new(&var);
let convert_path = out_dir.join("convert.rs");
let idempotent_path = out_dir.join("idempotent.rs");

let mut convert = File::create(&convert_path).unwrap();
let mut idempotent = File::create(&idempotent_path).unwrap();

for entry in fs::read_dir(input_path).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if !path.is_file() {
continue;
}
let file_name = path.file_name().unwrap();
let test_name = path.file_stem().unwrap().to_str().unwrap();
let file_name = file_name.to_str().unwrap();
let input_file = input_path.join(file_name).to_str().unwrap().to_owned();
let output_file = output_path.join(file_name).to_str().unwrap().to_owned();
writeln!(
convert,
r#"
#[test]
fn test_{}() {{
compare("{}", "{}", "{}");
}}
"#,
test_name, file_name, input_file, output_file
)
.unwrap();
writeln!(
idempotent,
r#"
#[test]
fn test_{}() {{
compare("{}", "{}");
}}
"#,
test_name, file_name, output_file
)
.unwrap();
}
}
11 changes: 0 additions & 11 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
use std::{
collections::HashMap,
ffi::OsString,
fs::File,
io::{self, Read, Write},
path::PathBuf,
process::Stdio,
thread,
};

pub fn get_json(dirname: impl Into<PathBuf>) -> io::Result<HashMap<OsString, PathBuf>> {
let mut project = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
project.push(dirname.into());
Ok(std::fs::read_dir(project)?
.filter_map(|f| f.ok())
.map(|f| (f.file_name(), f.path().to_owned()))
.collect())
}

pub fn read(path: &PathBuf) -> io::Result<Vec<u8>> {
let mut content = Vec::new();
let mut file = File::open(path)?;
Expand Down
24 changes: 8 additions & 16 deletions tests/convert.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
use std::{ffi::OsString, path::PathBuf};
use std::{path::PathBuf, str::FromStr};
mod common;
use common::{get_json, read, run};
use common::{read, run};

fn compare(name: &OsString, input: &PathBuf, output: &PathBuf) {
fn compare(name: &str, input: &str, output: &str) {
println!("comparing {:?}", name);
let input = read(input).expect("failed to read input");
let expected_output = read(output).expect("failed to read output file");
let input = PathBuf::from_str(input).unwrap();
let output = PathBuf::from_str(output).unwrap();
let input = read(&input).expect("failed to read input");
let expected_output = read(&output).expect("failed to read output file");
let output = run(input).expect("failed to execute a program");
assert!(expected_output == output);
}

#[test]
fn convert() {
let input = get_json("tests/input").expect("failed to read input dir");
assert!(!input.is_empty());
let output = get_json("tests/output").expect("failed to read output dir");
assert!(!output.is_empty());
input
.into_iter()
.filter_map(|i| output.get(&i.0).map(|o| (i.0, i.1, o)))
.for_each(|x| compare(&x.0, &x.1, x.2));
}
include!(concat!(env!("OUT_DIR"), "/convert.rs"));
16 changes: 6 additions & 10 deletions tests/idempotent.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
use std::{ffi::OsString, path::PathBuf};
use std::{path::PathBuf, str::FromStr};
mod common;
use common::{get_json, read, run};
use common::{read, run};

fn compare(name: &OsString, input: &PathBuf) {
fn compare(name: &str, input: &str) {
println!("comparing {:?}", name);
let input = read(input).expect("failed to read input");
let input = PathBuf::from_str(input).unwrap();
let input = read(&input).expect("failed to read input");
let output = run(input.clone()).expect("failed to execute a program");
assert!(input == output);
}

#[test]
fn idempotent() {
let input = get_json("tests/output").expect("failed to read input dir");
assert!(!input.is_empty());
input.into_iter().for_each(|x| compare(&x.0, &x.1));
}
include!(concat!(env!("OUT_DIR"), "/idempotent.rs"));

0 comments on commit 3f9f2ad

Please sign in to comment.