lofty-rs/tests/files/ogg.rs

185 lines
4.2 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, ParseOptions, Probe, TagExt, TagItem, TagType, TaggedFileExt,
};
2022-04-13 17:28:48 +00:00
use std::io::{Seek, 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_vorbis_comments() {
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,
)
}
fn read(path: &str, file_type: FileType) {
2022-09-24 06:34:22 +00:00
let file = Probe::open(path)
.unwrap()
.options(ParseOptions::new().read_properties(false))
.read()
.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
}
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
2022-09-24 06:34:22 +00:00
let mut tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.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
2022-04-13 17:28:48 +00:00
file.rewind().unwrap();
2022-09-24 06:34:22 +00:00
let mut tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.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);
2022-09-24 06:34:22 +00:00
let tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.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
);
2022-04-13 17:28:48 +00:00
file.rewind().unwrap();
tag_type.remove_from(&mut file).unwrap();
2022-04-13 17:28:48 +00:00
file.rewind().unwrap();
2022-09-24 06:34:22 +00:00
let tagged_file = Probe::new(&mut file)
.options(ParseOptions::new().read_properties(false))
.guess_file_type()
.unwrap()
.read()
.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);
}
2022-03-16 20:59:55 +00:00
#[test]
fn flac_with_id3v2() {
2022-03-18 19:06:42 +00:00
use lofty::flac::FlacFile;
2022-03-16 20:59:55 +00:00
use lofty::{Accessor, AudioFile};
let file = std::fs::read("tests/files/assets/flac_with_id3v2.flac").unwrap();
2022-09-24 06:34:22 +00:00
let flac_file =
FlacFile::read_from(&mut std::io::Cursor::new(file), ParseOptions::new()).unwrap();
2022-03-16 20:59:55 +00:00
2022-07-24 20:08:46 +00:00
assert!(flac_file.id3v2().is_some());
assert_eq!(
flac_file.id3v2().unwrap().artist().as_deref(),
Some("Foo artist")
);
2022-03-16 20:59:55 +00:00
assert!(flac_file.vorbis_comments().is_some());
}
#[test]
fn flac_remove_id3v2() {
crate::remove_tag!("tests/files/assets/flac_with_id3v2.flac", TagType::ID3v2);
}
#[test]
fn flac_try_write_non_empty_id3v2() {
use lofty::id3::v2::ID3v2Tag;
use lofty::Accessor;
let mut tag = ID3v2Tag::default();
tag.set_artist(String::from("Foo artist"));
assert!(tag
.save_to_path("tests/files/assets/flac_with_id3v2.flac")
.is_err());
}