]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env python3 | |
2 | ||
3 | # Allow direct execution | |
4 | import os | |
5 | import sys | |
6 | import unittest | |
7 | ||
8 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
9 | ||
10 | import math | |
11 | import re | |
12 | ||
13 | from yt_dlp.jsinterp import JS_Undefined, JSInterpreter | |
14 | ||
15 | ||
16 | class TestJSInterpreter(unittest.TestCase): | |
17 | def test_basic(self): | |
18 | jsi = JSInterpreter('function x(){;}') | |
19 | self.assertEqual(jsi.call_function('x'), None) | |
20 | ||
21 | jsi = JSInterpreter('function x3(){return 42;}') | |
22 | self.assertEqual(jsi.call_function('x3'), 42) | |
23 | ||
24 | jsi = JSInterpreter('function x3(){42}') | |
25 | self.assertEqual(jsi.call_function('x3'), None) | |
26 | ||
27 | jsi = JSInterpreter('var x5 = function(){return 42;}') | |
28 | self.assertEqual(jsi.call_function('x5'), 42) | |
29 | ||
30 | def test_calc(self): | |
31 | jsi = JSInterpreter('function x4(a){return 2*a+1;}') | |
32 | self.assertEqual(jsi.call_function('x4', 3), 7) | |
33 | ||
34 | def test_empty_return(self): | |
35 | jsi = JSInterpreter('function f(){return; y()}') | |
36 | self.assertEqual(jsi.call_function('f'), None) | |
37 | ||
38 | def test_morespace(self): | |
39 | jsi = JSInterpreter('function x (a) { return 2 * a + 1 ; }') | |
40 | self.assertEqual(jsi.call_function('x', 3), 7) | |
41 | ||
42 | jsi = JSInterpreter('function f () { x = 2 ; return x; }') | |
43 | self.assertEqual(jsi.call_function('f'), 2) | |
44 | ||
45 | def test_strange_chars(self): | |
46 | jsi = JSInterpreter('function $_xY1 ($_axY1) { var $_axY2 = $_axY1 + 1; return $_axY2; }') | |
47 | self.assertEqual(jsi.call_function('$_xY1', 20), 21) | |
48 | ||
49 | def test_operators(self): | |
50 | jsi = JSInterpreter('function f(){return 1 << 5;}') | |
51 | self.assertEqual(jsi.call_function('f'), 32) | |
52 | ||
53 | jsi = JSInterpreter('function f(){return 2 ** 5}') | |
54 | self.assertEqual(jsi.call_function('f'), 32) | |
55 | ||
56 | jsi = JSInterpreter('function f(){return 19 & 21;}') | |
57 | self.assertEqual(jsi.call_function('f'), 17) | |
58 | ||
59 | jsi = JSInterpreter('function f(){return 11 >> 2;}') | |
60 | self.assertEqual(jsi.call_function('f'), 2) | |
61 | ||
62 | jsi = JSInterpreter('function f(){return []? 2+3: 4;}') | |
63 | self.assertEqual(jsi.call_function('f'), 5) | |
64 | ||
65 | jsi = JSInterpreter('function f(){return 1 == 2}') | |
66 | self.assertEqual(jsi.call_function('f'), False) | |
67 | ||
68 | jsi = JSInterpreter('function f(){return 0 && 1 || 2;}') | |
69 | self.assertEqual(jsi.call_function('f'), 2) | |
70 | ||
71 | jsi = JSInterpreter('function f(){return 0 ?? 42;}') | |
72 | self.assertEqual(jsi.call_function('f'), 0) | |
73 | ||
74 | jsi = JSInterpreter('function f(){return "life, the universe and everything" < 42;}') | |
75 | self.assertFalse(jsi.call_function('f')) | |
76 | ||
77 | def test_array_access(self): | |
78 | jsi = JSInterpreter('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2.0] = 7; return x;}') | |
79 | self.assertEqual(jsi.call_function('f'), [5, 2, 7]) | |
80 | ||
81 | def test_parens(self): | |
82 | jsi = JSInterpreter('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}') | |
83 | self.assertEqual(jsi.call_function('f'), 7) | |
84 | ||
85 | jsi = JSInterpreter('function f(){return (1 + 2) * 3;}') | |
86 | self.assertEqual(jsi.call_function('f'), 9) | |
87 | ||
88 | def test_quotes(self): | |
89 | jsi = JSInterpreter(R'function f(){return "a\"\\("}') | |
90 | self.assertEqual(jsi.call_function('f'), R'a"\(') | |
91 | ||
92 | def test_assignments(self): | |
93 | jsi = JSInterpreter('function f(){var x = 20; x = 30 + 1; return x;}') | |
94 | self.assertEqual(jsi.call_function('f'), 31) | |
95 | ||
96 | jsi = JSInterpreter('function f(){var x = 20; x += 30 + 1; return x;}') | |
97 | self.assertEqual(jsi.call_function('f'), 51) | |
98 | ||
99 | jsi = JSInterpreter('function f(){var x = 20; x -= 30 + 1; return x;}') | |
100 | self.assertEqual(jsi.call_function('f'), -11) | |
101 | ||
102 | def test_comments(self): | |
103 | 'Skipping: Not yet fully implemented' | |
104 | return | |
105 | jsi = JSInterpreter(''' | |
106 | function x() { | |
107 | var x = /* 1 + */ 2; | |
108 | var y = /* 30 | |
109 | * 40 */ 50; | |
110 | return x + y; | |
111 | } | |
112 | ''') | |
113 | self.assertEqual(jsi.call_function('x'), 52) | |
114 | ||
115 | jsi = JSInterpreter(''' | |
116 | function f() { | |
117 | var x = "/*"; | |
118 | var y = 1 /* comment */ + 2; | |
119 | return y; | |
120 | } | |
121 | ''') | |
122 | self.assertEqual(jsi.call_function('f'), 3) | |
123 | ||
124 | def test_precedence(self): | |
125 | jsi = JSInterpreter(''' | |
126 | function x() { | |
127 | var a = [10, 20, 30, 40, 50]; | |
128 | var b = 6; | |
129 | a[0]=a[b%a.length]; | |
130 | return a; | |
131 | }''') | |
132 | self.assertEqual(jsi.call_function('x'), [20, 20, 30, 40, 50]) | |
133 | ||
134 | def test_builtins(self): | |
135 | jsi = JSInterpreter(''' | |
136 | function x() { return NaN } | |
137 | ''') | |
138 | self.assertTrue(math.isnan(jsi.call_function('x'))) | |
139 | ||
140 | jsi = JSInterpreter(''' | |
141 | function x() { return new Date('Wednesday 31 December 1969 18:01:26 MDT') - 0; } | |
142 | ''') | |
143 | self.assertEqual(jsi.call_function('x'), 86000) | |
144 | jsi = JSInterpreter(''' | |
145 | function x(dt) { return new Date(dt) - 0; } | |
146 | ''') | |
147 | self.assertEqual(jsi.call_function('x', 'Wednesday 31 December 1969 18:01:26 MDT'), 86000) | |
148 | ||
149 | def test_call(self): | |
150 | jsi = JSInterpreter(''' | |
151 | function x() { return 2; } | |
152 | function y(a) { return x() + (a?a:0); } | |
153 | function z() { return y(3); } | |
154 | ''') | |
155 | self.assertEqual(jsi.call_function('z'), 5) | |
156 | self.assertEqual(jsi.call_function('y'), 2) | |
157 | ||
158 | def test_for_loop(self): | |
159 | jsi = JSInterpreter(''' | |
160 | function x() { a=0; for (i=0; i-10; i++) {a++} return a } | |
161 | ''') | |
162 | self.assertEqual(jsi.call_function('x'), 10) | |
163 | ||
164 | def test_switch(self): | |
165 | jsi = JSInterpreter(''' | |
166 | function x(f) { switch(f){ | |
167 | case 1:f+=1; | |
168 | case 2:f+=2; | |
169 | case 3:f+=3;break; | |
170 | case 4:f+=4; | |
171 | default:f=0; | |
172 | } return f } | |
173 | ''') | |
174 | self.assertEqual(jsi.call_function('x', 1), 7) | |
175 | self.assertEqual(jsi.call_function('x', 3), 6) | |
176 | self.assertEqual(jsi.call_function('x', 5), 0) | |
177 | ||
178 | def test_switch_default(self): | |
179 | jsi = JSInterpreter(''' | |
180 | function x(f) { switch(f){ | |
181 | case 2: f+=2; | |
182 | default: f-=1; | |
183 | case 5: | |
184 | case 6: f+=6; | |
185 | case 0: break; | |
186 | case 1: f+=1; | |
187 | } return f } | |
188 | ''') | |
189 | self.assertEqual(jsi.call_function('x', 1), 2) | |
190 | self.assertEqual(jsi.call_function('x', 5), 11) | |
191 | self.assertEqual(jsi.call_function('x', 9), 14) | |
192 | ||
193 | def test_try(self): | |
194 | jsi = JSInterpreter(''' | |
195 | function x() { try{return 10} catch(e){return 5} } | |
196 | ''') | |
197 | self.assertEqual(jsi.call_function('x'), 10) | |
198 | ||
199 | def test_catch(self): | |
200 | jsi = JSInterpreter(''' | |
201 | function x() { try{throw 10} catch(e){return 5} } | |
202 | ''') | |
203 | self.assertEqual(jsi.call_function('x'), 5) | |
204 | ||
205 | def test_finally(self): | |
206 | jsi = JSInterpreter(''' | |
207 | function x() { try{throw 10} finally {return 42} } | |
208 | ''') | |
209 | self.assertEqual(jsi.call_function('x'), 42) | |
210 | jsi = JSInterpreter(''' | |
211 | function x() { try{throw 10} catch(e){return 5} finally {return 42} } | |
212 | ''') | |
213 | self.assertEqual(jsi.call_function('x'), 42) | |
214 | ||
215 | def test_nested_try(self): | |
216 | jsi = JSInterpreter(''' | |
217 | function x() {try { | |
218 | try{throw 10} finally {throw 42} | |
219 | } catch(e){return 5} } | |
220 | ''') | |
221 | self.assertEqual(jsi.call_function('x'), 5) | |
222 | ||
223 | def test_for_loop_continue(self): | |
224 | jsi = JSInterpreter(''' | |
225 | function x() { a=0; for (i=0; i-10; i++) { continue; a++ } return a } | |
226 | ''') | |
227 | self.assertEqual(jsi.call_function('x'), 0) | |
228 | ||
229 | def test_for_loop_break(self): | |
230 | jsi = JSInterpreter(''' | |
231 | function x() { a=0; for (i=0; i-10; i++) { break; a++ } return a } | |
232 | ''') | |
233 | self.assertEqual(jsi.call_function('x'), 0) | |
234 | ||
235 | def test_for_loop_try(self): | |
236 | jsi = JSInterpreter(''' | |
237 | function x() { | |
238 | for (i=0; i-10; i++) { try { if (i == 5) throw i} catch {return 10} finally {break} }; | |
239 | return 42 } | |
240 | ''') | |
241 | self.assertEqual(jsi.call_function('x'), 42) | |
242 | ||
243 | def test_literal_list(self): | |
244 | jsi = JSInterpreter(''' | |
245 | function x() { return [1, 2, "asdf", [5, 6, 7]][3] } | |
246 | ''') | |
247 | self.assertEqual(jsi.call_function('x'), [5, 6, 7]) | |
248 | ||
249 | def test_comma(self): | |
250 | jsi = JSInterpreter(''' | |
251 | function x() { a=5; a -= 1, a+=3; return a } | |
252 | ''') | |
253 | self.assertEqual(jsi.call_function('x'), 7) | |
254 | ||
255 | jsi = JSInterpreter(''' | |
256 | function x() { a=5; return (a -= 1, a+=3, a); } | |
257 | ''') | |
258 | self.assertEqual(jsi.call_function('x'), 7) | |
259 | ||
260 | jsi = JSInterpreter(''' | |
261 | function x() { return (l=[0,1,2,3], function(a, b){return a+b})((l[1], l[2]), l[3]) } | |
262 | ''') | |
263 | self.assertEqual(jsi.call_function('x'), 5) | |
264 | ||
265 | def test_void(self): | |
266 | jsi = JSInterpreter(''' | |
267 | function x() { return void 42; } | |
268 | ''') | |
269 | self.assertEqual(jsi.call_function('x'), None) | |
270 | ||
271 | def test_return_function(self): | |
272 | jsi = JSInterpreter(''' | |
273 | function x() { return [1, function(){return 1}][1] } | |
274 | ''') | |
275 | self.assertEqual(jsi.call_function('x')([]), 1) | |
276 | ||
277 | def test_null(self): | |
278 | jsi = JSInterpreter(''' | |
279 | function x() { return null; } | |
280 | ''') | |
281 | self.assertEqual(jsi.call_function('x'), None) | |
282 | ||
283 | jsi = JSInterpreter(''' | |
284 | function x() { return [null > 0, null < 0, null == 0, null === 0]; } | |
285 | ''') | |
286 | self.assertEqual(jsi.call_function('x'), [False, False, False, False]) | |
287 | ||
288 | jsi = JSInterpreter(''' | |
289 | function x() { return [null >= 0, null <= 0]; } | |
290 | ''') | |
291 | self.assertEqual(jsi.call_function('x'), [True, True]) | |
292 | ||
293 | def test_undefined(self): | |
294 | jsi = JSInterpreter(''' | |
295 | function x() { return undefined === undefined; } | |
296 | ''') | |
297 | self.assertEqual(jsi.call_function('x'), True) | |
298 | ||
299 | jsi = JSInterpreter(''' | |
300 | function x() { return undefined; } | |
301 | ''') | |
302 | self.assertEqual(jsi.call_function('x'), JS_Undefined) | |
303 | ||
304 | jsi = JSInterpreter(''' | |
305 | function x() { let v; return v; } | |
306 | ''') | |
307 | self.assertEqual(jsi.call_function('x'), JS_Undefined) | |
308 | ||
309 | jsi = JSInterpreter(''' | |
310 | function x() { return [undefined === undefined, undefined == undefined, undefined < undefined, undefined > undefined]; } | |
311 | ''') | |
312 | self.assertEqual(jsi.call_function('x'), [True, True, False, False]) | |
313 | ||
314 | jsi = JSInterpreter(''' | |
315 | function x() { return [undefined === 0, undefined == 0, undefined < 0, undefined > 0]; } | |
316 | ''') | |
317 | self.assertEqual(jsi.call_function('x'), [False, False, False, False]) | |
318 | ||
319 | jsi = JSInterpreter(''' | |
320 | function x() { return [undefined >= 0, undefined <= 0]; } | |
321 | ''') | |
322 | self.assertEqual(jsi.call_function('x'), [False, False]) | |
323 | ||
324 | jsi = JSInterpreter(''' | |
325 | function x() { return [undefined > null, undefined < null, undefined == null, undefined === null]; } | |
326 | ''') | |
327 | self.assertEqual(jsi.call_function('x'), [False, False, True, False]) | |
328 | ||
329 | jsi = JSInterpreter(''' | |
330 | function x() { return [undefined === null, undefined == null, undefined < null, undefined > null]; } | |
331 | ''') | |
332 | self.assertEqual(jsi.call_function('x'), [False, True, False, False]) | |
333 | ||
334 | jsi = JSInterpreter(''' | |
335 | function x() { let v; return [42+v, v+42, v**42, 42**v, 0**v]; } | |
336 | ''') | |
337 | for y in jsi.call_function('x'): | |
338 | self.assertTrue(math.isnan(y)) | |
339 | ||
340 | jsi = JSInterpreter(''' | |
341 | function x() { let v; return v**0; } | |
342 | ''') | |
343 | self.assertEqual(jsi.call_function('x'), 1) | |
344 | ||
345 | jsi = JSInterpreter(''' | |
346 | function x() { let v; return [v>42, v<=42, v&&42, 42&&v]; } | |
347 | ''') | |
348 | self.assertEqual(jsi.call_function('x'), [False, False, JS_Undefined, JS_Undefined]) | |
349 | ||
350 | jsi = JSInterpreter('function x(){return undefined ?? 42; }') | |
351 | self.assertEqual(jsi.call_function('x'), 42) | |
352 | ||
353 | def test_object(self): | |
354 | jsi = JSInterpreter(''' | |
355 | function x() { return {}; } | |
356 | ''') | |
357 | self.assertEqual(jsi.call_function('x'), {}) | |
358 | ||
359 | jsi = JSInterpreter(''' | |
360 | function x() { let a = {m1: 42, m2: 0 }; return [a["m1"], a.m2]; } | |
361 | ''') | |
362 | self.assertEqual(jsi.call_function('x'), [42, 0]) | |
363 | ||
364 | jsi = JSInterpreter(''' | |
365 | function x() { let a; return a?.qq; } | |
366 | ''') | |
367 | self.assertEqual(jsi.call_function('x'), JS_Undefined) | |
368 | ||
369 | jsi = JSInterpreter(''' | |
370 | function x() { let a = {m1: 42, m2: 0 }; return a?.qq; } | |
371 | ''') | |
372 | self.assertEqual(jsi.call_function('x'), JS_Undefined) | |
373 | ||
374 | def test_regex(self): | |
375 | jsi = JSInterpreter(''' | |
376 | function x() { let a=/,,[/,913,/](,)}/; } | |
377 | ''') | |
378 | self.assertEqual(jsi.call_function('x'), None) | |
379 | ||
380 | jsi = JSInterpreter(''' | |
381 | function x() { let a=/,,[/,913,/](,)}/; return a; } | |
382 | ''') | |
383 | self.assertIsInstance(jsi.call_function('x'), re.Pattern) | |
384 | ||
385 | jsi = JSInterpreter(''' | |
386 | function x() { let a=/,,[/,913,/](,)}/i; return a; } | |
387 | ''') | |
388 | self.assertEqual(jsi.call_function('x').flags & re.I, re.I) | |
389 | ||
390 | jsi = JSInterpreter(R''' | |
391 | function x() { let a=/,][}",],()}(\[)/; return a; } | |
392 | ''') | |
393 | self.assertEqual(jsi.call_function('x').pattern, r',][}",],()}(\[)') | |
394 | ||
395 | jsi = JSInterpreter(R''' | |
396 | function x() { let a=[/[)\\]/]; return a[0]; } | |
397 | ''') | |
398 | self.assertEqual(jsi.call_function('x').pattern, r'[)\\]') | |
399 | ||
400 | def test_char_code_at(self): | |
401 | jsi = JSInterpreter('function x(i){return "test".charCodeAt(i)}') | |
402 | self.assertEqual(jsi.call_function('x', 0), 116) | |
403 | self.assertEqual(jsi.call_function('x', 1), 101) | |
404 | self.assertEqual(jsi.call_function('x', 2), 115) | |
405 | self.assertEqual(jsi.call_function('x', 3), 116) | |
406 | self.assertEqual(jsi.call_function('x', 4), None) | |
407 | self.assertEqual(jsi.call_function('x', 'not_a_number'), 116) | |
408 | ||
409 | def test_bitwise_operators_overflow(self): | |
410 | jsi = JSInterpreter('function x(){return -524999584 << 5}') | |
411 | self.assertEqual(jsi.call_function('x'), 379882496) | |
412 | ||
413 | jsi = JSInterpreter('function x(){return 1236566549 << 5}') | |
414 | self.assertEqual(jsi.call_function('x'), 915423904) | |
415 | ||
416 | ||
417 | if __name__ == '__main__': | |
418 | unittest.main() |