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