Add tests and FileProperties methods

This commit is contained in:
Serial 2021-07-24 15:41:17 -04:00
parent 553db7a0fe
commit 3d6f1f846e
2 changed files with 72 additions and 1 deletions

View file

@ -3,7 +3,7 @@ pub(crate) mod tags;
use std::time::Duration;
/// Various audio properties
/// Various *immutable* audio properties
///
/// NOTE: All fields are invalidated after any type of conversion
pub struct FileProperties {
@ -23,3 +23,40 @@ impl Default for FileProperties {
}
}
}
impl FileProperties {
/// Create a new FileProperties
pub const fn new(
duration: Duration,
bitrate: Option<u32>,
sample_rate: Option<u32>,
channels: Option<u8>,
) -> Self {
Self {
duration,
bitrate,
sample_rate,
channels,
}
}
/// Duration
pub fn duration(&self) -> Duration {
self.duration
}
/// Bitrate (kbps)
pub fn bitrate(&self) -> Option<u32> {
self.bitrate
}
/// Sample rate (Hz)
pub fn sample_rate(&self) -> Option<u32> {
self.sample_rate
}
/// Channel count
pub fn channels(&self) -> Option<u8> {
self.channels
}
}

34
tests/properties.rs Normal file
View file

@ -0,0 +1,34 @@
use lofty::{FileProperties, Tag};
use std::time::Duration;
const OPUS_PROPERTIES: FileProperties =
FileProperties::new(Duration::from_millis(1428), Some(120), Some(48000), Some(2));
const VORBIS_PROPERTIES: FileProperties =
FileProperties::new(Duration::from_millis(1450), Some(112), Some(48000), Some(2));
const FLAC_PROPERTIES: FileProperties = FileProperties::new(
Duration::from_millis(1428),
Some(35084),
Some(48000),
Some(2),
);
macro_rules! properties_test {
($function:ident, $path:expr, $expected:ident) => {
#[test]
fn $function() {
let tag = Tag::new().read_from_path_signature($path).unwrap();
let read_properties = tag.properties();
assert_eq!(read_properties.duration(), $expected.duration());
assert_eq!(read_properties.sample_rate(), $expected.sample_rate());
assert_eq!(read_properties.channels(), $expected.channels());
}
};
}
properties_test!(test_opus, "tests/assets/a.opus", OPUS_PROPERTIES);
properties_test!(test_vorbis, "tests/assets/a.ogg", VORBIS_PROPERTIES);
properties_test!(test_flac, "tests/assets/a.flac", FLAC_PROPERTIES);