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