]>
jfr.im git - yt-dlp.git/blob - test/test_jsinterp.py
3 # Allow direct execution
8 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
13 from yt_dlp
.jsinterp
import JS_Undefined
, JSInterpreter
16 class TestJSInterpreter(unittest
.TestCase
):
17 def _test(self
, code
, ret
, func
='f', args
=()):
18 self
.assertEqual(JSInterpreter(code
).call_function(func
, *args
), ret
)
21 jsi
= JSInterpreter('function f(){;}')
22 self
.assertEqual(repr(jsi
.extract_function('f')), 'F<f>')
23 self
.assertEqual(jsi
.call_function('f'), None)
25 self
._test
('function f(){return 42;}', 42)
26 self
._test
('function f(){42}', None)
27 self
._test
('var f = function(){return 42;}', 42)
30 self
._test
('function f(a){return 2*a+1;}', 7, args
=[3])
32 def test_empty_return(self
):
33 self
._test
('function f(){return; y()}', None)
35 def test_morespace(self
):
36 self
._test
('function f (a) { return 2 * a + 1 ; }', 7, args
=[3])
37 self
._test
('function f () { x = 2 ; return x; }', 2)
39 def test_strange_chars(self
):
40 self
._test
('function $_xY1 ($_axY1) { var $_axY2 = $_axY1 + 1; return $_axY2; }',
41 21, args
=[20], func
='$_xY1')
43 def test_operators(self
):
44 self
._test
('function f(){return 1 << 5;}', 32)
45 self
._test
('function f(){return 2 ** 5}', 32)
46 self
._test
('function f(){return 19 & 21;}', 17)
47 self
._test
('function f(){return 11 >> 2;}', 2)
48 self
._test
('function f(){return []? 2+3: 4;}', 5)
49 self
._test
('function f(){return 1 == 2}', False)
50 self
._test
('function f(){return 0 && 1 || 2;}', 2)
51 self
._test
('function f(){return 0 ?? 42;}', 0)
52 self
._test
('function f(){return "life, the universe and everything" < 42;}', False)
54 def test_array_access(self
):
55 self
._test
('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2.0] = 7; return x;}', [5, 2, 7])
57 def test_parens(self
):
58 self
._test
('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}', 7)
59 self
._test
('function f(){return (1 + 2) * 3;}', 9)
61 def test_quotes(self
):
62 self
._test
(R
'function f(){return "a\"\\("}', R
'a"\(')
64 def test_assignments(self
):
65 self
._test
('function f(){var x = 20; x = 30 + 1; return x;}', 31)
66 self
._test
('function f(){var x = 20; x += 30 + 1; return x;}', 51)
67 self
._test
('function f(){var x = 20; x -= 30 + 1; return x;}', -11)
69 def test_comments(self
):
70 'Skipping: Not yet fully implemented'
84 var y = 1 /* comment */ + 2;
89 def test_precedence(self
):
92 var a = [10, 20, 30, 40, 50];
97 ''', [20, 20, 30, 40, 50])
99 def test_builtins(self
):
100 jsi
= JSInterpreter('function f() { return NaN }')
101 self
.assertTrue(math
.isnan(jsi
.call_function('f')))
103 self
._test
('function f() { return new Date("Wednesday 31 December 1969 18:01:26 MDT") - 0; }',
105 self
._test
('function f(dt) { return new Date(dt) - 0; }',
106 86000, args
=['Wednesday 31 December 1969 18:01:26 MDT'])
109 jsi
= JSInterpreter('''
110 function x() { return 2; }
111 function y(a) { return x() + (a?a:0); }
112 function z() { return y(3); }
114 self
.assertEqual(jsi
.call_function('z'), 5)
115 self
.assertEqual(jsi
.call_function('y'), 2)
128 if (0==0) {return 10}
143 else if (1==0) {return 2}
149 def test_for_loop(self
):
150 self
._test
('function f() { a=0; for (i=0; i-10; i++) {a++} return a }', 10)
152 def test_switch(self
):
153 jsi
= JSInterpreter('''
154 function f(x) { switch(x){
162 self
.assertEqual(jsi
.call_function('f', 1), 7)
163 self
.assertEqual(jsi
.call_function('f', 3), 6)
164 self
.assertEqual(jsi
.call_function('f', 5), 0)
166 def test_switch_default(self
):
167 jsi
= JSInterpreter('''
168 function f(x) { switch(x){
177 self
.assertEqual(jsi
.call_function('f', 1), 2)
178 self
.assertEqual(jsi
.call_function('f', 5), 11)
179 self
.assertEqual(jsi
.call_function('f', 9), 14)
182 self
._test
('function f() { try{return 10} catch(e){return 5} }', 10)
184 def test_catch(self
):
185 self
._test
('function f() { try{throw 10} catch(e){return 5} }', 5)
187 def test_finally(self
):
188 self
._test
('function f() { try{throw 10} finally {return 42} }', 42)
189 self
._test
('function f() { try{throw 10} catch(e){return 5} finally {return 42} }', 42)
191 def test_nested_try(self
):
194 try{throw 10} finally {throw 42}
195 } catch(e){return 5} }
198 def test_for_loop_continue(self
):
199 self
._test
('function f() { a=0; for (i=0; i-10; i++) { continue; a++ } return a }', 0)
201 def test_for_loop_break(self
):
202 self
._test
('function f() { a=0; for (i=0; i-10; i++) { break; a++ } return a }', 0)
204 def test_for_loop_try(self
):
207 for (i=0; i-10; i++) { try { if (i == 5) throw i} catch {return 10} finally {break} };
211 def test_literal_list(self
):
212 self
._test
('function f() { return [1, 2, "asdf", [5, 6, 7]][3] }', [5, 6, 7])
214 def test_comma(self
):
215 self
._test
('function f() { a=5; a -= 1, a+=3; return a }', 7)
216 self
._test
('function f() { a=5; return (a -= 1, a+=3, a); }', 7)
217 self
._test
('function f() { return (l=[0,1,2,3], function(a, b){return a+b})((l[1], l[2]), l[3]) }', 5)
220 self
._test
('function f() { return void 42; }', None)
222 def test_return_function(self
):
223 jsi
= JSInterpreter('''
224 function f() { return [1, function(){return 1}][1] }
226 self
.assertEqual(jsi
.call_function('f')([]), 1)
229 self
._test
('function f() { return null; }', None)
230 self
._test
('function f() { return [null > 0, null < 0, null == 0, null === 0]; }',
231 [False, False, False, False])
232 self
._test
('function f() { return [null >= 0, null <= 0]; }', [True, True])
234 def test_undefined(self
):
235 self
._test
('function f() { return undefined === undefined; }', True)
236 self
._test
('function f() { return undefined; }', JS_Undefined
)
237 self
._test
('function f() {return undefined ?? 42; }', 42)
238 self
._test
('function f() { let v; return v; }', JS_Undefined
)
239 self
._test
('function f() { let v; return v**0; }', 1)
240 self
._test
('function f() { let v; return [v>42, v<=42, v&&42, 42&&v]; }',
241 [False, False, JS_Undefined
, JS_Undefined
])
244 function f() { return [
245 undefined === undefined,
246 undefined == undefined,
248 undefined < undefined,
249 undefined > undefined,
260 ''', list(map(bool, (1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))))
262 jsi
= JSInterpreter('''
263 function f() { let v; return [42+v, v+42, v**42, 42**v, 0**v]; }
265 for y
in jsi
.call_function('f'):
266 self
.assertTrue(math
.isnan(y
))
268 def test_object(self
):
269 self
._test
('function f() { return {}; }', {})
270 self
._test
('function f() { let a = {m1: 42, m2: 0 }; return [a["m1"], a.m2]; }', [42, 0])
271 self
._test
('function f() { let a; return a?.qq; }', JS_Undefined
)
272 self
._test
('function f() { let a = {m1: 42, m2: 0 }; return a?.qq; }', JS_Undefined
)
274 def test_regex(self
):
275 self
._test
('function f() { let a=/,,[/,913,/](,)}/; }', None)
277 jsi
= JSInterpreter('function f() { let a=/,,[/,913,/](,)}/; return a; }')
278 self
.assertIsInstance(jsi
.call_function('f'), re
.Pattern
)
280 jsi
= JSInterpreter('function f() { let a=/,,[/,913,/](,)}/i; return a; }')
281 self
.assertEqual(jsi
.call_function('f').flags
& re
.I
, re
.I
)
283 jsi
= JSInterpreter(R
'function f() { let a=/,][}",],()}(\[)/; return a; }')
284 self
.assertEqual(jsi
.call_function('f').pattern
, r
',][}",],()}(\[)')
286 jsi
= JSInterpreter(R
'function f() { let a=[/[)\\]/]; return a[0]; }')
287 self
.assertEqual(jsi
.call_function('f').pattern
, r
'[)\\]')
289 def test_char_code_at(self
):
290 jsi
= JSInterpreter('function f(i){return "test".charCodeAt(i)}')
291 self
.assertEqual(jsi
.call_function('f', 0), 116)
292 self
.assertEqual(jsi
.call_function('f', 1), 101)
293 self
.assertEqual(jsi
.call_function('f', 2), 115)
294 self
.assertEqual(jsi
.call_function('f', 3), 116)
295 self
.assertEqual(jsi
.call_function('f', 4), None)
296 self
.assertEqual(jsi
.call_function('f', 'not_a_number'), 116)
298 def test_bitwise_operators_overflow(self
):
299 self
._test
('function f(){return -524999584 << 5}', 379882496)
300 self
._test
('function f(){return 1236566549 << 5}', 915423904)
302 def test_bitwise_operators_typecast(self
):
303 self
._test
('function f(){return null << 5}', 0)
304 self
._test
('function f(){return undefined >> 5}', 0)
305 self
._test
('function f(){return 42 << NaN}', 42)
307 def test_negative(self
):
308 self
._test
('function f(){return 2 * -2.0 ;}', -4)
309 self
._test
('function f(){return 2 - - -2 ;}', 0)
310 self
._test
('function f(){return 2 - - - -2 ;}', 4)
311 self
._test
('function f(){return 2 - + + - -2;}', 0)
312 self
._test
('function f(){return 2 + - + - -2;}', 0)
315 if __name__
== '__main__':