2015-02-01 22:38:26 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2016-12-29 06:29:59 +01:00
|
|
|
"""
|
|
|
|
see: `jstests`
|
|
|
|
"""
|
|
|
|
|
2015-02-01 22:38:26 +01:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2016-12-27 06:28:17 +01:00
|
|
|
# Allow direct execution
|
2015-02-01 22:38:26 +01:00
|
|
|
import os
|
|
|
|
import sys
|
2016-12-15 21:05:12 +01:00
|
|
|
import logging
|
2016-12-14 18:21:57 +01:00
|
|
|
|
2016-12-10 00:52:04 +01:00
|
|
|
if sys.version_info < (2, 7):
|
|
|
|
import unittest2 as unittest
|
|
|
|
else:
|
|
|
|
import unittest
|
2015-02-01 22:38:26 +01:00
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
|
|
from youtube_dl.jsinterp import JSInterpreter
|
2016-12-29 06:29:59 +01:00
|
|
|
from .jstests import gettestcases
|
2015-02-01 22:38:26 +01:00
|
|
|
|
2016-12-14 18:21:57 +01:00
|
|
|
defs = gettestcases()
|
2016-12-15 21:05:12 +01:00
|
|
|
# set level to logging.DEBUG to see messages about missing assertions
|
|
|
|
logging.basicConfig(stream=sys.stderr, level=logging.WARNING)
|
2015-02-01 22:38:26 +01:00
|
|
|
|
|
|
|
|
2016-12-14 18:21:57 +01:00
|
|
|
class TestJSInterpreter(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.defs = defs
|
|
|
|
|
|
|
|
|
2016-12-15 21:05:12 +01:00
|
|
|
def generator(test_case, name):
|
2016-12-14 18:21:57 +01:00
|
|
|
def test_template(self):
|
|
|
|
for test in test_case['subtests']:
|
2016-12-29 06:29:59 +01:00
|
|
|
if '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:
|
|
|
|
jsi = JSInterpreter(test['code'], variables=test.get('globals'))
|
2016-12-14 18:21:57 +01:00
|
|
|
for a in test['asserts']:
|
2016-12-29 06:29:59 +01:00
|
|
|
if 'value' in a:
|
|
|
|
if 'call' in a:
|
|
|
|
self.assertEqual(jsi.call_function(*a['call']), a['value'])
|
|
|
|
else:
|
|
|
|
self.assertEqual(jsi.run(), a['value'])
|
2016-12-14 18:21:57 +01:00
|
|
|
else:
|
2016-12-29 06:29:59 +01:00
|
|
|
log.debug('No value in assertion, skipping')
|
2016-12-15 21:05:12 +01:00
|
|
|
else:
|
2016-12-29 06:29:59 +01:00
|
|
|
log.debug(log_reason)
|
2016-12-15 21:05:12 +01:00
|
|
|
|
|
|
|
log = logging.getLogger('TestJSInterpreter.%s' % name)
|
2016-12-27 06:28:17 +01:00
|
|
|
return test_template
|
2016-12-14 18:21:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
# And add them to TestJSInterpreter
|
|
|
|
for n, tc in enumerate(defs):
|
2016-12-27 06:28:17 +01:00
|
|
|
reason = tc['skip'].get('interpret', 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))
|
2016-12-15 20:42:05 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|