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