]> jfr.im git - yt-dlp.git/blame - test/test_swfinterp.py
[__init__] Modernize
[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
54330a1c 10import errno
0cb20563
PH
11import io
12import json
13import re
14import subprocess
15
16from youtube_dl.swfinterp import SWFInterpreter
17
18
19TEST_DIR = os.path.join(
20 os.path.dirname(os.path.abspath(__file__)), 'swftests')
21
22
23class TestSWFInterpreter(unittest.TestCase):
24 pass
25
26
e75c24e8 27def _make_testfunc(testfile):
0cb20563
PH
28 m = re.match(r'^(.*)\.(as)$', testfile)
29 if not m:
e75c24e8 30 return
0cb20563
PH
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:
39f0a2a6
PH
40 subprocess.check_call([
41 'mxmlc', '-output', swf_file,
42 '-static-link-runtime-shared-libraries', as_file])
0cb20563
PH
43 except OSError as ose:
44 if ose.errno == errno.ENOENT:
45 print('mxmlc not found! Skipping test.')
46 return
47 raise
48
49 with open(swf_file, 'rb') as swf_f:
50 swf_content = swf_f.read()
51 swfi = SWFInterpreter(swf_content)
52
53 with io.open(as_file, 'r', encoding='utf-8') as as_f:
54 as_content = as_f.read()
55
56 def _find_spec(key):
57 m = re.search(
58 r'(?m)^//\s*%s:\s*(.*?)\n' % re.escape(key), as_content)
59 if not m:
60 raise ValueError('Cannot find %s in %s' % (key, testfile))
61 return json.loads(m.group(1))
62
63 input_args = _find_spec('input')
64 output = _find_spec('output')
65
66 swf_class = swfi.extract_class(test_id)
67 func = swfi.extract_function(swf_class, 'main')
68 res = func(input_args)
69 self.assertEqual(res, output)
70
71 test_func.__name__ = str('test_swf_' + test_id)
72 setattr(TestSWFInterpreter, test_func.__name__, test_func)
73
74
e75c24e8
PH
75for testfile in os.listdir(TEST_DIR):
76 _make_testfunc(testfile)
77
0cb20563
PH
78if __name__ == '__main__':
79 unittest.main()