1
0
Fork 0
mirror of https://github.com/sile/hls_m3u8.git synced 2024-06-09 08:49:28 +00:00

more tests

This commit is contained in:
Luro02 2019-09-27 14:30:46 +02:00
parent 8368df0c90
commit d845598b43
6 changed files with 96 additions and 49 deletions

View file

@ -23,12 +23,8 @@ impl ExtXStart {
/// # Panic
/// Panics if the time_offset value is infinite.
pub fn new(time_offset: f64) -> Self {
if time_offset.is_infinite() {
panic!("EXT-X-START: Floating point value must be finite!");
}
ExtXStart {
time_offset: SignedDecimalFloatingPoint::new(time_offset).unwrap(),
Self {
time_offset: SignedDecimalFloatingPoint::new(time_offset),
precise: false,
}
}
@ -38,12 +34,8 @@ impl ExtXStart {
/// # Panic
/// Panics if the time_offset value is infinite.
pub fn with_precise(time_offset: f64, precise: bool) -> Self {
if time_offset.is_infinite() {
panic!("EXT-X-START: Floating point value must be finite!");
}
ExtXStart {
time_offset: SignedDecimalFloatingPoint::new(time_offset).unwrap(),
Self {
time_offset: SignedDecimalFloatingPoint::new(time_offset),
precise,
}
}

View file

@ -16,11 +16,18 @@ pub struct ByteRange {
impl ByteRange {
/// Creates a new [ByteRange].
///
/// # Example
/// ```
/// # use hls_m3u8::types::ByteRange;
/// ByteRange::new(22, Some(12));
/// ```
pub const fn new(length: usize, start: Option<usize>) -> Self {
Self { length, start }
}
/// Returns the length of the range.
///
/// # Example
/// ```
/// # use hls_m3u8::types::ByteRange;
@ -32,6 +39,7 @@ impl ByteRange {
}
/// Sets the length of the range.
///
/// # Example
/// ```
/// # use hls_m3u8::types::ByteRange;
@ -48,6 +56,7 @@ impl ByteRange {
}
/// Returns the start of the range.
///
/// # Example
/// ```
/// # use hls_m3u8::types::ByteRange;
@ -59,6 +68,7 @@ impl ByteRange {
}
/// Sets the start of the range.
///
/// # Example
/// ```
/// # use hls_m3u8::types::ByteRange;
@ -103,7 +113,7 @@ impl FromStr for ByteRange {
None
}
};
Ok(ByteRange::new(length, start))
Ok(Self::new(length, start))
}
}

View file

@ -2,7 +2,7 @@ use std::fmt;
use std::str::FromStr;
use crate::utils::{quote, unquote};
use crate::{Error, Result};
use crate::Error;
/// The identifier of a closed captions group or its absence.
///
@ -10,7 +10,7 @@ use crate::{Error, Result};
///
/// [4.3.4.2. EXT-X-STREAM-INF]: https://tools.ietf.org/html/rfc8216#section-4.3.4.2
#[allow(missing_docs)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub enum ClosedCaptions {
GroupId(String),
None,
@ -19,19 +19,20 @@ pub enum ClosedCaptions {
impl fmt::Display for ClosedCaptions {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self {
ClosedCaptions::GroupId(value) => write!(f, "{}", quote(value)),
ClosedCaptions::None => "NONE".fmt(f),
Self::GroupId(value) => write!(f, "{}", quote(value)),
Self::None => write!(f, "NONE"),
}
}
}
impl FromStr for ClosedCaptions {
type Err = Error;
fn from_str(s: &str) -> Result<Self> {
if s == "NONE" {
Ok(ClosedCaptions::None)
fn from_str(input: &str) -> Result<Self, Self::Err> {
if input.trim() == "NONE" {
Ok(Self::None)
} else {
Ok(ClosedCaptions::GroupId(unquote(s)))
Ok(Self::GroupId(unquote(input)))
}
}
}
@ -42,21 +43,23 @@ mod tests {
#[test]
fn test_display() {
let closed_captions = ClosedCaptions::None;
assert_eq!(closed_captions.to_string(), "NONE".to_string());
assert_eq!(ClosedCaptions::None.to_string(), "NONE".to_string());
let closed_captions = ClosedCaptions::GroupId("value".into());
assert_eq!(closed_captions.to_string(), "\"value\"".to_string());
assert_eq!(
ClosedCaptions::GroupId("value".into()).to_string(),
"\"value\"".to_string()
);
}
#[test]
fn test_parser() {
let closed_captions = ClosedCaptions::None;
assert_eq!(closed_captions, "NONE".parse::<ClosedCaptions>().unwrap());
let closed_captions = ClosedCaptions::GroupId("value".into());
assert_eq!(
closed_captions,
ClosedCaptions::None,
"NONE".parse::<ClosedCaptions>().unwrap()
);
assert_eq!(
ClosedCaptions::GroupId("value".into()),
"\"value\"".parse::<ClosedCaptions>().unwrap()
);
}

View file

@ -20,10 +20,10 @@ impl DecimalFloatingPoint {
/// The given value must have a positive sign and be finite,
/// otherwise this function will return an error that has the kind `ErrorKind::InvalidInput`.
pub fn new(n: f64) -> crate::Result<Self> {
if !n.is_sign_positive() || !n.is_finite() {
if n.is_sign_negative() || n.is_infinite() {
return Err(Error::invalid_input());
}
Ok(DecimalFloatingPoint(n))
Ok(Self(n))
}
/// Converts `DecimalFloatingPoint` to `f64`.
@ -61,8 +61,7 @@ impl FromStr for DecimalFloatingPoint {
if !input.chars().all(|c| c.is_digit(10) || c == '.') {
return Err(Error::invalid_input());
}
let n = input.parse()?;
DecimalFloatingPoint::new(n)
Self::new(input.parse()?)
}
}
@ -110,7 +109,7 @@ mod tests {
#[test]
fn test_new() {
assert!(DecimalFloatingPoint::new(std::f64::INFINITY).is_err());
assert!(DecimalFloatingPoint::new(::std::f64::INFINITY).is_err());
assert!(DecimalFloatingPoint::new(-1.0).is_err());
}

View file

@ -49,6 +49,17 @@ impl fmt::Display for DecimalResolution {
}
}
/// [DecimalResolution] can be constructed from a tuple; (width, height).
impl<T, U> From<(T, U)> for DecimalResolution
where
T: Into<usize>,
U: Into<usize>,
{
fn from(value: (T, U)) -> Self {
Self::new(value.0.into(), value.1.into())
}
}
impl FromStr for DecimalResolution {
type Err = Error;
@ -65,7 +76,7 @@ impl FromStr for DecimalResolution {
let width = tokens[0];
let height = tokens[1];
Ok(DecimalResolution {
Ok(Self {
width: width.parse()?,
height: height.parse()?,
})

View file

@ -12,21 +12,18 @@ use crate::Error;
pub(crate) struct SignedDecimalFloatingPoint(f64);
impl SignedDecimalFloatingPoint {
/// Makes a new `SignedDecimalFloatingPoint` instance.
/// Makes a new [SignedDecimalFloatingPoint] instance.
///
/// # Errors
///
/// The given value must be finite,
/// otherwise this function will return an error that has the kind `ErrorKind::InvalidInput`.
pub fn new(n: f64) -> crate::Result<Self> {
if !n.is_finite() {
Err(Error::invalid_input())
} else {
Ok(SignedDecimalFloatingPoint(n))
/// # Panics
/// The given value must be finite, otherwise this function will panic!
pub fn new(n: f64) -> Self {
if n.is_infinite() {
panic!("Floating point value must be finite!");
}
Self(n)
}
/// Converts `DecimalFloatingPoint` to `f64`.
/// Converts [DecimalFloatingPoint] to [f64].
pub const fn as_f64(self) -> f64 {
self.0
}
@ -34,7 +31,7 @@ impl SignedDecimalFloatingPoint {
impl From<i32> for SignedDecimalFloatingPoint {
fn from(f: i32) -> Self {
SignedDecimalFloatingPoint(f64::from(f))
Self(f64::from(f))
}
}
@ -50,6 +47,41 @@ impl FromStr for SignedDecimalFloatingPoint {
type Err = Error;
fn from_str(input: &str) -> Result<Self, Self::Err> {
SignedDecimalFloatingPoint::new(input.parse().map_err(Error::parse_float_error)?)
Ok(Self::new(input.parse().map_err(Error::parse_float_error)?))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_display() {
assert_eq!(
SignedDecimalFloatingPoint::new(1.0).to_string(),
1.0f64.to_string()
);
}
#[test]
#[should_panic]
fn test_new_panic() {
SignedDecimalFloatingPoint::new(::std::f64::INFINITY);
}
#[test]
fn test_parser() {
assert_eq!(
SignedDecimalFloatingPoint::new(1.0),
"1.0".parse::<SignedDecimalFloatingPoint>().unwrap()
);
}
#[test]
fn test_from() {
assert_eq!(
SignedDecimalFloatingPoint::from(1i32),
SignedDecimalFloatingPoint::new(1.0)
);
}
}