]>
Commit | Line | Data |
---|---|---|
9993976a | 1 | # Various small unit tests |
2 | ||
dae7c920 | 3 | import sys |
44fb3454 | 4 | import unittest |
5 | ||
dae7c920 PH |
6 | # Allow direct execution |
7 | import os | |
8 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
9 | ||
44fb3454 | 10 | #from youtube_dl.utils import htmlentity_transform |
11 | from youtube_dl.utils import timeconvert | |
9993976a | 12 | from youtube_dl.utils import sanitize_filename |
44fb3454 | 13 | from youtube_dl.utils import unescapeHTML |
14 | from youtube_dl.utils import orderedSet | |
15 | ||
dae7c920 PH |
16 | if sys.version < (3,0): |
17 | _compat_str = lambda b: b.decode('unicode-escape') | |
18 | else: | |
19 | _compat_str = lambda s: s | |
44fb3454 | 20 | |
21 | class TestUtil(unittest.TestCase): | |
137c5803 | 22 | def test_timeconvert(self): |
4279a0ca | 23 | self.assertTrue(timeconvert('') is None) |
24 | self.assertTrue(timeconvert('bougrg') is None) | |
44fb3454 | 25 | |
137c5803 | 26 | def test_sanitize_filename(self): |
dae7c920 PH |
27 | self.assertEqual(sanitize_filename('abc'), 'abc') |
28 | self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e') | |
44fb3454 | 29 | |
dae7c920 | 30 | self.assertEqual(sanitize_filename('123'), '123') |
44fb3454 | 31 | |
dae7c920 PH |
32 | self.assertEqual('abc_de', sanitize_filename('abc/de')) |
33 | self.assertFalse('/' in sanitize_filename('abc/de///')) | |
44fb3454 | 34 | |
dae7c920 PH |
35 | self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de')) |
36 | self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|')) | |
37 | self.assertEqual('yes no', sanitize_filename('yes? no')) | |
38 | self.assertEqual('this - that', sanitize_filename('this: that')) | |
9993976a | 39 | |
dae7c920 PH |
40 | self.assertEqual(sanitize_filename('AT&T'), 'AT&T') |
41 | aumlaut = _compat_str('\xe4') | |
42 | self.assertEqual(sanitize_filename(aumlaut), aumlaut) | |
43 | tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430') | |
44 | self.assertEqual(sanitize_filename(tests), tests) | |
9993976a | 45 | |
dae7c920 | 46 | forbidden = '"\0\\/' |
1c469a94 PH |
47 | for fc in forbidden: |
48 | for fbc in forbidden: | |
49 | self.assertTrue(fbc not in sanitize_filename(fc)) | |
50 | ||
51 | def test_sanitize_filename_restricted(self): | |
dae7c920 PH |
52 | self.assertEqual(sanitize_filename('abc', restricted=True), 'abc') |
53 | self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e') | |
1c469a94 | 54 | |
dae7c920 | 55 | self.assertEqual(sanitize_filename('123', restricted=True), '123') |
1c469a94 | 56 | |
dae7c920 PH |
57 | self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True)) |
58 | self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True)) | |
1c469a94 | 59 | |
dae7c920 PH |
60 | self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True)) |
61 | self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True)) | |
62 | self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True)) | |
63 | self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True)) | |
1c469a94 | 64 | |
dae7c920 PH |
65 | tests =_compat_str('a\xe4b\u4e2d\u56fd\u7684c') |
66 | self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c') | |
67 | self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '') # No empty filename | |
56781d3d | 68 | |
dae7c920 | 69 | forbidden = '"\0\\/&!: \'\t\n' |
1c469a94 | 70 | for fc in forbidden: |
1c469a94 PH |
71 | for fbc in forbidden: |
72 | self.assertTrue(fbc not in sanitize_filename(fc, restricted=True)) | |
156a59e7 | 73 | |
46cbda0b | 74 | # Handle a common case more neatly |
dae7c920 PH |
75 | self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song') |
76 | self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech') | |
46cbda0b | 77 | # .. but make sure the file name is never empty |
dae7c920 PH |
78 | self.assertTrue(sanitize_filename('-', restricted=True) != '') |
79 | self.assertTrue(sanitize_filename(':', restricted=True) != '') | |
46cbda0b | 80 | |
137c5803 | 81 | def test_ordered_set(self): |
82 | self.assertEqual(orderedSet([1,1,2,3,4,4,5,6,7,3,5]), [1,2,3,4,5,6,7]) | |
83 | self.assertEqual(orderedSet([]), []) | |
84 | self.assertEqual(orderedSet([1]), [1]) | |
85 | #keep the list ordered | |
86 | self.assertEqual(orderedSet([135,1,1,1]), [135,1]) | |
9993976a | 87 | |
137c5803 | 88 | def test_unescape_html(self): |
dae7c920 PH |
89 | self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;')) |
90 | ||
91 | if __name__ == '__main__': | |
92 | unittest.main() |