From d97d71948ebadebe87341649eeb4aae69c746ae1 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 16 Aug 2024 12:53:12 +0200 Subject: Fix document set rules (#4768) --- tests/src/custom.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 tests/src/custom.rs (limited to 'tests/src/custom.rs') diff --git a/tests/src/custom.rs b/tests/src/custom.rs new file mode 100644 index 00000000..9a5fef03 --- /dev/null +++ b/tests/src/custom.rs @@ -0,0 +1,46 @@ +use std::fmt::Write; + +use typst::foundations::Smart; +use typst::model::{Document, DocumentInfo}; +use typst::World; + +use crate::collect::Test; +use crate::world::TestWorld; + +/// We don't want to panic when there is a failure. +macro_rules! test_eq { + ($sink:expr, $lhs:expr, $rhs:expr) => { + if $lhs != $rhs { + writeln!(&mut $sink, "{:?} != {:?}", $lhs, $rhs).unwrap(); + } + }; +} + +/// Run special checks for specific tests for which it is not worth it to create +/// custom annotations. +pub fn check(test: &Test, world: &TestWorld, doc: Option<&Document>) -> String { + let mut sink = String::new(); + match test.name.as_str() { + "document-set-author-date" => { + let info = info(doc); + test_eq!(sink, info.author, ["A", "B"]); + test_eq!(sink, info.date, Smart::Custom(world.today(None))); + } + "issue-4065-document-context" => { + let info = info(doc); + test_eq!(sink, info.title.as_deref(), Some("Top level")); + } + "issue-4769-document-context-conditional" => { + let info = info(doc); + test_eq!(sink, info.author, ["Changed"]); + test_eq!(sink, info.title.as_deref(), Some("Alternative")); + } + _ => {} + } + sink +} + +/// Extract the document information. +fn info(doc: Option<&Document>) -> DocumentInfo { + doc.map(|doc| doc.info.clone()).unwrap_or_default() +} -- cgit v1.2.3