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 (
25 if sys.version_info < (3, 0):
26 _compat_str = lambda b: b.decode('unicode-escape')
28 _compat_str = lambda s: s
31 class TestUtil(unittest.TestCase):
32 def test_timeconvert(self):
33 self.assertTrue(timeconvert('') is None)
34 self.assertTrue(timeconvert('bougrg') is None)
36 def test_sanitize_filename(self):
37 self.assertEqual(sanitize_filename('abc'), 'abc')
38 self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
40 self.assertEqual(sanitize_filename('123'), '123')
42 self.assertEqual('abc_de', sanitize_filename('abc/de'))
43 self.assertFalse('/' in sanitize_filename('abc/de///'))
45 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
46 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
47 self.assertEqual('yes no', sanitize_filename('yes? no'))
48 self.assertEqual('this - that', sanitize_filename('this: that'))
50 self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
51 aumlaut = _compat_str('\xe4')
52 self.assertEqual(sanitize_filename(aumlaut), aumlaut)
53 tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
54 self.assertEqual(sanitize_filename(tests), tests)
59 self.assertTrue(fbc not in sanitize_filename(fc))
61 def test_sanitize_filename_restricted(self):
62 self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
63 self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
65 self.assertEqual(sanitize_filename('123', restricted=True), '123')
67 self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
68 self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
70 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
71 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
72 self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
73 self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
75 tests = _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
76 self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
77 self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '') # No empty filename
79 forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
82 self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
84 # Handle a common case more neatly
85 self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song')
86 self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech')
87 # .. but make sure the file name is never empty
88 self.assertTrue(sanitize_filename('-', restricted=True) != '')
89 self.assertTrue(sanitize_filename(':', restricted=True) != '')
91 def test_sanitize_ids(self):
92 self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
93 self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
94 self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
96 def test_ordered_set(self):
97 self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
98 self.assertEqual(orderedSet([]), [])
99 self.assertEqual(orderedSet([1]), [1])
100 #keep the list ordered
101 self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
103 def test_unescape_html(self):
104 self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
106 def test_daterange(self):
107 _20century = DateRange("19000101","20000101")
108 self.assertFalse("17890714" in _20century)
109 _ac = DateRange("00010101")
110 self.assertTrue("19690721" in _ac)
111 _firstmilenium = DateRange(end="10000101")
112 self.assertTrue("07110427" in _firstmilenium)
114 def test_unified_dates(self):
115 self.assertEqual(unified_strdate('December 21, 2010'), '20101221')
116 self.assertEqual(unified_strdate('8/7/2009'), '20090708')
117 self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
118 self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
120 def test_find_xpath_attr(self):
127 doc = xml.etree.ElementTree.fromstring(testxml)
129 self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None)
130 self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1])
131 self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2])
133 def test_meta_parser(self):
136 <meta name="description" content="foo & bar">
137 <meta content='Plato' name='author'/>
140 get_meta = lambda name: get_meta_content(name, testhtml)
141 self.assertEqual(get_meta('description'), u'foo & bar')
142 self.assertEqual(get_meta('author'), 'Plato')
144 if __name__ == '__main__':