diff --git a/Cargo.toml b/Cargo.toml index 4492c84b1..dba398a5a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ serde = { version = "1", features = ["derive"], optional = true } [dev-dependencies] tiny-skia = "0.2" +walkdir = "2" [profile.dev] opt-level = 2 diff --git a/bench/src/bench.rs b/bench/src/bench.rs index b8093d531..58ecf9fd0 100644 --- a/bench/src/bench.rs +++ b/bench/src/bench.rs @@ -13,7 +13,7 @@ use typst::parse::parse; use typst::typeset; const FONT_DIR: &str = "../fonts"; -const COMA: &str = include_str!("../../tests/typ/example-coma.typ"); +const COMA: &str = include_str!("../../tests/typ/full/coma.typ"); fn benchmarks(c: &mut Criterion) { macro_rules! bench { diff --git a/src/layout/spacing.rs b/src/layout/spacing.rs index f0024fabd..1d6c7f9c2 100644 --- a/src/layout/spacing.rs +++ b/src/layout/spacing.rs @@ -3,7 +3,7 @@ use std::fmt::{self, Debug, Formatter}; use super::*; use crate::eval::Softness; -/// A spacing node. +/// A spacing node. #[derive(Copy, Clone, PartialEq)] pub struct NodeSpacing { /// The amount of spacing to insert. diff --git a/tests/README.md b/tests/README.md index fef134820..429207a5e 100644 --- a/tests/README.md +++ b/tests/README.md @@ -8,3 +8,8 @@ - `res`: Resource files used by tests. - `png`: PNG files produced by tests. - `pdf`: PDF files produced by tests. + +The test files are split into three categories: +- `full`: Tests of full documents. +- `lang`: Tests for specific language features. +- `library`: Tests for specific library functions. diff --git a/tests/ref/basics.png b/tests/ref/basics.png deleted file mode 100644 index fbe85320d..000000000 Binary files a/tests/ref/basics.png and /dev/null differ diff --git a/tests/ref/example-coma.png b/tests/ref/full/coma.png similarity index 100% rename from tests/ref/example-coma.png rename to tests/ref/full/coma.png diff --git a/tests/ref/func-page.png b/tests/ref/func-page.png deleted file mode 100644 index fb1b7b398..000000000 Binary files a/tests/ref/func-page.png and /dev/null differ diff --git a/tests/ref/arrays.png b/tests/ref/lang/arrays.png similarity index 100% rename from tests/ref/arrays.png rename to tests/ref/lang/arrays.png diff --git a/tests/ref/lang/basics.png b/tests/ref/lang/basics.png new file mode 100644 index 000000000..3c3dd7a0c Binary files /dev/null and b/tests/ref/lang/basics.png differ diff --git a/tests/ref/comments.png b/tests/ref/lang/comments.png similarity index 100% rename from tests/ref/comments.png rename to tests/ref/lang/comments.png diff --git a/tests/ref/dictionaries.png b/tests/ref/lang/dictionaries.png similarity index 100% rename from tests/ref/dictionaries.png rename to tests/ref/lang/dictionaries.png diff --git a/tests/ref/empty.png b/tests/ref/lang/empty.png similarity index 100% rename from tests/ref/empty.png rename to tests/ref/lang/empty.png diff --git a/tests/ref/escaping.png b/tests/ref/lang/escaping.png similarity index 100% rename from tests/ref/escaping.png rename to tests/ref/lang/escaping.png diff --git a/tests/ref/headings.png b/tests/ref/lang/headings.png similarity index 100% rename from tests/ref/headings.png rename to tests/ref/lang/headings.png diff --git a/tests/ref/func-font.png b/tests/ref/library/font.png similarity index 100% rename from tests/ref/func-font.png rename to tests/ref/library/font.png diff --git a/tests/ref/func-hv.png b/tests/ref/library/hv.png similarity index 100% rename from tests/ref/func-hv.png rename to tests/ref/library/hv.png diff --git a/tests/ref/func-image.png b/tests/ref/library/image.png similarity index 100% rename from tests/ref/func-image.png rename to tests/ref/library/image.png diff --git a/tests/ref/library/page.png b/tests/ref/library/page.png new file mode 100644 index 000000000..975574372 Binary files /dev/null and b/tests/ref/library/page.png differ diff --git a/tests/ref/func-pagebreak.png b/tests/ref/library/pagebreak.png similarity index 100% rename from tests/ref/func-pagebreak.png rename to tests/ref/library/pagebreak.png diff --git a/tests/ref/func-rgb.png b/tests/ref/library/rgb.png similarity index 100% rename from tests/ref/func-rgb.png rename to tests/ref/library/rgb.png diff --git a/tests/typ/example-coma.typ b/tests/typ/full/coma.typ similarity index 93% rename from tests/typ/example-coma.typ rename to tests/typ/full/coma.typ index 5374af1fa..b7863fce8 100644 --- a/tests/typ/example-coma.typ +++ b/tests/typ/full/coma.typ @@ -1,5 +1,3 @@ -// Test integration of syntax, library and layouting. - [page width: 450pt, height: 300pt, margins: 1cm] [box][ diff --git a/tests/typ/arrays.typ b/tests/typ/lang/arrays.typ similarity index 100% rename from tests/typ/arrays.typ rename to tests/typ/lang/arrays.typ diff --git a/tests/typ/basics.typ b/tests/typ/lang/basics.typ similarity index 68% rename from tests/typ/basics.typ rename to tests/typ/lang/basics.typ index b7630e7fe..9220e3765 100644 --- a/tests/typ/basics.typ +++ b/tests/typ/lang/basics.typ @@ -1,17 +1,9 @@ -// Test text, emph and strong. - Hello 🌏! _Emph_ and *strong*! ---- -// Test non-breaking space. - The non-breaking~space does not work. ---- -// Test backslash. - // Directly after word. Line\ Break diff --git a/tests/typ/comments.typ b/tests/typ/lang/comments.typ similarity index 100% rename from tests/typ/comments.typ rename to tests/typ/lang/comments.typ diff --git a/tests/typ/dictionaries.typ b/tests/typ/lang/dictionaries.typ similarity index 100% rename from tests/typ/dictionaries.typ rename to tests/typ/lang/dictionaries.typ diff --git a/tests/typ/empty.typ b/tests/typ/lang/empty.typ similarity index 100% rename from tests/typ/empty.typ rename to tests/typ/lang/empty.typ diff --git a/tests/typ/escaping.typ b/tests/typ/lang/escaping.typ similarity index 100% rename from tests/typ/escaping.typ rename to tests/typ/lang/escaping.typ diff --git a/tests/typ/headings.typ b/tests/typ/lang/headings.typ similarity index 73% rename from tests/typ/headings.typ rename to tests/typ/lang/headings.typ index 0f364a9bf..f62f6534a 100644 --- a/tests/typ/headings.typ +++ b/tests/typ/lang/headings.typ @@ -1,4 +1,4 @@ -// Number of hashtags. +// Test different numbers of hashtags. # One ### Three @@ -8,7 +8,7 @@ ####### Seven --- -// Heading vs. no heading. +// Test heading vs. no heading. /**/ # Heading {[## Heading]} @@ -24,7 +24,7 @@ Nr#1 #nope --- -// Heading continues over linebreak. +// Heading continuation over linebreak. # This { "works" diff --git a/tests/typ/func-font.typ b/tests/typ/library/font.typ similarity index 95% rename from tests/typ/func-font.typ rename to tests/typ/library/font.typ index 8199ecb43..beaddd4e6 100644 --- a/tests/typ/func-font.typ +++ b/tests/typ/library/font.typ @@ -19,8 +19,22 @@ // Set stretch (not available, matching closest). [font stretch: ultra-condensed][Condensed] +// Error: 1:7-1:12 unexpected argument +[font false] + +// Error: 3:14-3:18 expected font style, found font weight +// Error: 2:28-2:34 expected font weight, found string +// Error: 1:43-1:44 expected font family or array of font families, found integer +[font style: bold, weight: "thin", serif: 0] + +// Warning: 1:15-1:19 must be between 100 and 900 +[font weight: 2700] + +// Error: 1:7-1:27 unexpected argument +[font something: "invalid"] + --- -// Test font fallback. +// Test font fallback and class definitions. // Source Sans Pro + Segoe UI Emoji. Emoji: 🏀 @@ -40,21 +54,3 @@ Emoji: 🏀 [font sans-serif: "Noto Emoji"] [font sans-serif: ("Archivo", sans-serif)] New sans-serif. 🚀 - ---- -// Test error cases. -// Ref: false - -// Error: 1:7-1:12 unexpected argument -[font false] - -// Error: 3:14-3:18 expected font style, found font weight -// Error: 2:28-2:34 expected font weight, found string -// Error: 1:43-1:44 expected font family or array of font families, found integer -[font style: bold, weight: "thin", serif: 0] - -// Warning: 1:15-1:19 must be between 100 and 900 -[font weight: 2700] - -// Error: 1:7-1:27 unexpected argument -[font something: "invalid"] diff --git a/tests/typ/func-hv.typ b/tests/typ/library/hv.typ similarity index 91% rename from tests/typ/func-hv.typ rename to tests/typ/library/hv.typ index b32a645c7..36a6d242d 100644 --- a/tests/typ/func-hv.typ +++ b/tests/typ/library/hv.typ @@ -1,5 +1,3 @@ -// Test the `h` and `v` functions. - // Ends paragraphs. Tightly [v -5pt] packed diff --git a/tests/typ/func-image.typ b/tests/typ/library/image.typ similarity index 95% rename from tests/typ/func-image.typ rename to tests/typ/library/image.typ index 1f94a66ff..2e7b1eb52 100644 --- a/tests/typ/func-image.typ +++ b/tests/typ/library/image.typ @@ -7,6 +7,12 @@ // Load an RGB JPEG image. [image "res/tiger.jpg"] +// Error: 1:8-1:29 failed to load image +[image "path/does/not/exist"] + +// Error: 1:8-1:29 failed to load image +[image "typ/image-error.typ"] + --- // Test configuring the size and fitting behaviour of images. @@ -31,14 +37,3 @@ [align bottom, right][ [image "res/tiger.jpg", width: 60pt] ] - ---- -// Test error cases. -// -// Ref: false - -// Error: 1:8-1:29 failed to load image -[image "path/does/not/exist"] - -// Error: 1:8-1:29 failed to load image -[image "typ/image-error.typ"] diff --git a/tests/typ/func-page.typ b/tests/typ/library/page.typ similarity index 94% rename from tests/typ/func-page.typ rename to tests/typ/library/page.typ index 49671e140..9eeddc93c 100644 --- a/tests/typ/func-page.typ +++ b/tests/typ/library/page.typ @@ -21,8 +21,11 @@ // Ensure that specific margins override general margins. [page margins: 0pt, left: 20pt][Overriden] ---- -// Test flipping. +// Error: 1:7-1:18 unknown variable +[page nonexistant] + +// Error: 1:17-1:20 aligned axis +[page main-dir: ltr] // Flipped predefined paper. [page "a11", flip: true][Flipped A11] @@ -32,6 +35,11 @@ [page flip: true] Wide +// Test changing the layouting directions of pages. + +[page height: 50pt, main-dir: btt, cross-dir: rtl] +Right to left! + --- // Test a combination of pages with bodies and normal content. @@ -44,21 +52,3 @@ Fourth [page][] Sixth [page][Seventh and last] - ---- -// Test changing the layouting directions of pages. - -[page height: 50pt, main-dir: btt, cross-dir: rtl] - -Right to left! - ---- -// Test error cases. -// -// Ref: false - -// Error: 1:7-1:18 unknown variable -[page nonexistant] - -// Error: 1:17-1:20 aligned axis -[page main-dir: ltr] diff --git a/tests/typ/func-pagebreak.typ b/tests/typ/library/pagebreak.typ similarity index 60% rename from tests/typ/func-pagebreak.typ rename to tests/typ/library/pagebreak.typ index 603e11d41..2f539ce00 100644 --- a/tests/typ/func-pagebreak.typ +++ b/tests/typ/library/pagebreak.typ @@ -1,5 +1,3 @@ -// Test trailing pagebreak. - First of two [pagebreak] [page height: 40pt] diff --git a/tests/typ/func-rgb.typ b/tests/typ/library/rgb.typ similarity index 93% rename from tests/typ/func-rgb.typ rename to tests/typ/library/rgb.typ index d29c29409..66d63d0de 100644 --- a/tests/typ/func-rgb.typ +++ b/tests/typ/library/rgb.typ @@ -1,5 +1,3 @@ -// Test the `rgb` function. - // Check the output. [rgb 0.0, 0.3, 0.7] diff --git a/tests/typeset.rs b/tests/typeset.rs index f67649b0c..79a884ebf 100644 --- a/tests/typeset.rs +++ b/tests/typeset.rs @@ -12,6 +12,7 @@ use tiny_skia::{ Rect, SpreadMode, Transform, }; use ttf_parser::OutlineBuilder; +use walkdir::WalkDir; use typst::diag::{Diag, Feedback, Level, Pass}; use typst::env::{Env, ImageResource, ResourceLoader, SharedEnv}; @@ -37,15 +38,15 @@ fn main() { let filter = TestFilter::new(env::args().skip(1)); let mut filtered = Vec::new(); - for entry in fs::read_dir(TYP_DIR).unwrap() { - let src_path = entry.unwrap().path(); + for entry in WalkDir::new(TYP_DIR).into_iter() { + let entry = entry.unwrap(); + let src_path = entry.into_path(); if src_path.extension() != Some(OsStr::new("typ")) { continue; } - let name = src_path.file_stem().unwrap().to_string_lossy().to_string(); - if filter.matches(&name) { - filtered.push((name, src_path)); + if filter.matches(&src_path.to_string_lossy().to_string()) { + filtered.push(src_path); } } @@ -56,9 +57,6 @@ fn main() { println!("Running {} tests", len); } - fs::create_dir_all(PNG_DIR).unwrap(); - fs::create_dir_all(PDF_DIR).unwrap(); - let mut index = FsIndex::new(); index.search_dir(FONT_DIR); @@ -69,9 +67,8 @@ fn main() { })); let playground = Path::new("playground.typ"); - if playground.exists() { + if playground.exists() && filtered.is_empty() { test( - "playground", playground, Path::new("playground.png"), Path::new("playground.pdf"), @@ -81,18 +78,12 @@ fn main() { } let mut ok = true; - for (name, src_path) in filtered { - let png_path = Path::new(PNG_DIR).join(&name).with_extension("png"); - let pdf_path = Path::new(PDF_DIR).join(&name).with_extension("pdf"); - let ref_path = Path::new(REF_DIR).join(&name).with_extension("png"); - ok &= test( - &name, - &src_path, - &png_path, - &pdf_path, - Some(&ref_path), - &env, - ); + for src_path in filtered { + let relative = src_path.strip_prefix(TYP_DIR).unwrap(); + let png_path = Path::new(PNG_DIR).join(&relative).with_extension("png"); + let pdf_path = Path::new(PDF_DIR).join(&relative).with_extension("pdf"); + let ref_path = Path::new(REF_DIR).join(&relative).with_extension("png"); + ok &= test(&src_path, &png_path, &pdf_path, Some(&ref_path), &env); } if !ok { @@ -131,14 +122,14 @@ impl TestFilter { } fn test( - name: &str, src_path: &Path, png_path: &Path, pdf_path: &Path, ref_path: Option<&Path>, env: &SharedEnv, ) -> bool { - println!("Testing {}.", name); + let name = src_path.strip_prefix(TYP_DIR).unwrap_or(src_path); + println!("Testing {}", name.display()); let src = fs::read_to_string(src_path).unwrap(); @@ -154,9 +145,11 @@ fn test( let env = env.borrow(); if !frames.is_empty() { let pdf_data = pdf::export(&frames, &env); + fs::create_dir_all(&pdf_path.parent().unwrap()).unwrap(); fs::write(pdf_path, pdf_data).unwrap(); let canvas = draw(&frames, &env, 2.0); + fs::create_dir_all(&png_path.parent().unwrap()).unwrap(); canvas.pixmap.save_png(png_path).unwrap(); if let Some(ref_path) = ref_path { @@ -173,7 +166,7 @@ fn test( } if ok { - println!("\x1b[1ATesting {}. ✔", name); + println!("\x1b[1ATesting {} ✔", name.display()); } ok