Improve testers ♻

This commit is contained in:
Laurenz 2019-12-08 11:15:04 +01:00
parent f364395e1d
commit 64f938b449
8 changed files with 180 additions and 119 deletions

View File

@ -1,34 +1,49 @@
use std::fs; use std::fs::{self, create_dir_all, read_dir, read_to_string};
use std::ffi::OsStr; use std::ffi::OsStr;
fn main() { fn main() -> Result<(), Box<dyn std::error::Error>> {
create_dir_all("tests/cache")?;
// Make sure the script reruns if this file changes or files are
// added/deleted in the parsing folder.
println!("cargo:rerun-if-changed=build.rs"); println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=tests/parsing"); println!("cargo:rerun-if-changed=tests/parsing");
fs::create_dir_all("tests/cache").unwrap(); // Compile all parser tests into a single giant vector.
let paths = fs::read_dir("tests/parsing").unwrap()
.map(|entry| entry.unwrap().path())
.filter(|path| path.extension() == Some(OsStr::new("rs")));
let mut code = "vec![".to_string(); let mut code = "vec![".to_string();
for path in paths {
let name = path.file_stem().unwrap().to_str().unwrap();
let file = fs::read_to_string(&path).unwrap();
for entry in read_dir("tests/parsing")? {
let path = entry?.path();
if path.extension() != Some(OsStr::new("rs")) {
continue;
}
let name = path
.file_stem().ok_or("expected file stem")?
.to_string_lossy();
// Make sure this also reruns if the contents of a file in parsing
// change. This is not ensured by rerunning only on the folder.
println!("cargo:rerun-if-changed=tests/parsing/{}.rs", name); println!("cargo:rerun-if-changed=tests/parsing/{}.rs", name);
code.push_str(&format!("(\"{}\", tokens!{{", name)); code.push_str(&format!("(\"{}\", tokens!{{", name));
// Replace the `=>` arrows with a double arrow indicating the line
// number in the middle, such that the tester can tell which line number
// a test originated from.
let file = read_to_string(&path)?;
for (index, line) in file.lines().enumerate() { for (index, line) in file.lines().enumerate() {
let mut line = line.replace("=>", &format!("=>({})=>", index + 1)); let line = line.replace("=>", &format!("=>({})=>", index + 1));
line.push('\n');
code.push_str(&line); code.push_str(&line);
code.push('\n');
} }
code.push_str("}),"); code.push_str("}),");
} }
code.push(']'); code.push(']');
fs::write("tests/cache/parsing.rs", code).unwrap(); fs::write("tests/cache/parse", code)?;
Ok(())
} }

View File

@ -45,6 +45,21 @@ pub struct Layout {
pub actions: Vec<LayoutAction>, pub actions: Vec<LayoutAction>,
} }
impl Layout {
/// Returns a vector with all used font indices.
pub fn find_used_fonts(&self) -> Vec<usize> {
let mut fonts = Vec::new();
for action in &self.actions {
if let LayoutAction::SetFont(index, _) = action {
if !fonts.contains(index) {
fonts.push(*index);
}
}
}
fonts
}
}
/// The general context for layouting. /// The general context for layouting.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct LayoutContext<'a, 'p> { pub struct LayoutContext<'a, 'p> {

View File

