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.

79 lines
3.1 KiB

  1. # -*- coding: utf-8 -*-
  2. # Various small unit tests
  3. import unittest
  4. #from youtube_dl.utils import htmlentity_transform
  5. from youtube_dl.utils import timeconvert
  6. from youtube_dl.utils import sanitize_filename
  7. from youtube_dl.utils import unescapeHTML
  8. from youtube_dl.utils import orderedSet
  9. class TestUtil(unittest.TestCase):
  10. def test_timeconvert(self):
  11. self.assertTrue(timeconvert('') is None)
  12. self.assertTrue(timeconvert('bougrg') is None)
  13. def test_sanitize_filename(self):
  14. self.assertEqual(sanitize_filename(u'abc'), u'abc')
  15. self.assertEqual(sanitize_filename(u'abc_d-e'), u'abc_d-e')
  16. self.assertEqual(sanitize_filename(u'123'), u'123')
  17. self.assertEqual(u'abc_de', sanitize_filename(u'abc/de'))
  18. self.assertFalse(u'/' in sanitize_filename(u'abc/de///'))
  19. self.assertEqual(u'abc_de', sanitize_filename(u'abc/<>\\*|de'))
  20. self.assertEqual(u'xxx', sanitize_filename(u'xxx/<>\\*|'))
  21. self.assertEqual(u'yes no', sanitize_filename(u'yes? no'))
  22. self.assertEqual(u'this - that', sanitize_filename(u'this: that'))
  23. self.assertEqual(sanitize_filename(u'AT&T'), u'AT&T')
  24. self.assertEqual(sanitize_filename(u'ä'), u'ä')
  25. self.assertEqual(sanitize_filename(u'кириллица'), u'кириллица')
  26. forbidden = u'"\0\\/'
  27. for fc in forbidden:
  28. for fbc in forbidden:
  29. self.assertTrue(fbc not in sanitize_filename(fc))
  30. def test_sanitize_filename_restricted(self):
  31. self.assertEqual(sanitize_filename(u'abc', restricted=True), u'abc')
  32. self.assertEqual(sanitize_filename(u'abc_d-e', restricted=True), u'abc_d-e')
  33. self.assertEqual(sanitize_filename(u'123', restricted=True), u'123')
  34. self.assertEqual(u'abc_de', sanitize_filename(u'abc/de', restricted=True))
  35. self.assertFalse(u'/' in sanitize_filename(u'abc/de///', restricted=True))
  36. self.assertEqual(u'abc_de', sanitize_filename(u'abc/<>\\*|de', restricted=True))
  37. self.assertEqual(u'xxx', sanitize_filename(u'xxx/<>\\*|', restricted=True))
  38. self.assertEqual(u'yes_no', sanitize_filename(u'yes? no', restricted=True))
  39. self.assertEqual(u'this_-_that', sanitize_filename(u'this: that', restricted=True))
  40. self.assertEqual(sanitize_filename(u'aäb中国的c', restricted=True), u'a_b_c')
  41. self.assertTrue(sanitize_filename(u'ö', restricted=True) != u'') # No empty filename
  42. forbidden = u'"\0\\/&: \'\t\n'
  43. for fc in forbidden:
  44. for fbc in forbidden:
  45. self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
  46. # Handle a common case more neatly
  47. self.assertEqual(sanitize_filename(u'大声带 - Song', restricted=True), u'Song')
  48. self.assertEqual(sanitize_filename(u'总统: Speech', restricted=True), u'Speech')
  49. # .. but make sure the file name is never empty
  50. self.assertTrue(sanitize_filename(u'-', restricted=True) != u'')
  51. self.assertTrue(sanitize_filename(u':', restricted=True) != u'')
  52. def test_ordered_set(self):
  53. self.assertEqual(orderedSet([1,1,2,3,4,4,5,6,7,3,5]), [1,2,3,4,5,6,7])
  54. self.assertEqual(orderedSet([]), [])
  55. self.assertEqual(orderedSet([1]), [1])
  56. #keep the list ordered
  57. self.assertEqual(orderedSet([135,1,1,1]), [135,1])
  58. def test_unescape_html(self):
  59. self.assertEqual(unescapeHTML(u"%20;"), u"%20;")