oxedyne/fe2o3/fe2o3_austenite/tests/pdf_info_eval.rs
2.6 KiB, 5 runs
created by r1870400018:65166, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The Info dictionary of a PDF written through the evaluator carries the document's metadata, as Typst |
| 2 | //! applies it: from `set document` at the top level and from a template the document is shown through |
| 3 | //! (`show: doc.with(..)`), the engine named as creator and producer. |
| 4 | |
| 5 | use oxedyne_fe2o3_austenite::compile; |
| 6 | use oxedyne_fe2o3_austenite::emit::sinks::PdfSink; |
| 7 | use oxedyne_fe2o3_austenite::flow::text::FontStore; |
| 8 | |
| 9 | use oxedyne_fe2o3_core::prelude::*; |
| 10 | |
| 11 | use std::path::{ |
| 12 | Path, |
| 13 | PathBuf, |
| 14 | }; |
| 15 | |
| 16 | fn pdf_of(name: &str, files: &[(&str, &str)]) -> Outcome<String> { |
| 17 | let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("pdf-info-eval").join(name); |
| 18 | res!(std::fs::create_dir_all(&dir)); |
| 19 | for (f, text) in files { |
| 20 | res!(std::fs::write(dir.join(f), text)); |
| 21 | } |
| 22 | let main: PathBuf = dir.join("main.typ"); |
| 23 | let mut sink = res!(PdfSink::new()); |
| 24 | let done = res!(compile::assemble_eval(&main, &dir, FontStore::default(), &mut sink)); |
| 25 | res!(done.laid.as_ref().map_err(|e| err!("{} did not lay out: {}", name, e.plain(); Test))); |
| 26 | let out = res!(sink.output().ok_or_else(|| err!("no PDF"; Test))); |
| 27 | Ok(String::from_utf8_lossy(&out.to_vec()).to_string()) |
| 28 | } |
| 29 | |
| 30 | #[test] |
| 31 | fn a_top_level_set_document_reaches_the_info_dictionary() -> Outcome<()> { |
| 32 | let pdf = res!(pdf_of("top", &[("main.typ", |
| 33 | "#set document(title: [A Title], author: (\"Ann\", \"Bob\"), description: [About], keywords: (\"k1\", \"k2\"))\nBody.\n")])); |
| 34 | assert!(pdf.contains("/Title (A Title)"), "the title: {}", pdf); |
| 35 | assert!(pdf.contains("/Author (Ann, Bob)"), "the authors, joined"); |
| 36 | assert!(pdf.contains("/Subject (About)"), "the description is the subject"); |
| 37 | assert!(pdf.contains("/Keywords (k1, k2)"), "the keywords, joined"); |
| 38 | assert!(pdf.contains("/Creator (Austenite)") && pdf.contains("/Producer (Austenite)"), "the engine named, never a version"); |
| 39 | Ok(()) |
| 40 | } |
| 41 | |
| 42 | #[test] |
| 43 | fn a_set_document_inside_a_template_function_reaches_the_info_dictionary() -> Outcome<()> { |
| 44 | let pdf = res!(pdf_of("template", &[ |
| 45 | ("t.typ", "#let doc(title: none, body) = {\n set document(title: title, author: \"Templated\")\n body\n}\n"), |
| 46 | ("main.typ", "#import \"t.typ\": doc\n#show: doc.with(title: [From Template])\nBody.\n"), |
| 47 | ])); |
| 48 | assert!(pdf.contains("/Title (From Template)"), "the title set through doc.with: {}", pdf); |
| 49 | assert!(pdf.contains("/Author (Templated)"), "the author the template sets"); |
| 50 | Ok(()) |
| 51 | } |
| 52 | |
| 53 | #[test] |
| 54 | fn a_document_with_no_metadata_names_only_the_engine() -> Outcome<()> { |
| 55 | let pdf = res!(pdf_of("none", &[("main.typ", "Body.\n")])); |
| 56 | assert!(!pdf.contains("/Title") && !pdf.contains("/Author"), "no field is invented"); |
| 57 | assert!(pdf.contains("/Creator (Austenite)"), "the creator is the engine"); |
| 58 | Ok(()) |
| 59 | } |