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;
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=tests/parsing");
fs::create_dir_all("tests/cache").unwrap();
let paths = fs::read_dir("tests/parsing").unwrap()
.map(|entry| entry.unwrap().path())
.filter(|path| path.extension() == Some(OsStr::new("rs")));
// Compile all parser tests into a single giant vector.
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);
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() {
let mut line = line.replace("=>", &format!("=>({})=>", index + 1));
line.push('\n');
let line = line.replace("=>", &format!("=>({})=>", index + 1));
code.push_str(&line);
code.push('\n');
}
code.push_str("}),");
}
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>,
}
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.
#[derive(Debug, Clone)]
pub struct LayoutContext<'a, 'p> {

View File

@ -1,86 +1,114 @@
use std::fs::{self, File};
use std::io::{BufWriter, Read, Write};
use std::collections::HashMap;
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 typstc::export::pdf::PdfExporter;
use typstc::layout::{LayoutAction, Serialize};
use typstc::Typesetter;
use typstc::layout::{MultiLayout, Serialize};
use typstc::size::{Size, Size2D, SizeBox};
use typstc::style::PageStyle;
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() {
let mut perfect_match = false;
let mut filter = Vec::new();
fn main() -> Result<()> {
let opts = Options::parse();
for arg in std::env::args().skip(1) {
if arg.as_str() == "--nocapture" {
continue;
} else if arg.as_str() == "=" {
perfect_match = true;
} else {
filter.push(arg);
}
}
create_dir_all("tests/cache/serial")?;
create_dir_all("tests/cache/render")?;
create_dir_all("tests/cache/pdf")?;
fs::create_dir_all(format!("{}/serialized", CACHE_DIR)).unwrap();
fs::create_dir_all(format!("{}/rendered", CACHE_DIR)).unwrap();
fs::create_dir_all(format!("{}/pdf", CACHE_DIR)).unwrap();
let tests: Vec<_> = read_dir("tests/layouts/")?.collect();
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() {
let path = entry.unwrap().path();
if path.extension() != Some(std::ffi::OsStr::new("typ")) {
for entry in tests {
let path = entry?.path();
if path.extension() != Some(OsStr::new("typ")) {
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 {
filter.iter().any(|pattern| name == pattern)
} else {
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 opts.matches(&name) {
let src = read_to_string(&path)?;
panic::catch_unwind(|| test(&name, &src)).ok();
}
}
}
if failed > 0 {
println!("{} tests failed.", failed);
println!();
std::process::exit(-1);
}
println!();
Ok(())
}
/// 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);
let mut typesetter = Typesetter::new();
typesetter.set_page_style(PageStyle {
dimensions: Size2D::with_all(Size::pt(250.0)),
margins: SizeBox::with_all(Size::pt(10.0)),
});
let provider = FileSystemFontProvider::from_listing("fonts/fonts.toml").unwrap();
typesetter.add_font_provider(provider.clone());
let provider = FileSystemFontProvider::from_listing("fonts/fonts.toml")?;
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))] {
use std::time::Instant;
@ -89,6 +117,7 @@ fn test(name: &str, src: &str) {
let is_ok = typesetter.typeset(&src).is_ok();
let warmup_end = Instant::now();
// Only continue if the typesetting was successful.
if is_ok {
let start = Instant::now();
let tree = typesetter.parse(&src).unwrap();
@ -104,54 +133,46 @@ fn test(name: &str, src: &str) {
}
};
let layouts = match typesetter.typeset(&src) {
Ok(layouts) => layouts,
match typesetter.typeset(&src) {
Ok(layouts) => Some(layouts),
Err(err) => {
println!(" - compilation failed: {}", err);
#[cfg(not(debug_assertions))]
println!();
return;
}
};
// Write the serialed layout file.
let path = format!("{}/serialized/{}.tld", CACHE_DIR, name);
let mut file = File::create(path).unwrap();
// Find all used fonts and their filenames.
let mut map = Vec::new();
let mut loader = typesetter.loader().borrow_mut();
for layout in &layouts {
for action in &layout.actions {
if let LayoutAction::SetFont(index, _) = action {
if map.iter().find(|(i, _)| i == index).is_none() {
let (_, provider_index) = loader.get_provider_and_index(*index);
let filename = provider.get_path(provider_index).to_str().unwrap();
map.push((*index, filename));
None
}
}
}
/// Command line options.
struct Options {
filter: Vec<String>,
perfect: bool,
}
impl Options {
/// Parse the options from the environment arguments.
fn parse() -> Options {
let mut perfect = false;
let mut filter = Vec::new();
for arg in std::env::args().skip(1) {
match arg.as_str() {
"--nocapture" => {},
"=" => perfect = true,
_ => filter.push(arg),
}
}
Options { filter, perfect }
}
/// Whether a given test should be executed.
fn matches(&self, name: &str) -> bool {
match self.perfect {
true => self.filter.iter().any(|p| name == p),
false => self.filter.is_empty()
|| self.filter.iter().any(|p| name.contains(p))
}
}
}
}
drop(loader);
// Write the font mapping into the serialization file.
writeln!(file, "{}", map.len()).unwrap();
for (index, path) in map {
writeln!(file, "{} {}", index, path).unwrap();
}
layouts.serialize(&mut file).unwrap();
// Render the layout into a PNG.
Command::new("python")
.arg("tests/render.py")
.arg(name)
.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 Token::{
Space as S, Newline as N, LeftBracket as LB,
RightBracket as RB, Text as T, *
};
/// Parses the test syntax.
macro_rules! tokens {
($($src:expr =>($line:expr)=> $tokens:expr)*) => ({
#[allow(unused_mut)]
@ -15,18 +15,25 @@ macro_rules! tokens {
}
fn main() {
let tests = include!("cache/parsing.rs");
let tests = include!("cache/parse");
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() {
print!("Testing: {}. ", file);
let mut okay = 0;
let mut failed = 0;
// Go through all tests in a test file.
for (line, src, expected) in cases.into_iter() {
let found: Vec<_> = tokenize(src).map(Spanned::value).collect();
// Check whether the tokenization works correctly.
if found == expected {
okay += 1;
} else {
@ -44,6 +51,7 @@ fn main() {
}
}
// Print a small summary.
print!("{} okay, {} failed.", okay, failed);
if failed == 0 {
print!("")

View File

@ -7,23 +7,25 @@ from PIL import Image, ImageDraw, ImageFont
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():
assert len(sys.argv) == 2, "usage: python render.py <name>"
assert len(sys.argv) == 2, 'usage: python render.py <name>'
name = sys.argv[1]
filename = os.path.join(CACHE_DIR, f"serialized/{name}.tld")
with open(filename, encoding="utf-8") as file:
filename = os.path.join(SERIAL, f'{name}.tld')
with open(filename, encoding='utf-8') as file:
lines = [line[:-1] for line in file.readlines()]
renderer = MultiboxRenderer(lines)
renderer.render()
image = renderer.export()
pathlib.Path(os.path.join(CACHE_DIR, "rendered")).mkdir(parents=True, exist_ok=True)
image.save(CACHE_DIR + "rendered/" + name + ".png")
pathlib.Path(RENDER).mkdir(parents=True, exist_ok=True)
image.save(os.path.join(RENDER, f'{name}.png')
class MultiboxRenderer:
@ -36,7 +38,7 @@ class MultiboxRenderer:
parts = lines[i + 1].split(' ', 1)
index = int(parts[0])
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:]
@ -100,14 +102,14 @@ class BoxRenderer:
self.fonts = fonts
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)
for i in range(0, int(height)):
for j in range(0, int(width)):
if ((i // 2) % 2 == 0) == ((j // 2) % 2 == 0):
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.cursor = (0, 0)
@ -159,7 +161,7 @@ class BoxRenderer:
if color not in forbidden_colors:
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.rectangle(rect, fill=color + (255,))
@ -169,7 +171,7 @@ class BoxRenderer:
self.rects.append((rect, color))
else:
raise Exception("invalid command")
raise Exception('invalid command')
def export(self):
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])
if __name__ == "__main__":
if __name__ == '__main__':
main()