Hook the integration tests into rust's test framework.

Instead of using a hacked-together shell script, use rust's test framework to do the comparison.
This commit is contained in:
Tom Alexander 2023-04-18 22:38:18 -04:00
parent fba80f4f7b
commit 72c8da94a3
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
6 changed files with 96 additions and 1 deletions

View File

@ -5,6 +5,10 @@ edition = "2021"
license = "0BSD" license = "0BSD"
default-run = "toy" default-run = "toy"
[lib]
name = "organic"
path = "src/lib.rs"
[[bin]] [[bin]]
name = "toy" name = "toy"
path = "src/main.rs" path = "src/main.rs"
@ -23,6 +27,9 @@ tracing = "0.1.37"
tracing-opentelemetry = "0.17.2" tracing-opentelemetry = "0.17.2"
tracing-subscriber = {version="0.3.16", features=["env-filter"]} tracing-subscriber = {version="0.3.16", features=["env-filter"]}
[build-dependencies]
walkdir = "2.3.3"
[features] [features]
default = ["compare"] default = ["compare"]
compare = [] compare = []

View File

@ -20,7 +20,11 @@ clean:
.PHONY: test .PHONY: test
test: test:
> cargo test --bin toy > cargo test --lib
.PHONY: integrationtest
integrationtest:
> cargo test --no-fail-fast --test test_loader
.PHONY: run .PHONY: run
run: run:

58
build.rs Normal file
View File

@ -0,0 +1,58 @@
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use walkdir::WalkDir;
fn main() {
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();
write_header(&mut test_file);
let test_files = WalkDir::new("org_mode_samples")
.into_iter()
.filter(|e| match e {
Ok(dir_entry) => {
dir_entry.file_type().is_file()
&& Path::new(dir_entry.file_name())
.extension()
.map(|ext| ext.to_ascii_lowercase() == "org")
.unwrap_or(false)
}
Err(_) => true,
}).collect::<Result<Vec<_>, _>>().unwrap();
for test in test_files {
write_test(&mut test_file, &test);
}
}
fn write_test(test_file: &mut File, test: &walkdir::DirEntry) {
let test_name = test.path().strip_prefix("org_mode_samples/").expect("Paths should be under org_mode_samples/").to_string_lossy().to_lowercase().strip_suffix(".org").expect("Should have .org extension").replace("/", "_");
write!(
test_file,
include_str!("./tests/test_template"),
name = test_name,
path = test.path().display()
)
.unwrap();
}
fn write_header(test_file: &mut File) {
write!(
test_file,
r#"
#[feature(exit_status_error)]
use organic::compare_document;
use organic::document;
use organic::emacs_parse_org_document;
use organic::sexp;
"#
)
.unwrap();
}

8
src/lib.rs Normal file
View File

@ -0,0 +1,8 @@
#![feature(round_char_boundary)]
#![feature(exit_status_error)]
mod parser;
mod compare;
pub use parser::*;
pub use compare::emacs_parse_org_document;
pub use compare::sexp;
pub use compare::compare_document;

1
tests/test_loader.rs Normal file
View File

@ -0,0 +1 @@
include!(concat!(env!("OUT_DIR"), "/tests.rs"));

17
tests/test_template Normal file
View File

@ -0,0 +1,17 @@
#[test]
fn {name}() {{
let todo_org_path = "{path}";
let org_contents = std::fs::read_to_string(todo_org_path).expect("Read org file.");
let org_sexp = emacs_parse_org_document(todo_org_path).expect("Use emacs to parse org file.");
println!("{{}}", org_sexp);
let (_remaining, parsed_sexp) = sexp(org_sexp.as_str()).expect("Sexp Parse failure");
let (remaining, rust_parsed) = document(org_contents.as_str()).expect("Org Parse failure");
println!("{{:#?}}", rust_parsed);
let diff_result =
compare_document(&parsed_sexp, &rust_parsed).expect("Compare parsed documents.");
diff_result
.print()
.expect("Print document parse tree diff.");
assert!(!diff_result.is_bad());
assert_eq!(remaining, "");
}}