Initial Commit
This commit is contained in:
69 files changed
+7703
No files matched your search
@@ -0,0 +1,82 @@
|
||||
#! Contains the tests of `image`'s own functions: which format bytes, or a
|
||||
#! file's extension, say they're in, and an image read and written by them.
|
||||
|
||||
import std::io;
|
||||
import std::testing;
|
||||
import malunal::image;
|
||||
|
||||
@test def finds_each_format_by_its_first_bytes(t: &mut tester): void {
|
||||
val png := bytes_of("tests/images/rgb8.png");
|
||||
val jpeg := bytes_of("tests/images/rgb.jpg");
|
||||
t.expects(format_is(png.span, image::format::png));
|
||||
t.expects(format_is(jpeg.span, image::format::jpeg));
|
||||
t.expects(format_is("<?xml version='1.0'?>\n<!-- drawn -->\n<!DOCTYPE svg [<!ENTITY a 'b'>]><svg/>", image::format::svg));
|
||||
t.expects(format_is("<svg:svg xmlns:svg='http://www.w3.org/2000/svg'/>", image::format::svg));
|
||||
t.expects(image::format_of("<html><svg/></html>") is none);
|
||||
t.expects(image::format_of("GIF89a") is none);
|
||||
t.expects(fails(image::decode("plain text"), image::image_error::unrecognized));
|
||||
t.expects(fails(image::load("tests/images/missing.png"), image::image_error::unreadable));
|
||||
}
|
||||
|
||||
@test def names_formats_by_their_extensions(t: &mut tester): void {
|
||||
t.expects(image::format_named("png") is some(_) && image::format_named("PNG").unwrap() == image::format::png);
|
||||
t.expects(image::format_named("jpg").unwrap() == image::format::jpeg);
|
||||
t.expects(image::format_named("JPEG").unwrap() == image::format::jpeg);
|
||||
t.expects(image::format_named("jfif").unwrap() == image::format::jpeg);
|
||||
t.expects(image::format_named("svg").unwrap() == image::format::svg);
|
||||
t.expects(image::format_named("gif") is none);
|
||||
t.expects(image::format_named("") is none);
|
||||
}
|
||||
|
||||
@test def writes_in_the_format_asked(t: &mut tester): void {
|
||||
val picture := made_of("rgba", 24, 20);
|
||||
val png := image::encode(&picture, image::format::png) else (_) { t.expects(false); return; };
|
||||
val jpeg := image::encode(&picture, image::format::jpeg) else (_) { t.expects(false); return; };
|
||||
t.expects(format_is(png.span, image::format::png) && format_is(jpeg.span, image::format::jpeg));
|
||||
val again := read_back(png.span);
|
||||
t.expects(apart(&again, &picture) == 0);
|
||||
t.expects(fails_writing(image::encode(&picture, image::format::svg), image::image_error::unsupported));
|
||||
|
||||
# An image whose pixels aren't as many as its size says isn't written.
|
||||
var short := made_of("rgb", 4, 4);
|
||||
short.height = 5;
|
||||
t.expects(fails_writing(image::encode(&short, image::format::png), image::image_error::malformed));
|
||||
t.expects(fails_writing(image::encode(&short, image::format::jpeg), image::image_error::malformed));
|
||||
val empty := image::image { pixels = vector::<u8>::new() };
|
||||
t.expects(fails_writing(image::encode(&empty, image::format::png), image::image_error::malformed));
|
||||
}
|
||||
|
||||
@test def writes_into_a_stream(t: &mut tester): void {
|
||||
val picture := made_of("palette", 24, 20);
|
||||
var out := string::new(nothing {});
|
||||
t.expects(image::write_to(&picture, image::format::png, &mut out) is value);
|
||||
val again := read_back(out.span);
|
||||
t.expects(apart(&again, &picture) == 0);
|
||||
}
|
||||
|
||||
@test def saves_by_the_extension_and_loads_again(t: &mut tester): void {
|
||||
val picture := made_of("gray_alpha", 24, 20);
|
||||
t.expects(image::save(&picture, "tests/written.png") is value);
|
||||
val loaded := image::load("tests/written.png") else (_) { t.expects(false); return; };
|
||||
t.expects(apart(&loaded, &picture) == 0);
|
||||
t.expects(image::save(&picture, "tests/written.jpg") is value);
|
||||
val lossy := image::load("tests/written.jpg") else (_) { t.expects(false); return; };
|
||||
t.expects(lossy.width == 24 && lossy.height == 20);
|
||||
|
||||
match image::save(&picture, "tests/written.gif") {
|
||||
value(_) => t.expects(false);
|
||||
error(e) => t.expects(e == image::image_error::unrecognized);
|
||||
}
|
||||
match image::save(&picture, "tests/written.svg") {
|
||||
value(_) => t.expects(false);
|
||||
error(e) => t.expects(e == image::image_error::unsupported);
|
||||
}
|
||||
match image::save(&picture, "tests/no/such/folder/written.png") {
|
||||
value(_) => t.expects(false);
|
||||
error(e) => t.expects(e == image::image_error::unwritable);
|
||||
}
|
||||
for written in ["tests/written.png", "tests/written.jpg"] {
|
||||
val at := path::parse(written) else (_) { continue; };
|
||||
_ = io::remove(&at);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user