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.

100 lines
4.2 KiB

  1. #!/usr/bin/env python
  2. # Various small unit tests
  3. import sys
  4. import unittest
  5. # Allow direct execution
  6. import os
  7. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  8. #from youtube_dl.utils import htmlentity_transform
  9. from youtube_dl.utils import timeconvert
  10. from youtube_dl.utils import sanitize_filename
  11. from youtube_dl.utils import unescapeHTML
  12. from youtube_dl.utils import orderedSet
  13. if sys.version_info < (3, 0):
  14. _compat_str = lambda b: b.decode('unicode-escape')
  15. else:
  16. _compat_str = lambda s: s
  17. class TestUtil(unittest.TestCase):
  18. def test_timeconvert(self):
  19. self.assertTrue(timeconvert('') is None)
  20. self.assertTrue(timeconvert('bougrg') is None)
  21. def test_sanitize_filename(self):
  22. self.assertEqual(sanitize_filename('abc'), 'abc')
  23. self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
  24. self.assertEqual(sanitize_filename('123'), '123')
  25. self.assertEqual('abc_de', sanitize_filename('abc/de'))
  26. self.assertFalse('/' in sanitize_filename('abc/de///'))
  27. self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
  28. self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
  29. self.assertEqual('yes no', sanitize_filename('yes? no'))
  30. self.assertEqual('this - that', sanitize_filename('this: that'))
  31. self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
  32. aumlaut = _compat_str('\xe4')
  33. self.assertEqual(sanitize_filename(aumlaut), aumlaut)
  34. tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
  35. self.assertEqual(sanitize_filename(tests), tests)
  36. forbidden = '"\0\\/'
  37. for fc in forbidden:
  38. for fbc in forbidden:
  39. self.assertTrue(fbc not in sanitize_filename(fc))
  40. def test_sanitize_filename_restricted(self):
  41. self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
  42. self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
  43. self.assertEqual(sanitize_filename('123', restricted=True), '123')
  44. self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
  45. self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
  46. self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
  47. self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
  48. self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
  49. self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
  50. tests = _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
  51. self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
  52. self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '') # No empty filename
  53. forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
  54. for fc in forbidden:
  55. for fbc in forbidden:
  56. self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
  57. # Handle a common case more neatly
  58. self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song')
  59. self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech')
  60. # .. but make sure the file name is never empty
  61. self.assertTrue(sanitize_filename('-', restricted=True) != '')
  62. self.assertTrue(sanitize_filename(':', restricted=True) != '')
  63. def test_sanitize_ids(self):
  64. self.assertEquals(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
  65. self.assertEquals(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
  66. self.assertEquals(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
  67. def test_ordered_set(self):
  68. self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
  69. self.assertEqual(orderedSet([]), [])
  70. self.assertEqual(orderedSet([1]), [1])
  71. #keep the list ordered
  72. self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
  73. def test_unescape_html(self):
  74. self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
  75. if __name__ == '__main__':
  76. unittest.main()