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.

77 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(['mxmlc', '-output', swf_file, as_file])
  30. except OSError as ose:
  31. if ose.errno == errno.ENOENT:
  32. print('mxmlc not found! Skipping test.')
  33. return
  34. raise
  35. with open(swf_file, 'rb') as swf_f:
  36. swf_content = swf_f.read()
  37. swfi = SWFInterpreter(swf_content)
  38. with io.open(as_file, 'r', encoding='utf-8') as as_f:
  39. as_content = as_f.read()
  40. def _find_spec(key):
  41. m = re.search(
  42. r'(?m)^//\s*%s:\s*(.*?)\n' % re.escape(key), as_content)
  43. if not m:
  44. raise ValueError('Cannot find %s in %s' % (key, testfile))
  45. return json.loads(m.group(1))
  46. input_args = _find_spec('input')
  47. output = _find_spec('output')
  48. swf_class = swfi.extract_class(test_id)
  49. func = swfi.extract_function(swf_class, 'main')
  50. res = func(input_args)
  51. self.assertEqual(res, output)
  52. test_func.__name__ = str('test_swf_' + test_id)
  53. setattr(TestSWFInterpreter, test_func.__name__, test_func)
  54. for testfile in os.listdir(TEST_DIR):
  55. _make_testfunc(testfile)
  56. if __name__ == '__main__':
  57. unittest.main()