BlueGit

timfa posprint

Files

posprint / tests / escpos_golden.rs

escpos_golden.rs

escpos_golden.rs 1.6 KiB Download Raw
1//! Golden tests: the ESC/POS output must stay byte-identical to what master
2//! produced before TSPL support was added. Goldens in tests/golden/escpos/*.bin
3//! were generated from unmodified master (regenerate only deliberately with
4//! POSPRINT_REGEN_GOLDEN=1).
5
6use std::collections::HashMap;
7use std::fs;
8use std::path::Path;
9
10use posprint::io::PosFile;
11use posprint::pipeline::encode_pos_file;
12
13#[test]
14fn escpos_output_matches_master_goldens() {
15 let dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/golden/escpos");
16 let params: HashMap<String, String> =
17 serde_json::from_str(&fs::read_to_string(dir.join("params.json")).unwrap()).unwrap();
18 let regen = std::env::var_os("POSPRINT_REGEN_GOLDEN").is_some();
19 let mut checked = 0;
20 let mut entries: Vec<_> = fs::read_dir(&dir).unwrap().flatten().collect();
21 entries.sort_by_key(|e| e.path());
22 for entry in entries {
23 let path = entry.path();
24 if path.extension().and_then(|e| e.to_str()) != Some("pos") {
25 continue;
26 }
27 let file = PosFile::load_from_path(&path).unwrap();
28 let bytes = encode_pos_file(&file, Some(&dir), &params).unwrap();
29 let golden = path.with_extension("bin");
30 if regen {
31 fs::write(&golden, &bytes).unwrap();
32 }
33 let expected = fs::read(&golden).unwrap_or_else(|_| panic!("missing {}", golden.display()));
34 assert!(bytes == expected, "ESC/POS output changed for {}", path.display());
35 checked += 1;
36 }
37 assert!(checked >= 6, "expected golden corpus, checked {checked}");
38}