]> jfr.im git - yt-dlp.git/blame - test/test_compat.py
Completely change project name to yt-dlp (#85)
[yt-dlp.git] / test / test_compat.py
CommitLineData
8c25f81b
PH
1#!/usr/bin/env python
2# coding: utf-8
3
4from __future__ import unicode_literals
5
6# Allow direct execution
7import os
8import sys
9import unittest
10sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11
12
7a5c1cfe 13from yt_dlp.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,
732044af 22 compat_urllib_parse_quote,
23 compat_urllib_parse_quote_plus,
4a632911 24 compat_urllib_parse_unquote,
8954e481 25 compat_urllib_parse_unquote_plus,
e289d6d6 26 compat_urllib_parse_urlencode,
8c25f81b
PH
27)
28
29
30class TestCompat(unittest.TestCase):
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')
6864855e 45 test_str = r'C:\Documents and Settings\тест\Application Data'
12926387 46 compat_setenv('HOME', test_str)
8c25f81b 47 self.assertEqual(compat_expanduser('~'), test_str)
20cfdcc9 48 compat_setenv('HOME', old_home or '')
8c25f81b
PH
49
50 def test_all_present(self):
7a5c1cfe
P
51 import yt_dlp.compat
52 all_names = yt_dlp.compat.__all__
8c25f81b
PH
53 present_names = set(filter(
54 lambda c: '_' in c and not c.startswith('_'),
7a5c1cfe 55 dir(yt_dlp.compat))) - set(['unicode_literals'])
8c25f81b
PH
56 self.assertEqual(all_names, sorted(present_names))
57
732044af 58 def test_compat_urllib_parse_quote(self):
59 self.assertEqual(compat_urllib_parse_quote('abc def'), 'abc%20def')
5d0c5371 60 self.assertEqual(compat_urllib_parse_quote('/user/abc+def'), '/user/abc%2Bdef')
61 self.assertEqual(compat_urllib_parse_quote('/user/abc+def', safe='+'), '%2Fuser%2Fabc+def')
732044af 62 self.assertEqual(compat_urllib_parse_quote(''), '')
63 self.assertEqual(compat_urllib_parse_quote('%'), '%25')
64 self.assertEqual(compat_urllib_parse_quote('%', safe='%'), '%')
65 self.assertEqual(compat_urllib_parse_quote('津波'), '%E6%B4%A5%E6%B3%A2')
66 self.assertEqual(
67 compat_urllib_parse_quote('''<meta property="og:description" content="▁▂▃▄%▅▆▇█" />
68%<a href="https://ar.wikipedia.org/wiki/تسونامي">%a''', safe='<>=":%/ \r\n'),
69 '''<meta property="og:description" content="%E2%96%81%E2%96%82%E2%96%83%E2%96%84%%E2%96%85%E2%96%86%E2%96%87%E2%96%88" />
70%<a href="https://ar.wikipedia.org/wiki/%D8%AA%D8%B3%D9%88%D9%86%D8%A7%D9%85%D9%8A">%a''')
71 self.assertEqual(
72 compat_urllib_parse_quote('''(^◣_◢^)っ︻デ═一 ⇀ ⇀ ⇀ ⇀ ⇀ ↶%I%Break%25Things%''', safe='% '),
73 '''%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%''')
74
75 def test_compat_urllib_parse_quote_plus(self):
76 self.assertEqual(compat_urllib_parse_quote_plus('abc def'), 'abc+def')
5d0c5371 77 self.assertEqual(compat_urllib_parse_quote_plus('/abc def'), '%2Fabc+def')
732044af 78
4a632911 79 def test_compat_urllib_parse_unquote(self):
8954e481
S
80 self.assertEqual(compat_urllib_parse_unquote('abc%20def'), 'abc def')
81 self.assertEqual(compat_urllib_parse_unquote('%7e/abc+def'), '~/abc+def')
14309e1d
S
82 self.assertEqual(compat_urllib_parse_unquote(''), '')
83 self.assertEqual(compat_urllib_parse_unquote('%'), '%')
84 self.assertEqual(compat_urllib_parse_unquote('%%'), '%%')
85 self.assertEqual(compat_urllib_parse_unquote('%%%'), '%%%')
86 self.assertEqual(compat_urllib_parse_unquote('%2F'), '/')
87 self.assertEqual(compat_urllib_parse_unquote('%2f'), '/')
88 self.assertEqual(compat_urllib_parse_unquote('%E6%B4%A5%E6%B3%A2'), '津波')
14309e1d
S
89 self.assertEqual(
90 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" />
91%<a href="https://ar.wikipedia.org/wiki/%D8%AA%D8%B3%D9%88%D9%86%D8%A7%D9%85%D9%8A">%a'''),
92 '''<meta property="og:description" content="▁▂▃▄%▅▆▇█" />
93%<a href="https://ar.wikipedia.org/wiki/تسونامي">%a''')
94 self.assertEqual(
95 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%'''),
96 '''(^◣_◢^)っ︻デ═一 ⇀ ⇀ ⇀ ⇀ ⇀ ↶%I%Break%Things%''')
4a632911 97
8954e481
S
98 def test_compat_urllib_parse_unquote_plus(self):
99 self.assertEqual(compat_urllib_parse_unquote_plus('abc%20def'), 'abc def')
100 self.assertEqual(compat_urllib_parse_unquote_plus('%7e/abc+def'), '~/abc def')
101
e289d6d6
S
102 def test_compat_urllib_parse_urlencode(self):
103 self.assertEqual(compat_urllib_parse_urlencode({'abc': 'def'}), 'abc=def')
104 self.assertEqual(compat_urllib_parse_urlencode({'abc': b'def'}), 'abc=def')
105 self.assertEqual(compat_urllib_parse_urlencode({b'abc': 'def'}), 'abc=def')
106 self.assertEqual(compat_urllib_parse_urlencode({b'abc': b'def'}), 'abc=def')
92d5477d
YCH
107 self.assertEqual(compat_urllib_parse_urlencode([('abc', 'def')]), 'abc=def')
108 self.assertEqual(compat_urllib_parse_urlencode([('abc', b'def')]), 'abc=def')
109 self.assertEqual(compat_urllib_parse_urlencode([(b'abc', 'def')]), 'abc=def')
110 self.assertEqual(compat_urllib_parse_urlencode([(b'abc', b'def')]), 'abc=def')
e289d6d6 111
3513d414 112 def test_compat_shlex_split(self):
ee087c79 113 self.assertEqual(compat_shlex_split('-option "one two"'), ['-option', 'one two'])
a9eede39 114 self.assertEqual(compat_shlex_split('-option "one\ntwo" \n -flag'), ['-option', 'one\ntwo', '-flag'])
dfe5fa49 115 self.assertEqual(compat_shlex_split('-val 中文'), ['-val', '中文'])
ee087c79 116
399f7687
S
117 def test_compat_etree_Element(self):
118 try:
fca9baf0 119 compat_etree_Element.items
399f7687
S
120 except AttributeError:
121 self.fail('compat_etree_Element is not a type')
122
36e6f62c 123 def test_compat_etree_fromstring(self):
f7854627
JMF
124 xml = '''
125 <root foo="bar" spam="中文">
126 <normal>foo</normal>
127 <chinese>中文</chinese>
128 <foo><bar>spam</bar></foo>
129 </root>
130 '''
36e6f62c
JMF
131 doc = compat_etree_fromstring(xml.encode('utf-8'))
132 self.assertTrue(isinstance(doc.attrib['foo'], compat_str))
387db16a 133 self.assertTrue(isinstance(doc.attrib['spam'], compat_str))
f7854627
JMF
134 self.assertTrue(isinstance(doc.find('normal').text, compat_str))
135 self.assertTrue(isinstance(doc.find('chinese').text, compat_str))
136 self.assertTrue(isinstance(doc.find('foo/bar').text, compat_str))
36e6f62c 137
eb7941e3
YCH
138 def test_compat_etree_fromstring_doctype(self):
139 xml = '''<?xml version="1.0"?>
140<!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">
141<smil xmlns="http://www.w3.org/2001/SMIL20/Language"></smil>'''
142 compat_etree_fromstring(xml)
143
dab0daee 144 def test_struct_unpack(self):
edaa23f8 145 self.assertEqual(compat_struct_unpack('!B', b'\x00'), (0,))
dab0daee
YCH
146
147
8c25f81b
PH
148if __name__ == '__main__':
149 unittest.main()