]> Raphaël G. Git Repositories - youtubedl/blob - test/test_compat.py
Imported Upstream version 2016.02.22
[youtubedl] / test / test_compat.py
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
13 from youtube_dl.utils import get_filesystem_encoding
14 from youtube_dl.compat import (
15 compat_getenv,
16 compat_etree_fromstring,
17 compat_expanduser,
18 compat_shlex_split,
19 compat_str,
20 compat_urllib_parse_unquote,
21 compat_urllib_parse_unquote_plus,
22 )
23
24
25 class TestCompat(unittest.TestCase):
26 def test_compat_getenv(self):
27 test_str = 'тест'
28 os.environ['YOUTUBE-DL-TEST'] = (
29 test_str if sys.version_info >= (3, 0)
30 else test_str.encode(get_filesystem_encoding()))
31 self.assertEqual(compat_getenv('YOUTUBE-DL-TEST'), test_str)
32
33 def test_compat_expanduser(self):
34 old_home = os.environ.get('HOME')
35 test_str = 'C:\Documents and Settings\тест\Application Data'
36 os.environ['HOME'] = (
37 test_str if sys.version_info >= (3, 0)
38 else test_str.encode(get_filesystem_encoding()))
39 self.assertEqual(compat_expanduser('~'), test_str)
40 os.environ['HOME'] = old_home
41
42 def test_all_present(self):
43 import youtube_dl.compat
44 all_names = youtube_dl.compat.__all__
45 present_names = set(filter(
46 lambda c: '_' in c and not c.startswith('_'),
47 dir(youtube_dl.compat))) - set(['unicode_literals'])
48 self.assertEqual(all_names, sorted(present_names))
49
50 def test_compat_urllib_parse_unquote(self):
51 self.assertEqual(compat_urllib_parse_unquote('abc%20def'), 'abc def')
52 self.assertEqual(compat_urllib_parse_unquote('%7e/abc+def'), '~/abc+def')
53 self.assertEqual(compat_urllib_parse_unquote(''), '')
54 self.assertEqual(compat_urllib_parse_unquote('%'), '%')
55 self.assertEqual(compat_urllib_parse_unquote('%%'), '%%')
56 self.assertEqual(compat_urllib_parse_unquote('%%%'), '%%%')
57 self.assertEqual(compat_urllib_parse_unquote('%2F'), '/')
58 self.assertEqual(compat_urllib_parse_unquote('%2f'), '/')
59 self.assertEqual(compat_urllib_parse_unquote('%E6%B4%A5%E6%B3%A2'), '津波')
60 self.assertEqual(
61 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" />
62 %<a href="https://ar.wikipedia.org/wiki/%D8%AA%D8%B3%D9%88%D9%86%D8%A7%D9%85%D9%8A">%a'''),
63 '''<meta property="og:description" content="▁▂▃▄%▅▆▇█" />
64 %<a href="https://ar.wikipedia.org/wiki/تسونامي">%a''')
65 self.assertEqual(
66 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%'''),
67 '''(^◣_◢^)っ︻デ═一 ⇀ ⇀ ⇀ ⇀ ⇀ ↶%I%Break%Things%''')
68
69 def test_compat_urllib_parse_unquote_plus(self):
70 self.assertEqual(compat_urllib_parse_unquote_plus('abc%20def'), 'abc def')
71 self.assertEqual(compat_urllib_parse_unquote_plus('%7e/abc+def'), '~/abc def')
72
73 def test_compat_shlex_split(self):
74 self.assertEqual(compat_shlex_split('-option "one two"'), ['-option', 'one two'])
75
76 def test_compat_etree_fromstring(self):
77 xml = '''
78 <root foo="bar" spam="中文">
79 <normal>foo</normal>
80 <chinese>中文</chinese>
81 <foo><bar>spam</bar></foo>
82 </root>
83 '''
84 doc = compat_etree_fromstring(xml.encode('utf-8'))
85 self.assertTrue(isinstance(doc.attrib['foo'], compat_str))
86 self.assertTrue(isinstance(doc.attrib['spam'], compat_str))
87 self.assertTrue(isinstance(doc.find('normal').text, compat_str))
88 self.assertTrue(isinstance(doc.find('chinese').text, compat_str))
89 self.assertTrue(isinstance(doc.find('foo/bar').text, compat_str))
90
91 if __name__ == '__main__':
92 unittest.main()