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