]>
jfr.im git - yt-dlp.git/blob - test/test_utils.py
3 # Various small unit tests
7 import xml
.etree
.ElementTree
9 # Allow direct execution
11 sys
.path
.append(os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
13 #from youtube_dl.utils import htmlentity_transform
14 from youtube_dl
.utils
import timeconvert
15 from youtube_dl
.utils
import sanitize_filename
16 from youtube_dl
.utils
import unescapeHTML
17 from youtube_dl
.utils
import orderedSet
18 from youtube_dl
.utils
import DateRange
19 from youtube_dl
.utils
import unified_strdate
20 from youtube_dl
.utils
import find_xpath_attr
22 if sys
.version_info
< (3, 0):
23 _compat_str
= lambda b
: b
.decode('unicode-escape')
25 _compat_str
= lambda s
: s
28 class TestUtil(unittest
.TestCase
):
29 def test_timeconvert(self
):
30 self
.assertTrue(timeconvert('') is None)
31 self
.assertTrue(timeconvert('bougrg') is None)
33 def test_sanitize_filename(self
):
34 self
.assertEqual(sanitize_filename('abc'), 'abc')
35 self
.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
37 self
.assertEqual(sanitize_filename('123'), '123')
39 self
.assertEqual('abc_de', sanitize_filename('abc/de'))
40 self
.assertFalse('/' in sanitize_filename('abc/de///'))
42 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
43 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
44 self
.assertEqual('yes no', sanitize_filename('yes? no'))
45 self
.assertEqual('this - that', sanitize_filename('this: that'))
47 self
.assertEqual(sanitize_filename('AT&T'), 'AT&T')
48 aumlaut
= _compat_str('\xe4')
49 self
.assertEqual(sanitize_filename(aumlaut
), aumlaut
)
50 tests
= _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
51 self
.assertEqual(sanitize_filename(tests
), tests
)
56 self
.assertTrue(fbc
not in sanitize_filename(fc
))
58 def test_sanitize_filename_restricted(self
):
59 self
.assertEqual(sanitize_filename('abc', restricted
=True), 'abc')
60 self
.assertEqual(sanitize_filename('abc_d-e', restricted
=True), 'abc_d-e')
62 self
.assertEqual(sanitize_filename('123', restricted
=True), '123')
64 self
.assertEqual('abc_de', sanitize_filename('abc/de', restricted
=True))
65 self
.assertFalse('/' in sanitize_filename('abc/de///', restricted
=True))
67 self
.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted
=True))
68 self
.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted
=True))
69 self
.assertEqual('yes_no', sanitize_filename('yes? no', restricted
=True))
70 self
.assertEqual('this_-_that', sanitize_filename('this: that', restricted
=True))
72 tests
= _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
73 self
.assertEqual(sanitize_filename(tests
, restricted
=True), 'a_b_c')
74 self
.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted
=True) != '') # No empty filename
76 forbidden
= '"\0\\/&!: \'\t\n()[]{}$;`^,#'
79 self
.assertTrue(fbc
not in sanitize_filename(fc
, restricted
=True))
81 # Handle a common case more neatly
82 self
.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted
=True), 'Song')
83 self
.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted
=True), 'Speech')
84 # .. but make sure the file name is never empty
85 self
.assertTrue(sanitize_filename('-', restricted
=True) != '')
86 self
.assertTrue(sanitize_filename(':', restricted
=True) != '')
88 def test_sanitize_ids(self
):
89 self
.assertEqual(sanitize_filename('_n_cd26wFpw', is_id
=True), '_n_cd26wFpw')
90 self
.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id
=True), '_BD_eEpuzXw')
91 self
.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id
=True), 'N0Y__7-UOdI')
93 def test_ordered_set(self
):
94 self
.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
95 self
.assertEqual(orderedSet([]), [])
96 self
.assertEqual(orderedSet([1]), [1])
97 #keep the list ordered
98 self
.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
100 def test_unescape_html(self
):
101 self
.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
103 def test_daterange(self
):
104 _20century
= DateRange("19000101","20000101")
105 self
.assertFalse("17890714" in _20century
)
106 _ac
= DateRange("00010101")
107 self
.assertTrue("19690721" in _ac
)
108 _firstmilenium
= DateRange(end
="10000101")
109 self
.assertTrue("07110427" in _firstmilenium
)
111 def test_unified_dates(self
):
112 self
.assertEqual(unified_strdate('December 21, 2010'), '20101221')
113 self
.assertEqual(unified_strdate('8/7/2009'), '20090708')
114 self
.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
115 self
.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
117 def test_find_xpath_attr(self
):
124 doc
= xml
.etree
.ElementTree
.fromstring(testxml
)
126 self
.assertEqual(find_xpath_attr(doc
, './/fourohfour', 'n', 'v'), None)
127 self
.assertEqual(find_xpath_attr(doc
, './/node', 'x', 'a'), doc
[1])
128 self
.assertEqual(find_xpath_attr(doc
, './/node', 'y', 'c'), doc
[2])
130 if __name__
== '__main__':