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.

71 lines
3.1 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.utils import get_filesystem_encoding
  10. from youtube_dl.compat import (
  11. compat_getenv,
  12. compat_expanduser,
  13. compat_urllib_parse_unquote,
  14. compat_urllib_parse_unquote_plus,
  15. )
  16. class TestCompat(unittest.TestCase):
  17. def test_compat_getenv(self):
  18. test_str = 'тест'
  19. os.environ['YOUTUBE-DL-TEST'] = (
  20. test_str if sys.version_info >= (3, 0)
  21. else test_str.encode(get_filesystem_encoding()))
  22. self.assertEqual(compat_getenv('YOUTUBE-DL-TEST'), test_str)
  23. def test_compat_expanduser(self):
  24. old_home = os.environ.get('HOME')
  25. test_str = 'C:\Documents and Settings\тест\Application Data'
  26. os.environ['HOME'] = (
  27. test_str if sys.version_info >= (3, 0)
  28. else test_str.encode(get_filesystem_encoding()))
  29. self.assertEqual(compat_expanduser('~'), test_str)
  30. os.environ['HOME'] = old_home
  31. def test_all_present(self):
  32. import youtube_dl.compat
  33. all_names = youtube_dl.compat.__all__
  34. present_names = set(filter(
  35. lambda c: '_' in c and not c.startswith('_'),
  36. dir(youtube_dl.compat))) - set(['unicode_literals'])
  37. self.assertEqual(all_names, sorted(present_names))
  38. def test_compat_urllib_parse_unquote(self):
  39. self.assertEqual(compat_urllib_parse_unquote('abc%20def'), 'abc def')
  40. self.assertEqual(compat_urllib_parse_unquote('%7e/abc+def'), '~/abc+def')
  41. self.assertEqual(compat_urllib_parse_unquote(''), '')
  42. self.assertEqual(compat_urllib_parse_unquote('%'), '%')
  43. self.assertEqual(compat_urllib_parse_unquote('%%'), '%%')
  44. self.assertEqual(compat_urllib_parse_unquote('%%%'), '%%%')
  45. self.assertEqual(compat_urllib_parse_unquote('%2F'), '/')
  46. self.assertEqual(compat_urllib_parse_unquote('%2f'), '/')
  47. self.assertEqual(compat_urllib_parse_unquote('%E6%B4%A5%E6%B3%A2'), '津波')
  48. self.assertEqual(
  49. 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" />
  50. %<a href="https://ar.wikipedia.org/wiki/%D8%AA%D8%B3%D9%88%D9%86%D8%A7%D9%85%D9%8A">%a'''),
  51. '''<meta property="og:description" content="▁▂▃▄%▅▆▇█" />
  52. %<a href="https://ar.wikipedia.org/wiki/تسونامي">%a''')
  53. self.assertEqual(
  54. 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%'''),
  55. '''(^◣_◢^)っ︻デ═一 ⇀ ⇀ ⇀ ⇀ ⇀ ↶%I%Break%Things%''')
  56. def test_compat_urllib_parse_unquote_plus(self):
  57. self.assertEqual(compat_urllib_parse_unquote_plus('abc%20def'), 'abc def')
  58. self.assertEqual(compat_urllib_parse_unquote_plus('%7e/abc+def'), '~/abc def')
  59. if __name__ == '__main__':
  60. unittest.main()