summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2019-12-08 11:15:04 +0100
committerLaurenz <laurmaedje@gmail.com>2019-12-08 11:36:18 +0100
commit64f938b449b7ff5e53b6a06ed943bf9dedc1014b (patch)
treec8237458ea10c21e9b3ce9fc2c02bdd7df9a1fca /tests
parentf364395e1d774456500ea61bb7b931f48a62ddfa (diff)
Improve testers ♻
Diffstat (limited to 'tests')
-rw-r--r--tests/layout.rs201
-rw-r--r--tests/layouts/align.typ (renamed from tests/layouting/align.typ)0
-rw-r--r--tests/layouts/coma.typ (renamed from tests/layouting/coma.typ)0
-rw-r--r--tests/layouts/lines.typ (renamed from tests/layouting/lines.typ)0
-rw-r--r--tests/parse.rs14
-rw-r--r--tests/render.py26
6 files changed, 136 insertions, 105 deletions
diff --git a/tests/layout.rs b/tests/layout.rs
index 46835a2a..33c12a2b 100644
--- a/tests/layout.rs
+++ b/tests/layout.rs
@@ -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;
-
-const CACHE_DIR: &str = "tests/cache";
+use typstc::export::pdf::PdfExporter;
-fn main() {
- let mut perfect_match = false;
- let mut filter = Vec::new();
+type Result<T> = std::result::Result<T, Box<dyn Error>>;
- 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);
- }
- }
+fn main() -> Result<()> {
+ let opts = Options::parse();
- 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();
+ create_dir_all("tests/cache/serial")?;
+ create_dir_all("tests/cache/render")?;
+ create_dir_all("tests/cache/pdf")?;
- let mut failed = 0;
+ let tests: Vec<_> = read_dir("tests/layouts/")?.collect();
- for entry in fs::read_dir("tests/layouting/").unwrap() {
- let path = entry.unwrap().path();
+ let len = tests.len();
+ println!();
+ println!("Running {} test{}", len, if len > 1 { "s" } else { "" });
- 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 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();
+ let name = path
+ .file_stem().ok_or("expected file stem")?
+ .to_string_lossy();
- 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;
+ None
}
- };
+ }
+}
- // Write the serialed layout file.
- let path = format!("{}/serialized/{}.tld", CACHE_DIR, name);
- let mut file = File::create(path).unwrap();
+/// Command line options.
+struct Options {
+ filter: Vec<String>,
+ perfect: bool,
+}
- // 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));
- }
+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),
}
}
- }
- 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();
+ Options { filter, perfect }
}
- 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();
+ /// 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))
+ }
+ }
}
diff --git a/tests/layouting/align.typ b/tests/layouts/align.typ
index a7101932..a7101932 100644
--- a/tests/layouting/align.typ
+++ b/tests/layouts/align.typ
diff --git a/tests/layouting/coma.typ b/tests/layouts/coma.typ
index d0a02b74..d0a02b74 100644
--- a/tests/layouting/coma.typ
+++ b/tests/layouts/coma.typ
diff --git a/tests/layouting/lines.typ b/tests/layouts/lines.typ
index 705de638..705de638 100644
--- a/tests/layouting/lines.typ
+++ b/tests/layouts/lines.typ
diff --git a/tests/parse.rs b/tests/parse.rs
index a56059d7..953cc959 100644
--- a/tests/parse.rs
+++ b/tests/parse.rs
@@ -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!(" ✔")
diff --git a/tests/render.py b/tests/render.py
index 81dd6a4e..07a9b5b1 100644
--- a/tests/render.py
+++ b/tests/render.py
@@ -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()