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