4 from __future__
import unicode_literals
6 # Allow direct execution
10 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
13 # Various small unit tests
16 import xml
.etree
.ElementTree
18 from youtube_dl
.utils
import (
43 sanitize_url_path_consecutive_slashes
,
69 class TestUtil(unittest
.TestCase
):
70 def test_timeconvert(self
):
71 self
.assertTrue(timeconvert('') is None)
72 self
.assertTrue(timeconvert('bougrg') is None)
74 def test_sanitize_filename(self
):
75 self
.assertEqual(sanitize_filename('abc'), 'abc')
76 self
.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
78 self
.assertEqual(sanitize_filename('123'), '123')
80 self
.assertEqual('abc_de', sanitize_filename('abc/de'))
81 self
.assertFalse('/' in sanitize_filename('abc/de///'))
83 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
84 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
85 self
.assertEqual('yes no', sanitize_filename('yes? no'))
86 self
.assertEqual('this - that', sanitize_filename('this: that'))
88 self
.assertEqual(sanitize_filename('AT&T'), 'AT&T')
90 self
.assertEqual(sanitize_filename(aumlaut
), aumlaut
)
91 tests
= '\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430'
92 self
.assertEqual(sanitize_filename(tests
), tests
)
95 sanitize_filename('New World record at 0:12:34'),
96 'New World record at 0_12_34')
98 self
.assertEqual(sanitize_filename('--gasdgf'), '_-gasdgf')
99 self
.assertEqual(sanitize_filename('--gasdgf', is_id
=True), '--gasdgf')
100 self
.assertEqual(sanitize_filename('.gasdgf'), 'gasdgf')
101 self
.assertEqual(sanitize_filename('.gasdgf', is_id
=True), '.gasdgf')
105 for fbc
in forbidden
:
106 self
.assertTrue(fbc
not in sanitize_filename(fc
))
108 def test_sanitize_filename_restricted(self
):
109 self
.assertEqual(sanitize_filename('abc', restricted
=True), 'abc')
110 self
.assertEqual(sanitize_filename('abc_d-e', restricted
=True), 'abc_d-e')
112 self
.assertEqual(sanitize_filename('123', restricted
=True), '123')
114 self
.assertEqual('abc_de', sanitize_filename('abc/de', restricted
=True))
115 self
.assertFalse('/' in sanitize_filename('abc/de///', restricted
=True))
117 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted
=True))
118 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted
=True))
119 self
.assertEqual('yes_no', sanitize_filename('yes? no', restricted
=True))
120 self
.assertEqual('this_-_that', sanitize_filename('this: that', restricted
=True))
122 tests
= 'a\xe4b\u4e2d\u56fd\u7684c'
123 self
.assertEqual(sanitize_filename(tests
, restricted
=True), 'a_b_c')
124 self
.assertTrue(sanitize_filename('\xf6', restricted
=True) != '') # No empty filename
126 forbidden
= '"\0\\/&!: \'\t\n()[]{}$;`^,#'
128 for fbc
in forbidden
:
129 self
.assertTrue(fbc
not in sanitize_filename(fc
, restricted
=True))
131 # Handle a common case more neatly
132 self
.assertEqual(sanitize_filename('\u5927\u58f0\u5e26 - Song', restricted
=True), 'Song')
133 self
.assertEqual(sanitize_filename('\u603b\u7edf: Speech', restricted
=True), 'Speech')
134 # .. but make sure the file name is never empty
135 self
.assertTrue(sanitize_filename('-', restricted
=True) != '')
136 self
.assertTrue(sanitize_filename(':', restricted
=True) != '')
138 def test_sanitize_ids(self
):
139 self
.assertEqual(sanitize_filename('_n_cd26wFpw', is_id
=True), '_n_cd26wFpw')
140 self
.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id
=True), '_BD_eEpuzXw')
141 self
.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id
=True), 'N0Y__7-UOdI')
143 def test_sanitize_path(self
):
144 if sys
.platform
!= 'win32':
147 self
.assertEqual(sanitize_path('abc'), 'abc')
148 self
.assertEqual(sanitize_path('abc/def'), 'abc\\def')
149 self
.assertEqual(sanitize_path('abc\\def'), 'abc\\def')
150 self
.assertEqual(sanitize_path('abc|def'), 'abc#def')
151 self
.assertEqual(sanitize_path('<>:"|?*'), '#######')
152 self
.assertEqual(sanitize_path('C:/abc/def'), 'C:\\abc\\def')
153 self
.assertEqual(sanitize_path('C?:/abc/def'), 'C##\\abc\\def')
155 self
.assertEqual(sanitize_path('\\\\?\\UNC\\ComputerName\\abc'), '\\\\?\\UNC\\ComputerName\\abc')
156 self
.assertEqual(sanitize_path('\\\\?\\UNC/ComputerName/abc'), '\\\\?\\UNC\\ComputerName\\abc')
158 self
.assertEqual(sanitize_path('\\\\?\\C:\\abc'), '\\\\?\\C:\\abc')
159 self
.assertEqual(sanitize_path('\\\\?\\C:/abc'), '\\\\?\\C:\\abc')
160 self
.assertEqual(sanitize_path('\\\\?\\C:\\ab?c\\de:f'), '\\\\?\\C:\\ab#c\\de#f')
161 self
.assertEqual(sanitize_path('\\\\?\\C:\\abc'), '\\\\?\\C:\\abc')
164 sanitize_path('youtube/%(uploader)s/%(autonumber)s-%(title)s-%(upload_date)s.%(ext)s'),
165 'youtube\\%(uploader)s\\%(autonumber)s-%(title)s-%(upload_date)s.%(ext)s')
168 sanitize_path('youtube/TheWreckingYard ./00001-Not bad, Especially for Free! (1987 Yamaha 700)-20141116.mp4.part'),
169 'youtube\\TheWreckingYard #\\00001-Not bad, Especially for Free! (1987 Yamaha 700)-20141116.mp4.part')
170 self
.assertEqual(sanitize_path('abc/def...'), 'abc\\def..#')
171 self
.assertEqual(sanitize_path('abc.../def'), 'abc..#\\def')
172 self
.assertEqual(sanitize_path('abc.../def...'), 'abc..#\\def..#')
174 self
.assertEqual(sanitize_path('../abc'), '..\\abc')
175 self
.assertEqual(sanitize_path('../../abc'), '..\\..\\abc')
176 self
.assertEqual(sanitize_path('./abc'), 'abc')
177 self
.assertEqual(sanitize_path('./../abc'), '..\\abc')
179 def test_sanitize_url_path_consecutive_slashes(self
):
181 sanitize_url_path_consecutive_slashes('http://hostname/foo//bar/filename.html'),
182 'http://hostname/foo/bar/filename.html')
184 sanitize_url_path_consecutive_slashes('http://hostname//foo/bar/filename.html'),
185 'http://hostname/foo/bar/filename.html')
187 sanitize_url_path_consecutive_slashes('http://hostname//'),
190 sanitize_url_path_consecutive_slashes('http://hostname/foo/bar/filename.html'),
191 'http://hostname/foo/bar/filename.html')
193 sanitize_url_path_consecutive_slashes('http://hostname/'),
196 sanitize_url_path_consecutive_slashes('http://hostname/abc//'),
197 'http://hostname/abc/')
199 def test_prepend_extension(self
):
200 self
.assertEqual(prepend_extension('abc.ext', 'temp'), 'abc.temp.ext')
201 self
.assertEqual(prepend_extension('abc.ext', 'temp', 'ext'), 'abc.temp.ext')
202 self
.assertEqual(prepend_extension('abc.unexpected_ext', 'temp', 'ext'), 'abc.unexpected_ext.temp')
203 self
.assertEqual(prepend_extension('abc', 'temp'), 'abc.temp')
204 self
.assertEqual(prepend_extension('.abc', 'temp'), '.abc.temp')
205 self
.assertEqual(prepend_extension('.abc.ext', 'temp'), '.abc.temp.ext')
207 def test_replace_extension(self
):
208 self
.assertEqual(replace_extension('abc.ext', 'temp'), 'abc.temp')
209 self
.assertEqual(replace_extension('abc.ext', 'temp', 'ext'), 'abc.temp')
210 self
.assertEqual(replace_extension('abc.unexpected_ext', 'temp', 'ext'), 'abc.unexpected_ext.temp')
211 self
.assertEqual(replace_extension('abc', 'temp'), 'abc.temp')
212 self
.assertEqual(replace_extension('.abc', 'temp'), '.abc.temp')
213 self
.assertEqual(replace_extension('.abc.ext', 'temp'), '.abc.temp')
215 def test_ordered_set(self
):
216 self
.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
217 self
.assertEqual(orderedSet([]), [])
218 self
.assertEqual(orderedSet([1]), [1])
219 # keep the list ordered
220 self
.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
222 def test_unescape_html(self
):
223 self
.assertEqual(unescapeHTML('%20;'), '%20;')
224 self
.assertEqual(unescapeHTML('/'), '/')
225 self
.assertEqual(unescapeHTML('/'), '/')
227 unescapeHTML('é'), 'é')
229 def test_daterange(self
):
230 _20century
= DateRange("19000101", "20000101")
231 self
.assertFalse("17890714" in _20century
)
232 _ac
= DateRange("00010101")
233 self
.assertTrue("19690721" in _ac
)
234 _firstmilenium
= DateRange(end
="10000101")
235 self
.assertTrue("07110427" in _firstmilenium
)
237 def test_unified_dates(self
):
238 self
.assertEqual(unified_strdate('December 21, 2010'), '20101221')
239 self
.assertEqual(unified_strdate('8/7/2009'), '20090708')
240 self
.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
241 self
.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
242 self
.assertEqual(unified_strdate('1968 12 10'), '19681210')
243 self
.assertEqual(unified_strdate('1968-12-10'), '19681210')
244 self
.assertEqual(unified_strdate('28/01/2014 21:00:00 +0100'), '20140128')
246 unified_strdate('11/26/2014 11:30:00 AM PST', day_first
=False),
249 unified_strdate('2/2/2015 6:47:40 PM', day_first
=False),
251 self
.assertEqual(unified_strdate('25-09-2014'), '20140925')
253 def test_find_xpath_attr(self
):
260 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
262 self
.assertEqual(find_xpath_attr(doc
, './/fourohfour', 'n', 'v'), None)
263 self
.assertEqual(find_xpath_attr(doc
, './/node', 'x', 'a'), doc
[1])
264 self
.assertEqual(find_xpath_attr(doc
, './/node', 'y', 'c'), doc
[2])
266 def test_xpath_with_ns(self
):
267 testxml
= '''<root xmlns:media="http://example.com/">
269 <media:author>The Author</media:author>
270 <url>http://server.com/download.mp3</url>
273 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
274 find
= lambda p
: doc
.find(xpath_with_ns(p
, {'media': 'http://example.com/'}
))
275 self
.assertTrue(find('media:song') is not None)
276 self
.assertEqual(find('media:song/media:author').text
, 'The Author')
277 self
.assertEqual(find('media:song/url').text
, 'http://server.com/download.mp3')
279 def test_xpath_text(self
):
285 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
286 self
.assertEqual(xpath_text(doc
, 'div/p'), 'Foo')
287 self
.assertTrue(xpath_text(doc
, 'div/bar') is None)
288 self
.assertRaises(ExtractorError
, xpath_text
, doc
, 'div/bar', fatal
=True)
290 def test_smuggle_url(self
):
291 data
= {"ö": "ö", "abc": [3]}
292 url
= 'https://foo.bar/baz?x=y#a'
293 smug_url
= smuggle_url(url
, data
)
294 unsmug_url
, unsmug_data
= unsmuggle_url(smug_url
)
295 self
.assertEqual(url
, unsmug_url
)
296 self
.assertEqual(data
, unsmug_data
)
298 res_url
, res_data
= unsmuggle_url(url
)
299 self
.assertEqual(res_url
, url
)
300 self
.assertEqual(res_data
, None)
302 def test_shell_quote(self
):
303 args
= ['ffmpeg', '-i', encodeFilename('ñ€ß\'.mp4')]
304 self
.assertEqual(shell_quote(args
), """ffmpeg -i 'ñ€ß'"'"'.mp4'""")
306 def test_str_to_int(self
):
307 self
.assertEqual(str_to_int('123,456'), 123456)
308 self
.assertEqual(str_to_int('123.456'), 123456)
310 def test_url_basename(self
):
311 self
.assertEqual(url_basename('http://foo.de/'), '')
312 self
.assertEqual(url_basename('http://foo.de/bar/baz'), 'baz')
313 self
.assertEqual(url_basename('http://foo.de/bar/baz?x=y'), 'baz')
314 self
.assertEqual(url_basename('http://foo.de/bar/baz#x=y'), 'baz')
315 self
.assertEqual(url_basename('http://foo.de/bar/baz/'), 'baz')
317 url_basename('http://media.w3.org/2010/05/sintel/trailer.mp4'),
320 def test_parse_duration(self
):
321 self
.assertEqual(parse_duration(None), None)
322 self
.assertEqual(parse_duration(False), None)
323 self
.assertEqual(parse_duration('invalid'), None)
324 self
.assertEqual(parse_duration('1'), 1)
325 self
.assertEqual(parse_duration('1337:12'), 80232)
326 self
.assertEqual(parse_duration('9:12:43'), 33163)
327 self
.assertEqual(parse_duration('12:00'), 720)
328 self
.assertEqual(parse_duration('00:01:01'), 61)
329 self
.assertEqual(parse_duration('x:y'), None)
330 self
.assertEqual(parse_duration('3h11m53s'), 11513)
331 self
.assertEqual(parse_duration('3h 11m 53s'), 11513)
332 self
.assertEqual(parse_duration('3 hours 11 minutes 53 seconds'), 11513)
333 self
.assertEqual(parse_duration('3 hours 11 mins 53 secs'), 11513)
334 self
.assertEqual(parse_duration('62m45s'), 3765)
335 self
.assertEqual(parse_duration('6m59s'), 419)
336 self
.assertEqual(parse_duration('49s'), 49)
337 self
.assertEqual(parse_duration('0h0m0s'), 0)
338 self
.assertEqual(parse_duration('0m0s'), 0)
339 self
.assertEqual(parse_duration('0s'), 0)
340 self
.assertEqual(parse_duration('01:02:03.05'), 3723.05)
341 self
.assertEqual(parse_duration('T30M38S'), 1838)
342 self
.assertEqual(parse_duration('5 s'), 5)
343 self
.assertEqual(parse_duration('3 min'), 180)
344 self
.assertEqual(parse_duration('2.5 hours'), 9000)
345 self
.assertEqual(parse_duration('02:03:04'), 7384)
346 self
.assertEqual(parse_duration('01:02:03:04'), 93784)
347 self
.assertEqual(parse_duration('1 hour 3 minutes'), 3780)
349 def test_fix_xml_ampersands(self
):
351 fix_xml_ampersands('"&x=y&z=a'), '"&x=y&z=a')
353 fix_xml_ampersands('"&x=y&wrong;&z=a'),
354 '"&x=y&wrong;&z=a')
356 fix_xml_ampersands('&'><"'),
357 '&'><"')
359 fix_xml_ampersands('Ӓ᪼'), 'Ӓ᪼')
360 self
.assertEqual(fix_xml_ampersands('&#&#'), '&#&#')
362 def test_paged_list(self
):
363 def testPL(size
, pagesize
, sliceargs
, expected
):
364 def get_page(pagenum
):
365 firstid
= pagenum
* pagesize
366 upto
= min(size
, pagenum
* pagesize
+ pagesize
)
367 for i
in range(firstid
, upto
):
370 pl
= OnDemandPagedList(get_page
, pagesize
)
371 got
= pl
.getslice(*sliceargs
)
372 self
.assertEqual(got
, expected
)
374 iapl
= InAdvancePagedList(get_page
, size
// pagesize
+ 1, pagesize
)
375 got
= iapl
.getslice(*sliceargs
)
376 self
.assertEqual(got
, expected
)
378 testPL(5, 2, (), [0, 1, 2, 3, 4])
379 testPL(5, 2, (1,), [1, 2, 3, 4])
380 testPL(5, 2, (2,), [2, 3, 4])
381 testPL(5, 2, (4,), [4])
382 testPL(5, 2, (0, 3), [0, 1, 2])
383 testPL(5, 2, (1, 4), [1, 2, 3])
384 testPL(5, 2, (2, 99), [2, 3, 4])
385 testPL(5, 2, (20, 99), [])
387 def test_struct_unpack(self
):
388 self
.assertEqual(struct_unpack('!B', b
'\x00'), (0,))
390 def test_read_batch_urls(self
):
391 f
= io
.StringIO('''\xef\xbb\xbf foo
394 # More after this line\r
397 self
.assertEqual(read_batch_urls(f
), ['foo', 'bar', 'baz', 'bam'])
399 def test_urlencode_postdata(self
):
400 data
= urlencode_postdata({'username': 'foo@bar.com', 'password': '1234'}
)
401 self
.assertTrue(isinstance(data
, bytes))
403 def test_parse_iso8601(self
):
404 self
.assertEqual(parse_iso8601('2014-03-23T23:04:26+0100'), 1395612266)
405 self
.assertEqual(parse_iso8601('2014-03-23T22:04:26+0000'), 1395612266)
406 self
.assertEqual(parse_iso8601('2014-03-23T22:04:26Z'), 1395612266)
407 self
.assertEqual(parse_iso8601('2014-03-23T22:04:26.1234Z'), 1395612266)
409 def test_strip_jsonp(self
):
410 stripped
= strip_jsonp('cb ([ {"id":"532cb",\n\n\n"x":\n3}\n]\n);')
411 d
= json
.loads(stripped
)
412 self
.assertEqual(d
, [{"id": "532cb", "x": 3}
])
414 stripped
= strip_jsonp('parseMetadata({"STATUS":"OK"})\n\n\n//epc')
415 d
= json
.loads(stripped
)
416 self
.assertEqual(d
, {'STATUS': 'OK'}
)
418 def test_uppercase_escape(self
):
419 self
.assertEqual(uppercase_escape('aä'), 'aä')
420 self
.assertEqual(uppercase_escape('\\U0001d550'), '𝕐')
422 def test_lowercase_escape(self
):
423 self
.assertEqual(lowercase_escape('aä'), 'aä')
424 self
.assertEqual(lowercase_escape('\\u0026'), '&')
426 def test_limit_length(self
):
427 self
.assertEqual(limit_length(None, 12), None)
428 self
.assertEqual(limit_length('foo', 12), 'foo')
430 limit_length('foo bar baz asd', 12).startswith('foo bar'))
431 self
.assertTrue('...' in limit_length('foo bar baz asd', 12))
433 def test_escape_rfc3986(self
):
434 reserved
= "!*'();:@&=+$,/?#[]"
435 unreserved
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.~'
436 self
.assertEqual(escape_rfc3986(reserved
), reserved
)
437 self
.assertEqual(escape_rfc3986(unreserved
), unreserved
)
438 self
.assertEqual(escape_rfc3986('тест'), '%D1%82%D0%B5%D1%81%D1%82')
439 self
.assertEqual(escape_rfc3986('%D1%82%D0%B5%D1%81%D1%82'), '%D1%82%D0%B5%D1%81%D1%82')
440 self
.assertEqual(escape_rfc3986('foo bar'), 'foo%20bar')
441 self
.assertEqual(escape_rfc3986('foo%20bar'), 'foo%20bar')
443 def test_escape_url(self
):
445 escape_url('http://wowza.imust.org/srv/vod/telemb/new/UPLOAD/UPLOAD/20224_IncendieHavré_FD.mp4'),
446 'http://wowza.imust.org/srv/vod/telemb/new/UPLOAD/UPLOAD/20224_IncendieHavre%CC%81_FD.mp4'
449 escape_url('http://www.ardmediathek.de/tv/Sturm-der-Liebe/Folge-2036-Zu-Mann-und-Frau-erklärt/Das-Erste/Video?documentId=22673108&bcastId=5290'),
450 'http://www.ardmediathek.de/tv/Sturm-der-Liebe/Folge-2036-Zu-Mann-und-Frau-erkl%C3%A4rt/Das-Erste/Video?documentId=22673108&bcastId=5290'
453 escape_url('http://тест.рф/фрагмент'),
454 'http://тест.рф/%D1%84%D1%80%D0%B0%D0%B3%D0%BC%D0%B5%D0%BD%D1%82'
457 escape_url('http://тест.рф/абв?абв=абв#абв'),
458 'http://тест.рф/%D0%B0%D0%B1%D0%B2?%D0%B0%D0%B1%D0%B2=%D0%B0%D0%B1%D0%B2#%D0%B0%D0%B1%D0%B2'
460 self
.assertEqual(escape_url('http://vimeo.com/56015672#at=0'), 'http://vimeo.com/56015672#at=0')
462 def test_js_to_json_realworld(self
):
464 'clip':{'provider':'pseudo'}
466 self
.assertEqual(js_to_json(inp
), '''{
467 "clip":{"provider":"pseudo"}
469 json
.loads(js_to_json(inp
))
472 'playlist':[{'controls':{'all':null}}]
474 self
.assertEqual(js_to_json(inp
), '''{
475 "playlist":[{"controls":{"all":null}}]
478 inp
= '"SAND Number: SAND 2013-7800P\\nPresenter: Tom Russo\\nHabanero Software Training - Xyce Software\\nXyce, Sandia\\u0027s"'
479 json_code
= js_to_json(inp
)
480 self
.assertEqual(json
.loads(json_code
), json
.loads(inp
))
482 def test_js_to_json_edgecases(self
):
483 on
= js_to_json("{abc_def:'1\\'\\\\2\\\\\\'3\"4'}")
484 self
.assertEqual(json
.loads(on
), {"abc_def": "1'\\2\\'3\"4"}
)
486 on
= js_to_json('{"abc": true}')
487 self
.assertEqual(json
.loads(on
), {'abc': True}
)
489 # Ignore JavaScript code as well
496 self
.assertEqual(d
['x'], 1)
497 self
.assertEqual(d
['y'], 'a')
499 on
= js_to_json('["abc", "def",]')
500 self
.assertEqual(json
.loads(on
), ['abc', 'def'])
502 on
= js_to_json('{"abc": "def",}')
503 self
.assertEqual(json
.loads(on
), {'abc': 'def'}
)
505 def test_clean_html(self
):
506 self
.assertEqual(clean_html('a:\nb'), 'a: b')
507 self
.assertEqual(clean_html('a:\n "b"'), 'a: "b"')
509 def test_intlist_to_bytes(self
):
511 intlist_to_bytes([0, 1, 127, 128, 255]),
512 b
'\x00\x01\x7f\x80\xff')
514 def test_args_to_str(self
):
516 args_to_str(['foo', 'ba/r', '-baz', '2 be', '']),
517 'foo ba/r -baz \'2 be\' \'\''
520 def test_parse_filesize(self
):
521 self
.assertEqual(parse_filesize(None), None)
522 self
.assertEqual(parse_filesize(''), None)
523 self
.assertEqual(parse_filesize('91 B'), 91)
524 self
.assertEqual(parse_filesize('foobar'), None)
525 self
.assertEqual(parse_filesize('2 MiB'), 2097152)
526 self
.assertEqual(parse_filesize('5 GB'), 5000000000)
527 self
.assertEqual(parse_filesize('1.2Tb'), 1200000000000)
528 self
.assertEqual(parse_filesize('1,24 KB'), 1240)
530 def test_version_tuple(self
):
531 self
.assertEqual(version_tuple('1'), (1,))
532 self
.assertEqual(version_tuple('10.23.344'), (10, 23, 344))
533 self
.assertEqual(version_tuple('10.1-6'), (10, 1, 6)) # avconv style
535 def test_detect_exe_version(self
):
536 self
.assertEqual(detect_exe_version('''ffmpeg version 1.2.1
537 built on May 27 2013 08:37:26 with gcc 4.7 (Debian 4.7.3-4)
538 configuration: --prefix=/usr --extra-'''), '1.2.1')
539 self
.assertEqual(detect_exe_version('''ffmpeg version N-63176-g1fb4685
540 built on May 15 2014 22:09:06 with gcc 4.8.2 (GCC)'''), 'N-63176-g1fb4685')
541 self
.assertEqual(detect_exe_version('''X server found. dri2 connection failed!
542 Trying to open render node...
543 Success at /dev/dri/renderD128.
544 ffmpeg version 2.4.4 Copyright (c) 2000-2014 the FFmpeg ...'''), '2.4.4')
546 def test_age_restricted(self
):
547 self
.assertFalse(age_restricted(None, 10)) # unrestricted content
548 self
.assertFalse(age_restricted(1, None)) # unrestricted policy
549 self
.assertFalse(age_restricted(8, 10))
550 self
.assertTrue(age_restricted(18, 14))
551 self
.assertFalse(age_restricted(18, 18))
553 def test_is_html(self
):
554 self
.assertFalse(is_html(b
'\x49\x44\x43<html'))
555 self
.assertTrue(is_html(b
'<!DOCTYPE foo>\xaaa'))
556 self
.assertTrue(is_html( # UTF-8 with BOM
557 b
'\xef\xbb\xbf<!DOCTYPE foo>\xaaa'))
558 self
.assertTrue(is_html( # UTF-16-LE
559 b
'\xff\xfe<\x00h\x00t\x00m\x00l\x00>\x00\xe4\x00'
561 self
.assertTrue(is_html( # UTF-16-BE
562 b
'\xfe\xff\x00<\x00h\x00t\x00m\x00l\x00>\x00\xe4'
564 self
.assertTrue(is_html( # UTF-32-BE
565 b
'\x00\x00\xFE\xFF\x00\x00\x00<\x00\x00\x00h\x00\x00\x00t\x00\x00\x00m\x00\x00\x00l\x00\x00\x00>\x00\x00\x00\xe4'))
566 self
.assertTrue(is_html( # UTF-32-LE
567 b
'\xFF\xFE\x00\x00<\x00\x00\x00h\x00\x00\x00t\x00\x00\x00m\x00\x00\x00l\x00\x00\x00>\x00\x00\x00\xe4\x00\x00\x00'))
569 def test_render_table(self
):
573 [[123, 4], [9999, 51]]),
578 def test_match_str(self
):
579 self
.assertRaises(ValueError, match_str
, 'xy>foobar', {})
580 self
.assertFalse(match_str('xy', {'x': 1200}
))
581 self
.assertTrue(match_str('!xy', {'x': 1200}
))
582 self
.assertTrue(match_str('x', {'x': 1200}
))
583 self
.assertFalse(match_str('!x', {'x': 1200}
))
584 self
.assertTrue(match_str('x', {'x': 0}
))
585 self
.assertFalse(match_str('x>0', {'x': 0}
))
586 self
.assertFalse(match_str('x>0', {}))
587 self
.assertTrue(match_str('x>?0', {}))
588 self
.assertTrue(match_str('x>1K', {'x': 1200}
))
589 self
.assertFalse(match_str('x>2K', {'x': 1200}
))
590 self
.assertTrue(match_str('x>=1200 & x < 1300', {'x': 1200}
))
591 self
.assertFalse(match_str('x>=1100 & x < 1200', {'x': 1200}
))
592 self
.assertFalse(match_str('y=a212', {'y': 'foobar42'}
))
593 self
.assertTrue(match_str('y=foobar42', {'y': 'foobar42'}
))
594 self
.assertFalse(match_str('y!=foobar42', {'y': 'foobar42'}
))
595 self
.assertTrue(match_str('y!=foobar2', {'y': 'foobar42'}
))
596 self
.assertFalse(match_str(
597 'like_count > 100 & dislike_count <? 50 & description',
598 {'like_count': 90, 'description': 'foo'}
))
599 self
.assertTrue(match_str(
600 'like_count > 100 & dislike_count <? 50 & description',
601 {'like_count': 190, 'description': 'foo'}
))
602 self
.assertFalse(match_str(
603 'like_count > 100 & dislike_count <? 50 & description',
604 {'like_count': 190, 'dislike_count': 60, 'description': 'foo'}
))
605 self
.assertFalse(match_str(
606 'like_count > 100 & dislike_count <? 50 & description',
607 {'like_count': 190, 'dislike_count': 10}
))
609 def test_parse_dfxp_time_expr(self
):
610 self
.assertEqual(parse_dfxp_time_expr(None), 0.0)
611 self
.assertEqual(parse_dfxp_time_expr(''), 0.0)
612 self
.assertEqual(parse_dfxp_time_expr('0.1'), 0.1)
613 self
.assertEqual(parse_dfxp_time_expr('0.1s'), 0.1)
614 self
.assertEqual(parse_dfxp_time_expr('00:00:01'), 1.0)
615 self
.assertEqual(parse_dfxp_time_expr('00:00:01.100'), 1.1)
617 def test_dfxp2srt(self
):
618 dfxp_data
= '''<?xml version="1.0" encoding="UTF-8"?>
619 <tt xmlns="http://www.w3.org/ns/ttml" xml:lang="en" xmlns:tts="http://www.w3.org/ns/ttml#parameter">
622 <p begin="0" end="1">The following line contains Chinese characters and special symbols</p>
623 <p begin="1" end="2">第二行<br/>♪♪</p>
624 <p begin="2" end="3"><span>Third<br/>Line</span></p>
629 00:00:00,000 --> 00:00:01,000
630 The following line contains Chinese characters and special symbols
633 00:00:01,000 --> 00:00:02,000
638 00:00:02,000 --> 00:00:03,000
643 self
.assertEqual(dfxp2srt(dfxp_data
), srt_data
)
646 if __name__
== '__main__':