4 # Allow direct execution
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11 # Various small unit tests
12 import xml.etree.ElementTree
14 #from youtube_dl.utils import htmlentity_transform
15 from youtube_dl.utils import (
34 if sys.version_info < (3, 0):
35 _compat_str = lambda b: b.decode('unicode-escape')
37 _compat_str = lambda s: s
40 class TestUtil(unittest.TestCase):
41 def test_timeconvert(self):
42 self.assertTrue(timeconvert('') is None)
43 self.assertTrue(timeconvert('bougrg') is None)
45 def test_sanitize_filename(self):
46 self.assertEqual(sanitize_filename('abc'), 'abc')
47 self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
49 self.assertEqual(sanitize_filename('123'), '123')
51 self.assertEqual('abc_de', sanitize_filename('abc/de'))
52 self.assertFalse('/' in sanitize_filename('abc/de///'))
54 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
55 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
56 self.assertEqual('yes no', sanitize_filename('yes? no'))
57 self.assertEqual('this - that', sanitize_filename('this: that'))
59 self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
60 aumlaut = _compat_str('\xe4')
61 self.assertEqual(sanitize_filename(aumlaut), aumlaut)
62 tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
63 self.assertEqual(sanitize_filename(tests), tests)
68 self.assertTrue(fbc not in sanitize_filename(fc))
70 def test_sanitize_filename_restricted(self):
71 self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
72 self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
74 self.assertEqual(sanitize_filename('123', restricted=True), '123')
76 self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
77 self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
79 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
80 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
81 self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
82 self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
84 tests = _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
85 self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
86 self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '') # No empty filename
88 forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
91 self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
93 # Handle a common case more neatly
94 self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song')
95 self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech')
96 # .. but make sure the file name is never empty
97 self.assertTrue(sanitize_filename('-', restricted=True) != '')
98 self.assertTrue(sanitize_filename(':', restricted=True) != '')
100 def test_sanitize_ids(self):
101 self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
102 self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
103 self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
105 def test_ordered_set(self):
106 self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
107 self.assertEqual(orderedSet([]), [])
108 self.assertEqual(orderedSet([1]), [1])
109 #keep the list ordered
110 self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
112 def test_unescape_html(self):
113 self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
115 def test_daterange(self):
116 _20century = DateRange("19000101","20000101")
117 self.assertFalse("17890714" in _20century)
118 _ac = DateRange("00010101")
119 self.assertTrue("19690721" in _ac)
120 _firstmilenium = DateRange(end="10000101")
121 self.assertTrue("07110427" in _firstmilenium)
123 def test_unified_dates(self):
124 self.assertEqual(unified_strdate('December 21, 2010'), '20101221')
125 self.assertEqual(unified_strdate('8/7/2009'), '20090708')
126 self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
127 self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
129 def test_find_xpath_attr(self):
136 doc = xml.etree.ElementTree.fromstring(testxml)
138 self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None)
139 self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1])
140 self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2])
142 def test_meta_parser(self):
145 <meta name="description" content="foo & bar">
146 <meta content='Plato' name='author'/>
149 get_meta = lambda name: get_meta_content(name, testhtml)
150 self.assertEqual(get_meta('description'), u'foo & bar')
151 self.assertEqual(get_meta('author'), 'Plato')
153 def test_xpath_with_ns(self):
154 testxml = u'''<root xmlns:media="http://example.com/">
156 <media:author>The Author</media:author>
157 <url>http://server.com/download.mp3</url>
160 doc = xml.etree.ElementTree.fromstring(testxml)
161 find = lambda p: doc.find(xpath_with_ns(p, {'media': 'http://example.com/'}))
162 self.assertTrue(find('media:song') is not None)
163 self.assertEqual(find('media:song/media:author').text, u'The Author')
164 self.assertEqual(find('media:song/url').text, u'http://server.com/download.mp3')
166 def test_smuggle_url(self):
167 data = {u"ö": u"ö", u"abc": [3]}
168 url = 'https://foo.bar/baz?x=y#a'
169 smug_url = smuggle_url(url, data)
170 unsmug_url, unsmug_data = unsmuggle_url(smug_url)
171 self.assertEqual(url, unsmug_url)
172 self.assertEqual(data, unsmug_data)
174 res_url, res_data = unsmuggle_url(url)
175 self.assertEqual(res_url, url)
176 self.assertEqual(res_data, None)
178 def test_shell_quote(self):
179 args = ['ffmpeg', '-i', encodeFilename(u'ñ€ß\'.mp4')]
180 self.assertEqual(shell_quote(args), u"""ffmpeg -i 'ñ€ß'"'"'.mp4'""")
182 def test_str_to_int(self):
183 self.assertEqual(str_to_int('123,456'), 123456)
184 self.assertEqual(str_to_int('123.456'), 123456)
186 def test_url_basename(self):
187 self.assertEqual(url_basename(u'http://foo.de/'), u'')
188 self.assertEqual(url_basename(u'http://foo.de/bar/baz'), u'baz')
189 self.assertEqual(url_basename(u'http://foo.de/bar/baz?x=y'), u'baz')
190 self.assertEqual(url_basename(u'http://foo.de/bar/baz#x=y'), u'baz')
191 self.assertEqual(url_basename(u'http://foo.de/bar/baz/'), u'baz')
193 url_basename(u'http://media.w3.org/2010/05/sintel/trailer.mp4'),
196 def test_parse_duration(self):
197 self.assertEqual(parse_duration(None), None)
198 self.assertEqual(parse_duration('1'), 1)
199 self.assertEqual(parse_duration('1337:12'), 80232)
200 self.assertEqual(parse_duration('9:12:43'), 33163)
201 self.assertEqual(parse_duration('x:y'), None)
203 if __name__ == '__main__':