id3-image-rs/tests/test_processing.rs

127 lines
3.1 KiB
Rust
Raw Normal View History

2019-02-17 14:11:07 +00:00
use std::env;
use std::fs;
use std::path::{PathBuf, Path};
use std::ops::Deref;
2019-02-17 14:11:07 +00:00
use tempfile::TempDir;
2019-02-17 14:11:07 +00:00
use id3_image::*;
struct Fixture {
path_buf: PathBuf,
_tempdir: TempDir,
}
impl AsRef<Path> for Fixture {
fn as_ref(&self) -> &Path {
self.path_buf.as_path()
}
}
impl Deref for Fixture {
type Target = Path;
fn deref(&self) -> &Self::Target {
self.path_buf.deref()
2019-02-17 14:11:07 +00:00
}
}
macro_rules! fixture {
($filename:expr) => {
{
let root_dir = &env::var("CARGO_MANIFEST_DIR").expect("$CARGO_MANIFEST_DIR");
let mut source_path = PathBuf::from(root_dir);
source_path.push("tests/fixtures");
source_path.push($filename);
2019-02-17 14:11:07 +00:00
let tempdir = tempfile::tempdir().unwrap();
let mut target_path = PathBuf::from(&tempdir.path());
target_path.push($filename);
fs::copy(&source_path, &target_path).unwrap();
Fixture { _tempdir: tempdir, path_buf: target_path }
}
2019-02-17 14:11:07 +00:00
}
}
fn read_tag(path: &Path) -> id3::Tag {
id3::Tag::read_from_path(path).unwrap()
}
#[test]
fn test_unsuccessful_image_embedding() {
let song = fixture!("attempt_1_no_image.mp3");
let image = fixture!("attempt_1.jpg");
// Nonexistent files
assert!(embed_image(&song, &PathBuf::from("nonexistent.jpg")).is_err());
assert!(embed_image(&PathBuf::from("nonexistent.mp3"), &image).is_err());
assert!(embed_image(&PathBuf::from("nonexistent.mp3"), &PathBuf::from("nonexistent.jpg")).is_err());
// Wrong kinds of files
assert!(embed_image(&image, &song).is_err());
assert!(embed_image(&song, &song).is_err());
assert!(embed_image(&image, &image).is_err());
}
2019-02-17 14:11:07 +00:00
#[test]
fn test_successful_jpeg_image_embedding() {
let song = fixture!("attempt_1_no_image.mp3");
let image = fixture!("attempt_1.jpg");
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() == 0);
2019-02-17 14:11:07 +00:00
embed_image(&song, &image).unwrap();
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
}
#[test]
fn test_successful_png_image_embedding() {
let song = fixture!("attempt_1_no_image.mp3");
let image = fixture!("attempt_1.png");
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() == 0);
2019-02-17 14:11:07 +00:00
embed_image(&song, &image).unwrap();
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
}
#[test]
fn test_successful_image_embedding_in_a_file_that_already_has_an_image() {
let song = fixture!("attempt_1.mp3");
let image = fixture!("attempt_1.jpg");
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
embed_image(&song, &image).unwrap();
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
}
#[test]
fn test_remove_and_add_image() {
let song = fixture!("attempt_1.mp3");
let image = fixture!("attempt_1.jpg");
2019-02-17 14:11:07 +00:00
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
remove_images(&song).unwrap();
let tag = read_tag(&song);
assert!(tag.pictures().count() == 0);
embed_image(&song, &image).unwrap();
let tag = read_tag(&song);
assert!(tag.pictures().count() > 0);
2019-02-17 14:11:07 +00:00
}