aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/test_jsinterp.py
diff options
context:
space:
mode:
authordirkf <[email protected]>2023-02-02 14:28:32 +0000
committerdirkf <[email protected]>2023-02-02 16:31:49 +0000
commit295736c9cba714fb5de7d1c3dd31d86e50091cf8 (patch)
treedae76550fa3b47badbd657e218be1f7198dc1edb /test/test_jsinterp.py
parent14ef89a8dab4f6ba6185d6f5bf0317a705d7b842 (diff)
downloadyoutube-dl-295736c9cba714fb5de7d1c3dd31d86e50091cf8.tar.gz
youtube-dl-295736c9cba714fb5de7d1c3dd31d86e50091cf8.zip
[jsinterp] Improve parsing
* support subset `... else if ...` * support `while` * add `RegExp` class * generalise `new` support * limited more debug strings * matching test changes
Diffstat (limited to 'test/test_jsinterp.py')
-rw-r--r--test/test_jsinterp.py53
1 files changed, 50 insertions, 3 deletions
diff --git a/test/test_jsinterp.py b/test/test_jsinterp.py
index c47def737..b5962356c 100644
--- a/test/test_jsinterp.py
+++ b/test/test_jsinterp.py
@@ -11,8 +11,6 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import math
import re
-from youtube_dl.compat import compat_re_Pattern
-
from youtube_dl.jsinterp import JS_Undefined, JSInterpreter
@@ -140,15 +138,23 @@ class TestJSInterpreter(unittest.TestCase):
''')
self.assertTrue(math.isnan(jsi.call_function('x')))
+ def test_Date(self):
jsi = JSInterpreter('''
function x() { return new Date('Wednesday 31 December 1969 18:01:26 MDT') - 0; }
''')
self.assertEqual(jsi.call_function('x'), 86000)
+
jsi = JSInterpreter('''
function x(dt) { return new Date(dt) - 0; }
''')
self.assertEqual(jsi.call_function('x', 'Wednesday 31 December 1969 18:01:26 MDT'), 86000)
+ # date format m/d/y
+ jsi = JSInterpreter('''
+ function x() { return new Date('12/31/1969 18:01:26 MDT') - 0; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 86000)
+
def test_call(self):
jsi = JSInterpreter('''
function x() { return 2; }
@@ -183,11 +189,30 @@ class TestJSInterpreter(unittest.TestCase):
""" # Unsupported
jsi = JSInterpreter('''
function x() {
+ if (0!=0) return 1;
+ else {return 10}
+ }''')
+ self.assertEqual(jsi.call_function('x'), 10)
+ """
+
+ def test_elseif(self):
+ jsi = JSInterpreter('''
+ function x() {
if (0!=0) {return 1}
else if (1==0) {return 2}
else {return 10}
}''')
self.assertEqual(jsi.call_function('x'), 10)
+
+ """ # Unsupported
+ jsi = JSInterpreter('''
+ function x() {
+ if (0!=0) return 1;
+ else if (1==0) {return 2}
+ else {return 10}
+ }''')
+ self.assertEqual(jsi.call_function('x'), 10)
+ # etc
"""
def test_for_loop(self):
@@ -197,6 +222,13 @@ class TestJSInterpreter(unittest.TestCase):
''')
self.assertEqual(jsi.call_function('x'), 10)
+ def test_while_loop(self):
+ # function x() { a=0; while (a<10) {a++} a }
+ jsi = JSInterpreter('''
+ function x() { a=0; while (a<10) {a++} return a }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 10)
+
def test_switch(self):
jsi = JSInterpreter('''
function x(f) { switch(f){
@@ -415,13 +447,28 @@ class TestJSInterpreter(unittest.TestCase):
jsi = JSInterpreter('''
function x() { let a=/,,[/,913,/](,)}/; return a; }
''')
- self.assertIsInstance(jsi.call_function('x'), compat_re_Pattern)
+ attrs = set(('findall', 'finditer', 'flags', 'groupindex',
+ 'groups', 'match', 'pattern', 'scanner',
+ 'search', 'split', 'sub', 'subn'))
+ self.assertTrue(set(dir(jsi.call_function('x'))) > attrs)
jsi = JSInterpreter('''
function x() { let a=/,,[/,913,/](,)}/i; return a; }
''')
self.assertEqual(jsi.call_function('x').flags & ~re.U, re.I)
+ jsi = JSInterpreter(r'''
+ function x() { let a=[/[)\\]/]; return a[0]; }
+ ''')
+ self.assertEqual(jsi.call_function('x').pattern, r'[)\\]')
+
+ """ # fails
+ jsi = JSInterpreter(r'''
+ function x() { let a=100; a/=/[0-9]+/.exec('divide by 20 today')[0]; }
+ ''')
+ self.assertEqual(jsi.call_function('x'), 5)
+ """
+
def test_char_code_at(self):
jsi = JSInterpreter('function x(i){return "test".charCodeAt(i)}')
self.assertEqual(jsi.call_function('x', 0), 116)