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