@ -1,86 +1,114 @@
use std::fs::{self, File}; use std::collections::HashMap;
use std::io::{BufWriter, Read, Write}; use std::error::Error;
use std::ffi::OsStr;
use std::fs::{File, create_dir_all, read_dir, read_to_string};
use std::io::{BufWriter, Write};
use std::panic;
use std::process::Command; use std::process::Command;
use typstc::export::pdf::PdfExporter; use typstc::Typesetter;
use typstc::layout::{LayoutAction, Serialize}; use typstc::layout::{MultiLayout, Serialize};
use typstc::size::{Size, Size2D, SizeBox}; use typstc::size::{Size, Size2D, SizeBox};
use typstc::style::PageStyle; use typstc::style::PageStyle;
use typstc::toddle::query::FileSystemFontProvider; use typstc::toddle::query::FileSystemFontProvider;
use typstc::Typesetter; use typstc::export::pdf::PdfExporter;
const CACHE_DIR: &str = "tests/cache"; type Result<T> = std::result::Result<T, Box<dyn Error>>;
fn main() { fn main() -> Result<()> {
let mut perfect_match = false; let opts = Options::parse();
let mut filter = Vec::new();
for arg in std::env::args().skip(1) { create_dir_all("tests/cache/serial")?;
if arg.as_str() == "--nocapture" { create_dir_all("tests/cache/render")?;
continue; create_dir_all("tests/cache/pdf")?;
} else if arg.as_str() == "=" {
perfect_match = true;
} else {
filter.push(arg);
}
}
fs::create_dir_all(format!("{}/serialized", CACHE_DIR)).unwrap(); let tests: Vec<_> = read_dir("tests/layouts/")?.collect();
fs::create_dir_all(format!("{}/rendered", CACHE_DIR)).unwrap();
fs::create_dir_all(format!("{}/pdf", CACHE_DIR)).unwrap();
let mut failed = 0; let len = tests.len();
println!();
println!("Running {} test{}", len, if len > 1 { "s" } else { "" });
for entry in fs::read_dir("tests/layouting/").unwrap() { for entry in tests {
let path = entry.unwrap().path(); let path = entry?.path();
if path.extension() != Some(OsStr::new("typ")) {
if path.extension() != Some(std::ffi::OsStr::new("typ")) {
continue; continue;
} }
let name = path.file_stem().unwrap().to_str().unwrap(); let name = path
.file_stem().ok_or("expected file stem")?
.to_string_lossy();
let matches = if perfect_match { if opts.matches(&name) {
filter.iter().any(|pattern| name == pattern) let src = read_to_string(&path)?;
} else { panic::catch_unwind(|| test(&name, &src)).ok();
filter.is_empty() || filter.iter().any(|pattern| name.contains(pattern))
};
if matches {
let mut file = File::open(&path).unwrap();
let mut src = String::new();
file.read_to_string(&mut src).unwrap();
if std::panic::catch_unwind(|| test(name, &src)).is_err() {
failed += 1;
println!();
}
} }
} }
if failed > 0 {
println!("{} tests failed.", failed);
println!();
std::process::exit(-1);
}
println!(); println!();
Ok(())
} }
/// Create a _PDF_ with a name from the source code. /// Create a _PDF_ with a name from the source code.
fn test(name: &str, src: &str) { fn test(name: &str, src: &str) -> Result<()> {
println!("Testing: {}.", name); println!("Testing: {}.", name);
let mut typesetter = Typesetter::new(); let mut typesetter = Typesetter::new();
typesetter.set_page_style(PageStyle { typesetter.set_page_style(PageStyle {
dimensions: Size2D::with_all(Size::pt(250.0)), dimensions: Size2D::with_all(Size::pt(250.0)),
margins: SizeBox::with_all(Size::pt(10.0)), margins: SizeBox::with_all(Size::pt(10.0)),
}); });
let provider = FileSystemFontProvider::from_listing("fonts/fonts.toml").unwrap(); let provider = FileSystemFontProvider::from_listing("fonts/fonts.toml")?;
typesetter.add_font_provider(provider.clone()); let font_paths = provider.paths();
typesetter.add_font_provider(provider);
let layouts = match compile(&typesetter, src) {
Some(layouts) => layouts,
None => return Ok(()),
};
// Compute the font's paths.
let mut fonts = HashMap::new();
let loader = typesetter.loader().borrow();
for layout in &layouts {
for index in layout.find_used_fonts() {
fonts.entry(index).or_insert_with(|| {
let provider_index = loader.get_provider_and_index(index).1;
font_paths[provider_index].to_string_lossy()
});
}
}
// Write the serialized layout file.
let path = format!("tests/cache/serial/{}", name);
let mut file = BufWriter::new(File::create(path)?);
// Write the font mapping into the serialization file.
writeln!(file, "{}", fonts.len())?;
for (index, path) in fonts.iter() {
writeln!(file, "{} {}", index, path)?;
}
layouts.serialize(&mut file)?;
// Render the layout into a PNG.
Command::new("python")
.arg("tests/render.py")
.arg(name)
.spawn()
.expect("failed to run python renderer");
// Write the PDF file.
let path = format!("tests/cache/pdf/{}.pdf", name);
let file = BufWriter::new(File::create(path)?);
let exporter = PdfExporter::new();
exporter.export(&layouts, typesetter.loader(), file)?;
Ok(())
}
/// Compile the source code with the typesetter.
fn compile(typesetter: &Typesetter, src: &str) -> Option<MultiLayout> {
#[cfg(not(debug_assertions))] { #[cfg(not(debug_assertions))] {
use std::time::Instant; use std::time::Instant;
@ -89,6 +117,7 @@ fn test(name: &str, src: &str) {
let is_ok = typesetter.typeset(&src).is_ok(); let is_ok = typesetter.typeset(&src).is_ok();
let warmup_end = Instant::now(); let warmup_end = Instant::now();
// Only continue if the typesetting was successful.
if is_ok { if is_ok {
let start = Instant::now(); let start = Instant::now();
let tree = typesetter.parse(&src).unwrap(); let tree = typesetter.parse(&src).unwrap();
@ -104,54 +133,46 @@ fn test(name: &str, src: &str) {
} }
}; };
let layouts = match typesetter.typeset(&src) { match typesetter.typeset(&src) {
Ok(layouts) => layouts, Ok(layouts) => Some(layouts),
Err(err) => { Err(err) => {
println!(" - compilation failed: {}", err); println!(" - compilation failed: {}", err);
#[cfg(not(debug_assertions))] #[cfg(not(debug_assertions))]
println!(); println!();
return; None
} }
}; }
}
// Write the serialed layout file. /// Command line options.
let path = format!("{}/serialized/{}.tld", CACHE_DIR, name); struct Options {
let mut file = File::create(path).unwrap(); filter: Vec<String>,
perfect: bool,
}
// Find all used fonts and their filenames. impl Options {
let mut map = Vec::new(); /// Parse the options from the environment arguments.
let mut loader = typesetter.loader().borrow_mut(); fn parse() -> Options {
for layout in &layouts { let mut perfect = false;
for action in &layout.actions { let mut filter = Vec::new();
if let LayoutAction::SetFont(index, _) = action {
if map.iter().find(|(i, _)| i == index).is_none() { for arg in std::env::args().skip(1) {
let (_, provider_index) = loader.get_provider_and_index(*index); match arg.as_str() {
let filename = provider.get_path(provider_index).to_str().unwrap(); "--nocapture" => {},
map.push((*index, filename)); "=" => perfect = true,
} _ => filter.push(arg),
} }
} }
}
drop(loader);
// Write the font mapping into the serialization file. Options { filter, perfect }
writeln!(file, "{}", map.len()).unwrap();
for (index, path) in map {
writeln!(file, "{} {}", index, path).unwrap();
} }
layouts.serialize(&mut file).unwrap(); /// Whether a given test should be executed.
fn matches(&self, name: &str) -> bool {
// Render the layout into a PNG. match self.perfect {
Command::new("python") true => self.filter.iter().any(|p| name == p),
.arg("tests/render.py") false => self.filter.is_empty()
.arg(name) || self.filter.iter().any(|p| name.contains(p))
.spawn() }
.expect("failed to run python-based renderer"); }
// Write the PDF file.
let path = format!("{}/pdf/{}.pdf", CACHE_DIR, name);
let file = BufWriter::new(File::create(path).unwrap());
let exporter = PdfExporter::new();
exporter.export(&layouts, typesetter.loader(), file).unwrap();
} }

