]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
54007a45 | 2 | |
9e3f1991 PH |
3 | # Allow direct execution |
4 | import os | |
5 | import sys | |
6 | import unittest | |
f8271158 | 7 | |
9e3f1991 PH |
8 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
9 | ||
54007a45 | 10 | |
7a5c1cfe | 11 | from yt_dlp.jsinterp import JSInterpreter |
9e3f1991 PH |
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 | ||
8f53dc44 | 22 | jsi = JSInterpreter('function x3(){42}') |
23 | self.assertEqual(jsi.call_function('x3'), None) | |
24 | ||
ff29bf81 YCH |
25 | jsi = JSInterpreter('var x5 = function(){return 42;}') |
26 | self.assertEqual(jsi.call_function('x5'), 42) | |
27 | ||
9e3f1991 PH |
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 | ||
49b4ceae | 51 | jsi = JSInterpreter('function f(){return 2 ** 5}') |
52 | self.assertEqual(jsi.call_function('f'), 32) | |
53 | ||
9e3f1991 PH |
54 | jsi = JSInterpreter('function f(){return 19 & 21;}') |
55 | self.assertEqual(jsi.call_function('f'), 17) | |
56 | ||
57 | jsi = JSInterpreter('function f(){return 11 >> 2;}') | |
58 | self.assertEqual(jsi.call_function('f'), 2) | |
59 | ||
8f53dc44 | 60 | jsi = JSInterpreter('function f(){return []? 2+3: 4;}') |
61 | self.assertEqual(jsi.call_function('f'), 5) | |
62 | ||
49b4ceae | 63 | jsi = JSInterpreter('function f(){return 1 == 2}') |
64 | self.assertEqual(jsi.call_function('f'), False) | |
65 | ||
66 | jsi = JSInterpreter('function f(){return 0 && 1 || 2;}') | |
67 | self.assertEqual(jsi.call_function('f'), 2) | |
68 | ||
9e3f1991 | 69 | def test_array_access(self): |
8f53dc44 | 70 | jsi = JSInterpreter('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2.0] = 7; return x;}') |
9e3f1991 PH |
71 | self.assertEqual(jsi.call_function('f'), [5, 2, 7]) |
72 | ||
73 | def test_parens(self): | |
74 | jsi = JSInterpreter('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}') | |
75 | self.assertEqual(jsi.call_function('f'), 7) | |
76 | ||
77 | jsi = JSInterpreter('function f(){return (1 + 2) * 3;}') | |
78 | self.assertEqual(jsi.call_function('f'), 9) | |
79 | ||
8f53dc44 | 80 | def test_quotes(self): |
81 | jsi = JSInterpreter(R'function f(){return "a\"\\("}') | |
82 | self.assertEqual(jsi.call_function('f'), R'a"\(') | |
83 | ||
9e3f1991 PH |
84 | def test_assignments(self): |
85 | jsi = JSInterpreter('function f(){var x = 20; x = 30 + 1; return x;}') | |
86 | self.assertEqual(jsi.call_function('f'), 31) | |
87 | ||
88 | jsi = JSInterpreter('function f(){var x = 20; x += 30 + 1; return x;}') | |
89 | self.assertEqual(jsi.call_function('f'), 51) | |
90 | ||
91 | jsi = JSInterpreter('function f(){var x = 20; x -= 30 + 1; return x;}') | |
92 | self.assertEqual(jsi.call_function('f'), -11) | |
93 | ||
94 | def test_comments(self): | |
3eff81fb PH |
95 | 'Skipping: Not yet fully implemented' |
96 | return | |
9e3f1991 PH |
97 | jsi = JSInterpreter(''' |
98 | function x() { | |
99 | var x = /* 1 + */ 2; | |
100 | var y = /* 30 | |
101 | * 40 */ 50; | |
102 | return x + y; | |
103 | } | |
104 | ''') | |
105 | self.assertEqual(jsi.call_function('x'), 52) | |
106 | ||
3eff81fb PH |
107 | jsi = JSInterpreter(''' |
108 | function f() { | |
109 | var x = "/*"; | |
110 | var y = 1 /* comment */ + 2; | |
111 | return y; | |
112 | } | |
113 | ''') | |
114 | self.assertEqual(jsi.call_function('f'), 3) | |
115 | ||
9e3f1991 PH |
116 | def test_precedence(self): |
117 | jsi = JSInterpreter(''' | |
118 | function x() { | |
119 | var a = [10, 20, 30, 40, 50]; | |
120 | var b = 6; | |
121 | a[0]=a[b%a.length]; | |
122 | return a; | |
123 | }''') | |
124 | self.assertEqual(jsi.call_function('x'), [20, 20, 30, 40, 50]) | |
125 | ||
49b4ceae | 126 | def test_builtins(self): |
127 | jsi = JSInterpreter(''' | |
128 | function x() { return new Date('Wednesday 31 December 1969 18:01:26 MDT') - 0; } | |
129 | ''') | |
130 | self.assertEqual(jsi.call_function('x'), 86000) | |
131 | jsi = JSInterpreter(''' | |
132 | function x(dt) { return new Date(dt) - 0; } | |
133 | ''') | |
134 | self.assertEqual(jsi.call_function('x', 'Wednesday 31 December 1969 18:01:26 MDT'), 86000) | |
135 | ||
189935f1 KM |
136 | def test_call(self): |
137 | jsi = JSInterpreter(''' | |
138 | function x() { return 2; } | |
8f53dc44 | 139 | function y(a) { return x() + (a?a:0); } |
189935f1 KM |
140 | function z() { return y(3); } |
141 | ''') | |
142 | self.assertEqual(jsi.call_function('z'), 5) | |
8f53dc44 | 143 | self.assertEqual(jsi.call_function('y'), 2) |
9e3f1991 | 144 | |
404f611f | 145 | def test_for_loop(self): |
146 | jsi = JSInterpreter(''' | |
8f53dc44 | 147 | function x() { a=0; for (i=0; i-10; i++) {a++} return a } |
404f611f | 148 | ''') |
149 | self.assertEqual(jsi.call_function('x'), 10) | |
150 | ||
151 | def test_switch(self): | |
152 | jsi = JSInterpreter(''' | |
153 | function x(f) { switch(f){ | |
154 | case 1:f+=1; | |
155 | case 2:f+=2; | |
156 | case 3:f+=3;break; | |
157 | case 4:f+=4; | |
158 | default:f=0; | |
159 | } return f } | |
160 | ''') | |
161 | self.assertEqual(jsi.call_function('x', 1), 7) | |
162 | self.assertEqual(jsi.call_function('x', 3), 6) | |
163 | self.assertEqual(jsi.call_function('x', 5), 0) | |
164 | ||
a1fc7ca0 | 165 | def test_switch_default(self): |
166 | jsi = JSInterpreter(''' | |
167 | function x(f) { switch(f){ | |
168 | case 2: f+=2; | |
169 | default: f-=1; | |
170 | case 5: | |
171 | case 6: f+=6; | |
172 | case 0: break; | |
173 | case 1: f+=1; | |
174 | } return f } | |
175 | ''') | |
176 | self.assertEqual(jsi.call_function('x', 1), 2) | |
177 | self.assertEqual(jsi.call_function('x', 5), 11) | |
178 | self.assertEqual(jsi.call_function('x', 9), 14) | |
179 | ||
404f611f | 180 | def test_try(self): |
181 | jsi = JSInterpreter(''' | |
182 | function x() { try{return 10} catch(e){return 5} } | |
183 | ''') | |
184 | self.assertEqual(jsi.call_function('x'), 10) | |
185 | ||
186 | def test_for_loop_continue(self): | |
187 | jsi = JSInterpreter(''' | |
8f53dc44 | 188 | function x() { a=0; for (i=0; i-10; i++) { continue; a++ } return a } |
404f611f | 189 | ''') |
190 | self.assertEqual(jsi.call_function('x'), 0) | |
191 | ||
192 | def test_for_loop_break(self): | |
193 | jsi = JSInterpreter(''' | |
8f53dc44 | 194 | function x() { a=0; for (i=0; i-10; i++) { break; a++ } return a } |
404f611f | 195 | ''') |
196 | self.assertEqual(jsi.call_function('x'), 0) | |
197 | ||
198 | def test_literal_list(self): | |
199 | jsi = JSInterpreter(''' | |
8f53dc44 | 200 | function x() { return [1, 2, "asdf", [5, 6, 7]][3] } |
404f611f | 201 | ''') |
202 | self.assertEqual(jsi.call_function('x'), [5, 6, 7]) | |
203 | ||
204 | def test_comma(self): | |
205 | jsi = JSInterpreter(''' | |
206 | function x() { a=5; a -= 1, a+=3; return a } | |
207 | ''') | |
208 | self.assertEqual(jsi.call_function('x'), 7) | |
209 | ||
49b4ceae | 210 | jsi = JSInterpreter(''' |
211 | function x() { a=5; return (a -= 1, a+=3, a); } | |
212 | ''') | |
213 | self.assertEqual(jsi.call_function('x'), 7) | |
214 | ||
6d3e7424 | 215 | jsi = JSInterpreter(''' |
216 | function x() { return (l=[0,1,2,3], function(a, b){return a+b})((l[1], l[2]), l[3]) } | |
217 | ''') | |
218 | self.assertEqual(jsi.call_function('x'), 5) | |
219 | ||
49b4ceae | 220 | def test_void(self): |
221 | jsi = JSInterpreter(''' | |
222 | function x() { return void 42; } | |
223 | ''') | |
224 | self.assertEqual(jsi.call_function('x'), None) | |
225 | ||
8f53dc44 | 226 | def test_return_function(self): |
227 | jsi = JSInterpreter(''' | |
228 | function x() { return [1, function(){return 1}][1] } | |
229 | ''') | |
230 | self.assertEqual(jsi.call_function('x')([]), 1) | |
231 | ||
582be358 | 232 | |
9e3f1991 PH |
233 | if __name__ == '__main__': |
234 | unittest.main() |