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.

80 lines
2.2 KiB

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