You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
5.2 KiB

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