]>
Commit | Line | Data |
---|---|---|
8c25f81b PH |
1 | #!/usr/bin/env python |
2 | # coding: utf-8 | |
3 | ||
4 | from __future__ import unicode_literals | |
5 | ||
6 | # Allow direct execution | |
7 | import os | |
8 | import sys | |
9 | import unittest | |
10 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
11 | ||
12 | ||
8c25f81b PH |
13 | from youtube_dl.compat import ( |
14 | compat_getenv, | |
fe40f9ee | 15 | compat_setenv, |
36e6f62c | 16 | compat_etree_fromstring, |
8c25f81b | 17 | compat_expanduser, |
ee087c79 | 18 | compat_shlex_split, |
36e6f62c | 19 | compat_str, |
4a632911 | 20 | compat_urllib_parse_unquote, |
8954e481 | 21 | compat_urllib_parse_unquote_plus, |
e289d6d6 | 22 | compat_urllib_parse_urlencode, |
8c25f81b PH |
23 | ) |
24 | ||
25 | ||
26 | class TestCompat(unittest.TestCase): | |
27 | def test_compat_getenv(self): | |
28 | test_str = 'тест' | |
12926387 | 29 | compat_setenv('YOUTUBE-DL-TEST', test_str) |
8c25f81b PH |
30 | self.assertEqual(compat_getenv('YOUTUBE-DL-TEST'), test_str) |
31 | ||
fe40f9ee S |
32 | def test_compat_setenv(self): |
33 | test_var = 'YOUTUBE-DL-TEST' | |
34 | test_str = 'тест' | |
35 | compat_setenv(test_var, test_str) | |
36 | compat_getenv(test_var) | |
37 | self.assertEqual(compat_getenv(test_var), test_str) | |
38 | ||
8c25f81b | 39 | def test_compat_expanduser(self): |
f56875f2 | 40 | old_home = os.environ.get('HOME') |
8c25f81b | 41 | test_str = 'C:\Documents and Settings\тест\Application Data' |
12926387 | 42 | compat_setenv('HOME', test_str) |
8c25f81b | 43 | self.assertEqual(compat_expanduser('~'), test_str) |
20cfdcc9 | 44 | compat_setenv('HOME', old_home or '') |
8c25f81b PH |
45 | |
46 | def test_all_present(self): | |
47 | import youtube_dl.compat | |
48 | all_names = youtube_dl.compat.__all__ | |
49 | present_names = set(filter( | |
50 | lambda c: '_' in c and not c.startswith('_'), | |
278143df | 51 | dir(youtube_dl.compat))) - set(['unicode_literals']) |
8c25f81b PH |
52 | self.assertEqual(all_names, sorted(present_names)) |
53 | ||
4a632911 | 54 | def test_compat_urllib_parse_unquote(self): |
8954e481 S |
55 | self.assertEqual(compat_urllib_parse_unquote('abc%20def'), 'abc def') |
56 | self.assertEqual(compat_urllib_parse_unquote('%7e/abc+def'), '~/abc+def') | |
14309e1d S |
57 | self.assertEqual(compat_urllib_parse_unquote(''), '') |
58 | self.assertEqual(compat_urllib_parse_unquote('%'), '%') | |
59 | self.assertEqual(compat_urllib_parse_unquote('%%'), '%%') | |
60 | self.assertEqual(compat_urllib_parse_unquote('%%%'), '%%%') | |
61 | self.assertEqual(compat_urllib_parse_unquote('%2F'), '/') | |
62 | self.assertEqual(compat_urllib_parse_unquote('%2f'), '/') | |
63 | self.assertEqual(compat_urllib_parse_unquote('%E6%B4%A5%E6%B3%A2'), '津波') | |
14309e1d S |
64 | self.assertEqual( |
65 | compat_urllib_parse_unquote('''<meta property="og:description" content="%E2%96%81%E2%96%82%E2%96%83%E2%96%84%25%E2%96%85%E2%96%86%E2%96%87%E2%96%88" /> | |
66 | %<a href="https://ar.wikipedia.org/wiki/%D8%AA%D8%B3%D9%88%D9%86%D8%A7%D9%85%D9%8A">%a'''), | |
67 | '''<meta property="og:description" content="▁▂▃▄%▅▆▇█" /> | |
68 | %<a href="https://ar.wikipedia.org/wiki/تسونامي">%a''') | |
69 | self.assertEqual( | |
70 | compat_urllib_parse_unquote('''%28%5E%E2%97%A3_%E2%97%A2%5E%29%E3%81%A3%EF%B8%BB%E3%83%87%E2%95%90%E4%B8%80 %E2%87%80 %E2%87%80 %E2%87%80 %E2%87%80 %E2%87%80 %E2%86%B6%I%Break%25Things%'''), | |
71 | '''(^◣_◢^)っ︻デ═一 ⇀ ⇀ ⇀ ⇀ ⇀ ↶%I%Break%Things%''') | |
4a632911 | 72 | |
8954e481 S |
73 | def test_compat_urllib_parse_unquote_plus(self): |
74 | self.assertEqual(compat_urllib_parse_unquote_plus('abc%20def'), 'abc def') | |
75 | self.assertEqual(compat_urllib_parse_unquote_plus('%7e/abc+def'), '~/abc def') | |
76 | ||
e289d6d6 S |
77 | def test_compat_urllib_parse_urlencode(self): |
78 | self.assertEqual(compat_urllib_parse_urlencode({'abc': 'def'}), 'abc=def') | |
79 | self.assertEqual(compat_urllib_parse_urlencode({'abc': b'def'}), 'abc=def') | |
80 | self.assertEqual(compat_urllib_parse_urlencode({b'abc': 'def'}), 'abc=def') | |
81 | self.assertEqual(compat_urllib_parse_urlencode({b'abc': b'def'}), 'abc=def') | |
92d5477d YCH |
82 | self.assertEqual(compat_urllib_parse_urlencode([('abc', 'def')]), 'abc=def') |
83 | self.assertEqual(compat_urllib_parse_urlencode([('abc', b'def')]), 'abc=def') | |
84 | self.assertEqual(compat_urllib_parse_urlencode([(b'abc', 'def')]), 'abc=def') | |
85 | self.assertEqual(compat_urllib_parse_urlencode([(b'abc', b'def')]), 'abc=def') | |
e289d6d6 | 86 | |
3513d414 | 87 | def test_compat_shlex_split(self): |
ee087c79 S |
88 | self.assertEqual(compat_shlex_split('-option "one two"'), ['-option', 'one two']) |
89 | ||
36e6f62c | 90 | def test_compat_etree_fromstring(self): |
f7854627 JMF |
91 | xml = ''' |
92 | <root foo="bar" spam="中文"> | |
93 | <normal>foo</normal> | |
94 | <chinese>中文</chinese> | |
95 | <foo><bar>spam</bar></foo> | |
96 | </root> | |
97 | ''' | |
36e6f62c JMF |
98 | doc = compat_etree_fromstring(xml.encode('utf-8')) |
99 | self.assertTrue(isinstance(doc.attrib['foo'], compat_str)) | |
387db16a | 100 | self.assertTrue(isinstance(doc.attrib['spam'], compat_str)) |
f7854627 JMF |
101 | self.assertTrue(isinstance(doc.find('normal').text, compat_str)) |
102 | self.assertTrue(isinstance(doc.find('chinese').text, compat_str)) | |
103 | self.assertTrue(isinstance(doc.find('foo/bar').text, compat_str)) | |
36e6f62c | 104 | |
8c25f81b PH |
105 | if __name__ == '__main__': |
106 | unittest.main() |