]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
9e3f1991 PH |
2 | # Allow direct execution |
3 | import os | |
4 | import sys | |
5 | import unittest | |
6 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
7 | ||
7a5c1cfe | 8 | from yt_dlp.jsinterp import JSInterpreter |
9e3f1991 PH |
9 | |
10 | ||
11 | class TestJSInterpreter(unittest.TestCase): | |
12 | def test_basic(self): | |
13 | jsi = JSInterpreter('function x(){;}') | |
14 | self.assertEqual(jsi.call_function('x'), None) | |
15 | ||
16 | jsi = JSInterpreter('function x3(){return 42;}') | |
17 | self.assertEqual(jsi.call_function('x3'), 42) | |
18 | ||
ff29bf81 YCH |
19 | jsi = JSInterpreter('var x5 = function(){return 42;}') |
20 | self.assertEqual(jsi.call_function('x5'), 42) | |
21 | ||
9e3f1991 PH |
22 | def test_calc(self): |
23 | jsi = JSInterpreter('function x4(a){return 2*a+1;}') | |
24 | self.assertEqual(jsi.call_function('x4', 3), 7) | |
25 | ||
26 | def test_empty_return(self): | |
27 | jsi = JSInterpreter('function f(){return; y()}') | |
28 | self.assertEqual(jsi.call_function('f'), None) | |
29 | ||
30 | def test_morespace(self): | |
31 | jsi = JSInterpreter('function x (a) { return 2 * a + 1 ; }') | |
32 | self.assertEqual(jsi.call_function('x', 3), 7) | |
33 | ||
34 | jsi = JSInterpreter('function f () { x = 2 ; return x; }') | |
35 | self.assertEqual(jsi.call_function('f'), 2) | |
36 | ||
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) | |
40 | ||
41 | def test_operators(self): | |
42 | jsi = JSInterpreter('function f(){return 1 << 5;}') | |
43 | self.assertEqual(jsi.call_function('f'), 32) | |
44 | ||
45 | jsi = JSInterpreter('function f(){return 19 & 21;}') | |
46 | self.assertEqual(jsi.call_function('f'), 17) | |
47 | ||
48 | jsi = JSInterpreter('function f(){return 11 >> 2;}') | |
49 | self.assertEqual(jsi.call_function('f'), 2) | |
50 | ||
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]) | |
54 | ||
55 | def test_parens(self): | |
56 | jsi = JSInterpreter('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}') | |
57 | self.assertEqual(jsi.call_function('f'), 7) | |
58 | ||
59 | jsi = JSInterpreter('function f(){return (1 + 2) * 3;}') | |
60 | self.assertEqual(jsi.call_function('f'), 9) | |
61 | ||
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) | |
65 | ||
66 | jsi = JSInterpreter('function f(){var x = 20; x += 30 + 1; return x;}') | |
67 | self.assertEqual(jsi.call_function('f'), 51) | |
68 | ||
69 | jsi = JSInterpreter('function f(){var x = 20; x -= 30 + 1; return x;}') | |
70 | self.assertEqual(jsi.call_function('f'), -11) | |
71 | ||
72 | def test_comments(self): | |
3eff81fb PH |
73 | 'Skipping: Not yet fully implemented' |
74 | return | |
9e3f1991 PH |
75 | jsi = JSInterpreter(''' |
76 | function x() { | |
77 | var x = /* 1 + */ 2; | |
78 | var y = /* 30 | |
79 | * 40 */ 50; | |
80 | return x + y; | |
81 | } | |
82 | ''') | |
83 | self.assertEqual(jsi.call_function('x'), 52) | |
84 | ||
3eff81fb PH |
85 | jsi = JSInterpreter(''' |
86 | function f() { | |
87 | var x = "/*"; | |
88 | var y = 1 /* comment */ + 2; | |
89 | return y; | |
90 | } | |
91 | ''') | |
92 | self.assertEqual(jsi.call_function('f'), 3) | |
93 | ||
9e3f1991 PH |
94 | def test_precedence(self): |
95 | jsi = JSInterpreter(''' | |
96 | function x() { | |
97 | var a = [10, 20, 30, 40, 50]; | |
98 | var b = 6; | |
99 | a[0]=a[b%a.length]; | |
100 | return a; | |
101 | }''') | |
102 | self.assertEqual(jsi.call_function('x'), [20, 20, 30, 40, 50]) | |
103 | ||
189935f1 KM |
104 | def test_call(self): |
105 | jsi = JSInterpreter(''' | |
106 | function x() { return 2; } | |
107 | function y(a) { return x() + a; } | |
108 | function z() { return y(3); } | |
109 | ''') | |
110 | self.assertEqual(jsi.call_function('z'), 5) | |
9e3f1991 | 111 | |
404f611f | 112 | def test_for_loop(self): |
113 | jsi = JSInterpreter(''' | |
114 | function x() { a=0; for (i=0; i-10; i++) {a++} a } | |
115 | ''') | |
116 | self.assertEqual(jsi.call_function('x'), 10) | |
117 | ||
118 | def test_switch(self): | |
119 | jsi = JSInterpreter(''' | |
120 | function x(f) { switch(f){ | |
121 | case 1:f+=1; | |
122 | case 2:f+=2; | |
123 | case 3:f+=3;break; | |
124 | case 4:f+=4; | |
125 | default:f=0; | |
126 | } return f } | |
127 | ''') | |
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) | |
131 | ||
a1fc7ca0 | 132 | def test_switch_default(self): |
133 | jsi = JSInterpreter(''' | |
134 | function x(f) { switch(f){ | |
135 | case 2: f+=2; | |
136 | default: f-=1; | |
137 | case 5: | |
138 | case 6: f+=6; | |
139 | case 0: break; | |
140 | case 1: f+=1; | |
141 | } return f } | |
142 | ''') | |
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) | |
146 | ||
404f611f | 147 | def test_try(self): |
148 | jsi = JSInterpreter(''' | |
149 | function x() { try{return 10} catch(e){return 5} } | |
150 | ''') | |
151 | self.assertEqual(jsi.call_function('x'), 10) | |
152 | ||
153 | def test_for_loop_continue(self): | |
154 | jsi = JSInterpreter(''' | |
155 | function x() { a=0; for (i=0; i-10; i++) { continue; a++ } a } | |
156 | ''') | |
157 | self.assertEqual(jsi.call_function('x'), 0) | |
158 | ||
159 | def test_for_loop_break(self): | |
160 | jsi = JSInterpreter(''' | |
161 | function x() { a=0; for (i=0; i-10; i++) { break; a++ } a } | |
162 | ''') | |
163 | self.assertEqual(jsi.call_function('x'), 0) | |
164 | ||
165 | def test_literal_list(self): | |
166 | jsi = JSInterpreter(''' | |
167 | function x() { [1, 2, "asdf", [5, 6, 7]][3] } | |
168 | ''') | |
169 | self.assertEqual(jsi.call_function('x'), [5, 6, 7]) | |
170 | ||
171 | def test_comma(self): | |
172 | jsi = JSInterpreter(''' | |
173 | function x() { a=5; a -= 1, a+=3; return a } | |
174 | ''') | |
175 | self.assertEqual(jsi.call_function('x'), 7) | |
176 | ||
582be358 | 177 | |
9e3f1991 PH |
178 | if __name__ == '__main__': |
179 | unittest.main() |