lofty-rs/tests/ogg.rs

99 lines
2.4 KiB
Rust
Raw Normal View History

2021-09-06 14:41:24 +00:00
mod util;
use lofty::{FileType, ItemKey, ItemValue, Probe, TagItem, TagType};
use std::io::Write;
2021-09-03 19:37:37 +00:00
// The tests for OGG Opus/Vorbis are nearly identical
2021-09-06 14:41:24 +00:00
// We have the vendor string and a title stored in the tag
2021-09-03 19:37:37 +00:00
#[test]
fn opus_read() {
2021-09-06 14:41:24 +00:00
read("tests/assets/a.opus", &FileType::Opus)
2021-09-03 19:37:37 +00:00
}
#[test]
fn opus_write() {
2021-09-06 14:41:24 +00:00
write("tests/assets/a.opus", &FileType::Opus)
}
2021-09-03 19:37:37 +00:00
2021-09-06 14:41:24 +00:00
#[test]
fn opus_remove() {
remove("tests/assets/a.opus", TagType::VorbisComments)
}
#[test]
fn flac_read() {
2021-09-06 14:41:24 +00:00
// FLAC does **not** require a Vorbis comment block be present, this file has one
read("tests/assets/a.flac", &FileType::FLAC)
}
2021-09-03 19:37:37 +00:00
2021-09-06 14:41:24 +00:00
#[test]
fn flac_write() {
2021-09-06 14:41:24 +00:00
write("tests/assets/a.flac", &FileType::FLAC)
2021-09-03 19:37:37 +00:00
}
#[test]
fn flac_remove() {
crate::remove_tag!("tests/assets/a.flac", TagType::VorbisComments);
}
#[test]
fn vorbis_read() {
2021-09-06 14:41:24 +00:00
read("tests/assets/a.ogg", &FileType::Vorbis)
}
2021-09-03 19:37:37 +00:00
2021-09-06 14:41:24 +00:00
#[test]
fn vorbis_write() {
2021-09-06 14:41:24 +00:00
write("tests/assets/a.ogg", &FileType::Vorbis)
}
2021-09-03 19:37:37 +00:00
#[test]
fn vorbis_remove() {
remove("tests/assets/a.ogg", TagType::VorbisComments)
}
2021-09-06 14:41:24 +00:00
fn read(path: &str, file_type: &FileType) {
let file = Probe::new().read_from_path(path).unwrap();
2021-09-03 19:37:37 +00:00
2021-09-06 14:41:24 +00:00
assert_eq!(file.file_type(), file_type);
2021-09-03 19:37:37 +00:00
2021-09-06 14:41:24 +00:00
crate::verify_artist!(file, primary_tag, "Foo artist", 2);
2021-09-03 19:37:37 +00:00
}
2021-09-06 14:41:24 +00:00
fn write(path: &str, file_type: &FileType) {
2021-09-03 19:37:37 +00:00
let mut file = std::fs::OpenOptions::new()
.read(true)
.write(true)
2021-09-06 14:41:24 +00:00
.open(path)
2021-09-03 19:37:37 +00:00
.unwrap();
let mut tagged_file = Probe::new().read_from(&mut file).unwrap();
2021-09-06 14:41:24 +00:00
assert_eq!(tagged_file.file_type(), file_type);
2021-09-03 19:37:37 +00:00
2021-09-06 14:41:24 +00:00
crate::set_artist!(tagged_file, primary_tag_mut, "Foo artist", 2 => file, "Bar artist");
2021-09-03 19:37:37 +00:00
// Now reread the file
let mut tagged_file = Probe::new().read_from(&mut file).unwrap();
2021-09-06 14:41:24 +00:00
crate::set_artist!(tagged_file, primary_tag_mut, "Bar artist", 2 => file, "Foo artist");
2021-09-03 19:37:37 +00:00
}
fn remove(path: &str, tag_type: TagType) {
let mut file = tempfile::tempfile().unwrap();
file.write_all(&std::fs::read(path).unwrap()).unwrap();
let tagged_file = Probe::new().read_from(&mut file).unwrap();
// Verify we have both the vendor and artist
assert!(
tagged_file.tag(&tag_type).is_some()
&& tagged_file.tag(&tag_type).unwrap().item_count() == 2
);
assert!(tag_type.remove_from(&mut file));
let tagged_file = Probe::new().read_from(&mut file).unwrap();
// We can't completely remove the tag since metadata packets are mandatory, but it should only have to vendor now
assert_eq!(tagged_file.tag(&tag_type).unwrap().item_count(), 1);
}