]>
jfr.im git - yt-dlp.git/blob - test/test_jsinterp.py
2 # Allow direct execution
7 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
9 from yt_dlp
.jsinterp
import JSInterpreter
12 class TestJSInterpreter(unittest
.TestCase
):
14 jsi
= JSInterpreter('function x(){;}')
15 self
.assertEqual(jsi
.call_function('x'), None)
17 jsi
= JSInterpreter('function x3(){return 42;}')
18 self
.assertEqual(jsi
.call_function('x3'), 42)
20 jsi
= JSInterpreter('var x5 = function(){return 42;}')
21 self
.assertEqual(jsi
.call_function('x5'), 42)
24 jsi
= JSInterpreter('function x4(a){return 2*a+1;}')
25 self
.assertEqual(jsi
.call_function('x4', 3), 7)
27 def test_empty_return(self
):
28 jsi
= JSInterpreter('function f(){return; y()}')
29 self
.assertEqual(jsi
.call_function('f'), None)
31 def test_morespace(self
):
32 jsi
= JSInterpreter('function x (a) { return 2 * a + 1 ; }')
33 self
.assertEqual(jsi
.call_function('x', 3), 7)
35 jsi
= JSInterpreter('function f () { x = 2 ; return x; }')
36 self
.assertEqual(jsi
.call_function('f'), 2)
38 def test_strange_chars(self
):
39 jsi
= JSInterpreter('function $_xY1 ($_axY1) { var $_axY2 = $_axY1 + 1; return $_axY2; }')
40 self
.assertEqual(jsi
.call_function('$_xY1', 20), 21)
42 def test_operators(self
):
43 jsi
= JSInterpreter('function f(){return 1 << 5;}')
44 self
.assertEqual(jsi
.call_function('f'), 32)
46 jsi
= JSInterpreter('function f(){return 19 & 21;}')
47 self
.assertEqual(jsi
.call_function('f'), 17)
49 jsi
= JSInterpreter('function f(){return 11 >> 2;}')
50 self
.assertEqual(jsi
.call_function('f'), 2)
52 def test_array_access(self
):
53 jsi
= JSInterpreter('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2] = 7; return x;}')
54 self
.assertEqual(jsi
.call_function('f'), [5, 2, 7])
56 def test_parens(self
):
57 jsi
= JSInterpreter('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}')
58 self
.assertEqual(jsi
.call_function('f'), 7)
60 jsi
= JSInterpreter('function f(){return (1 + 2) * 3;}')
61 self
.assertEqual(jsi
.call_function('f'), 9)
63 def test_assignments(self
):
64 jsi
= JSInterpreter('function f(){var x = 20; x = 30 + 1; return x;}')
65 self
.assertEqual(jsi
.call_function('f'), 31)
67 jsi
= JSInterpreter('function f(){var x = 20; x += 30 + 1; return x;}')
68 self
.assertEqual(jsi
.call_function('f'), 51)
70 jsi
= JSInterpreter('function f(){var x = 20; x -= 30 + 1; return x;}')
71 self
.assertEqual(jsi
.call_function('f'), -11)
73 def test_comments(self
):
74 'Skipping: Not yet fully implemented'
76 jsi
= JSInterpreter('''
84 self
.assertEqual(jsi
.call_function('x'), 52)
86 jsi
= JSInterpreter('''
89 var y = 1 /* comment */ + 2;
93 self
.assertEqual(jsi
.call_function('f'), 3)
95 def test_precedence(self
):
96 jsi
= JSInterpreter('''
98 var a = [10, 20, 30, 40, 50];
103 self
.assertEqual(jsi
.call_function('x'), [20, 20, 30, 40, 50])
106 jsi
= JSInterpreter('''
107 function x() { return 2; }
108 function y(a) { return x() + a; }
109 function z() { return y(3); }
111 self
.assertEqual(jsi
.call_function('z'), 5)
113 def test_for_loop(self
):
114 jsi
= JSInterpreter('''
115 function x() { a=0; for (i=0; i-10; i++) {a++} a }
117 self
.assertEqual(jsi
.call_function('x'), 10)
119 def test_switch(self
):
120 jsi
= JSInterpreter('''
121 function x(f) { switch(f){
129 self
.assertEqual(jsi
.call_function('x', 1), 7)
130 self
.assertEqual(jsi
.call_function('x', 3), 6)
131 self
.assertEqual(jsi
.call_function('x', 5), 0)
133 def test_switch_default(self
):
134 jsi
= JSInterpreter('''
135 function x(f) { switch(f){
144 self
.assertEqual(jsi
.call_function('x', 1), 2)
145 self
.assertEqual(jsi
.call_function('x', 5), 11)
146 self
.assertEqual(jsi
.call_function('x', 9), 14)
149 jsi
= JSInterpreter('''
150 function x() { try{return 10} catch(e){return 5} }
152 self
.assertEqual(jsi
.call_function('x'), 10)
154 def test_for_loop_continue(self
):
155 jsi
= JSInterpreter('''
156 function x() { a=0; for (i=0; i-10; i++) { continue; a++ } a }
158 self
.assertEqual(jsi
.call_function('x'), 0)
160 def test_for_loop_break(self
):
161 jsi
= JSInterpreter('''
162 function x() { a=0; for (i=0; i-10; i++) { break; a++ } a }
164 self
.assertEqual(jsi
.call_function('x'), 0)
166 def test_literal_list(self
):
167 jsi
= JSInterpreter('''
168 function x() { [1, 2, "asdf", [5, 6, 7]][3] }
170 self
.assertEqual(jsi
.call_function('x'), [5, 6, 7])
172 def test_comma(self
):
173 jsi
= JSInterpreter('''
174 function x() { a=5; a -= 1, a+=3; return a }
176 self
.assertEqual(jsi
.call_function('x'), 7)
179 if __name__
== '__main__':