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.

92 lines
3.8 KiB

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