Skip to content

Commit

Permalink
Added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blokfyuh committed Nov 9, 2017
1 parent 6b5d78b commit d6ec551
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
5 changes: 3 additions & 2 deletions m3u8/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ def parse(content, strict=False):
data['segment_map'] = segment_map_info

elif line.startswith(protocol.ext_x_start):
attribute_parser = {}
attribute_parser["time_offset"] = lambda x: float(x)
attribute_parser = {
"time_offset": lambda x: float(x)
}
start_info = _parse_attribute_list(protocol.ext_x_start, line, attribute_parser)
data['start'] = start_info

Expand Down
18 changes: 18 additions & 0 deletions tests/playlists.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@
#EXT-X-ENDLIST
'''

SIMPLE_PLAYLIST_WITH_START_NEGATIVE_OFFSET = '''
#EXTM3U
#EXT-X-TARGETDURATION:5220
#EXT-X-START:TIME-OFFSET=-2.0
#EXTINF:5220,
http://media.example.com/entire.ts
#EXT-X-ENDLIST
'''

SIMPLE_PLAYLIST_WITH_START_PRECISE = '''
#EXTM3U
#EXT-X-TARGETDURATION:5220
#EXT-X-START:TIME-OFFSET=10.5,PRECISE=YES
#EXTINF:5220,
http://media.example.com/entire.ts
#EXT-X-ENDLIST
'''

SIMPLE_PLAYLIST_FILENAME = abspath(
join(dirname(__file__), 'playlists/simple-playlist.m3u8'))

Expand Down
18 changes: 18 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

import arrow
import datetime

from m3u8.protocol import ext_x_start

import m3u8
import playlists
from m3u8.model import Segment, Key, Media
Expand Down Expand Up @@ -712,6 +715,21 @@ def test_segment_map_uri_attribute_with_byterange():
obj = m3u8.M3U8(playlists.MAP_URI_PLAYLIST_WITH_BYTERANGE)
assert obj.segment_map['uri'] == "main.mp4"


def test_start_with_negative_offset():
obj = m3u8.M3U8(playlists.SIMPLE_PLAYLIST_WITH_START_NEGATIVE_OFFSET)
assert obj.start.time_offset == -2.0
assert obj.start.precise is None
assert ext_x_start + ':TIME-OFFSET=-2.0\n' in obj.dumps()


def test_start_with_precise():
obj = m3u8.M3U8(playlists.SIMPLE_PLAYLIST_WITH_START_PRECISE)
assert obj.start.time_offset == 10.5
assert obj.start.precise == 'YES'
assert ext_x_start + ':TIME-OFFSET=10.5,PRECISE=YES\n' in obj.dumps()


# custom asserts


Expand Down
12 changes: 12 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,3 +275,15 @@ def test_should_parse_empty_uri_with_base_path():
assert media.uri is None
assert media.base_path is None
assert 'base_uri/' == media.base_uri


def test_should_parse_start_with_negative_time_offset():
data = m3u8.parse(playlists.SIMPLE_PLAYLIST_WITH_START_NEGATIVE_OFFSET)
assert data['start']['time_offset'] == -2.0
assert not hasattr(data['start'], 'precise')


def test_should_parse_start_with_precise():
data = m3u8.parse(playlists.SIMPLE_PLAYLIST_WITH_START_PRECISE)
assert data['start']['time_offset'] == 10.5
assert data['start']['precise'] == 'YES'

0 comments on commit d6ec551

Please sign in to comment.