1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-05-24 11:18:29 +00:00

added more tests

This commit is contained in:
Luro02 2019-09-08 12:49:22 +02:00
parent cf97a45f60
commit 91c6698f16
3 changed files with 69 additions and 20 deletions

View file

@ -38,9 +38,17 @@ mod test {
use super::*;
#[test]
fn extm3u() {
fn test_display() {
assert_eq!(ExtM3u.to_string(), "#EXTM3U".to_string());
}
#[test]
fn test_parser() {
assert_eq!("#EXTM3U".parse::<ExtM3u>().ok(), Some(ExtM3u));
assert_eq!(ExtM3u.to_string(), "#EXTM3U");
}
#[test]
fn test_requires_version() {
assert_eq!(ExtM3u.requires_version(), ProtocolVersion::V1);
}
}

View file

@ -7,21 +7,19 @@ use std::str::FromStr;
///
/// [4.3.1.2. EXT-X-VERSION]: https://tools.ietf.org/html/rfc8216#section-4.3.1.2
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ExtXVersion {
version: ProtocolVersion,
}
pub struct ExtXVersion(ProtocolVersion);
impl ExtXVersion {
pub(crate) const PREFIX: &'static str = "#EXT-X-VERSION:";
/// Makes a new `ExtXVersion` tag.
pub const fn new(version: ProtocolVersion) -> Self {
ExtXVersion { version }
Self(version)
}
/// Returns the protocol compatibility version of the playlist containing this tag.
pub const fn version(&self) -> ProtocolVersion {
self.version
self.0
}
/// Returns the protocol compatibility version that this tag requires.
@ -32,17 +30,18 @@ impl ExtXVersion {
impl fmt::Display for ExtXVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}{}", Self::PREFIX, self.version)
write!(f, "{}{}", Self::PREFIX, self.0)
}
}
impl FromStr for ExtXVersion {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
track_assert!(s.starts_with(Self::PREFIX), ErrorKind::InvalidInput);
let suffix = s.split_at(Self::PREFIX.len()).1;
let version = track!(suffix.parse())?;
Ok(ExtXVersion { version })
Ok(ExtXVersion::new(version))
}
}
@ -51,11 +50,34 @@ mod test {
use super::*;
#[test]
fn ext_x_version() {
let tag = ExtXVersion::new(ProtocolVersion::V6);
assert_eq!("#EXT-X-VERSION:6".parse().ok(), Some(tag));
assert_eq!(tag.to_string(), "#EXT-X-VERSION:6");
assert_eq!(tag.version(), ProtocolVersion::V6);
assert_eq!(tag.requires_version(), ProtocolVersion::V1);
fn test_display() {
assert_eq!(
ExtXVersion::new(ProtocolVersion::V6).to_string(),
"#EXT-X-VERSION:6"
);
}
#[test]
fn test_parser() {
assert_eq!(
"#EXT-X-VERSION:6".parse().ok(),
Some(ExtXVersion::new(ProtocolVersion::V6))
);
}
#[test]
fn test_requires_version() {
assert_eq!(
ExtXVersion::new(ProtocolVersion::V6).requires_version(),
ProtocolVersion::V1
);
}
#[test]
fn test_version() {
assert_eq!(
ExtXVersion::new(ProtocolVersion::V6).version(),
ProtocolVersion::V6
);
}
}

View file

@ -152,11 +152,30 @@ mod test {
use super::*;
#[test]
fn ext_x_i_frame_stream_inf() {
let tag = ExtXIFrameStreamInf::new("foo", 1000);
fn test_display() {
let text = r#"#EXT-X-I-FRAME-STREAM-INF:URI="foo",BANDWIDTH=1000"#;
assert_eq!(text.parse().ok(), Some(tag.clone()));
assert_eq!(tag.to_string(), text);
assert_eq!(tag.requires_version(), ProtocolVersion::V1);
assert_eq!(ExtXIFrameStreamInf::new("foo", 1000).to_string(), text);
}
#[test]
fn test_parser() {
let text = r#"#EXT-X-I-FRAME-STREAM-INF:URI="foo",BANDWIDTH=1000"#;
let i_frame_stream_inf = ExtXIFrameStreamInf::new("foo", 1000);
assert_eq!(
text.parse::<ExtXIFrameStreamInf>().unwrap(),
i_frame_stream_inf.clone()
);
assert_eq!(i_frame_stream_inf.uri(), "foo");
assert_eq!(i_frame_stream_inf.bandwidth(), 1000);
// TODO: test all the optional fields
}
#[test]
fn test_requires_version() {
assert_eq!(
ExtXIFrameStreamInf::new("foo", 1000).requires_version(),
ProtocolVersion::V1
);
}
}