Skip to content

Commit

Permalink
Verify webp save logics
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallku committed Jun 4, 2024
1 parent 7cda9af commit 33a31c6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"iname",
"libressl",
"localforage",
"Luma",
"Malgun",
"marshallku",
"mindepth",
Expand Down
49 changes: 44 additions & 5 deletions src/img.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,55 @@ mod tests {
use crate::path;

use super::*;
use image::io::Reader as ImageReader;
use std::fs;
use image::{io::Reader as ImageReader, DynamicImage};
use std::fs::{self, read};
use tempfile::tempdir;
use webp::Decoder;

const IMAGE_PATH: &str = "src/tests/fixtures";
const IMAGE_NAME: &str = "image";
const IMAGE_EXT: &str = "jpg";

#[tokio::test]
async fn test_save_image_to_webp() {
const IMAGE_SIZE: u32 = 100;

let dir = tempdir().unwrap();
let output_path = dir.path().join("test_image.webp");

let image = DynamicImage::ImageRgb8(image::RgbImage::new(IMAGE_SIZE, IMAGE_SIZE));

match save_image_to_webp(&image, &output_path) {
Ok(_) => {}
Err(e) => {
panic!("Failed to save image as WebP: {}", e);
}
};

assert!(output_path.exists());

let webp_data = read(&output_path).unwrap();
let decoded_webp = Decoder::new(&webp_data).decode().unwrap();

assert_eq!(decoded_webp.width(), IMAGE_SIZE);
assert_eq!(decoded_webp.width(), decoded_webp.height());
}

#[tokio::test]
async fn test_save_image_to_webp_invalid_image() {
let dir = tempdir().unwrap();
let output_path = dir.path().join("test_image.webp");

let invalid_image = DynamicImage::ImageLuma8(image::GrayImage::new(0, 0));

let result = save_image_to_webp(&invalid_image, &output_path);

assert!(result.is_err());
}

#[tokio::test]
async fn test_save_resized_image() {
const TARGET_WIDTH: u32 = 100;
const IMAGE_PATH: &str = "src/tests/fixtures";
const IMAGE_NAME: &str = "image";
const IMAGE_EXT: &str = "jpg";

let dir = tempdir().unwrap();

Expand Down

0 comments on commit 33a31c6

Please sign in to comment.