test_compat.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. from __future__ import unicode_literals
  4. # Allow direct execution
  5. import os
  6. import sys
  7. import unittest
  8. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  9. from youtube_dl.compat import (
  10. compat_casefold,
  11. compat_getenv,
  12. compat_setenv,
  13. compat_etree_Element,
  14. compat_etree_fromstring,
  15. compat_expanduser,
  16. compat_shlex_split,
  17. compat_str,
  18. compat_struct_unpack,
  19. compat_urllib_parse_unquote,
  20. compat_urllib_parse_unquote_plus,
  21. compat_urllib_parse_urlencode,
  22. compat_urllib_request,
  23. )
  24. class TestCompat(unittest.TestCase):
  25. def test_compat_getenv(self):
  26. test_str = 'тест'
  27. compat_setenv('YOUTUBE_DL_COMPAT_GETENV', test_str)
  28. self.assertEqual(compat_getenv('YOUTUBE_DL_COMPAT_GETENV'), test_str)
  29. def test_compat_setenv(self):
  30. test_var = 'YOUTUBE_DL_COMPAT_SETENV'
  31. test_str = 'тест'
  32. compat_setenv(test_var, test_str)
  33. compat_getenv(test_var)
  34. self.assertEqual(compat_getenv(test_var), test_str)
  35. def test_compat_expanduser(self):
  36. old_home = os.environ.get('HOME')
  37. test_str = r'C:\Documents and Settings\тест\Application Data'
  38. compat_setenv('HOME', test_str)
  39. self.assertEqual(compat_expanduser('~'), test_str)
  40. compat_setenv('HOME', old_home or '')
  41. def test_all_present(self):
  42. import youtube_dl.compat
  43. all_names = sorted(
  44. youtube_dl.compat.__all__ + youtube_dl.compat.legacy)
  45. present_names = set(map(compat_str, filter(
  46. lambda c: '_' in c and not c.startswith('_'),
  47. dir(youtube_dl.compat)))) - set(['unicode_literals'])
  48. self.assertEqual(all_names, sorted(present_names))
  49. def test_compat_urllib_parse_unquote(self):
  50. self.assertEqual(compat_urllib_parse_unquote('abc%20def'), 'abc def')
  51. self.assertEqual(compat_urllib_parse_unquote('%7e/abc+def'), '~/abc+def')
  52. self.assertEqual(compat_urllib_parse_unquote(''), '')
  53. self.assertEqual(compat_urllib_parse_unquote('%'), '%')
  54. self.assertEqual(compat_urllib_parse_unquote('%%'), '%%')
  55. self.assertEqual(compat_urllib_parse_unquote('%%%'), '%%%')
  56. self.assertEqual(compat_urllib_parse_unquote('%2F'), '/')
  57. self.assertEqual(compat_urllib_parse_unquote('%2f'), '/')
  58. self.assertEqual(compat_urllib_parse_unquote('%E6%B4%A5%E6%B3%A2'), '津波')
  59. self.assertEqual(
  60. 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" />
  61. %<a href="https://ar.wikipedia.org/wiki/%D8%AA%D8%B3%D9%88%D9%86%D8%A7%D9%85%D9%8A">%a'''),
  62. '''<meta property="og:description" content="▁▂▃▄%▅▆▇█" />
  63. %<a href="https://ar.wikipedia.org/wiki/تسونامي">%a''')
  64. self.assertEqual(
  65. 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%'''),
  66. '''(^◣_◢^)っ︻デ═一 ⇀ ⇀ ⇀ ⇀ ⇀ ↶%I%Break%Things%''')
  67. def test_compat_urllib_parse_unquote_plus(self):
  68. self.assertEqual(compat_urllib_parse_unquote_plus('abc%20def'), 'abc def')
  69. self.assertEqual(compat_urllib_parse_unquote_plus('%7e/abc+def'), '~/abc def')
  70. def test_compat_urllib_parse_urlencode(self):
  71. self.assertEqual(compat_urllib_parse_urlencode({'abc': 'def'}), 'abc=def')
  72. self.assertEqual(compat_urllib_parse_urlencode({'abc': b'def'}), 'abc=def')
  73. self.assertEqual(compat_urllib_parse_urlencode({b'abc': 'def'}), 'abc=def')
  74. self.assertEqual(compat_urllib_parse_urlencode({b'abc': b'def'}), 'abc=def')
  75. self.assertEqual(compat_urllib_parse_urlencode([('abc', 'def')]), 'abc=def')
  76. self.assertEqual(compat_urllib_parse_urlencode([('abc', b'def')]), 'abc=def')
  77. self.assertEqual(compat_urllib_parse_urlencode([(b'abc', 'def')]), 'abc=def')
  78. self.assertEqual(compat_urllib_parse_urlencode([(b'abc', b'def')]), 'abc=def')
  79. def test_compat_shlex_split(self):
  80. self.assertEqual(compat_shlex_split('-option "one two"'), ['-option', 'one two'])
  81. self.assertEqual(compat_shlex_split('-option "one\ntwo" \n -flag'), ['-option', 'one\ntwo', '-flag'])
  82. self.assertEqual(compat_shlex_split('-val 中文'), ['-val', '中文'])
  83. def test_compat_etree_Element(self):
  84. try:
  85. compat_etree_Element.items
  86. except AttributeError:
  87. self.fail('compat_etree_Element is not a type')
  88. def test_compat_etree_fromstring(self):
  89. xml = '''
  90. <root foo="bar" spam="中文">
  91. <normal>foo</normal>
  92. <chinese>中文</chinese>
  93. <foo><bar>spam</bar></foo>
  94. </root>
  95. '''
  96. doc = compat_etree_fromstring(xml.encode('utf-8'))
  97. self.assertTrue(isinstance(doc.attrib['foo'], compat_str))
  98. self.assertTrue(isinstance(doc.attrib['spam'], compat_str))
  99. self.assertTrue(isinstance(doc.find('normal').text, compat_str))
  100. self.assertTrue(isinstance(doc.find('chinese').text, compat_str))
  101. self.assertTrue(isinstance(doc.find('foo/bar').text, compat_str))
  102. def test_compat_etree_fromstring_doctype(self):
  103. xml = '''<?xml version="1.0"?>
  104. <!DOCTYPE smil PUBLIC "-//W3C//DTD SMIL 2.0//EN" "http://www.w3.org/2001/SMIL20/SMIL20.dtd">
  105. <smil xmlns="http://www.w3.org/2001/SMIL20/Language"></smil>'''
  106. compat_etree_fromstring(xml)
  107. def test_compat_struct_unpack(self):
  108. self.assertEqual(compat_struct_unpack('!B', b'\x00'), (0,))
  109. def test_compat_casefold(self):
  110. if hasattr(compat_str, 'casefold'):
  111. # don't bother to test str.casefold() (again)
  112. return
  113. # thanks https://bugs.python.org/file24232/casefolding.patch
  114. self.assertEqual(compat_casefold('hello'), 'hello')
  115. self.assertEqual(compat_casefold('hELlo'), 'hello')
  116. self.assertEqual(compat_casefold('ß'), 'ss')
  117. self.assertEqual(compat_casefold('fi'), 'fi')
  118. self.assertEqual(compat_casefold('\u03a3'), '\u03c3')
  119. self.assertEqual(compat_casefold('A\u0345\u03a3'), 'a\u03b9\u03c3')
  120. def test_compat_urllib_request_Request(self):
  121. self.assertEqual(
  122. compat_urllib_request.Request('http://127.0.0.1', method='PUT').get_method(),
  123. 'PUT')
  124. class PUTrequest(compat_urllib_request.Request):
  125. def get_method(self):
  126. return 'PUT'
  127. self.assertEqual(
  128. PUTrequest('http://127.0.0.1').get_method(),
  129. 'PUT')
  130. if __name__ == '__main__':
  131. unittest.main()