l1ving_youtube-dl/test/test_jsinterp.py

100 lines
3.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python
2018-05-31 06:37:27 +02:00
# """
# see: `js2tests`
# """
from __future__ import unicode_literals
# Allow direct execution
import os
import sys
import logging
2016-12-14 18:21:57 +01:00
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from youtube_dl.jsinterp import JSInterpreter
from .js2tests import gettestcases
2016-12-14 18:21:57 +01:00
defs = gettestcases()
# set level to logging.DEBUG to see messages about missing assertions
logging.basicConfig(stream=sys.stderr, level=logging.WARNING)
2016-12-14 18:21:57 +01:00
class TestJSInterpreter(unittest.TestCase):
def setUp(self):
self.defs = defs
def generator(test_case, name):
2016-12-14 18:21:57 +01:00
def test_template(self):
for test in test_case['subtests']:
excluded = test.get('exclude')
if excluded is not None and 'jsinterp' in excluded:
log_reason = 'jsinterp does not support this subtest:\n%s' % test['code']
elif 'code' not in test:
log_reason = 'No code in subtest, skipping'
elif 'asserts' not in test:
log_reason = 'No assertion in subtest, skipping'
else:
log_reason = None
if log_reason is None:
variables = test.get('globals')
code = test['code']
call = None
if variables is not None:
code = 'function f(%s){%s}' % ((''.join(variables.keys())), code)
call = ('f',) + tuple(v for v in variables.values())
jsi = JSInterpreter(code, objects=variables)
for assertion in test['asserts']:
if 'value' in assertion:
if call is None:
call = assertion['call']
self.assertEqual(jsi.call_function(*call), assertion['value'])
2016-12-14 18:21:57 +01:00
else:
log.debug('No value in assertion, skipping')
else:
log.debug(log_reason)
log = logging.getLogger('TestJSInterpreter.%s' % name)
return test_template
2016-12-14 18:21:57 +01:00
# And add them to TestJSInterpreter
for n, tc in enumerate(defs):
reason = tc['skip'].get('jsinterp', False)
tname = 'test_' + str(tc['name'])
i = 1
while hasattr(TestJSInterpreter, tname):
tname = 'test_%s_%d' % (tc['name'], i)
i += 1
if reason is not True:
log_reason = 'Entirely'
elif not any('asserts' in test for test in tc['subtests']):
log_reason = '''There isn't any assertion'''
else:
log_reason = None
if log_reason is not None:
test_method = generator(tc, tname)
test_method.__name__ = str(tname)
if reason is not False:
test_method.__unittest_skip__ = True
test_method.__unittest_skip_why__ = reason
setattr(TestJSInterpreter, test_method.__name__, test_method)
del test_method
else:
log = logging.getLogger('TestJSInterpreter')
log.debug('Skipping %s:%s' % (tname, log_reason))
if __name__ == '__main__':
unittest.main()