2021-06-03 09:43:42 +00:00
|
|
|
#!/usr/bin/env python3
|
2022-06-24 11:06:16 +00:00
|
|
|
|
2015-02-01 21:38:26 +00:00
|
|
|
# Allow direct execution
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
2022-04-11 22:32:57 +00:00
|
|
|
|
2015-02-01 21:38:26 +00:00
|
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
2022-08-18 22:16:16 +00:00
|
|
|
import math
|
|
|
|
import re
|
2022-06-24 11:06:16 +00:00
|
|
|
|
2022-08-18 22:16:16 +00:00
|
|
|
from yt_dlp.jsinterp import JS_Undefined, JSInterpreter
|
2015-02-01 21:38:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestJSInterpreter(unittest.TestCase):
|
|
|
|
def test_basic(self):
|
|
|
|
jsi = JSInterpreter('function x(){;}')
|
|
|
|
self.assertEqual(jsi.call_function('x'), None)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('function x3(){return 42;}')
|
|
|
|
self.assertEqual(jsi.call_function('x3'), 42)
|
|
|
|
|
2022-08-13 23:21:54 +00:00
|
|
|
jsi = JSInterpreter('function x3(){42}')
|
|
|
|
self.assertEqual(jsi.call_function('x3'), None)
|
|
|
|
|
2015-11-10 04:54:02 +00:00
|
|
|
jsi = JSInterpreter('var x5 = function(){return 42;}')
|
|
|
|
self.assertEqual(jsi.call_function('x5'), 42)
|
|
|
|
|
2015-02-01 21:38:26 +00:00
|
|
|
def test_calc(self):
|
|
|
|
jsi = JSInterpreter('function x4(a){return 2*a+1;}')
|
|
|
|
self.assertEqual(jsi.call_function('x4', 3), 7)
|
|
|
|
|
|
|
|
def test_empty_return(self):
|
|
|
|
jsi = JSInterpreter('function f(){return; y()}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), None)
|
|
|
|
|
|
|
|
def test_morespace(self):
|
|
|
|
jsi = JSInterpreter('function x (a) { return 2 * a + 1 ; }')
|
|
|
|
self.assertEqual(jsi.call_function('x', 3), 7)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('function f () { x = 2 ; return x; }')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 2)
|
|
|
|
|
|
|
|
def test_strange_chars(self):
|
|
|
|
jsi = JSInterpreter('function $_xY1 ($_axY1) { var $_axY2 = $_axY1 + 1; return $_axY2; }')
|
|
|
|
self.assertEqual(jsi.call_function('$_xY1', 20), 21)
|
|
|
|
|
|
|
|
def test_operators(self):
|
|
|
|
jsi = JSInterpreter('function f(){return 1 << 5;}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 32)
|
|
|
|
|
2022-08-14 21:50:36 +00:00
|
|
|
jsi = JSInterpreter('function f(){return 2 ** 5}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 32)
|
|
|
|
|
2015-02-01 21:38:26 +00:00
|
|
|
jsi = JSInterpreter('function f(){return 19 & 21;}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 17)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('function f(){return 11 >> 2;}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 2)
|
|
|
|
|
2022-08-13 23:21:54 +00:00
|
|
|
jsi = JSInterpreter('function f(){return []? 2+3: 4;}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 5)
|
|
|
|
|
2022-08-14 21:50:36 +00:00
|
|
|
jsi = JSInterpreter('function f(){return 1 == 2}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), False)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('function f(){return 0 && 1 || 2;}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 2)
|
|
|
|
|
2022-08-18 22:16:16 +00:00
|
|
|
jsi = JSInterpreter('function f(){return 0 ?? 42;}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 0)
|
|
|
|
|
2015-02-01 21:38:26 +00:00
|
|
|
def test_array_access(self):
|
2022-08-13 23:21:54 +00:00
|
|
|
jsi = JSInterpreter('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2.0] = 7; return x;}')
|
2015-02-01 21:38:26 +00:00
|
|
|
self.assertEqual(jsi.call_function('f'), [5, 2, 7])
|
|
|
|
|
|
|
|
def test_parens(self):
|
|
|
|
jsi = JSInterpreter('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 7)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('function f(){return (1 + 2) * 3;}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 9)
|
|
|
|
|
2022-08-13 23:21:54 +00:00
|
|
|
def test_quotes(self):
|
|
|
|
jsi = JSInterpreter(R'function f(){return "a\"\\("}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), R'a"\(')
|
|
|
|
|
2015-02-01 21:38:26 +00:00
|
|
|
def test_assignments(self):
|
|
|
|
jsi = JSInterpreter('function f(){var x = 20; x = 30 + 1; return x;}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 31)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('function f(){var x = 20; x += 30 + 1; return x;}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 51)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('function f(){var x = 20; x -= 30 + 1; return x;}')
|
|
|
|
self.assertEqual(jsi.call_function('f'), -11)
|
|
|
|
|
|
|
|
def test_comments(self):
|
2015-02-18 09:47:40 +00:00
|
|
|
'Skipping: Not yet fully implemented'
|
|
|
|
return
|
2015-02-01 21:38:26 +00:00
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() {
|
|
|
|
var x = /* 1 + */ 2;
|
|
|
|
var y = /* 30
|
|
|
|
* 40 */ 50;
|
|
|
|
return x + y;
|
|
|
|
}
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), 52)
|
|
|
|
|
2015-02-18 09:47:40 +00:00
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function f() {
|
|
|
|
var x = "/*";
|
|
|
|
var y = 1 /* comment */ + 2;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('f'), 3)
|
|
|
|
|
2015-02-01 21:38:26 +00:00
|
|
|
def test_precedence(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() {
|
|
|
|
var a = [10, 20, 30, 40, 50];
|
|
|
|
var b = 6;
|
|
|
|
a[0]=a[b%a.length];
|
|
|
|
return a;
|
|
|
|
}''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), [20, 20, 30, 40, 50])
|
|
|
|
|
2022-08-14 21:50:36 +00:00
|
|
|
def test_builtins(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)
|
|
|
|
|
2016-11-05 05:11:51 +00:00
|
|
|
def test_call(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return 2; }
|
2022-08-13 23:21:54 +00:00
|
|
|
function y(a) { return x() + (a?a:0); }
|
2016-11-05 05:11:51 +00:00
|
|
|
function z() { return y(3); }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('z'), 5)
|
2022-08-13 23:21:54 +00:00
|
|
|
self.assertEqual(jsi.call_function('y'), 2)
|
2015-02-01 21:38:26 +00:00
|
|
|
|
2021-10-31 04:23:58 +00:00
|
|
|
def test_for_loop(self):
|
|
|
|
jsi = JSInterpreter('''
|
2022-08-13 23:21:54 +00:00
|
|
|
function x() { a=0; for (i=0; i-10; i++) {a++} return a }
|
2021-10-31 04:23:58 +00:00
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), 10)
|
|
|
|
|
|
|
|
def test_switch(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x(f) { switch(f){
|
|
|
|
case 1:f+=1;
|
|
|
|
case 2:f+=2;
|
|
|
|
case 3:f+=3;break;
|
|
|
|
case 4:f+=4;
|
|
|
|
default:f=0;
|
|
|
|
} return f }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x', 1), 7)
|
|
|
|
self.assertEqual(jsi.call_function('x', 3), 6)
|
|
|
|
self.assertEqual(jsi.call_function('x', 5), 0)
|
|
|
|
|
2021-11-03 10:55:48 +00:00
|
|
|
def test_switch_default(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x(f) { switch(f){
|
|
|
|
case 2: f+=2;
|
|
|
|
default: f-=1;
|
|
|
|
case 5:
|
|
|
|
case 6: f+=6;
|
|
|
|
case 0: break;
|
|
|
|
case 1: f+=1;
|
|
|
|
} return f }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x', 1), 2)
|
|
|
|
self.assertEqual(jsi.call_function('x', 5), 11)
|
|
|
|
self.assertEqual(jsi.call_function('x', 9), 14)
|
|
|
|
|
2021-10-31 04:23:58 +00:00
|
|
|
def test_try(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { try{return 10} catch(e){return 5} }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), 10)
|
|
|
|
|
|
|
|
def test_for_loop_continue(self):
|
|
|
|
jsi = JSInterpreter('''
|
2022-08-13 23:21:54 +00:00
|
|
|
function x() { a=0; for (i=0; i-10; i++) { continue; a++ } return a }
|
2021-10-31 04:23:58 +00:00
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), 0)
|
|
|
|
|
|
|
|
def test_for_loop_break(self):
|
|
|
|
jsi = JSInterpreter('''
|
2022-08-13 23:21:54 +00:00
|
|
|
function x() { a=0; for (i=0; i-10; i++) { break; a++ } return a }
|
2021-10-31 04:23:58 +00:00
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), 0)
|
|
|
|
|
|
|
|
def test_literal_list(self):
|
|
|
|
jsi = JSInterpreter('''
|
2022-08-13 23:21:54 +00:00
|
|
|
function x() { return [1, 2, "asdf", [5, 6, 7]][3] }
|
2021-10-31 04:23:58 +00:00
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), [5, 6, 7])
|
|
|
|
|
|
|
|
def test_comma(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { a=5; a -= 1, a+=3; return a }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), 7)
|
|
|
|
|
2022-08-14 21:50:36 +00:00
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { a=5; return (a -= 1, a+=3, a); }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), 7)
|
|
|
|
|
2022-08-16 01:23:45 +00:00
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return (l=[0,1,2,3], function(a, b){return a+b})((l[1], l[2]), l[3]) }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), 5)
|
|
|
|
|
2022-08-14 21:50:36 +00:00
|
|
|
def test_void(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return void 42; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), None)
|
|
|
|
|
2022-08-13 23:21:54 +00:00
|
|
|
def test_return_function(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return [1, function(){return 1}][1] }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x')([]), 1)
|
|
|
|
|
2022-08-18 22:16:16 +00:00
|
|
|
def test_null(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return null; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), None)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return [null > 0, null < 0, null == 0, null === 0]; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), [False, False, False, False])
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return [null >= 0, null <= 0]; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), [True, True])
|
|
|
|
|
|
|
|
def test_undefined(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return undefined === undefined; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), True)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return undefined; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), JS_Undefined)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { let v; return v; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), JS_Undefined)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return [undefined === undefined, undefined == undefined, undefined < undefined, undefined > undefined]; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), [True, True, False, False])
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return [undefined === 0, undefined == 0, undefined < 0, undefined > 0]; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), [False, False, False, False])
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return [undefined >= 0, undefined <= 0]; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), [False, False])
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return [undefined > null, undefined < null, undefined == null, undefined === null]; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), [False, False, True, False])
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return [undefined === null, undefined == null, undefined < null, undefined > null]; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), [False, True, False, False])
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { let v; return [42+v, v+42, v**42, 42**v, 0**v]; }
|
|
|
|
''')
|
|
|
|
for y in jsi.call_function('x'):
|
|
|
|
self.assertTrue(math.isnan(y))
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { let v; return v**0; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), 1)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { let v; return [v>42, v<=42, v&&42, 42&&v]; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), [False, False, JS_Undefined, JS_Undefined])
|
|
|
|
|
|
|
|
jsi = JSInterpreter('function x(){return undefined ?? 42; }')
|
|
|
|
self.assertEqual(jsi.call_function('x'), 42)
|
|
|
|
|
|
|
|
def test_object(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { return {}; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), {})
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { let a = {m1: 42, m2: 0 }; return [a["m1"], a.m2]; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), [42, 0])
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { let a; return a?.qq; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), JS_Undefined)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { let a = {m1: 42, m2: 0 }; return a?.qq; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), JS_Undefined)
|
|
|
|
|
|
|
|
def test_regex(self):
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { let a=/,,[/,913,/](,)}/; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x'), None)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { let a=/,,[/,913,/](,)}/; return a; }
|
|
|
|
''')
|
|
|
|
self.assertIsInstance(jsi.call_function('x'), re.Pattern)
|
|
|
|
|
|
|
|
jsi = JSInterpreter('''
|
|
|
|
function x() { let a=/,,[/,913,/](,)}/i; return a; }
|
|
|
|
''')
|
|
|
|
self.assertEqual(jsi.call_function('x').flags & re.I, re.I)
|
|
|
|
|
2016-11-17 11:42:56 +00:00
|
|
|
|
2015-02-01 21:38:26 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|