]>
jfr.im git - yt-dlp.git/blob - test/test_jsinterp.py
2 # Allow direct execution
6 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
8 from yt_dlp
.jsinterp
import JSInterpreter
11 class TestJSInterpreter(unittest
.TestCase
):
13 jsi
= JSInterpreter('function x(){;}')
14 self
.assertEqual(jsi
.call_function('x'), None)
16 jsi
= JSInterpreter('function x3(){return 42;}')
17 self
.assertEqual(jsi
.call_function('x3'), 42)
19 jsi
= JSInterpreter('var x5 = function(){return 42;}')
20 self
.assertEqual(jsi
.call_function('x5'), 42)
23 jsi
= JSInterpreter('function x4(a){return 2*a+1;}')
24 self
.assertEqual(jsi
.call_function('x4', 3), 7)
26 def test_empty_return(self
):
27 jsi
= JSInterpreter('function f(){return; y()}')
28 self
.assertEqual(jsi
.call_function('f'), None)
30 def test_morespace(self
):
31 jsi
= JSInterpreter('function x (a) { return 2 * a + 1 ; }')
32 self
.assertEqual(jsi
.call_function('x', 3), 7)
34 jsi
= JSInterpreter('function f () { x = 2 ; return x; }')
35 self
.assertEqual(jsi
.call_function('f'), 2)
37 def test_strange_chars(self
):
38 jsi
= JSInterpreter('function $_xY1 ($_axY1) { var $_axY2 = $_axY1 + 1; return $_axY2; }')
39 self
.assertEqual(jsi
.call_function('$_xY1', 20), 21)
41 def test_operators(self
):
42 jsi
= JSInterpreter('function f(){return 1 << 5;}')
43 self
.assertEqual(jsi
.call_function('f'), 32)
45 jsi
= JSInterpreter('function f(){return 19 & 21;}')
46 self
.assertEqual(jsi
.call_function('f'), 17)
48 jsi
= JSInterpreter('function f(){return 11 >> 2;}')
49 self
.assertEqual(jsi
.call_function('f'), 2)
51 def test_array_access(self
):
52 jsi
= JSInterpreter('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2] = 7; return x;}')
53 self
.assertEqual(jsi
.call_function('f'), [5, 2, 7])
55 def test_parens(self
):
56 jsi
= JSInterpreter('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}')
57 self
.assertEqual(jsi
.call_function('f'), 7)
59 jsi
= JSInterpreter('function f(){return (1 + 2) * 3;}')
60 self
.assertEqual(jsi
.call_function('f'), 9)
62 def test_assignments(self
):
63 jsi
= JSInterpreter('function f(){var x = 20; x = 30 + 1; return x;}')
64 self
.assertEqual(jsi
.call_function('f'), 31)
66 jsi
= JSInterpreter('function f(){var x = 20; x += 30 + 1; return x;}')
67 self
.assertEqual(jsi
.call_function('f'), 51)
69 jsi
= JSInterpreter('function f(){var x = 20; x -= 30 + 1; return x;}')
70 self
.assertEqual(jsi
.call_function('f'), -11)
72 def test_comments(self
):
73 'Skipping: Not yet fully implemented'
75 jsi
= JSInterpreter('''
83 self
.assertEqual(jsi
.call_function('x'), 52)
85 jsi
= JSInterpreter('''
88 var y = 1 /* comment */ + 2;
92 self
.assertEqual(jsi
.call_function('f'), 3)
94 def test_precedence(self
):
95 jsi
= JSInterpreter('''
97 var a = [10, 20, 30, 40, 50];
102 self
.assertEqual(jsi
.call_function('x'), [20, 20, 30, 40, 50])
105 jsi
= JSInterpreter('''
106 function x() { return 2; }
107 function y(a) { return x() + a; }
108 function z() { return y(3); }
110 self
.assertEqual(jsi
.call_function('z'), 5)
112 def test_for_loop(self
):
113 jsi
= JSInterpreter('''
114 function x() { a=0; for (i=0; i-10; i++) {a++} a }
116 self
.assertEqual(jsi
.call_function('x'), 10)
118 def test_switch(self
):
119 jsi
= JSInterpreter('''
120 function x(f) { switch(f){
128 self
.assertEqual(jsi
.call_function('x', 1), 7)
129 self
.assertEqual(jsi
.call_function('x', 3), 6)
130 self
.assertEqual(jsi
.call_function('x', 5), 0)
132 def test_switch_default(self
):
133 jsi
= JSInterpreter('''
134 function x(f) { switch(f){
143 self
.assertEqual(jsi
.call_function('x', 1), 2)
144 self
.assertEqual(jsi
.call_function('x', 5), 11)
145 self
.assertEqual(jsi
.call_function('x', 9), 14)
148 jsi
= JSInterpreter('''
149 function x() { try{return 10} catch(e){return 5} }
151 self
.assertEqual(jsi
.call_function('x'), 10)
153 def test_for_loop_continue(self
):
154 jsi
= JSInterpreter('''
155 function x() { a=0; for (i=0; i-10; i++) { continue; a++ } a }
157 self
.assertEqual(jsi
.call_function('x'), 0)
159 def test_for_loop_break(self
):
160 jsi
= JSInterpreter('''
161 function x() { a=0; for (i=0; i-10; i++) { break; a++ } a }
163 self
.assertEqual(jsi
.call_function('x'), 0)
165 def test_literal_list(self
):
166 jsi
= JSInterpreter('''
167 function x() { [1, 2, "asdf", [5, 6, 7]][3] }
169 self
.assertEqual(jsi
.call_function('x'), [5, 6, 7])
171 def test_comma(self
):
172 jsi
= JSInterpreter('''
173 function x() { a=5; a -= 1, a+=3; return a }
175 self
.assertEqual(jsi
.call_function('x'), 7)
178 if __name__
== '__main__':