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.

73 lines
2.8 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', restricted=True), u'a_b')
  41. self.assertTrue(sanitize_filename(u'ö', restricted=True) != u'') # No empty filename
  42. forbidden = u'"\0\\/&: \'\t\n'
  43. for fc in forbidden:
  44. print('input: ' + fc + ', result: ' + repr(sanitize_filename(fc, restricted=True)))
  45. for fbc in forbidden:
  46. self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
  47. def test_ordered_set(self):
  48. self.assertEqual(orderedSet([1,1,2,3,4,4,5,6,7,3,5]), [1,2,3,4,5,6,7])
  49. self.assertEqual(orderedSet([]), [])
  50. self.assertEqual(orderedSet([1]), [1])
  51. #keep the list ordered
  52. self.assertEqual(orderedSet([135,1,1,1]), [135,1])
  53. def test_unescape_html(self):
  54. self.assertEqual(unescapeHTML(u"%20;"), u"%20;")