From 16e67f8bea7e6891e954420e2e005976fb48a528 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 16 Sep 2024 16:41:18 +0200 Subject: Shrink tests (#4967) --- tests/src/args.rs | 2 ++ tests/src/collect.rs | 4 +-- tests/src/run.rs | 2 +- tests/src/tests.rs | 21 +++++++++++- tests/src/world.rs | 90 +++++++++++++++++++++++++++++++++------------------- 5 files changed, 82 insertions(+), 37 deletions(-) (limited to 'tests/src') diff --git a/tests/src/args.rs b/tests/src/args.rs index b3441e90..786733cc 100644 --- a/tests/src/args.rs +++ b/tests/src/args.rs @@ -75,4 +75,6 @@ impl CliArguments { pub enum Command { /// Clears the on-disk test artifact store. Clean, + /// Deletes all dangling reference images. + Undangle, } diff --git a/tests/src/collect.rs b/tests/src/collect.rs index f10f4a2e..4dae0b70 100644 --- a/tests/src/collect.rs +++ b/tests/src/collect.rs @@ -408,8 +408,8 @@ fn selected(name: &str, abs: PathBuf) -> bool { /// An error in a test file. pub struct TestParseError { - pos: FilePos, - message: String, + pub pos: FilePos, + pub message: String, } impl Display for TestParseError { diff --git a/tests/src/run.rs b/tests/src/run.rs index f356f0d0..b09b3eaf 100644 --- a/tests/src/run.rs +++ b/tests/src/run.rs @@ -431,7 +431,7 @@ fn skippable_frame(frame: &Frame) -> bool { }) } -/// Whether to pixel images are approximately equal. +/// Whether two pixel images are approximately equal. fn approx_equal(a: &sk::Pixmap, b: &sk::Pixmap) -> bool { a.width() == b.width() && a.height() == b.height() diff --git a/tests/src/tests.rs b/tests/src/tests.rs index 8558abb6..a2d85fec 100644 --- a/tests/src/tests.rs +++ b/tests/src/tests.rs @@ -38,7 +38,8 @@ fn main() { match &ARGS.command { None => test(), - Some(Command::Clean) => std::fs::remove_dir_all(STORE_PATH).unwrap(), + Some(Command::Clean) => clean(), + Some(Command::Undangle) => undangle(), } } @@ -120,3 +121,21 @@ fn test() { std::process::exit(1); } } + +fn clean() { + std::fs::remove_dir_all(STORE_PATH).unwrap(); +} + +fn undangle() { + match crate::collect::collect() { + Ok(_) => eprintln!("no danging reference images"), + Err(errors) => { + for error in errors { + if error.message == "dangling reference image" { + std::fs::remove_file(&error.pos.path).unwrap(); + eprintln!("✅ deleted {}", error.pos.path.display()); + } + } + } + } +} diff --git a/tests/src/world.rs b/tests/src/world.rs index 799527fa..f5e49ba6 100644 --- a/tests/src/world.rs +++ b/tests/src/world.rs @@ -3,13 +3,19 @@ use std::collections::HashMap; use std::fs; use std::io::Write; use std::path::{Path, PathBuf}; +use std::str::FromStr; use std::sync::OnceLock; +use comemo::Tracked; use parking_lot::Mutex; -use typst::diag::{bail, FileError, FileResult, StrResult}; -use typst::foundations::{func, Bytes, Datetime, NoneValue, Repr, Smart, Value}; +use typst::diag::{bail, At, FileError, FileResult, SourceResult, StrResult}; +use typst::engine::Engine; +use typst::foundations::{ + func, Array, Bytes, Context, Datetime, IntoValue, NoneValue, Repr, Smart, Value, +}; use typst::layout::{Abs, Margin, PageElem}; -use typst::syntax::{FileId, Source}; +use typst::model::{Numbering, NumberingPattern}; +use typst::syntax::{FileId, Source, Span}; use typst::text::{Font, FontBook, TextElem, TextSize}; use typst::utils::{singleton, LazyHash}; use typst::visualize::Color; @@ -176,40 +182,11 @@ fn library() -> Library { // that it multiplies to nice round numbers. let mut lib = Library::default(); - #[func] - fn test(lhs: Value, rhs: Value) -> StrResult { - if lhs != rhs { - bail!("Assertion failed: {} != {}", lhs.repr(), rhs.repr()); - } - Ok(NoneValue) - } - - #[func] - fn test_repr(lhs: Value, rhs: Value) -> StrResult { - if lhs.repr() != rhs.repr() { - bail!("Assertion failed: {} != {}", lhs.repr(), rhs.repr()); - } - Ok(NoneValue) - } - - #[func] - fn print(#[variadic] values: Vec) -> NoneValue { - let mut out = std::io::stdout().lock(); - write!(out, "> ").unwrap(); - for (i, value) in values.into_iter().enumerate() { - if i > 0 { - write!(out, ", ").unwrap(); - } - write!(out, "{value:?}").unwrap(); - } - writeln!(out).unwrap(); - NoneValue - } - // Hook up helpers into the global scope. lib.global.scope_mut().define_func::(); lib.global.scope_mut().define_func::(); lib.global.scope_mut().define_func::(); + lib.global.scope_mut().define_func::(); lib.global .scope_mut() .define("conifer", Color::from_u8(0x9f, 0xEB, 0x52, 0xFF)); @@ -228,3 +205,50 @@ fn library() -> Library { lib } + +#[func] +fn test(lhs: Value, rhs: Value) -> StrResult { + if lhs != rhs { + bail!("Assertion failed: {} != {}", lhs.repr(), rhs.repr()); + } + Ok(NoneValue) +} + +#[func] +fn test_repr(lhs: Value, rhs: Value) -> StrResult { + if lhs.repr() != rhs.repr() { + bail!("Assertion failed: {} != {}", lhs.repr(), rhs.repr()); + } + Ok(NoneValue) +} + +#[func] +fn print(#[variadic] values: Vec) -> NoneValue { + let mut out = std::io::stdout().lock(); + write!(out, "> ").unwrap(); + for (i, value) in values.into_iter().enumerate() { + if i > 0 { + write!(out, ", ").unwrap(); + } + write!(out, "{value:?}").unwrap(); + } + writeln!(out).unwrap(); + NoneValue +} + +/// Generates `count` lines of text based on the numbering. +#[func] +fn lines( + engine: &mut Engine, + context: Tracked, + span: Span, + count: usize, + #[default(Numbering::Pattern(NumberingPattern::from_str("A").unwrap()))] + numbering: Numbering, +) -> SourceResult { + (1..=count) + .map(|n| numbering.apply(engine, context, &[n])) + .collect::>()? + .join(Some('\n'.into_value()), None) + .at(span) +} -- cgit v1.2.3