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.

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