summaryrefslogtreecommitdiff
path: root/tests/src
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2024-09-16 16:41:18 +0200
committerGitHub <noreply@github.com>2024-09-16 14:41:18 +0000
commit16e67f8bea7e6891e954420e2e005976fb48a528 (patch)
treeb1de8e17f184d10894447a602da1722a71dd23a9 /tests/src
parentdb71a178bef7f1525d732a190ac75a1a6d56f24b (diff)
Shrink tests (#4967)
Diffstat (limited to 'tests/src')
-rw-r--r--tests/src/args.rs2
-rw-r--r--tests/src/collect.rs4
-rw-r--r--tests/src/run.rs2
-rw-r--r--tests/src/tests.rs21
-rw-r--r--tests/src/world.rs90
5 files changed, 82 insertions, 37 deletions
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<NoneValue> {
- if lhs != rhs {
- bail!("Assertion failed: {} != {}", lhs.repr(), rhs.repr());
- }
- Ok(NoneValue)
- }
-
- #[func]
- fn test_repr(lhs: Value, rhs: Value) -> StrResult<NoneValue> {
- if lhs.repr() != rhs.repr() {
- bail!("Assertion failed: {} != {}", lhs.repr(), rhs.repr());
- }
- Ok(NoneValue)
- }
-
- #[func]
- fn print(#[variadic] values: Vec<Value>) -> 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::<test>();
lib.global.scope_mut().define_func::<test_repr>();
lib.global.scope_mut().define_func::<print>();
+ lib.global.scope_mut().define_func::<lines>();
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<NoneValue> {
+ if lhs != rhs {
+ bail!("Assertion failed: {} != {}", lhs.repr(), rhs.repr());
+ }
+ Ok(NoneValue)
+}
+
+#[func]
+fn test_repr(lhs: Value, rhs: Value) -> StrResult<NoneValue> {
+ if lhs.repr() != rhs.repr() {
+ bail!("Assertion failed: {} != {}", lhs.repr(), rhs.repr());
+ }
+ Ok(NoneValue)
+}
+
+#[func]
+fn print(#[variadic] values: Vec<Value>) -> 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<Context>,
+ span: Span,
+ count: usize,
+ #[default(Numbering::Pattern(NumberingPattern::from_str("A").unwrap()))]
+ numbering: Numbering,
+) -> SourceResult<Value> {
+ (1..=count)
+ .map(|n| numbering.apply(engine, context, &[n]))
+ .collect::<SourceResult<Array>>()?
+ .join(Some('\n'.into_value()), None)
+ .at(span)
+}