]>
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 | ||
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 | ||
8f53dc44 | 57 | jsi = JSInterpreter('function f(){return []? 2+3: 4;}') |
58 | self.assertEqual(jsi.call_function('f'), 5) | |
59 | ||
9e3f1991 | 60 | def test_array_access(self): |
8f53dc44 | 61 | jsi = JSInterpreter('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2.0] = 7; return x;}') |
9e3f1991 PH |
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 | ||
8f53dc44 | 71 | def test_quotes(self): |
72 | jsi = JSInterpreter(R'function f(){return "a\"\\("}') | |
73 | self.assertEqual(jsi.call_function('f'), R'a"\(') | |
74 | ||
9e3f1991 PH |
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): | |
3eff81fb PH |
86 | 'Skipping: Not yet fully implemented' |
87 | return | |
9e3f1991 PH |
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 | ||
3eff81fb PH |
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 | ||
9e3f1991 PH |
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 | ||
189935f1 KM |
117 | def test_call(self): |
118 | jsi = JSInterpreter(''' | |
119 | function x() { return 2; } | |
8f53dc44 | 120 | function y(a) { return x() + (a?a:0); } |
189935f1 KM |
121 | function z() { return y(3); } |
122 | ''') | |
123 | self.assertEqual(jsi.call_function('z'), 5) | |
8f53dc44 | 124 | self.assertEqual(jsi.call_function('y'), 2) |
9e3f1991 | 125 | |
404f611f | 126 | def test_for_loop(self): |
127 | jsi = JSInterpreter(''' | |
8f53dc44 | 128 | function x() { a=0; for (i=0; i-10; i++) {a++} return a } |
404f611f | 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 | ||
a1fc7ca0 | 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 | ||
404f611f | 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(''' | |
8f53dc44 | 169 | function x() { a=0; for (i=0; i-10; i++) { continue; a++ } return a } |
404f611f | 170 | ''') |
171 | self.assertEqual(jsi.call_function('x'), 0) | |
172 | ||
173 | def test_for_loop_break(self): | |
174 | jsi = JSInterpreter(''' | |
8f53dc44 | 175 | function x() { a=0; for (i=0; i-10; i++) { break; a++ } return a } |
404f611f | 176 | ''') |
177 | self.assertEqual(jsi.call_function('x'), 0) | |
178 | ||
179 | def test_literal_list(self): | |
180 | jsi = JSInterpreter(''' | |
8f53dc44 | 181 | function x() { return [1, 2, "asdf", [5, 6, 7]][3] } |
404f611f | 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 | ||
8f53dc44 | 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 | ||
582be358 | 197 | |
9e3f1991 PH |
198 | if __name__ == '__main__': |
199 | unittest.main() |