]> jfr.im git - yt-dlp.git/blob - test/test_jsinterp.py
Update to ytdl-commit-e6a836d
[yt-dlp.git] / test / test_jsinterp.py
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
11 from yt_dlp.jsinterp import JSInterpreter
12
13
14 class TestJSInterpreter(unittest.TestCase):
15 def test_basic(self):
16 jsi = JSInterpreter('function x(){;}')
17 self.assertEqual(jsi.call_function('x'), None)
18
19 jsi = JSInterpreter('function x3(){return 42;}')
20 self.assertEqual(jsi.call_function('x3'), 42)
21
22 jsi = JSInterpreter('function x3(){42}')
23 self.assertEqual(jsi.call_function('x3'), None)
24
25 jsi = JSInterpreter('var x5 = function(){return 42;}')
26 self.assertEqual(jsi.call_function('x5'), 42)
27
28 def test_calc(self):
29 jsi = JSInterpreter('function x4(a){return 2*a+1;}')
30 self.assertEqual(jsi.call_function('x4', 3), 7)
31
32 def test_empty_return(self):
33 jsi = JSInterpreter('function f(){return; y()}')
34 self.assertEqual(jsi.call_function('f'), None)
35
36 def test_morespace(self):
37 jsi = JSInterpreter('function x (a) { return 2 * a + 1 ; }')
38 self.assertEqual(jsi.call_function('x', 3), 7)
39
40 jsi = JSInterpreter('function f () { x = 2 ; return x; }')
41 self.assertEqual(jsi.call_function('f'), 2)
42
43 def test_strange_chars(self):
44 jsi = JSInterpreter('function $_xY1 ($_axY1) { var $_axY2 = $_axY1 + 1; return $_axY2; }')
45 self.assertEqual(jsi.call_function('$_xY1', 20), 21)
46
47 def test_operators(self):
48 jsi = JSInterpreter('function f(){return 1 << 5;}')
49 self.assertEqual(jsi.call_function('f'), 32)
50
51 jsi = JSInterpreter('function f(){return 19 & 21;}')
52 self.assertEqual(jsi.call_function('f'), 17)
53
54 jsi = JSInterpreter('function f(){return 11 >> 2;}')
55 self.assertEqual(jsi.call_function('f'), 2)
56
57 jsi = JSInterpreter('function f(){return []? 2+3: 4;}')
58 self.assertEqual(jsi.call_function('f'), 5)
59
60 def test_array_access(self):
61 jsi = JSInterpreter('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2.0] = 7; return x;}')
62 self.assertEqual(jsi.call_function('f'), [5, 2, 7])
63
64 def test_parens(self):
65 jsi = JSInterpreter('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}')
66 self.assertEqual(jsi.call_function('f'), 7)
67
68 jsi = JSInterpreter('function f(){return (1 + 2) * 3;}')
69 self.assertEqual(jsi.call_function('f'), 9)
70
71 def test_quotes(self):
72 jsi = JSInterpreter(R'function f(){return "a\"\\("}')
73 self.assertEqual(jsi.call_function('f'), R'a"\(')
74
75 def test_assignments(self):
76 jsi = JSInterpreter('function f(){var x = 20; x = 30 + 1; return x;}')
77 self.assertEqual(jsi.call_function('f'), 31)
78
79 jsi = JSInterpreter('function f(){var x = 20; x += 30 + 1; return x;}')
80 self.assertEqual(jsi.call_function('f'), 51)
81
82 jsi = JSInterpreter('function f(){var x = 20; x -= 30 + 1; return x;}')
83 self.assertEqual(jsi.call_function('f'), -11)
84
85 def test_comments(self):
86 'Skipping: Not yet fully implemented'
87 return
88 jsi = JSInterpreter('''
89 function x() {
90 var x = /* 1 + */ 2;
91 var y = /* 30
92 * 40 */ 50;
93 return x + y;
94 }
95 ''')
96 self.assertEqual(jsi.call_function('x'), 52)
97
98 jsi = JSInterpreter('''
99 function f() {
100 var x = "/*";
101 var y = 1 /* comment */ + 2;
102 return y;
103 }
104 ''')
105 self.assertEqual(jsi.call_function('f'), 3)
106
107 def test_precedence(self):
108 jsi = JSInterpreter('''
109 function x() {
110 var a = [10, 20, 30, 40, 50];
111 var b = 6;
112 a[0]=a[b%a.length];
113 return a;
114 }''')
115 self.assertEqual(jsi.call_function('x'), [20, 20, 30, 40, 50])
116
117 def test_call(self):
118 jsi = JSInterpreter('''
119 function x() { return 2; }
120 function y(a) { return x() + (a?a:0); }
121 function z() { return y(3); }
122 ''')
123 self.assertEqual(jsi.call_function('z'), 5)
124 self.assertEqual(jsi.call_function('y'), 2)
125
126 def test_for_loop(self):
127 jsi = JSInterpreter('''
128 function x() { a=0; for (i=0; i-10; i++) {a++} return a }
129 ''')
130 self.assertEqual(jsi.call_function('x'), 10)
131
132 def test_switch(self):
133 jsi = JSInterpreter('''
134 function x(f) { switch(f){
135 case 1:f+=1;
136 case 2:f+=2;
137 case 3:f+=3;break;
138 case 4:f+=4;
139 default:f=0;
140 } return f }
141 ''')
142 self.assertEqual(jsi.call_function('x', 1), 7)
143 self.assertEqual(jsi.call_function('x', 3), 6)
144 self.assertEqual(jsi.call_function('x', 5), 0)
145
146 def test_switch_default(self):
147 jsi = JSInterpreter('''
148 function x(f) { switch(f){
149 case 2: f+=2;
150 default: f-=1;
151 case 5:
152 case 6: f+=6;
153 case 0: break;
154 case 1: f+=1;
155 } return f }
156 ''')
157 self.assertEqual(jsi.call_function('x', 1), 2)
158 self.assertEqual(jsi.call_function('x', 5), 11)
159 self.assertEqual(jsi.call_function('x', 9), 14)
160
161 def test_try(self):
162 jsi = JSInterpreter('''
163 function x() { try{return 10} catch(e){return 5} }
164 ''')
165 self.assertEqual(jsi.call_function('x'), 10)
166
167 def test_for_loop_continue(self):
168 jsi = JSInterpreter('''
169 function x() { a=0; for (i=0; i-10; i++) { continue; a++ } return a }
170 ''')
171 self.assertEqual(jsi.call_function('x'), 0)
172
173 def test_for_loop_break(self):
174 jsi = JSInterpreter('''
175 function x() { a=0; for (i=0; i-10; i++) { break; a++ } return a }
176 ''')
177 self.assertEqual(jsi.call_function('x'), 0)
178
179 def test_literal_list(self):
180 jsi = JSInterpreter('''
181 function x() { return [1, 2, "asdf", [5, 6, 7]][3] }
182 ''')
183 self.assertEqual(jsi.call_function('x'), [5, 6, 7])
184
185 def test_comma(self):
186 jsi = JSInterpreter('''
187 function x() { a=5; a -= 1, a+=3; return a }
188 ''')
189 self.assertEqual(jsi.call_function('x'), 7)
190
191 def test_return_function(self):
192 jsi = JSInterpreter('''
193 function x() { return [1, function(){return 1}][1] }
194 ''')
195 self.assertEqual(jsi.call_function('x')([]), 1)
196
197
198 if __name__ == '__main__':
199 unittest.main()