test_compat.py 5.6 KB

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