Skip to content

Commit e667f73

Browse files
committed
Tests: Add TagLib Vorbis Comments tests
1 parent 96bcc60 commit e667f73

File tree

3 files changed

+166
-0
lines changed

3 files changed

+166
-0
lines changed
4.37 KB
Binary file not shown.

tests/taglib/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ mod test_mp4;
1212
mod test_mpeg;
1313
mod test_speex;
1414
mod test_wavpack;
15+
mod test_xiphcomment;

tests/taglib/test_xiphcomment.rs

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
use crate::temp_file;
2+
use lofty::ogg::{OggPictureStorage, VorbisComments, VorbisFile};
3+
use lofty::{
4+
Accessor, AudioFile, MimeType, ParseOptions, Picture, PictureInformation, PictureType, TagExt,
5+
};
6+
use std::io::Seek;
7+
8+
#[test]
9+
fn test_year() {
10+
let mut cmt = VorbisComments::default();
11+
assert_eq!(cmt.year(), None);
12+
cmt.insert(String::from("YEAR"), String::from("2009"), false);
13+
assert_eq!(cmt.year(), Some(2009));
14+
15+
// NOTE: Lofty will *always* prioritize "YEAR" over "DATE". TagLib doesn't have the same ideas,
16+
// so we have to remove "YEAR".
17+
let _ = cmt.remove("YEAR");
18+
19+
cmt.insert(String::from("DATE"), String::from("2008"), false);
20+
assert_eq!(cmt.year(), Some(2008));
21+
}
22+
23+
#[test]
24+
fn test_set_year() {
25+
let mut cmt = VorbisComments::default();
26+
cmt.insert(String::from("YEAR"), String::from("2009"), false);
27+
cmt.insert(String::from("DATE"), String::from("2008"), false);
28+
cmt.set_year(1995);
29+
assert!(cmt.get("YEAR").is_none());
30+
assert_eq!(cmt.get("DATE"), Some("1995"));
31+
}
32+
33+
#[test]
34+
fn test_track() {
35+
let mut cmt = VorbisComments::default();
36+
assert_eq!(cmt.track(), None);
37+
cmt.insert(String::from("TRACKNUM"), String::from("7"), false);
38+
assert_eq!(cmt.track(), Some(7));
39+
cmt.insert(String::from("TRACKNUMBER"), String::from("8"), false);
40+
assert_eq!(cmt.year(), Some(8));
41+
}
42+
43+
#[test]
44+
fn test_set_track() {
45+
let mut cmt = VorbisComments::default();
46+
cmt.insert(String::from("TRACKNUM"), String::from("7"), false);
47+
cmt.insert(String::from("TRACKNUMBER"), String::from("8"), false);
48+
cmt.set_track(3);
49+
assert!(cmt.get("TRACKNUM").is_none());
50+
assert_eq!(cmt.get("TRACKNUMBER"), Some("3"));
51+
}
52+
53+
#[test]
54+
#[ignore]
55+
fn test_invalid_keys1() {
56+
// Marker test, Lofty does not replicate the properties API
57+
}
58+
59+
#[test]
60+
fn test_invalid_keys2() {
61+
let mut cmt = VorbisComments::default();
62+
cmt.insert(String::new(), String::new(), false);
63+
cmt.insert(String::from("A=B"), String::new(), false);
64+
cmt.insert(String::from("A~B"), String::new(), false);
65+
cmt.insert(String::from("A\x7F"), String::new(), false);
66+
cmt.insert(String::from("A\u{3456}"), String::new(), false);
67+
68+
assert!(cmt.is_empty());
69+
}
70+
71+
#[test]
72+
fn test_clear_comment() {
73+
let mut file = temp_file!("tests/taglib/data/empty.ogg");
74+
75+
{
76+
let mut f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap();
77+
file.rewind().unwrap();
78+
79+
f.vorbis_comments_mut()
80+
.insert(String::from("COMMENT"), String::from("Comment1"), false);
81+
f.save_to(&mut file).unwrap();
82+
}
83+
file.rewind().unwrap();
84+
{
85+
let mut f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap();
86+
f.vorbis_comments_mut().remove_comment();
87+
assert_eq!(f.vorbis_comments().comment(), None);
88+
}
89+
}
90+
91+
#[test]
92+
#[ignore]
93+
fn test_remove_fields() {
94+
// Market test, TagLib has some incredibly strange behavior in this test.
95+
//
96+
// When adding a field of the same key, TagLib will append each value to the same value.
97+
// Meaning:
98+
//
99+
// tag.insert("title", "Title1", false);
100+
// tag.insert("title, "Title2", false);
101+
// assert_eq!(tag.title(), Some("Title1 Title2");
102+
//
103+
// Lofty will never behave in this way.
104+
}
105+
106+
#[test]
107+
fn test_picture() {
108+
let mut file = temp_file!("tests/taglib/data/empty.ogg");
109+
110+
{
111+
let mut f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap();
112+
file.rewind().unwrap();
113+
114+
let picture = Picture::new_unchecked(
115+
PictureType::CoverBack,
116+
MimeType::Jpeg,
117+
Some(String::from("new image")),
118+
b"JPEG data".to_vec(),
119+
);
120+
let info = PictureInformation {
121+
width: 5,
122+
height: 6,
123+
color_depth: 16,
124+
num_colors: 7,
125+
};
126+
127+
f.vorbis_comments_mut()
128+
.insert_picture(picture, Some(info))
129+
.unwrap();
130+
f.save_to(&mut file).unwrap();
131+
}
132+
file.rewind().unwrap();
133+
{
134+
let f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap();
135+
let pictures = f.vorbis_comments().pictures();
136+
assert_eq!(pictures.len(), 1);
137+
assert_eq!(pictures[0].1.width, 5);
138+
assert_eq!(pictures[0].1.height, 6);
139+
assert_eq!(pictures[0].1.color_depth, 16);
140+
assert_eq!(pictures[0].1.num_colors, 7);
141+
assert_eq!(pictures[0].0.mime_type(), &MimeType::Jpeg);
142+
assert_eq!(pictures[0].0.description(), Some("new image"));
143+
assert_eq!(pictures[0].0.data(), b"JPEG data");
144+
}
145+
}
146+
147+
#[test]
148+
fn test_lowercase_fields() {
149+
let mut file = temp_file!("tests/taglib/data/lowercase-fields.ogg");
150+
151+
{
152+
let f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap();
153+
file.rewind().unwrap();
154+
155+
assert_eq!(f.vorbis_comments().title().as_deref(), Some("TEST TITLE"));
156+
assert_eq!(f.vorbis_comments().artist().as_deref(), Some("TEST ARTIST"));
157+
assert_eq!(f.vorbis_comments().pictures().len(), 1);
158+
f.save_to(&mut file).unwrap();
159+
}
160+
file.rewind().unwrap();
161+
{
162+
let f = VorbisFile::read_from(&mut file, ParseOptions::new()).unwrap();
163+
assert!(!f.vorbis_comments().pictures().is_empty());
164+
}
165+
}

0 commit comments

Comments
 (0)