View File

@ -1,10 +1,10 @@
use typstc::syntax::*; use typstc::syntax::*;
use Token::{ use Token::{
Space as S, Newline as N, LeftBracket as LB, Space as S, Newline as N, LeftBracket as LB,
RightBracket as RB, Text as T, * RightBracket as RB, Text as T, *
}; };
/// Parses the test syntax.
macro_rules! tokens { macro_rules! tokens {
($($src:expr =>($line:expr)=> $tokens:expr)*) => ({ ($($src:expr =>($line:expr)=> $tokens:expr)*) => ({
#[allow(unused_mut)] #[allow(unused_mut)]
@ -15,18 +15,25 @@ macro_rules! tokens {
} }
fn main() { fn main() {
let tests = include!("cache/parsing.rs"); let tests = include!("cache/parse");
let mut errors = false; let mut errors = false;
let len = tests.len();
println!();
println!("Running {} test{}", len, if len > 1 { "s" } else { "" });
// Go through all test files.
for (file, cases) in tests.into_iter() { for (file, cases) in tests.into_iter() {
print!("Testing: {}. ", file); print!("Testing: {}. ", file);
let mut okay = 0; let mut okay = 0;
let mut failed = 0; let mut failed = 0;
// Go through all tests in a test file.
for (line, src, expected) in cases.into_iter() { for (line, src, expected) in cases.into_iter() {
let found: Vec<_> = tokenize(src).map(Spanned::value).collect(); let found: Vec<_> = tokenize(src).map(Spanned::value).collect();
// Check whether the tokenization works correctly.
if found == expected { if found == expected {
okay += 1; okay += 1;
} else { } else {
@ -44,6 +51,7 @@ fn main() {
} }
} }
// Print a small summary.
print!("{} okay, {} failed.", okay, failed); print!("{} okay, {} failed.", okay, failed);
if failed == 0 { if failed == 0 {
print!("") print!("")

View File

@ -7,23 +7,25 @@ from PIL import Image, ImageDraw, ImageFont
BASE = os.path.dirname(__file__) BASE = os.path.dirname(__file__)
CACHE_DIR = os.path.join(BASE, "cache/") CACHE = os.path.join(BASE, 'cache/')
SERIAL = os.path.join(CACHE, 'serial/')
RENDER = os.path.join(CACHE, 'render/')
def main(): def main():
assert len(sys.argv) == 2, "usage: python render.py <name>" assert len(sys.argv) == 2, 'usage: python render.py <name>'
name = sys.argv[1] name = sys.argv[1]
filename = os.path.join(CACHE_DIR, f"serialized/{name}.tld") filename = os.path.join(SERIAL, f'{name}.tld')
with open(filename, encoding="utf-8") as file: with open(filename, encoding='utf-8') as file:
lines = [line[:-1] for line in file.readlines()] lines = [line[:-1] for line in file.readlines()]
renderer = MultiboxRenderer(lines) renderer = MultiboxRenderer(lines)
renderer.render() renderer.render()
image = renderer.export() image = renderer.export()
pathlib.Path(os.path.join(CACHE_DIR, "rendered")).mkdir(parents=True, exist_ok=True) pathlib.Path(RENDER).mkdir(parents=True, exist_ok=True)
image.save(CACHE_DIR + "rendered/" + name + ".png") image.save(os.path.join(RENDER, f'{name}.png')
class MultiboxRenderer: class MultiboxRenderer:
@ -36,7 +38,7 @@ class MultiboxRenderer:
parts = lines[i + 1].split(' ', 1) parts = lines[i + 1].split(' ', 1)
index = int(parts[0]) index = int(parts[0])
path = parts[1] path = parts[1]
self.fonts[index] = os.path.join(BASE, "../fonts", path) self.fonts[index] = os.path.join(BASE, '../fonts', path)
self.content = lines[font_count + 1:] self.content = lines[font_count + 1:]
@ -100,14 +102,14 @@ class BoxRenderer:
self.fonts = fonts self.fonts = fonts
self.size = (pix(width), pix(height)) self.size = (pix(width), pix(height))
img = Image.new("RGBA", self.size, (255, 255, 255, 255)) img = Image.new('RGBA', self.size, (255, 255, 255, 255))
pixels = numpy.array(img) pixels = numpy.array(img)
for i in range(0, int(height)): for i in range(0, int(height)):
for j in range(0, int(width)): for j in range(0, int(width)):
if ((i // 2) % 2 == 0) == ((j // 2) % 2 == 0): if ((i // 2) % 2 == 0) == ((j // 2) % 2 == 0):
pixels[4*i:4*(i+1), 4*j:4*(j+1)] = (225, 225, 225, 255) pixels[4*i:4*(i+1), 4*j:4*(j+1)] = (225, 225, 225, 255)
self.img = Image.fromarray(pixels, "RGBA") self.img = Image.fromarray(pixels, 'RGBA')
self.draw = ImageDraw.Draw(self.img) self.draw = ImageDraw.Draw(self.img)
self.cursor = (0, 0) self.cursor = (0, 0)
@ -159,7 +161,7 @@ class BoxRenderer:
if color not in forbidden_colors: if color not in forbidden_colors:
break break
overlay = Image.new("RGBA", self.size, (0, 0, 0, 0)) overlay = Image.new('RGBA', self.size, (0, 0, 0, 0))
draw = ImageDraw.Draw(overlay) draw = ImageDraw.Draw(overlay)
draw.rectangle(rect, fill=color + (255,)) draw.rectangle(rect, fill=color + (255,))
@ -169,7 +171,7 @@ class BoxRenderer:
self.rects.append((rect, color)) self.rects.append((rect, color))
else: else:
raise Exception("invalid command") raise Exception('invalid command')
def export(self): def export(self):
return self.img return self.img
@ -182,5 +184,5 @@ def overlap(a, b):
return (a[0] < b[2] and b[0] < a[2]) and (a[1] < b[3] and b[1] < a[3]) return (a[0] < b[2] and b[0] < a[2]) and (a[1] < b[3] and b[1] < a[3])
if __name__ == "__main__": if __name__ == '__main__':
main() main()