Add tests for dump_to

This commit is contained in:
Serial 2022-01-05 12:52:29 -05:00
parent 94e5ed635f
commit 7942a5bd84
8 changed files with 180 additions and 90 deletions

View file

@ -266,7 +266,7 @@ mod tests {
use crate::{ItemValue, Tag, TagType};
use crate::ape::tag::read_ape_header;
use std::io::{Cursor, Read};
use std::io::Cursor;
#[test]
fn parse_ape() {
@ -316,12 +316,7 @@ mod tests {
expected_tag.insert(track_number_item);
expected_tag.insert(genre_item);
let mut tag = Vec::new();
std::fs::File::open("tests/tags/assets/test.apev2")
.unwrap()
.read_to_end(&mut tag)
.unwrap();
let tag = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.apev2");
let mut reader = Cursor::new(tag);
let header = read_ape_header(&mut reader, false).unwrap();
@ -335,13 +330,29 @@ mod tests {
}
#[test]
fn ape_to_tag() {
let mut tag_bytes = Vec::new();
std::fs::File::open("tests/tags/assets/test.apev2")
.unwrap()
.read_to_end(&mut tag_bytes)
.unwrap();
fn ape_re_read() {
let tag_bytes = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.apev2");
let mut reader = Cursor::new(tag_bytes);
let header = read_ape_header(&mut reader, false).unwrap();
let parsed_tag = crate::ape::tag::read::read_ape_tag(&mut reader, header).unwrap();
let mut writer = Vec::new();
parsed_tag.dump_to(&mut writer).unwrap();
// Remove the APE preamble
let mut temp_reader = Cursor::new(&writer[8..]);
let temp_header = read_ape_header(&mut temp_reader, false).unwrap();
let temp_parsed_tag =
crate::ape::tag::read::read_ape_tag(&mut temp_reader, temp_header).unwrap();
assert_eq!(parsed_tag, temp_parsed_tag);
}
#[test]
fn ape_to_tag() {
let tag_bytes = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.apev2");
let mut reader = Cursor::new(tag_bytes);
let header = read_ape_header(&mut reader, false).unwrap();

View file

@ -282,8 +282,6 @@ mod tests {
use crate::id3::v1::Id3v1Tag;
use crate::{Tag, TagType};
use std::io::Read;
#[test]
fn parse_id3v1() {
let expected_tag = Id3v1Tag {
@ -296,26 +294,29 @@ mod tests {
genre: Some(32),
};
let mut tag = [0; 128];
std::fs::File::open("tests/tags/assets/test.id3v1")
.unwrap()
.read_exact(&mut tag)
.unwrap();
let parsed_tag = crate::id3::v1::read::parse_id3v1(tag);
let tag = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.id3v1");
let parsed_tag = crate::id3::v1::read::parse_id3v1(tag.try_into().unwrap());
assert_eq!(expected_tag, parsed_tag);
}
#[test]
fn id3v1_to_tag() {
let mut tag_bytes = [0; 128];
std::fs::File::open("tests/tags/assets/test.id3v1")
.unwrap()
.read_exact(&mut tag_bytes)
.unwrap();
fn id3v2_re_read() {
let tag = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.id3v1");
let parsed_tag = crate::id3::v1::read::parse_id3v1(tag.try_into().unwrap());
let id3v1 = crate::id3::v1::read::parse_id3v1(tag_bytes);
let mut writer = Vec::new();
parsed_tag.dump_to(&mut writer).unwrap();
let temp_parsed_tag = crate::id3::v1::read::parse_id3v1(writer.try_into().unwrap());
assert_eq!(parsed_tag, temp_parsed_tag);
}
#[test]
fn id3v1_to_tag() {
let tag_bytes = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.id3v1");
let id3v1 = crate::id3::v1::read::parse_id3v1(tag_bytes.try_into().unwrap());
let tag: Tag = id3v1.into();

View file

@ -445,12 +445,10 @@ impl<'a> Into<Id3v2TagRef<'a>> for &'a Id3v2Tag {
#[cfg(test)]
mod tests {
use crate::id3::v2::read_id3v2_header;
use crate::id3::v2::{Frame, FrameFlags, FrameValue, Id3v2Tag, LanguageFrame, TextEncoding};
use crate::{Tag, TagType};
use crate::id3::v2::read_id3v2_header;
use std::io::Read;
#[test]
fn parse_id3v2() {
let mut expected_tag = Id3v2Tag::default();
@ -544,12 +542,7 @@ mod tests {
.unwrap(),
);
let mut tag = Vec::new();
std::fs::File::open("tests/tags/assets/test.id3v2")
.unwrap()
.read_to_end(&mut tag)
.unwrap();
let tag = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.id3v2");
let mut reader = std::io::Cursor::new(&tag[..]);
let header = read_id3v2_header(&mut reader).unwrap();
@ -558,13 +551,28 @@ mod tests {
assert_eq!(expected_tag, parsed_tag);
}
#[test]
fn id3v2_re_read() {
let tag = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.id3v2");
let mut reader = std::io::Cursor::new(&tag[..]);
let header = read_id3v2_header(&mut reader).unwrap();
let parsed_tag = crate::id3::v2::read::parse_id3v2(&mut reader, header).unwrap();
let mut writer = Vec::new();
parsed_tag.dump_to(&mut writer).unwrap();
let temp_reader = &mut &*writer;
let temp_header = read_id3v2_header(temp_reader).unwrap();
let temp_parsed_tag = crate::id3::v2::read::parse_id3v2(temp_reader, temp_header).unwrap();
assert_eq!(parsed_tag, temp_parsed_tag);
}
#[test]
fn id3v2_to_tag() {
let mut tag_bytes = Vec::new();
std::fs::File::open("tests/tags/assets/test.id3v2")
.unwrap()
.read_to_end(&mut tag_bytes)
.unwrap();
let tag_bytes = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.id3v2");
let mut reader = std::io::Cursor::new(&tag_bytes[..]);

View file

@ -235,7 +235,7 @@ where
T: AsRef<str>,
AI: IntoIterator<Item = T>,
{
pub(super) fn new(
pub(crate) fn new(
name: Option<&'a str>,
author: Option<&'a str>,
copyright: Option<&'a str>,
@ -411,7 +411,7 @@ mod tests {
use crate::iff::{AiffTextChunks, Comment};
use crate::{ItemKey, ItemValue, Tag, TagItem, TagType};
use std::io::{Cursor, Read};
use std::io::Cursor;
#[test]
fn parse_aiff_text() {
@ -437,11 +437,7 @@ mod tests {
]),
};
let mut tag = Vec::new();
std::fs::File::open("tests/tags/assets/test.aiff_text")
.unwrap()
.read_to_end(&mut tag)
.unwrap();
let tag = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.aiff_text");
let parsed_tag = super::super::read::read_from(&mut Cursor::new(tag), false)
.unwrap()
@ -452,13 +448,29 @@ mod tests {
}
#[test]
fn aiff_text_to_tag() {
let mut tag_bytes = Vec::new();
std::fs::File::open("tests/tags/assets/test.aiff_text")
fn aiff_text_re_read() {
let tag = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.aiff_text");
let parsed_tag = super::super::read::read_from(&mut Cursor::new(tag), false)
.unwrap()
.read_to_end(&mut tag_bytes)
.text_chunks
.unwrap();
// Create a fake AIFF signature
let mut writer = vec![b'F', b'O', b'R', b'M', 0, 0, 0, 0, b'A', b'I', b'F', b'F'];
parsed_tag.dump_to(&mut writer).unwrap();
let temp_parsed_tag = super::super::read::read_from(&mut Cursor::new(writer), false)
.unwrap()
.text_chunks
.unwrap();
assert_eq!(parsed_tag, temp_parsed_tag);
}
#[test]
fn aiff_text_to_tag() {
let tag_bytes = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.aiff_text");
let aiff_text = super::super::read::read_from(&mut Cursor::new(tag_bytes), false)
.unwrap()
.text_chunks

View file

@ -235,7 +235,7 @@ mod tests {
use crate::iff::RiffInfoList;
use crate::{Tag, TagType};
use std::io::Read;
use std::io::Cursor;
#[test]
fn parse_riff_info() {
@ -248,27 +248,50 @@ mod tests {
expected_tag.insert(String::from("IPRD"), String::from("Baz album"));
expected_tag.insert(String::from("IPRT"), String::from("1"));
let mut tag = Vec::new();
std::fs::File::open("tests/tags/assets/test.riff")
.unwrap()
.read_to_end(&mut tag)
.unwrap();
let mut reader = std::io::Cursor::new(&tag[..]);
let tag = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.riff");
let mut parsed_tag = RiffInfoList::default();
super::read::parse_riff_info(&mut reader, (tag.len() - 1) as u64, &mut parsed_tag).unwrap();
super::read::parse_riff_info(
&mut Cursor::new(&tag[..]),
(tag.len() - 1) as u64,
&mut parsed_tag,
)
.unwrap();
assert_eq!(expected_tag, parsed_tag);
}
#[test]
fn riff_info_re_read() {
let tag = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.riff");
let mut parsed_tag = RiffInfoList::default();
super::read::parse_riff_info(
&mut Cursor::new(&tag[..]),
(tag.len() - 1) as u64,
&mut parsed_tag,
)
.unwrap();
let mut writer = Vec::new();
parsed_tag.dump_to(&mut writer).unwrap();
let mut temp_parsed_tag = RiffInfoList::default();
// Remove the LIST....INFO from the tag
super::read::parse_riff_info(
&mut Cursor::new(&writer[12..]),
(tag.len() - 13) as u64,
&mut temp_parsed_tag,
)
.unwrap();
assert_eq!(parsed_tag, temp_parsed_tag);
}
#[test]
fn riff_info_to_tag() {
let mut tag_bytes = Vec::new();
std::fs::File::open("tests/tags/assets/test.riff")
.unwrap()
.read_to_end(&mut tag_bytes)
.unwrap();
let tag_bytes = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.riff");
let mut reader = std::io::Cursor::new(&tag_bytes[..]);
let mut riff_info = RiffInfoList::default();

View file

@ -418,8 +418,6 @@ mod tests {
use crate::mp4::{Atom, AtomData, AtomIdent, Ilst};
use crate::{ItemKey, Tag, TagType};
use std::io::Read;
#[test]
fn parse_ilst() {
let mut expected_tag = Ilst::default();
@ -474,24 +472,31 @@ mod tests {
AtomData::UTF8(String::from("Foo title")),
));
let mut tag = Vec::new();
std::fs::File::open("tests/tags/assets/test.ilst")
.unwrap()
.read_to_end(&mut tag)
.unwrap();
let tag = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.ilst");
let parsed_tag = super::read::parse_ilst(&mut &tag[..], tag.len() as u64).unwrap();
assert_eq!(expected_tag, parsed_tag);
}
#[test]
fn ilst_re_read() {
let tag = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.ilst");
let parsed_tag = super::read::parse_ilst(&mut &tag[..], tag.len() as u64).unwrap();
let mut writer = Vec::new();
parsed_tag.dump_to(&mut writer).unwrap();
// Remove the ilst identifier and size
let temp_parsed_tag =
super::read::parse_ilst(&mut &writer[8..], (writer.len() - 8) as u64).unwrap();
assert_eq!(parsed_tag, temp_parsed_tag);
}
#[test]
fn ilst_to_tag() {
let mut tag_bytes = Vec::new();
std::fs::File::open("tests/tags/assets/test.ilst")
.unwrap()
.read_to_end(&mut tag_bytes)
.unwrap();
let tag_bytes = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.ilst");
let ilst = super::read::parse_ilst(&mut &tag_bytes[..], tag_bytes.len() as u64).unwrap();

View file

@ -307,6 +307,14 @@ mod tests {
use std::io::Read;
fn read_tag(tag: &[u8]) -> VorbisComments {
let mut reader = std::io::Cursor::new(tag);
let mut parsed_tag = VorbisComments::default();
crate::ogg::read::read_comments(&mut reader, &mut parsed_tag).unwrap();
parsed_tag
}
#[test]
fn parse_vorbis_comments() {
let mut expected_tag = VorbisComments::default();
@ -321,20 +329,28 @@ mod tests {
expected_tag.insert_item(String::from("TITLE"), String::from("Foo title"), false);
expected_tag.insert_item(String::from("TRACKNUMBER"), String::from("1"), false);
let mut tag = Vec::new();
std::fs::File::open("tests/tags/assets/test.vorbis")
.unwrap()
.read_to_end(&mut tag)
.unwrap();
let mut reader = std::io::Cursor::new(&tag[..]);
let mut parsed_tag = VorbisComments::default();
crate::ogg::read::read_comments(&mut reader, &mut parsed_tag).unwrap();
let file_cont = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.vorbis");
let parsed_tag = read_tag(&*file_cont);
assert_eq!(expected_tag, parsed_tag);
}
#[test]
fn vorbis_comments_re_read() {
let file_cont = crate::tag_utils::test_utils::read_path("tests/tags/assets/test.vorbis");
let mut parsed_tag = read_tag(&*file_cont);
// Create a zero-size vendor for comparison
parsed_tag.vendor = String::new();
let mut writer = vec![0, 0, 0, 0];
parsed_tag.dump_to(&mut writer).unwrap();
let temp_parsed_tag = read_tag(&*writer);
assert_eq!(parsed_tag, temp_parsed_tag);
}
#[test]
fn vorbis_comments_to_tag() {
let mut tag_bytes = Vec::new();

View file

@ -110,6 +110,8 @@ pub(crate) use tag_methods;
// Used for tag conversion tests
pub(crate) mod test_utils {
use crate::{ItemKey, Tag, TagType};
use std::fs::File;
use std::io::Read;
pub(crate) fn create_tag(tag_type: TagType) -> Tag {
let mut tag = Tag::new(tag_type);
@ -138,4 +140,16 @@ pub(crate) mod test_utils {
assert_eq!(tag.get_string(&ItemKey::Genre), Some("Classical"));
}
}
pub(crate) fn read_path(path: &str) -> Vec<u8> {
read_file(&mut File::open(path).unwrap())
}
pub(crate) fn read_file(file: &mut File) -> Vec<u8> {
let mut tag = Vec::new();
file.read_to_end(&mut tag).unwrap();
tag
}
}