mirror of
https://github.com/v0l/m3u8-rs.git
synced 2025-06-20 02:20:30 +00:00
Apply cargo fmt
This commit is contained in:
266
tests/lib.rs
266
tests/lib.rs
@ -1,20 +1,21 @@
|
||||
#![allow(unused_variables, unused_imports, dead_code)]
|
||||
|
||||
extern crate nom;
|
||||
extern crate m3u8_rs;
|
||||
extern crate nom;
|
||||
|
||||
use std::fs;
|
||||
use std::path;
|
||||
use m3u8_rs::*;
|
||||
use m3u8_rs::playlist::*;
|
||||
use std::io::Read;
|
||||
use std::fs::File;
|
||||
use m3u8_rs::*;
|
||||
use nom::*;
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path;
|
||||
|
||||
fn all_sample_m3u_playlists() -> Vec<path::PathBuf> {
|
||||
let path: std::path::PathBuf = ["sample-playlists"].iter().collect();
|
||||
fs::read_dir(path.to_str().unwrap()).unwrap()
|
||||
fs::read_dir(path.to_str().unwrap())
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
.map(|dir| dir.path())
|
||||
.filter(|path| path.extension().map_or(false, |ext| ext == "m3u8"))
|
||||
@ -41,11 +42,10 @@ fn print_parse_playlist_test(playlist_name: &str) -> bool {
|
||||
println!("Parsing playlist file: {:?}", playlist_name);
|
||||
let parsed = parse_playlist(input.as_bytes());
|
||||
|
||||
if let Result::Ok((i,o)) = parsed {
|
||||
if let Result::Ok((i, o)) = parsed {
|
||||
println!("{:?}", o);
|
||||
true
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
println!("Parsing failed:\n {:?}", parsed);
|
||||
false
|
||||
}
|
||||
@ -56,21 +56,23 @@ fn playlist_master_with_alternatives() {
|
||||
assert!(print_parse_playlist_test("master-with-alternatives.m3u8"));
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn playlist_master_with_alternatives_2_3() {
|
||||
assert!(print_parse_playlist_test("master-with-alternatives-2.m3u8"));
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn playlist_master_with_i_frame_stream_inf() {
|
||||
assert!(print_parse_playlist_test("master-with-i-frame-stream-inf.m3u8"));
|
||||
assert!(print_parse_playlist_test(
|
||||
"master-with-i-frame-stream-inf.m3u8"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn playlist_master_with_multiple_codecs() {
|
||||
assert!(print_parse_playlist_test("master-with-multiple-codecs.m3u8"));
|
||||
assert!(print_parse_playlist_test(
|
||||
"master-with-multiple-codecs.m3u8"
|
||||
));
|
||||
}
|
||||
|
||||
// -- Media playlists
|
||||
@ -82,7 +84,9 @@ fn playlist_media_standard() {
|
||||
|
||||
#[test]
|
||||
fn playlist_media_without_segments() {
|
||||
assert!(print_parse_playlist_test("media-playlist-without-segments.m3u8"));
|
||||
assert!(print_parse_playlist_test(
|
||||
"media-playlist-without-segments.m3u8"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -102,7 +106,9 @@ fn playlist_media_with_scte35() {
|
||||
|
||||
#[test]
|
||||
fn playlist_media_with_scte35_1() {
|
||||
assert!(print_parse_playlist_test("media-playlist-with-scte35-1.m3u8"));
|
||||
assert!(print_parse_playlist_test(
|
||||
"media-playlist-with-scte35-1.m3u8"
|
||||
));
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
@ -110,17 +116,23 @@ fn playlist_media_with_scte35_1() {
|
||||
|
||||
#[test]
|
||||
fn playlist_not_ending_in_newline_master() {
|
||||
assert!(print_parse_playlist_test("master-not-ending-in-newline.m3u8"));
|
||||
assert!(print_parse_playlist_test(
|
||||
"master-not-ending-in-newline.m3u8"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn playlist_not_ending_in_newline_master1() {
|
||||
assert!(print_parse_playlist_test("master-not-ending-in-newline-1.m3u8"));
|
||||
assert!(print_parse_playlist_test(
|
||||
"master-not-ending-in-newline-1.m3u8"
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn playlist_not_ending_in_newline_media() {
|
||||
assert!(print_parse_playlist_test("media-not-ending-in-newline.m3u8"));
|
||||
assert!(print_parse_playlist_test(
|
||||
"media-not-ending-in-newline.m3u8"
|
||||
));
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
@ -164,7 +176,6 @@ fn variant_stream() {
|
||||
println!("{:?}", result);
|
||||
}
|
||||
|
||||
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
// Other
|
||||
|
||||
@ -203,7 +214,6 @@ fn test_key_value_pairs() {
|
||||
println!("{:?}\n\n", res);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_key_value_pair() {
|
||||
assert_eq!(
|
||||
@ -219,7 +229,13 @@ fn test_key_value_pair() {
|
||||
fn ext_with_value() {
|
||||
assert_eq!(
|
||||
ext_tag(b"#EXT-X-CUE-OUT:DURATION=30\nxxx"),
|
||||
Result::Ok((b"xxx".as_bytes(), ExtTag { tag: "X-CUE-OUT".into(), rest: Some("DURATION=30".into()) }))
|
||||
Result::Ok((
|
||||
b"xxx".as_bytes(),
|
||||
ExtTag {
|
||||
tag: "X-CUE-OUT".into(),
|
||||
rest: Some("DURATION=30".into())
|
||||
}
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
@ -227,7 +243,13 @@ fn ext_with_value() {
|
||||
fn ext_without_value() {
|
||||
assert_eq!(
|
||||
ext_tag(b"#EXT-X-CUE-IN\nxxx"),
|
||||
Result::Ok((b"xxx".as_bytes(), ExtTag { tag: "X-CUE-IN".into(), rest: None }))
|
||||
Result::Ok((
|
||||
b"xxx".as_bytes(),
|
||||
ExtTag {
|
||||
tag: "X-CUE-IN".into(),
|
||||
rest: None
|
||||
}
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
@ -280,18 +302,12 @@ fn float_() {
|
||||
|
||||
#[test]
|
||||
fn float_no_decimal() {
|
||||
assert_eq!(
|
||||
float(b"33rest"),
|
||||
Result::Ok(("rest".as_bytes(), 33f32))
|
||||
);
|
||||
assert_eq!(float(b"33rest"), Result::Ok(("rest".as_bytes(), 33f32)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn float_should_ignore_trailing_dot() {
|
||||
assert_eq!(
|
||||
float(b"33.rest"),
|
||||
Result::Ok((".rest".as_bytes(), 33f32))
|
||||
);
|
||||
assert_eq!(float(b"33.rest"), Result::Ok((".rest".as_bytes(), 33f32)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -312,10 +328,12 @@ fn print_create_and_parse_playlist(playlist_original: &mut Playlist) -> Playlist
|
||||
let m3u8_str: &str = std::str::from_utf8(&utf8).unwrap();
|
||||
|
||||
let playlist_parsed = match *playlist_original {
|
||||
Playlist::MasterPlaylist(_) =>
|
||||
Playlist::MasterPlaylist(parse_master_playlist_res(m3u8_str.as_bytes()).unwrap()),
|
||||
Playlist::MediaPlaylist(_) =>
|
||||
Playlist::MediaPlaylist(parse_media_playlist_res(m3u8_str.as_bytes()).unwrap()),
|
||||
Playlist::MasterPlaylist(_) => {
|
||||
Playlist::MasterPlaylist(parse_master_playlist_res(m3u8_str.as_bytes()).unwrap())
|
||||
}
|
||||
Playlist::MediaPlaylist(_) => {
|
||||
Playlist::MediaPlaylist(parse_media_playlist_res(m3u8_str.as_bytes()).unwrap())
|
||||
}
|
||||
};
|
||||
|
||||
print!("\n\n---- utf8 result\n\n{}", m3u8_str);
|
||||
@ -327,64 +345,57 @@ fn print_create_and_parse_playlist(playlist_original: &mut Playlist) -> Playlist
|
||||
|
||||
#[test]
|
||||
fn create_and_parse_master_playlist_empty() {
|
||||
let mut playlist_original = Playlist::MasterPlaylist(MasterPlaylist { ..Default::default() });
|
||||
let mut playlist_original = Playlist::MasterPlaylist(MasterPlaylist {
|
||||
..Default::default()
|
||||
});
|
||||
let playlist_parsed = print_create_and_parse_playlist(&mut playlist_original);
|
||||
assert_eq!(playlist_original, playlist_parsed);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_and_parse_master_playlist_full() {
|
||||
|
||||
let mut playlist_original = Playlist::MasterPlaylist(MasterPlaylist {
|
||||
version: 6,
|
||||
alternatives: vec! [
|
||||
AlternativeMedia {
|
||||
media_type: AlternativeMediaType::Audio,
|
||||
uri: Some("alt-media-uri".into()),
|
||||
group_id: "group-id".into(),
|
||||
language: Some("language".into()),
|
||||
assoc_language: Some("assoc-language".into()),
|
||||
name: "Xmedia".into(),
|
||||
default: true, // Its absence indicates an implicit value of NO
|
||||
autoselect: true, // Its absence indicates an implicit value of NO
|
||||
forced: true, // Its absence indicates an implicit value of NO
|
||||
instream_id: Some("instream_id".into()),
|
||||
characteristics: Some("characteristics".into()),
|
||||
channels: Some("channels".into()),
|
||||
}
|
||||
],
|
||||
variants: vec![
|
||||
VariantStream {
|
||||
is_i_frame: false,
|
||||
uri: "masterplaylist-uri".into(),
|
||||
bandwidth: "10010010".into(),
|
||||
average_bandwidth: Some("10010010".into()),
|
||||
codecs: Some("TheCODEC".into()),
|
||||
resolution: Some("1000x3000".into()),
|
||||
frame_rate: Some("60".into()),
|
||||
hdcp_level: Some("NONE".into()),
|
||||
audio: Some("audio".into()),
|
||||
video: Some("video".into()),
|
||||
subtitles: Some("subtitles".into()),
|
||||
closed_captions: Some("closed_captions".into()),
|
||||
}
|
||||
],
|
||||
session_data: vec![
|
||||
SessionData {
|
||||
data_id: "****".into(),
|
||||
field: SessionDataField::Value("%%%%".to_string()),
|
||||
language: Some("SessionDataLanguage".into()),
|
||||
}
|
||||
],
|
||||
session_key: vec![
|
||||
SessionKey(Key {
|
||||
method: "AES-128".into(),
|
||||
uri: Some("https://secure.domain.com".into()),
|
||||
iv: Some("0xb059217aa2649ce170b734".into()),
|
||||
keyformat: Some("xXkeyformatXx".into()),
|
||||
keyformatversions: Some("xXFormatVers".into()),
|
||||
})
|
||||
],
|
||||
alternatives: vec![AlternativeMedia {
|
||||
media_type: AlternativeMediaType::Audio,
|
||||
uri: Some("alt-media-uri".into()),
|
||||
group_id: "group-id".into(),
|
||||
language: Some("language".into()),
|
||||
assoc_language: Some("assoc-language".into()),
|
||||
name: "Xmedia".into(),
|
||||
default: true, // Its absence indicates an implicit value of NO
|
||||
autoselect: true, // Its absence indicates an implicit value of NO
|
||||
forced: true, // Its absence indicates an implicit value of NO
|
||||
instream_id: Some("instream_id".into()),
|
||||
characteristics: Some("characteristics".into()),
|
||||
channels: Some("channels".into()),
|
||||
}],
|
||||
variants: vec![VariantStream {
|
||||
is_i_frame: false,
|
||||
uri: "masterplaylist-uri".into(),
|
||||
bandwidth: "10010010".into(),
|
||||
average_bandwidth: Some("10010010".into()),
|
||||
codecs: Some("TheCODEC".into()),
|
||||
resolution: Some("1000x3000".into()),
|
||||
frame_rate: Some("60".into()),
|
||||
hdcp_level: Some("NONE".into()),
|
||||
audio: Some("audio".into()),
|
||||
video: Some("video".into()),
|
||||
subtitles: Some("subtitles".into()),
|
||||
closed_captions: Some("closed_captions".into()),
|
||||
}],
|
||||
session_data: vec![SessionData {
|
||||
data_id: "****".into(),
|
||||
field: SessionDataField::Value("%%%%".to_string()),
|
||||
language: Some("SessionDataLanguage".into()),
|
||||
}],
|
||||
session_key: vec![SessionKey(Key {
|
||||
method: "AES-128".into(),
|
||||
uri: Some("https://secure.domain.com".into()),
|
||||
iv: Some("0xb059217aa2649ce170b734".into()),
|
||||
keyformat: Some("xXkeyformatXx".into()),
|
||||
keyformatversions: Some("xXFormatVers".into()),
|
||||
})],
|
||||
start: Some(Start {
|
||||
time_offset: "123123123".into(),
|
||||
precise: Some("YES".into()),
|
||||
@ -398,7 +409,9 @@ fn create_and_parse_master_playlist_full() {
|
||||
|
||||
#[test]
|
||||
fn create_and_parse_media_playlist_empty() {
|
||||
let mut playlist_original = Playlist::MediaPlaylist(MediaPlaylist { ..Default::default() });
|
||||
let mut playlist_original = Playlist::MediaPlaylist(MediaPlaylist {
|
||||
..Default::default()
|
||||
});
|
||||
let playlist_parsed = print_create_and_parse_playlist(&mut playlist_original);
|
||||
assert_eq!(playlist_original, playlist_parsed);
|
||||
}
|
||||
@ -406,15 +419,13 @@ fn create_and_parse_media_playlist_empty() {
|
||||
#[test]
|
||||
fn create_and_parse_media_playlist_single_segment() {
|
||||
let mut playlist_original = Playlist::MediaPlaylist(MediaPlaylist {
|
||||
segments: vec![
|
||||
MediaSegment {
|
||||
uri: "20140311T113819-01-338559live.ts".into(),
|
||||
duration: 2.002,
|
||||
title: Some("hey".into()),
|
||||
..Default::default()
|
||||
},
|
||||
],
|
||||
..Default::default()
|
||||
segments: vec![MediaSegment {
|
||||
uri: "20140311T113819-01-338559live.ts".into(),
|
||||
duration: 2.002,
|
||||
title: Some("hey".into()),
|
||||
..Default::default()
|
||||
}],
|
||||
..Default::default()
|
||||
});
|
||||
let playlist_parsed = print_create_and_parse_playlist(&mut playlist_original);
|
||||
assert_eq!(playlist_original, playlist_parsed);
|
||||
@ -422,7 +433,6 @@ fn create_and_parse_media_playlist_single_segment() {
|
||||
|
||||
#[test]
|
||||
fn create_and_parse_media_playlist_full() {
|
||||
|
||||
let mut playlist_original = Playlist::MediaPlaylist(MediaPlaylist {
|
||||
version: 4,
|
||||
target_duration: 3.0,
|
||||
@ -436,34 +446,36 @@ fn create_and_parse_media_playlist_full() {
|
||||
precise: Some("YES".into()),
|
||||
}),
|
||||
independent_segments: true,
|
||||
segments: vec![
|
||||
MediaSegment {
|
||||
uri: "20140311T113819-01-338559live.ts".into(),
|
||||
duration: 2.002,
|
||||
title: Some("338559".into()),
|
||||
byte_range: Some(ByteRange { length: 137116, offset: Some(4559) }),
|
||||
discontinuity: true,
|
||||
key: Some(Key {
|
||||
method: "AES-128".into(),
|
||||
uri: Some("https://secure.domain.com".into()),
|
||||
iv: Some("0xb059217aa2649ce170b734".into()),
|
||||
keyformat: Some("xXkeyformatXx".into()),
|
||||
keyformatversions: Some("xXFormatVers".into()),
|
||||
segments: vec![MediaSegment {
|
||||
uri: "20140311T113819-01-338559live.ts".into(),
|
||||
duration: 2.002,
|
||||
title: Some("338559".into()),
|
||||
byte_range: Some(ByteRange {
|
||||
length: 137116,
|
||||
offset: Some(4559),
|
||||
}),
|
||||
discontinuity: true,
|
||||
key: Some(Key {
|
||||
method: "AES-128".into(),
|
||||
uri: Some("https://secure.domain.com".into()),
|
||||
iv: Some("0xb059217aa2649ce170b734".into()),
|
||||
keyformat: Some("xXkeyformatXx".into()),
|
||||
keyformatversions: Some("xXFormatVers".into()),
|
||||
}),
|
||||
map: Some(Map {
|
||||
uri: "www.map-uri.com".into(),
|
||||
byte_range: Some(ByteRange {
|
||||
length: 137116,
|
||||
offset: Some(4559),
|
||||
}),
|
||||
map: Some(Map {
|
||||
uri: "www.map-uri.com".into(),
|
||||
byte_range: Some(ByteRange { length: 137116, offset: Some(4559) }),
|
||||
}),
|
||||
program_date_time: Some("broodlordinfestorgg".into()),
|
||||
daterange: None,
|
||||
unknown_tags: vec![
|
||||
ExtTag {
|
||||
tag: "X-CUE-OUT".into(),
|
||||
rest: Some("DURATION=2.002".into())
|
||||
}
|
||||
]
|
||||
},
|
||||
],
|
||||
}),
|
||||
program_date_time: Some("broodlordinfestorgg".into()),
|
||||
daterange: None,
|
||||
unknown_tags: vec![ExtTag {
|
||||
tag: "X-CUE-OUT".into(),
|
||||
rest: Some("DURATION=2.002".into()),
|
||||
}],
|
||||
}],
|
||||
});
|
||||
let playlist_parsed = print_create_and_parse_playlist(&mut playlist_original);
|
||||
assert_eq!(playlist_original, playlist_parsed);
|
||||
@ -472,7 +484,6 @@ fn create_and_parse_media_playlist_full() {
|
||||
//
|
||||
// Roundtrip
|
||||
|
||||
|
||||
#[test]
|
||||
fn parsing_write_to_should_produce_the_same_structure() {
|
||||
for playlist in all_sample_m3u_playlists() {
|
||||
@ -485,8 +496,11 @@ fn parsing_write_to_should_produce_the_same_structure() {
|
||||
let actual = parse_playlist_res(&written).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
expected, actual,
|
||||
expected,
|
||||
actual,
|
||||
"\n\nFailed parser input:\n\n{}\n\nOriginal input:\n\n{}",
|
||||
std::str::from_utf8(&written).unwrap(), input);
|
||||
std::str::from_utf8(&written).unwrap(),
|
||||
input
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user