3 # Various small unit tests
8 # Allow direct execution
10 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12 #from youtube_dl.utils import htmlentity_transform
13 from youtube_dl.utils import timeconvert
14 from youtube_dl.utils import sanitize_filename
15 from youtube_dl.utils import unescapeHTML
16 from youtube_dl.utils import orderedSet
18 if sys.version_info < (3, 0):
19 _compat_str = lambda b: b.decode('unicode-escape')
21 _compat_str = lambda s: s
24 class TestUtil(unittest.TestCase):
25 def test_timeconvert(self):
26 self.assertTrue(timeconvert('') is None)
27 self.assertTrue(timeconvert('bougrg') is None)
29 def test_sanitize_filename(self):
30 self.assertEqual(sanitize_filename('abc'), 'abc')
31 self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
33 self.assertEqual(sanitize_filename('123'), '123')
35 self.assertEqual('abc_de', sanitize_filename('abc/de'))
36 self.assertFalse('/' in sanitize_filename('abc/de///'))
38 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
39 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
40 self.assertEqual('yes no', sanitize_filename('yes? no'))
41 self.assertEqual('this - that', sanitize_filename('this: that'))
43 self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
44 aumlaut = _compat_str('\xe4')
45 self.assertEqual(sanitize_filename(aumlaut), aumlaut)
46 tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
47 self.assertEqual(sanitize_filename(tests), tests)
52 self.assertTrue(fbc not in sanitize_filename(fc))
54 def test_sanitize_filename_restricted(self):
55 self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
56 self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
58 self.assertEqual(sanitize_filename('123', restricted=True), '123')
60 self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
61 self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
63 self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
64 self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
65 self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
66 self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
68 tests = _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
69 self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
70 self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '') # No empty filename
72 forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
75 self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
77 # Handle a common case more neatly
78 self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song')
79 self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech')
80 # .. but make sure the file name is never empty
81 self.assertTrue(sanitize_filename('-', restricted=True) != '')
82 self.assertTrue(sanitize_filename(':', restricted=True) != '')
84 def test_sanitize_ids(self):
85 self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
86 self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
87 self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
89 def test_ordered_set(self):
90 self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
91 self.assertEqual(orderedSet([]), [])
92 self.assertEqual(orderedSet([1]), [1])
93 #keep the list ordered
94 self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
96 def test_unescape_html(self):
97 self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
99 if __name__ == '__main__':