2015-02-01 22:38:26 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
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-14 18:21:57 +01:00
|
|
|
from test.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']:
|
|
|
|
jsi = JSInterpreter(test['code'], variables=None if 'globals' not in test else test['globals'])
|
|
|
|
if 'asserts' in test:
|
|
|
|
for a in test['asserts']:
|
|
|
|
if 'call' in a:
|
|
|
|
self.assertEqual(jsi.call_function(*a['call']), a['value'])
|
|
|
|
else:
|
|
|
|
self.assertEqual(jsi.run(), a['value'])
|
2016-12-15 21:05:12 +01:00
|
|
|
else:
|
|
|
|
log.debug('No asserts, skipping subtest')
|
|
|
|
|
|
|
|
log = logging.getLogger('TestJSInterpreter.%s' % name)
|
2016-12-14 18:21:57 +01:00
|
|
|
|
2016-12-15 20:02:04 +01:00
|
|
|
if 'i' not in test_case['skip']:
|
2016-12-14 18:21:57 +01:00
|
|
|
reason = False
|
|
|
|
else:
|
|
|
|
reason = test_case['skip']['i']
|
|
|
|
|
|
|
|
return test_template if not reason else unittest.skip(reason)(test_template)
|
|
|
|
|
|
|
|
|
|
|
|
# And add them to TestJSInterpreter
|
|
|
|
for n, tc in enumerate(defs):
|
2016-12-15 21:05:12 +01:00
|
|
|
if 'i' not in tc['skip'] or tc['skip']['i'] is not True:
|
2016-12-14 18:21:57 +01:00
|
|
|
tname = 'test_' + str(tc['name'])
|
|
|
|
i = 1
|
|
|
|
while hasattr(TestJSInterpreter, tname):
|
|
|
|
tname = 'test_%s_%d' % (tc['name'], i)
|
|
|
|
i += 1
|
2016-12-15 21:05:12 +01:00
|
|
|
if any('asserts' in test for test in tc['subtests']):
|
|
|
|
test_method = generator(tc, tname)
|
|
|
|
test_method.__name__ = str(tname)
|
|
|
|
setattr(TestJSInterpreter, test_method.__name__, test_method)
|
|
|
|
del test_method
|
|
|
|
else:
|
|
|
|
log = logging.getLogger('TestJSInterpreter')
|
|
|
|
log.debug('''Skipping %s:There isn't any assertion''' % tname)
|
2016-12-15 20:42:05 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|