Files
FiscusPublic/crates/fiscus-statement/tests/fixtures.rs
T

63 lines
2.5 KiB
Rust

//! Mandatory test 2: every real sample in `fixtures/<bank>/` parses to exactly its `.expected.json`, and
//! every truncation of it is an error or a prefix of whole lines.
//!
//! A sample is any file in `fixtures/<bank>/` other than `*.expected.json` and `README*`. Day 1 has no
//! samples, so this test walks an empty tree; it starts to bite the moment a sample is added (and fails until
//! that bank has a parser and the expected rows).
use fiscus_statement::{parse, Statement};
use std::path::{Path, PathBuf};
fn samples() -> Vec<PathBuf> {
let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../fixtures");
let mut out = Vec::new();
let Ok(banks) = std::fs::read_dir(&root) else { return out };
for bank in banks.flatten().filter(|e| e.path().is_dir()) {
for f in std::fs::read_dir(bank.path()).unwrap().flatten() {
let name = f.file_name().to_string_lossy().to_string();
if f.path().is_file() && !name.ends_with(".expected.json") && !name.starts_with("README") {
out.push(f.path());
}
}
}
out.sort();
out
}
fn expected_for(sample: &Path) -> PathBuf {
let name = sample.file_name().unwrap().to_string_lossy();
sample.with_file_name(format!("{name}.expected.json"))
}
#[test]
fn every_fixture_parses_to_its_expected_rows() {
let all = samples();
eprintln!("statement fixtures: {}", all.len());
for sample in all {
let bytes = std::fs::read(&sample).unwrap();
let want: Statement = serde_json::from_slice(
&std::fs::read(expected_for(&sample)).unwrap_or_else(|_| panic!("{} has no .expected.json", sample.display())),
)
.unwrap();
let got = parse(&bytes).unwrap_or_else(|e| panic!("{}: {e}", sample.display()));
assert_eq!(got, want, "{}", sample.display());
}
}
#[test]
fn every_truncated_fixture_is_an_error_or_whole_lines() {
for sample in samples() {
let bytes = std::fs::read(&sample).unwrap();
let full = parse(&bytes).unwrap();
let step = (bytes.len() / 2000).max(1);
for cut in (0..bytes.len()).step_by(step) {
if let Ok(st) = parse(&bytes[..cut]) {
assert!(st.lines.len() <= full.lines.len(), "{} cut at {cut}", sample.display());
for (got, want) in st.lines.iter().zip(&full.lines) {
assert_eq!(got, want, "{} cut at {cut}: a partial line", sample.display());
}
}
}
}
}