]> Raphaël G. Git Repositories - youtubedl/blob - test/test_compat.py
Rebuild with only Python 3 support.
[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.compat import (
14 compat_getenv,
15 compat_setenv,
16 compat_etree_fromstring,
17 compat_expanduser,
18 compat_shlex_split,
19 compat_str,
20 compat_struct_unpack,
21 compat_urllib_parse_unquote,
22 compat_urllib_parse_unquote_plus,
23 compat_urllib_parse_urlencode,
24 )
25
26
27 class TestCompat(unittest.TestCase):
28 def test_compat_getenv(self):
29 test_str = 'тест'
30 compat_setenv('YOUTUBE-DL-TEST', test_str)
31 self.assertEqual(compat_getenv('YOUTUBE-DL-TEST'), test_str)
32
33 def test_compat_setenv(self):
34 test_var = 'YOUTUBE-DL-TEST'
35 test_str = 'тест'
36 compat_setenv(test_var, test_str)
37 compat_getenv(test_var)
38 self.assertEqual(compat_getenv(test_var), test_str)
39
40 def test_compat_expanduser(self):
41 old_home = os.environ.get('HOME')
42 test_str = 'C:\Documents and Settings\тест\Application Data'
43 compat_setenv('HOME', test_str)
44 self.assertEqual(compat_expanduser('~'), test_str)
45 compat_setenv('HOME', old_home or '')
46
47 def test_all_present(self):
48 import youtube_dl.compat
49 all_names = youtube_dl.compat.__all__
50 present_names = set(filter(
51 lambda c: '_' in c and not c.startswith('_'),
52 dir(youtube_dl.compat))) - set(['unicode_literals'])
53 self.assertEqual(all_names, sorted(present_names))
54
55 def test_compat_urllib_parse_unquote(self):
56 self.assertEqual(compat_urllib_parse_unquote('abc%20def'), 'abc def')
57 self.assertEqual(compat_urllib_parse_unquote('%7e/abc+def'), '~/abc+def')
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('%%%'), '%%%')
62 self.assertEqual(compat_urllib_parse_unquote('%2F'), '/')
63 self.assertEqual(compat_urllib_parse_unquote('%2f'), '/')
64 self.assertEqual(compat_urllib_parse_unquote('%E6%B4%A5%E6%B3%A2'), '津波')
65 self.assertEqual(
66 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" />
67 %<a href="https://ar.wikipedia.org/wiki/%D8%AA%D8%B3%D9%88%D9%86%D8%A7%D9%85%D9%8A">%a'''),
68 '''<meta property="og:description" content="▁▂▃▄%▅▆▇█" />
69 %<a href="https://ar.wikipedia.org/wiki/تسونامي">%a''')
70 self.assertEqual(
71 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%'''),
72 '''(^◣_◢^)っ︻デ═一 ⇀ ⇀ ⇀ ⇀ ⇀ ↶%I%Break%Things%''')
73
74 def test_compat_urllib_parse_unquote_plus(self):
75 self.assertEqual(compat_urllib_parse_unquote_plus('abc%20def'), 'abc def')
76 self.assertEqual(compat_urllib_parse_unquote_plus('%7e/abc+def'), '~/abc def')
77
78 def test_compat_urllib_parse_urlencode(self):
79 self.assertEqual(compat_urllib_parse_urlencode({'abc': 'def'}), 'abc=def')
80 self.assertEqual(compat_urllib_parse_urlencode({'abc': b'def'}), 'abc=def')
81 self.assertEqual(compat_urllib_parse_urlencode({b'abc': 'def'}), 'abc=def')
82 self.assertEqual(compat_urllib_parse_urlencode({b'abc': b'def'}), 'abc=def')
83 self.assertEqual(compat_urllib_parse_urlencode([('abc', 'def')]), 'abc=def')
84 self.assertEqual(compat_urllib_parse_urlencode([('abc', b'def')]), 'abc=def')
85 self.assertEqual(compat_urllib_parse_urlencode([(b'abc', 'def')]), 'abc=def')
86 self.assertEqual(compat_urllib_parse_urlencode([(b'abc', b'def')]), 'abc=def')
87
88 def test_compat_shlex_split(self):
89 self.assertEqual(compat_shlex_split('-option "one two"'), ['-option', 'one two'])
90 self.assertEqual(compat_shlex_split('-option "one\ntwo" \n -flag'), ['-option', 'one\ntwo', '-flag'])
91 self.assertEqual(compat_shlex_split('-val 中文'), ['-val', '中文'])
92
93 def test_compat_etree_fromstring(self):
94 xml = '''
95 <root foo="bar" spam="中文">
96 <normal>foo</normal>
97 <chinese>中文</chinese>
98 <foo><bar>spam</bar></foo>
99 </root>
100 '''
101 doc = compat_etree_fromstring(xml.encode('utf-8'))
102 self.assertTrue(isinstance(doc.attrib['foo'], compat_str))
103 self.assertTrue(isinstance(doc.attrib['spam'], compat_str))
104 self.assertTrue(isinstance(doc.find('normal').text, compat_str))
105 self.assertTrue(isinstance(doc.find('chinese').text, compat_str))
106 self.assertTrue(isinstance(doc.find('foo/bar').text, compat_str))
107
108 def test_compat_etree_fromstring_doctype(self):
109 xml = '''<?xml version="1.0"?>
110 <!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">
111 <smil xmlns="http://www.w3.org/2001/SMIL20/Language"></smil>'''
112 compat_etree_fromstring(xml)
113
114 def test_struct_unpack(self):
115 self.assertEqual(compat_struct_unpack('!B', b'\x00'), (0,))
116
117
118 if __name__ == '__main__':
119 unittest.main()