summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ref/document-set-after-content.pngbin0 -> 245 bytes
-rw-r--r--tests/src/custom.rs46
-rw-r--r--tests/src/run.rs13
-rw-r--r--tests/src/tests.rs1
-rw-r--r--tests/suite/model/document.typ28
5 files changed, 83 insertions, 5 deletions
diff --git a/tests/ref/document-set-after-content.png b/tests/ref/document-set-after-content.png
new file mode 100644
index 00000000..37e13773
--- /dev/null
+++ b/tests/ref/document-set-after-content.png
Binary files differ
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()
+}
diff --git a/tests/src/run.rs b/tests/src/run.rs
index 9681ae4c..c65f5e38 100644
--- a/tests/src/run.rs
+++ b/tests/src/run.rs
@@ -89,6 +89,7 @@ impl<'a> Runner<'a> {
log!(self, "no document, but also no errors");
}
+ self.check_custom(doc.as_ref());
self.check_document(doc.as_ref());
for error in &errors {
@@ -129,6 +130,18 @@ impl<'a> Runner<'a> {
}
}
+ /// Run custom checks for which it is not worth to create special
+ /// annotations.
+ fn check_custom(&mut self, doc: Option<&Document>) {
+ let errors = crate::custom::check(self.test, &self.world, doc);
+ if !errors.is_empty() {
+ log!(self, "custom check failed");
+ for line in errors.lines() {
+ log!(self, " {line}");
+ }
+ }
+ }
+
/// Check that the document output is correct.
fn check_document(&mut self, document: Option<&Document>) {
let live_path = format!("{}/render/{}.png", crate::STORE_PATH, self.test.name);
diff --git a/tests/src/tests.rs b/tests/src/tests.rs
index 3e29c0ff..77c8b210 100644
--- a/tests/src/tests.rs
+++ b/tests/src/tests.rs
@@ -2,6 +2,7 @@
mod args;
mod collect;
+mod custom;
mod logger;
mod run;
mod world;
diff --git a/tests/suite/model/document.typ b/tests/suite/model/document.typ
index 6f8e7131..2ed199fe 100644
--- a/tests/suite/model/document.typ
+++ b/tests/suite/model/document.typ
@@ -1,12 +1,10 @@
// Test document and page-level styles.
--- document-set-title ---
-// This is okay.
#set document(title: [Hello])
What's up?
--- document-set-author-date ---
-// This, too.
#set document(author: ("A", "B"), date: datetime.today())
--- document-date-bad ---
@@ -14,15 +12,14 @@ What's up?
#set document(date: "today")
--- document-author-bad ---
-// This, too.
// Error: 23-29 expected string, found integer
#set document(author: (123,))
What's up?
--- document-set-after-content ---
+// Document set rules can appear anywhere in top-level realization, also after
+// content.
Hello
-
-// Error: 2-30 document set rules must appear before any content
#set document(title: [Hello])
--- document-constructor ---
@@ -34,3 +31,24 @@ Hello
// Error: 4-32 document set rules are not allowed inside of containers
#set document(title: [Hello])
]
+
+--- issue-4065-document-context ---
+// Test that we can set document properties based on context.
+#show: body => context {
+ let all = query(heading)
+ let title = if all.len() > 0 { all.first().body }
+ set document(title: title)
+ body
+}
+
+#show heading: none
+= Top level
+
+--- issue-4769-document-context-conditional ---
+// Test that document set rule can be conditional on document information
+// itself.
+#set document(author: "Normal", title: "Alternative")
+#context {
+ set document(author: "Changed") if "Normal" in document.author
+ set document(title: "Changed") if document.title == "Normal"
+}