]> gitweb @ CieloNegro.org - youtube-dl.git/blob - test/test_utils.py
Temporary skip Escapist test as it fails only on Travis; we'll make a more specific...
[youtube-dl.git] / test / test_utils.py
1 #!/usr/bin/env python
2
3 # Various small unit tests
4
5 import sys
6 import unittest
7
8 # Allow direct execution
9 import os
10 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11
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
17
18 if sys.version_info < (3, 0):
19     _compat_str = lambda b: b.decode('unicode-escape')
20 else:
21     _compat_str = lambda s: s
22
23
24 class TestUtil(unittest.TestCase):
25     def test_timeconvert(self):
26         self.assertTrue(timeconvert('') is None)
27         self.assertTrue(timeconvert('bougrg') is None)
28
29     def test_sanitize_filename(self):
30         self.assertEqual(sanitize_filename('abc'), 'abc')
31         self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
32
33         self.assertEqual(sanitize_filename('123'), '123')
34
35         self.assertEqual('abc_de', sanitize_filename('abc/de'))
36         self.assertFalse('/' in sanitize_filename('abc/de///'))
37
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'))
42
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)
48
49         forbidden = '"\0\\/'
50         for fc in forbidden:
51             for fbc in forbidden:
52                 self.assertTrue(fbc not in sanitize_filename(fc))
53
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')
57
58         self.assertEqual(sanitize_filename('123', restricted=True), '123')
59
60         self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
61         self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
62
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))
67
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
71
72         forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
73         for fc in forbidden:
74             for fbc in forbidden:
75                 self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
76
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) != '')
83
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')
88
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])
95
96     def test_unescape_html(self):
97         self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
98
99 if __name__ == '__main__':
100     unittest.main()