]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_jsinterp.py
Update changelog.
[youtubedl] / test / test_jsinterp.py
1 #!/usr/bin/env python
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 youtube_dl.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 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):
73 'Skipping: Not yet fully implemented'
74 return
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
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
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
104
105 if __name__ == '__main__':
106 unittest.main()