]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
8c25f81b PH |
2 | # Allow direct execution |
3 | import os | |
4 | import sys | |
5 | import unittest | |
f8271158 | 6 | |
8c25f81b PH |
7 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
8 | ||
9 | ||
9196cbfe | 10 | from yt_dlp import compat |
7a5c1cfe | 11 | from yt_dlp.compat import ( |
36e6f62c | 12 | compat_etree_fromstring, |
8c25f81b | 13 | compat_expanduser, |
f8271158 | 14 | compat_getenv, |
15 | compat_setenv, | |
36e6f62c | 16 | compat_str, |
edaa23f8 | 17 | compat_struct_unpack, |
4a632911 | 18 | compat_urllib_parse_unquote, |
8954e481 | 19 | compat_urllib_parse_unquote_plus, |
e289d6d6 | 20 | compat_urllib_parse_urlencode, |
8c25f81b PH |
21 | ) |
22 | ||
23 | ||
24 | class TestCompat(unittest.TestCase): | |
9196cbfe | 25 | def test_compat_passthrough(self): |
26 | with self.assertWarns(DeprecationWarning): | |
27 | compat.compat_basestring | |
28 | ||
29 | compat.asyncio.events # Must not raise error | |
30 | ||
8c25f81b PH |
31 | def test_compat_getenv(self): |
32 | test_str = 'тест' | |
7a5c1cfe P |
33 | compat_setenv('yt_dlp_COMPAT_GETENV', test_str) |
34 | self.assertEqual(compat_getenv('yt_dlp_COMPAT_GETENV'), test_str) | |
8c25f81b | 35 | |
fe40f9ee | 36 | def test_compat_setenv(self): |
7a5c1cfe | 37 | test_var = 'yt_dlp_COMPAT_SETENV' |
fe40f9ee S |
38 | test_str = 'тест' |
39 | compat_setenv(test_var, test_str) | |
40 | compat_getenv(test_var) | |
41 | self.assertEqual(compat_getenv(test_var), test_str) | |
42 | ||
8c25f81b | 43 | def test_compat_expanduser(self): |
f56875f2 | 44 | old_home = os.environ.get('HOME') |
19a03940 | 45 | test_str = R'C:\Documents and Settings\тест\Application Data' |
46 | try: | |
47 | compat_setenv('HOME', test_str) | |
48 | self.assertEqual(compat_expanduser('~'), test_str) | |
49 | finally: | |
50 | compat_setenv('HOME', old_home or '') | |
8c25f81b | 51 | |
4a632911 | 52 | def test_compat_urllib_parse_unquote(self): |
8954e481 S |
53 | self.assertEqual(compat_urllib_parse_unquote('abc%20def'), 'abc def') |
54 | self.assertEqual(compat_urllib_parse_unquote('%7e/abc+def'), '~/abc+def') | |
14309e1d S |
55 | self.assertEqual(compat_urllib_parse_unquote(''), '') |
56 | self.assertEqual(compat_urllib_parse_unquote('%'), '%') | |
57 | self.assertEqual(compat_urllib_parse_unquote('%%'), '%%') | |
58 | self.assertEqual(compat_urllib_parse_unquote('%%%'), '%%%') | |
59 | self.assertEqual(compat_urllib_parse_unquote('%2F'), '/') | |
60 | self.assertEqual(compat_urllib_parse_unquote('%2f'), '/') | |
61 | self.assertEqual(compat_urllib_parse_unquote('%E6%B4%A5%E6%B3%A2'), '津波') | |
14309e1d S |
62 | self.assertEqual( |
63 | 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" /> | |
64 | %<a href="https://ar.wikipedia.org/wiki/%D8%AA%D8%B3%D9%88%D9%86%D8%A7%D9%85%D9%8A">%a'''), | |
65 | '''<meta property="og:description" content="▁▂▃▄%▅▆▇█" /> | |
66 | %<a href="https://ar.wikipedia.org/wiki/تسونامي">%a''') | |
67 | self.assertEqual( | |
68 | 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%'''), | |
69 | '''(^◣_◢^)っ︻デ═一 ⇀ ⇀ ⇀ ⇀ ⇀ ↶%I%Break%Things%''') | |
4a632911 | 70 | |
8954e481 S |
71 | def test_compat_urllib_parse_unquote_plus(self): |
72 | self.assertEqual(compat_urllib_parse_unquote_plus('abc%20def'), 'abc def') | |
73 | self.assertEqual(compat_urllib_parse_unquote_plus('%7e/abc+def'), '~/abc def') | |
74 | ||
e289d6d6 S |
75 | def test_compat_urllib_parse_urlencode(self): |
76 | self.assertEqual(compat_urllib_parse_urlencode({'abc': 'def'}), 'abc=def') | |
77 | self.assertEqual(compat_urllib_parse_urlencode({'abc': b'def'}), 'abc=def') | |
78 | self.assertEqual(compat_urllib_parse_urlencode({b'abc': 'def'}), 'abc=def') | |
79 | self.assertEqual(compat_urllib_parse_urlencode({b'abc': b'def'}), 'abc=def') | |
92d5477d YCH |
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') | |
e289d6d6 | 84 | |
36e6f62c | 85 | def test_compat_etree_fromstring(self): |
f7854627 JMF |
86 | xml = ''' |
87 | <root foo="bar" spam="中文"> | |
88 | <normal>foo</normal> | |
89 | <chinese>中文</chinese> | |
90 | <foo><bar>spam</bar></foo> | |
91 | </root> | |
92 | ''' | |
0f06bcd7 | 93 | doc = compat_etree_fromstring(xml.encode()) |
36e6f62c | 94 | self.assertTrue(isinstance(doc.attrib['foo'], compat_str)) |
387db16a | 95 | self.assertTrue(isinstance(doc.attrib['spam'], compat_str)) |
f7854627 JMF |
96 | self.assertTrue(isinstance(doc.find('normal').text, compat_str)) |
97 | self.assertTrue(isinstance(doc.find('chinese').text, compat_str)) | |
98 | self.assertTrue(isinstance(doc.find('foo/bar').text, compat_str)) | |
36e6f62c | 99 | |
eb7941e3 YCH |
100 | def test_compat_etree_fromstring_doctype(self): |
101 | xml = '''<?xml version="1.0"?> | |
102 | <!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd"> | |
103 | <smil xmlns="http://www.w3.org/2001/SMIL20/Language"></smil>''' | |
104 | compat_etree_fromstring(xml) | |
105 | ||
dab0daee | 106 | def test_struct_unpack(self): |
edaa23f8 | 107 | self.assertEqual(compat_struct_unpack('!B', b'\x00'), (0,)) |
dab0daee YCH |
108 | |
109 | ||
8c25f81b PH |
110 | if __name__ == '__main__': |
111 | unittest.main() |