oxipng/tests/lib.rs
andrews05 462e982784
Move file-specific options under OutFile (#529)
This PR is addressing #220. It's not super important but it's a breaking
change, so if it's something we want to do then I thought I should get
it in now before the next release.

- [x] pretend can become another variant of OutFile, probably
OutFile::None, as that's what it essentially is - just another output
destination and not a separate option
- [x] ~~backup and~~ preserve_attrs should become properties of
OutFile::Path variant (so that it would contain Path { path, ~~backup,~~
preserve_attrs }) as they don't have any effect on any other output and
so semantically belong there best

Closes #220
2023-09-25 11:15:13 +02:00

77 lines
2 KiB
Rust

use oxipng::*;
use std::fs;
use std::fs::File;
use std::io::prelude::*;
#[test]
fn optimize_from_memory() {
let mut in_file = File::open("tests/files/fully_optimized.png").unwrap();
let mut in_file_buf: Vec<u8> = Vec::new();
in_file.read_to_end(&mut in_file_buf).unwrap();
let result = oxipng::optimize_from_memory(&in_file_buf, &Options::default());
assert!(result.is_ok());
}
#[test]
fn optimize_from_memory_corrupted() {
let mut in_file = File::open("tests/files/corrupted_header.png").unwrap();
let mut in_file_buf: Vec<u8> = Vec::new();
in_file.read_to_end(&mut in_file_buf).unwrap();
let result = oxipng::optimize_from_memory(&in_file_buf, &Options::default());
assert!(result.is_err());
}
#[test]
fn optimize_from_memory_apng() {
let mut in_file = File::open("tests/files/apng_file.png").unwrap();
let mut in_file_buf: Vec<u8> = Vec::new();
in_file.read_to_end(&mut in_file_buf).unwrap();
let result = oxipng::optimize_from_memory(&in_file_buf, &Options::default());
assert!(result.is_ok());
}
#[test]
fn optimize() {
let result = oxipng::optimize(
&"tests/files/fully_optimized.png".into(),
&OutFile::None,
&Options::default(),
);
assert!(result.is_ok());
}
#[test]
fn optimize_corrupted() {
let result = oxipng::optimize(
&"tests/files/corrupted_header.png".into(),
&OutFile::None,
&Options::default(),
);
assert!(result.is_err());
}
#[test]
fn optimize_apng() {
let result = oxipng::optimize(
&"tests/files/apng_file.png".into(),
&OutFile::None,
&Options::from_preset(0),
);
assert!(result.is_ok());
}
#[test]
fn optimize_srgb_icc() {
let file = fs::read("tests/files/badsrgb.png").unwrap();
let mut opts = Options::default();
let result = oxipng::optimize_from_memory(&file, &opts);
assert!(result.unwrap().len() > 1000);
opts.strip = StripChunks::Safe;
let result = oxipng::optimize_from_memory(&file, &opts);
assert!(result.unwrap().len() < 1000);
}