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
2.1 KiB

  1. #!/usr/bin/env python
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. import errno
  8. import io
  9. import json
  10. import re
  11. import subprocess
  12. from youtube_dl.swfinterp import SWFInterpreter
  13. TEST_DIR = os.path.join(
  14. os.path.dirname(os.path.abspath(__file__)), 'swftests')
  15. class TestSWFInterpreter(unittest.TestCase):
  16. pass
  17. def _make_testfunc(testfile):
  18. m = re.match(r'^(.*)\.(as)$', testfile)
  19. if not m:
  20. return
  21. test_id = m.group(1)
  22. def test_func(self):
  23. as_file = os.path.join(TEST_DIR, testfile)
  24. swf_file = os.path.join(TEST_DIR, test_id + '.swf')
  25. if ((not os.path.exists(swf_file))
  26. or os.path.getmtime(swf_file) < os.path.getmtime(as_file)):
  27. # Recompile
  28. try:
  29. subprocess.check_call([
  30. 'mxmlc', '-output', swf_file,
  31. '-static-link-runtime-shared-libraries', as_file])
  32. except OSError as ose:
  33. if ose.errno == errno.ENOENT:
  34. print('mxmlc not found! Skipping test.')
  35. return
  36. raise
  37. with open(swf_file, 'rb') as swf_f:
  38. swf_content = swf_f.read()
  39. swfi = SWFInterpreter(swf_content)
  40. with io.open(as_file, 'r', encoding='utf-8') as as_f:
  41. as_content = as_f.read()
  42. def _find_spec(key):
  43. m = re.search(
  44. r'(?m)^//\s*%s:\s*(.*?)\n' % re.escape(key), as_content)
  45. if not m:
  46. raise ValueError('Cannot find %s in %s' % (key, testfile))
  47. return json.loads(m.group(1))
  48. input_args = _find_spec('input')
  49. output = _find_spec('output')
  50. swf_class = swfi.extract_class(test_id)
  51. func = swfi.extract_function(swf_class, 'main')
  52. res = func(input_args)
  53. self.assertEqual(res, output)
  54. test_func.__name__ = str('test_swf_' + test_id)
  55. setattr(TestSWFInterpreter, test_func.__name__, test_func)
  56. for testfile in os.listdir(TEST_DIR):
  57. _make_testfunc(testfile)
  58. if __name__ == '__main__':
  59. unittest.main()