lofty-rs/tests/files/ogg.rs

130 lines
3 KiB
Rust
Raw Normal View History

2021-12-05 22:02:22 +00:00
use crate::{set_artist, temp_file, verify_artist};
use lofty::{FileType, ItemKey, ItemValue, TagIO, TagItem, TagType};
2021-12-05 22:02:22 +00:00
use std::io::{Seek, SeekFrom, 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() {
read("tests/files/assets/minimal/full_test.opus", &FileType::Opus)
2021-09-03 19:37:37 +00:00
}
#[test]
fn opus_write() {
write("tests/files/assets/minimal/full_test.opus", &FileType::Opus)
2021-09-06 14:41:24 +00:00
}
2021-09-03 19:37:37 +00:00
2021-09-06 14:41:24 +00:00
#[test]
fn opus_remove() {
remove(
"tests/files/assets/minimal/full_test.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/files/assets/minimal/full_test.flac", &FileType::FLAC)
2021-09-06 14:41:24 +00:00
}
2021-09-03 19:37:37 +00:00
2021-09-06 14:41:24 +00:00
#[test]
fn flac_write() {
write("tests/files/assets/minimal/full_test.flac", &FileType::FLAC)
2021-09-03 19:37:37 +00:00
}
#[test]
fn flac_remove() {
crate::remove_tag!(
"tests/files/assets/minimal/full_test.flac",
TagType::VorbisComments
);
}
#[test]
fn vorbis_read() {
read(
"tests/files/assets/minimal/full_test.ogg",
&FileType::Vorbis,
)
2021-09-06 14:41:24 +00:00
}
2021-09-03 19:37:37 +00:00
2021-09-06 14:41:24 +00:00
#[test]
fn vorbis_write() {
write(
"tests/files/assets/minimal/full_test.ogg",
&FileType::Vorbis,
)
2021-09-06 14:41:24 +00:00
}
2021-09-03 19:37:37 +00:00
#[test]
fn vorbis_remove() {
remove(
"tests/files/assets/minimal/full_test.ogg",
TagType::VorbisComments,
)
2022-01-31 23:19:11 +00:00
}
#[test]
fn speex_read() {
read("tests/files/assets/minimal/full_test.spx", &FileType::Speex)
2022-01-31 23:19:11 +00:00
}
#[test]
fn speex_write() {
write("tests/files/assets/minimal/full_test.spx", &FileType::Speex)
2022-01-31 23:19:11 +00:00
}
#[test]
fn speex_remove() {
remove(
"tests/files/assets/minimal/full_test.spx",
TagType::VorbisComments,
)
}
2021-09-06 14:41:24 +00:00
fn read(path: &str, file_type: &FileType) {
2021-12-22 01:20:24 +00:00
let file = lofty::read_from_path(path, false).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-12-05 22:02:22 +00:00
let mut file = temp_file!(path);
2021-09-03 19:37:37 +00:00
2021-12-22 01:20:24 +00:00
let mut tagged_file = lofty::read_from(&mut file, false).unwrap();
2021-09-03 19:37:37 +00:00
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
2021-12-05 22:02:22 +00:00
file.seek(SeekFrom::Start(0)).unwrap();
2021-12-22 01:20:24 +00:00
let mut tagged_file = lofty::read_from(&mut file, false).unwrap();
2021-09-03 19:37:37 +00:00
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) {
2021-12-05 22:02:22 +00:00
let mut file = temp_file!(path);
2021-12-22 01:20:24 +00:00
let tagged_file = lofty::read_from(&mut file, false).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
);
2021-12-05 22:02:22 +00:00
file.seek(SeekFrom::Start(0)).unwrap();
tag_type.remove_from(&mut file).unwrap();
2021-12-05 22:02:22 +00:00
file.seek(SeekFrom::Start(0)).unwrap();
2021-12-22 01:20:24 +00:00
let tagged_file = lofty::read_from(&mut file, false).unwrap();
2021-12-05 22:02:22 +00:00
// 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);
}