BlueGit

timfa posprint

Tests: ESC/POS golden corpus generated from unmodified master

timfa Ada · pushed by timfa

Authored 2026-10-30 · 2 weeks ago

Hash
750185341cf659ac48796683b812468d66144c22
Parent
400789b4
15 files changed +93 −0
tests/escpos_golden.rs added +38 −0
@@ -0,0 +1,38 @@
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
6 use std::collections::HashMap;
7 use std::fs;
8 use std::path::Path;
9
10 use posprint::io::PosFile;
11 use posprint::pipeline::encode_pos_file;
12
13 #[test]
14 fn 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 }
@@ -0,0 +1,38 @@
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}
tests/golden/escpos/barcodes.bin added Binary file +0 −0

The contents of this binary file are not shown.

tests/golden/escpos/barcodes.pos added +8 −0
@@ -0,0 +1,8 @@
1 {
2 "version": 1,
3 "metadata": {
4 "title": "barcodes",
5 "page_width_mm": 80.0
6 },
7 "body": "@barcode CODE128 ${Sku}\n@barcode EAN13 590123412345 height=80\n@barcode RASTER CODE128 ${Sku}\n@barcode RASTER CODE93 ABC-1234\n:center:\n@barcode CODE39 HELLO\n@cut\n"
8 }
@@ -0,0 +1,8 @@
1{
2 "version": 1,
3 "metadata": {
4 "title": "barcodes",
5 "page_width_mm": 80.0
6 },
7 "body": "@barcode CODE128 ${Sku}\n@barcode EAN13 590123412345 height=80\n@barcode RASTER CODE128 ${Sku}\n@barcode RASTER CODE93 ABC-1234\n:center:\n@barcode CODE39 HELLO\n@cut\n"
8}
tests/golden/escpos/custom-cols.bin added Binary file +0 −0

The contents of this binary file are not shown.

tests/golden/escpos/custom-cols.pos added +9 −0
@@ -0,0 +1,9 @@
1 {
2 "version": 1,
3 "metadata": {
4 "title": "custom-cols",
5 "page_width_mm": 53.0,
6 "chars_per_line": 32
7 },
8 "body": "Custom chars per line set to thirty-two columns wrapping text\n---\n"
9 }
@@ -0,0 +1,9 @@
1{
2 "version": 1,
3 "metadata": {
4 "title": "custom-cols",
5 "page_width_mm": 53.0,
6 "chars_per_line": 32
7 },
8 "body": "Custom chars per line set to thirty-two columns wrapping text\n---\n"
9}
tests/golden/escpos/image.bin added Binary file +0 −0

The contents of this binary file are not shown.

tests/golden/escpos/image.pos added +8 −0
@@ -0,0 +1,8 @@
1 {
2 "version": 1,
3 "metadata": {
4 "title": "image",
5 "page_width_mm": 58.0
6 },
7 "body": "@image logo.png\n:center:\n@image logo.png width=50%\n@image logo.png width=100\n@cut\n"
8 }
@@ -0,0 +1,8 @@
1{
2 "version": 1,
3 "metadata": {
4 "title": "image",
5 "page_width_mm": 58.0
6 },
7 "body": "@image logo.png\n:center:\n@image logo.png width=50%\n@image logo.png width=100\n@cut\n"
8}
tests/golden/escpos/logo.png added Binary file +0 −0

The contents of this binary file are not shown.

tests/golden/escpos/params.json added +6 −0
@@ -0,0 +1,6 @@
1 {
2 "StoreName": "Ada & Co",
3 "Amount": "12.34",
4 "Paid": "true",
5 "Sku": "SKU-0042"
6 }
@@ -0,0 +1,6 @@
1{
2 "StoreName": "Ada & Co",
3 "Amount": "12.34",
4 "Paid": "true",
5 "Sku": "SKU-0042"
6}
tests/golden/escpos/qr.bin added Binary file +0 −0

The contents of this binary file are not shown.

tests/golden/escpos/qr.pos added +8 −0
@@ -0,0 +1,8 @@
1 {
2 "version": 1,
3 "metadata": {
4 "title": "qr",
5 "page_width_mm": 80.0
6 },
7 "body": "@qr https://example.com/r/${Sku}\n@qr hello size=6 ecc=H\n@qr RASTER https://example.com version=5 scale=4 ecc=M\n@cut\n"
8 }
@@ -0,0 +1,8 @@
1{
2 "version": 1,
3 "metadata": {
4 "title": "qr",
5 "page_width_mm": 80.0
6 },
7 "body": "@qr https://example.com/r/${Sku}\n@qr hello size=6 ecc=H\n@qr RASTER https://example.com version=5 scale=4 ecc=M\n@cut\n"
8}
tests/golden/escpos/receipt-58mm.bin added Binary file +0 −0

The contents of this binary file are not shown.

tests/golden/escpos/receipt-58mm.pos added +8 −0
@@ -0,0 +1,8 @@
1 {
2 "version": 1,
3 "metadata": {
4 "title": "receipt-58mm",
5 "page_width_mm": 58.0
6 },
7 "body": "# A heading that is long enough to wrap on narrow paper\nItem 12.00\n rightish note\n@feed 2\n@cut\n"
8 }
@@ -0,0 +1,8 @@
1{
2 "version": 1,
3 "metadata": {
4 "title": "receipt-58mm",
5 "page_width_mm": 58.0
6 },
7 "body": "# A heading that is long enough to wrap on narrow paper\nItem 12.00\n rightish note\n@feed 2\n@cut\n"
8}
tests/golden/escpos/receipt-80mm.bin added Binary file +0 −0

The contents of this binary file are not shown.

tests/golden/escpos/receipt-80mm.pos added +8 −0
@@ -0,0 +1,8 @@
1 {
2 "version": 1,
3 "metadata": {
4 "title": "receipt-80mm",
5 "page_width_mm": 80.0
6 },
7 "body": "# Store ${StoreName}\n## Receipt\n:center:\nThank you!\n:right:\nTotal: ${Amount}\n:left: Apple:right: 1.20\n\n- Groceries\n - Milk with a very long description that wraps over lines\n- Wash car\n$[Paid] Paid\n[] Pending\n[x] Done\n---\nUnicode: caf\u00e9 \u20ac \u2610\n@feed 3\n@cut\n"
8 }
@@ -0,0 +1,8 @@
1{
2 "version": 1,
3 "metadata": {
4 "title": "receipt-80mm",
5 "page_width_mm": 80.0
6 },
7 "body": "# Store ${StoreName}\n## Receipt\n:center:\nThank you!\n:right:\nTotal: ${Amount}\n:left: Apple:right: 1.20\n\n- Groceries\n - Milk with a very long description that wraps over lines\n- Wash car\n$[Paid] Paid\n[] Pending\n[x] Done\n---\nUnicode: caf\u00e9 \u20ac \u2610\n@feed 3\n@cut\n"
8}