Enable multiple tests per file 🧱

This commit is contained in:
Laurenz 2021-01-13 14:07:38 +01:00
parent 539735e668
commit 60154474ba
27 changed files with 258 additions and 176 deletions

13
.gitignore vendored
View File

@ -1,8 +1,15 @@
.vscode
_things
/target
bench/target
**/*.rs.bk
Cargo.lock
bench/target
tests/out
_things
tests/png
tests/pdf
tests/playground.typ
tests/playground.png
tests/playground.pdf
tarpaulin-report.html

View File

@ -6,5 +6,5 @@
`oxipng -o max tests/ref/<img>` when creating or updating reference
images (note that `<img>` can be `*` to optimize all images).
- `res`: Resource files used by tests.
- `out`: PNG and PDF files produced by tests.
- `png`: PNG files produced by tests.
- `pdf`: PDF files produced by tests.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 KiB

BIN
tests/ref/func-font.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.4 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.0 KiB

After

Width:  |  Height:  |  Size: 4.3 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 186 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 105 KiB

BIN
tests/ref/func-image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 295 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.0 KiB

BIN
tests/ref/func-page.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.7 KiB

View File

@ -1,21 +0,0 @@
// Test error cases of the `font` function.
// Not one of the valid things for positional arguments.
[font false]
// Wrong types.
[font style: bold, weight: "thin", serif: 0]
// Weight out of range.
[font weight: 2700]
// Non-existing argument.
[font something: "invalid"]
// compare-ref: false
// error: 4:7-4:12 unexpected argument
// error: 7:14-7:18 expected font style, found font weight
// error: 7:28-7:34 expected font weight, found string
// error: 7:43-7:44 expected font family or array of font families, found integer
// warning: 10:15-10:19 must be between 100 and 900
// error: 13:7-13:27 unexpected argument

View File

@ -1,20 +0,0 @@
// Test font fallback.
// Source Sans Pro + Segoe UI Emoji.
Emoji: 🏀
// CMU Serif + Noto Emoji.
[font "CMU Serif", "Noto Emoji"][
Emoji: 🏀
]
// Class definitions.
[font serif: ("CMU Serif", "Latin Modern Math", "Noto Emoji")]
[font serif][
Math: α + β 3
]
// Class definition reused.
[font sans-serif: "Noto Emoji"]
[font sans-serif: ("Archivo", sans-serif)]
New sans-serif. 🚀

View File

@ -1,20 +0,0 @@
// Test configuring font properties.
[font "PT Sans", 10pt]
// Set same font size in three different ways.
[font 20pt][A]
[font 200%][A]
[font 15pt + 50%][A]
// Do nothing.
[font][Normal]
// Set style (is available).
[font style: italic][Italic]
// Set weight (is available).
[font weight: bold][Bold]
// Set stretch (not available, matching closest).
[font stretch: ultra-condensed][Condensed]

65
tests/typ/func-font.typ Normal file
View File

@ -0,0 +1,65 @@
// Test configuring font properties.
[font "PT Sans", 10pt]
// Set same font size in three different ways.
[font 20pt][A]
[font 200%][A]
[font 15pt + 50%][A]
// Do nothing.
[font][Normal]
// Set style (is available).
[font style: italic][Italic]
// Set weight (is available).
[font weight: bold][Bold]
// Set stretch (not available, matching closest).
[font stretch: ultra-condensed][Condensed]
---
// Test font fallback.
// Source Sans Pro + Segoe UI Emoji.
Emoji: 🏀
// CMU Serif + Noto Emoji.
[font "CMU Serif", "Noto Emoji"][
Emoji: 🏀
]
// Class definitions.
[font serif: ("CMU Serif", "Latin Modern Math", "Noto Emoji")]
[font serif][
Math: α + β 3
]
// Class definition reused.
[font sans-serif: "Noto Emoji"]
[font sans-serif: ("Archivo", sans-serif)]
New sans-serif. 🚀
---
// Test error cases.
//
// ref: false
// error: 3:7-3:12 unexpected argument
// error: 6:14-6:18 expected font style, found font weight
// error: 6:28-6:34 expected font weight, found string
// error: 6:43-6:44 expected font family or array of font families, found integer
// warning: 9:15-9:19 must be between 100 and 900
// error: 12:7-12:27 unexpected argument
// Not one of the valid things for positional arguments.
[font false]
// Wrong types.
[font style: bold, weight: "thin", serif: 0]
// Weight out of range.
[font weight: 2700]
// Non-existing argument.
[font something: "invalid"]

View File

@ -12,9 +12,6 @@ Add [h 10pt] [h 10pt] up
// Relative to font size.
Relative [h 100%] spacing
// Missing spacing.
Totally [h] ignored
// Swapped axes.
[page main-dir: rtl, cross-dir: ttb][
1 [h 1cm] 2
@ -22,4 +19,10 @@ Totally [h] ignored
3 [v 1cm] 4 [v -1cm] 5
]
// error: 16:11-16:11 missing argument: spacing
---
// Test error cases.
//
// error: 3:11-3:11 missing argument: spacing
// Missing spacing.
Totally [h] ignored

View File

@ -1,11 +0,0 @@
// Test error cases of the `image` function.
// File does not exist.
[image "path/does/not/exist"]
// File exists, but is no image.
[image "typ/image-error.typ"]
// compare-ref: false
// error: 4:8-4:29 failed to load image
// error: 7:8-7:29 failed to load image

View File

@ -1,8 +0,0 @@
// Test loading different image formats.
// Load an RGBA PNG image.
[image "res/rhino.png"]
[pagebreak]
// Load an RGB JPEG image.
[image "res/tiger.jpg"]

View File

@ -1,3 +1,13 @@
// Test loading different image formats.
// Load an RGBA PNG image.
[image "res/rhino.png"]
[pagebreak]
// Load an RGB JPEG image.
[image "res/tiger.jpg"]
---
// Test configuring the size and fitting behaviour of images.
// Fit to width of page.
@ -21,3 +31,16 @@
[align bottom, right][
[image "res/tiger.jpg"]
]
---
// Test error cases.
//
// ref: false
// error: 3:8-3:29 failed to load image
// error: 6:8-6:29 failed to load image
// File does not exist.
[image "path/does/not/exist"]
// File exists, but is no image.
[image "typ/image-error.typ"]

View File

@ -1,11 +0,0 @@
// Test a combination of pages with bodies and normal content.
[page height: 50pt]
[page][First]
[page][Second]
[pagebreak]
Fourth
[page][]
Sixth
[page][Seventh and last]

View File

@ -1,5 +0,0 @@
// Test changing the layouting directions of pages.
[page main-dir: btt, cross-dir: rtl]
Right to left!

View File

@ -1,11 +0,0 @@
// Test error cases of the `page` function.
// Invalid paper.
[page nonexistant]
// Aligned axes.
[page main-dir: ltr]
// compare-ref: false
// error: 4:7-4:18 unknown variable
// error: 7:17-7:20 aligned axis

View File

@ -23,3 +23,37 @@
// Flip the page.
[page "a10", flip: true][Flipped]
---
// Test a combination of pages with bodies and normal content.
[page height: 50pt]
[page][First]
[page][Second]
[pagebreak]
Fourth
[page][]
Sixth
[page][Seventh and last]
---
// Test changing the layouting directions of pages.
[page main-dir: btt, cross-dir: rtl]
Right to left!
---
// Test error cases.
//
// ref: false
// error: 3:7-3:18 unknown variable
// error: 6:17-6:20 aligned axis
// Invalid paper.
[page nonexistant]
// Aligned axes.
[page main-dir: ltr]

View File

@ -1,4 +1,11 @@
// Test the `rgb` function.
//
// warning: 9:6-9:9 must be between 0.0 and 1.0
// warning: 9:11-9:15 must be between 0.0 and 1.0
// error: 12:6-12:10 missing argument: blue component
// error: 15:5-15:5 missing argument: red component
// error: 15:5-15:5 missing argument: green component
// error: 15:5-15:5 missing argument: blue component
// Check the output.
[rgb 0.0, 0.3, 0.7]
@ -14,10 +21,3 @@
// Missing all components.
[rgb]
// warning: 10:6-10:9 must be between 0.0 and 1.0
// warning: 10:11-10:15 must be between 0.0 and 1.0
// error: 13:6-13:10 missing argument: blue component
// error: 16:5-16:5 missing argument: red component
// error: 16:5-16:5 missing argument: green component
// error: 16:5-16:5 missing argument: blue component

View File

@ -27,8 +27,8 @@ use typst::typeset;
const TYP_DIR: &str = "typ";
const REF_DIR: &str = "ref";
const PNG_DIR: &str = "out/png";
const PDF_DIR: &str = "out/pdf";
const PNG_DIR: &str = "png";
const PDF_DIR: &str = "pdf";
const FONT_DIR: &str = "../fonts";
fn main() {
@ -76,8 +76,26 @@ fn main() {
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,
);
}
ok &= test(&name, &src_path, &pdf_path, &png_path, &ref_path, &env);
let playground = Path::new("playground.typ");
if playground.exists() {
test(
"playground",
playground,
Path::new("playground.png"),
Path::new("playground.pdf"),
None,
&env,
);
}
if !ok {
@ -118,38 +136,73 @@ impl TestFilter {
fn test(
name: &str,
src_path: &Path,
pdf_path: &Path,
png_path: &Path,
ref_path: &Path,
pdf_path: &Path,
ref_path: Option<&Path>,
env: &SharedEnv,
) -> bool {
println!("Testing {}.", name);
let src = fs::read_to_string(src_path).unwrap();
let map = LineMap::new(&src);
let (ref_diags, compare_ref) = parse_metadata(&src, &map);
let mut state = State::default();
state.page.size = Size::uniform(Length::pt(120.0));
state.page.margins = Sides::uniform(Some(Length::pt(10.0).into()));
let mut ok = true;
let mut frames = vec![];
let Pass {
output: frames,
feedback: Feedback { mut diags, .. },
} = typeset(&src, Rc::clone(env), state);
diags.sort_by_key(|d| d.span);
for (i, part) in src.split("---").enumerate() {
let (part_ok, part_frames) = test_part(i, part, env);
ok &= part_ok;
frames.extend(part_frames);
}
let env = env.borrow();
if !frames.is_empty() {
let canvas = draw(&frames, &env, 2.0);
canvas.pixmap.save_png(png_path).unwrap();
let pdf_data = pdf::export(&frames, &env);
fs::write(pdf_path, pdf_data).unwrap();
let mut ok = true;
if let Some(ref_path) = ref_path {
if let Ok(ref_pixmap) = Pixmap::load_png(ref_path) {
if canvas.pixmap != ref_pixmap {
println!(" Does not match reference image. ❌");
ok = false;
}
} else {
println!(" Failed to open reference image. ❌");
ok = false;
}
}
}
if ok {
println!("\x1b[1ATesting {}. ✔", name);
}
ok
}
fn test_part(i: usize, src: &str, env: &SharedEnv) -> (bool, Vec<Frame>) {
let (src, compare_ref, map, ref_diags) = parse_metadata(&src, i);
let mut state = State::default();
state.page.size = Size::uniform(Length::pt(120.0));
state.page.margins = Sides::uniform(Some(Length::pt(10.0).into()));
let Pass {
output: mut frames,
feedback: Feedback { mut diags, .. },
} = typeset(&src, Rc::clone(env), state);
if !compare_ref {
frames.clear();
}
diags.sort_by_key(|d| d.span);
let mut ok = true;
if diags != ref_diags {
println!(" Does not match expected diagnostics. ❌");
println!(" Subtest {} does not match expected diagnostics. ❌", i);
ok = false;
for diag in &diags {
@ -167,40 +220,42 @@ fn test(
}
}
if compare_ref {
if let Ok(ref_pixmap) = Pixmap::load_png(&ref_path) {
if canvas.pixmap != ref_pixmap {
println!(" Does not match reference image. ❌");
ok = false;
}
} else {
println!(" Failed to open reference image. ❌");
ok = false;
}
(ok, frames)
}
if ok {
println!("\x1b[1ATesting {}. ✔", name);
}
ok
}
fn parse_metadata(src: &str, map: &LineMap) -> (SpanVec<Diag>, bool) {
fn parse_metadata(src: &str, i: usize) -> (&str, bool, LineMap, SpanVec<Diag>) {
let mut diags = vec![];
let mut compare_ref = true;
for line in src.lines() {
compare_ref &= !line.starts_with("// compare-ref: false");
let mut s = Scanner::new(src);
for k in 0 .. {
// Allow a newline directly after "---" (that is, if i > 0 and k == 0).
if !(i > 0 && k == 0) && !s.rest().starts_with("//") {
break;
}
let (level, rest) = if let Some(rest) = line.strip_prefix("// error: ") {
(Level::Error, rest)
} else if let Some(rest) = line.strip_prefix("// warning: ") {
let line = s.eat_until(typst::parse::is_newline);
s.eat_merging_crlf();
compare_ref &= !line.starts_with("// ref: false");
let (level, rest) = if let Some(rest) = line.strip_prefix("// warning: ") {
(Level::Warning, rest)
} else if let Some(rest) = line.strip_prefix("// error: ") {
(Level::Error, rest)
} else {
continue;
};
diags.push((level, rest));
}
let src = s.rest();
let map = LineMap::new(src);
let mut diags: Vec<_> = diags
.into_iter()
.map(|(level, rest)| {
fn pos(s: &mut Scanner, map: &LineMap) -> Pos {
let (line, _, column) = (num(s), s.eat_assert(':'), num(s));
map.pos(Location { line, column }).unwrap()
@ -211,13 +266,15 @@ fn parse_metadata(src: &str, map: &LineMap) -> (SpanVec<Diag>, bool) {
}
let mut s = Scanner::new(rest);
let (start, _, end) = (pos(&mut s, map), s.eat_assert('-'), pos(&mut s, map));
diags.push(Diag::new(level, s.rest().trim()).with_span(start .. end));
}
let (start, _, end) =
(pos(&mut s, &map), s.eat_assert('-'), pos(&mut s, &map));
Diag::new(level, s.rest().trim()).with_span(start .. end)
})
.collect();
diags.sort_by_key(|d| d.span);
(diags, compare_ref)
(src, compare_ref, map, diags)
}
fn print_diag(diag: &Spanned<Diag>, map: &LineMap) {