]> jfr.im git - yt-dlp.git/blob - test/test_jsinterp.py
[cleanup] Sort imports
[yt-dlp.git] / test / test_jsinterp.py
1 #!/usr/bin/env python3
2 # Allow direct execution
3 import os
4 import sys
5 import unittest
6
7 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
9 from yt_dlp.jsinterp import JSInterpreter
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
20 jsi = JSInterpreter('var x5 = function(){return 42;}')
21 self.assertEqual(jsi.call_function('x5'), 42)
22
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):
74 'Skipping: Not yet fully implemented'
75 return
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
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
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
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)
112
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
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
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
178
179 if __name__ == '__main__':
180 unittest.main()