]>
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 | ||
cefecac1 | 13 | from youtube_dlc.compat import ( |
8c25f81b | 14 | compat_getenv, |
fe40f9ee | 15 | compat_setenv, |
399f7687 | 16 | compat_etree_Element, |
36e6f62c | 17 | compat_etree_fromstring, |
8c25f81b | 18 | compat_expanduser, |
ee087c79 | 19 | compat_shlex_split, |
36e6f62c | 20 | compat_str, |
edaa23f8 | 21 | compat_struct_unpack, |
4a632911 | 22 | compat_urllib_parse_unquote, |
8954e481 | 23 | compat_urllib_parse_unquote_plus, |
e289d6d6 | 24 | compat_urllib_parse_urlencode, |
8c25f81b PH |
25 | ) |
26 | ||
27 | ||
28 | class TestCompat(unittest.TestCase): | |
29 | def test_compat_getenv(self): | |
30 | test_str = 'тест' | |
cefecac1 U |
31 | compat_setenv('youtube_dlc_COMPAT_GETENV', test_str) |
32 | self.assertEqual(compat_getenv('youtube_dlc_COMPAT_GETENV'), test_str) | |
8c25f81b | 33 | |
fe40f9ee | 34 | def test_compat_setenv(self): |
cefecac1 | 35 | test_var = 'youtube_dlc_COMPAT_SETENV' |
fe40f9ee S |
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 | ||
8c25f81b | 41 | def test_compat_expanduser(self): |
f56875f2 | 42 | old_home = os.environ.get('HOME') |
6864855e | 43 | test_str = r'C:\Documents and Settings\тест\Application Data' |
12926387 | 44 | compat_setenv('HOME', test_str) |
8c25f81b | 45 | self.assertEqual(compat_expanduser('~'), test_str) |
20cfdcc9 | 46 | compat_setenv('HOME', old_home or '') |
8c25f81b PH |
47 | |
48 | def test_all_present(self): | |
cefecac1 U |
49 | import youtube_dlc.compat |
50 | all_names = youtube_dlc.compat.__all__ | |
8c25f81b PH |
51 | present_names = set(filter( |
52 | lambda c: '_' in c and not c.startswith('_'), | |
cefecac1 | 53 | dir(youtube_dlc.compat))) - set(['unicode_literals']) |
8c25f81b PH |
54 | self.assertEqual(all_names, sorted(present_names)) |
55 | ||
4a632911 | 56 | def test_compat_urllib_parse_unquote(self): |
8954e481 S |
57 | self.assertEqual(compat_urllib_parse_unquote('abc%20def'), 'abc def') |
58 | self.assertEqual(compat_urllib_parse_unquote('%7e/abc+def'), '~/abc+def') | |
14309e1d S |
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'), '津波') | |
14309e1d S |
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%''') | |
4a632911 | 74 | |
8954e481 S |
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 | ||
e289d6d6 S |
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') | |
92d5477d YCH |
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') | |
e289d6d6 | 88 | |
3513d414 | 89 | def test_compat_shlex_split(self): |
ee087c79 | 90 | self.assertEqual(compat_shlex_split('-option "one two"'), ['-option', 'one two']) |
a9eede39 | 91 | self.assertEqual(compat_shlex_split('-option "one\ntwo" \n -flag'), ['-option', 'one\ntwo', '-flag']) |
dfe5fa49 | 92 | self.assertEqual(compat_shlex_split('-val 中文'), ['-val', '中文']) |
ee087c79 | 93 | |
399f7687 S |
94 | def test_compat_etree_Element(self): |
95 | try: | |
fca9baf0 | 96 | compat_etree_Element.items |
399f7687 S |
97 | except AttributeError: |
98 | self.fail('compat_etree_Element is not a type') | |
99 | ||
36e6f62c | 100 | def test_compat_etree_fromstring(self): |
f7854627 JMF |
101 | xml = ''' |
102 | <root foo="bar" spam="中文"> | |
103 | <normal>foo</normal> | |
104 | <chinese>中文</chinese> | |
105 | <foo><bar>spam</bar></foo> | |
106 | </root> | |
107 | ''' | |
36e6f62c JMF |
108 | doc = compat_etree_fromstring(xml.encode('utf-8')) |
109 | self.assertTrue(isinstance(doc.attrib['foo'], compat_str)) | |
387db16a | 110 | self.assertTrue(isinstance(doc.attrib['spam'], compat_str)) |
f7854627 JMF |
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)) | |
36e6f62c | 114 | |
eb7941e3 YCH |
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 | ||
dab0daee | 121 | def test_struct_unpack(self): |
edaa23f8 | 122 | self.assertEqual(compat_struct_unpack('!B', b'\x00'), (0,)) |
dab0daee YCH |
123 | |
124 | ||
8c25f81b PH |
125 | if __name__ == '__main__': |
126 | unittest.main() |