]>
Raphaƫl G. Git Repositories - youtubedl/blob - test/test_jsinterp.py
3 from __future__
import unicode_literals
5 # Allow direct execution
9 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
11 from youtube_dl
.jsinterp
import JSInterpreter
14 class TestJSInterpreter(unittest
.TestCase
):
16 jsi
= JSInterpreter('function x(){;}')
17 self
.assertEqual(jsi
.call_function('x'), None)
19 jsi
= JSInterpreter('function x3(){return 42;}')
20 self
.assertEqual(jsi
.call_function('x3'), 42)
22 jsi
= JSInterpreter('var x5 = function(){return 42;}')
23 self
.assertEqual(jsi
.call_function('x5'), 42)
26 jsi
= JSInterpreter('function x4(a){return 2*a+1;}')
27 self
.assertEqual(jsi
.call_function('x4', 3), 7)
29 def test_empty_return(self
):
30 jsi
= JSInterpreter('function f(){return; y()}')
31 self
.assertEqual(jsi
.call_function('f'), None)
33 def test_morespace(self
):
34 jsi
= JSInterpreter('function x (a) { return 2 * a + 1 ; }')
35 self
.assertEqual(jsi
.call_function('x', 3), 7)
37 jsi
= JSInterpreter('function f () { x = 2 ; return x; }')
38 self
.assertEqual(jsi
.call_function('f'), 2)
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)
44 def test_operators(self
):
45 jsi
= JSInterpreter('function f(){return 1 << 5;}')
46 self
.assertEqual(jsi
.call_function('f'), 32)
48 jsi
= JSInterpreter('function f(){return 19 & 21;}')
49 self
.assertEqual(jsi
.call_function('f'), 17)
51 jsi
= JSInterpreter('function f(){return 11 >> 2;}')
52 self
.assertEqual(jsi
.call_function('f'), 2)
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])
58 def test_parens(self
):
59 jsi
= JSInterpreter('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}')
60 self
.assertEqual(jsi
.call_function('f'), 7)
62 jsi
= JSInterpreter('function f(){return (1 + 2) * 3;}')
63 self
.assertEqual(jsi
.call_function('f'), 9)
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)
69 jsi
= JSInterpreter('function f(){var x = 20; x += 30 + 1; return x;}')
70 self
.assertEqual(jsi
.call_function('f'), 51)
72 jsi
= JSInterpreter('function f(){var x = 20; x -= 30 + 1; return x;}')
73 self
.assertEqual(jsi
.call_function('f'), -11)
75 def test_comments(self
):
76 'Skipping: Not yet fully implemented'
78 jsi
= JSInterpreter('''
86 self
.assertEqual(jsi
.call_function('x'), 52)
88 jsi
= JSInterpreter('''
91 var y = 1 /* comment */ + 2;
95 self
.assertEqual(jsi
.call_function('f'), 3)
97 def test_precedence(self
):
98 jsi
= JSInterpreter('''
100 var a = [10, 20, 30, 40, 50];
105 self
.assertEqual(jsi
.call_function('x'), [20, 20, 30, 40, 50])
108 jsi
= JSInterpreter('''
109 function x() { return 2; }
110 function y(a) { return x() + a; }
111 function z() { return y(3); }
113 self
.assertEqual(jsi
.call_function('z'), 5)
116 if __name__
== '__main__':