You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.7 KiB

  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. # Allow direct execution
  4. import os
  5. import sys
  6. import unittest
  7. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  8. from youtube_dl.jsinterp import JSInterpreter
  9. class TestJSInterpreter(unittest.TestCase):
  10. def test_basic(self):
  11. jsi = JSInterpreter('function x(){;}')
  12. self.assertEqual(jsi.call_function('x'), None)
  13. jsi = JSInterpreter('function x3(){return 42;}')
  14. self.assertEqual(jsi.call_function('x3'), 42)
  15. jsi = JSInterpreter('var x5 = function(){return 42;}')
  16. self.assertEqual(jsi.call_function('x5'), 42)
  17. def test_calc(self):
  18. jsi = JSInterpreter('function x4(a){return 2*a+1;}')
  19. self.assertEqual(jsi.call_function('x4', 3), 7)
  20. def test_empty_return(self):
  21. jsi = JSInterpreter('function f(){return; y()}')
  22. self.assertEqual(jsi.call_function('f'), None)
  23. def test_morespace(self):
  24. jsi = JSInterpreter('function x (a) { return 2 * a + 1 ; }')
  25. self.assertEqual(jsi.call_function('x', 3), 7)
  26. jsi = JSInterpreter('function f () { x = 2 ; return x; }')
  27. self.assertEqual(jsi.call_function('f'), 2)
  28. def test_strange_chars(self):
  29. jsi = JSInterpreter('function $_xY1 ($_axY1) { var $_axY2 = $_axY1 + 1; return $_axY2; }')
  30. self.assertEqual(jsi.call_function('$_xY1', 20), 21)
  31. def test_operators(self):
  32. jsi = JSInterpreter('function f(){return 1 << 5;}')
  33. self.assertEqual(jsi.call_function('f'), 32)
  34. jsi = JSInterpreter('function f(){return 19 & 21;}')
  35. self.assertEqual(jsi.call_function('f'), 17)
  36. jsi = JSInterpreter('function f(){return 11 >> 2;}')
  37. self.assertEqual(jsi.call_function('f'), 2)
  38. def test_array_access(self):
  39. jsi = JSInterpreter('function f(){var x = [1,2,3]; x[0] = 4; x[0] = 5; x[2] = 7; return x;}')
  40. self.assertEqual(jsi.call_function('f'), [5, 2, 7])
  41. def test_parens(self):
  42. jsi = JSInterpreter('function f(){return (1) + (2) * ((( (( (((((3)))))) )) ));}')
  43. self.assertEqual(jsi.call_function('f'), 7)
  44. jsi = JSInterpreter('function f(){return (1 + 2) * 3;}')
  45. self.assertEqual(jsi.call_function('f'), 9)
  46. def test_assignments(self):
  47. jsi = JSInterpreter('function f(){var x = 20; x = 30 + 1; return x;}')
  48. self.assertEqual(jsi.call_function('f'), 31)
  49. jsi = JSInterpreter('function f(){var x = 20; x += 30 + 1; return x;}')
  50. self.assertEqual(jsi.call_function('f'), 51)
  51. jsi = JSInterpreter('function f(){var x = 20; x -= 30 + 1; return x;}')
  52. self.assertEqual(jsi.call_function('f'), -11)
  53. def test_comments(self):
  54. 'Skipping: Not yet fully implemented'
  55. return
  56. jsi = JSInterpreter('''
  57. function x() {
  58. var x = /* 1 + */ 2;
  59. var y = /* 30
  60. * 40 */ 50;
  61. return x + y;
  62. }
  63. ''')
  64. self.assertEqual(jsi.call_function('x'), 52)
  65. jsi = JSInterpreter('''
  66. function f() {
  67. var x = "/*";
  68. var y = 1 /* comment */ + 2;
  69. return y;
  70. }
  71. ''')
  72. self.assertEqual(jsi.call_function('f'), 3)
  73. def test_precedence(self):
  74. jsi = JSInterpreter('''
  75. function x() {
  76. var a = [10, 20, 30, 40, 50];
  77. var b = 6;
  78. a[0]=a[b%a.length];
  79. return a;
  80. }''')
  81. self.assertEqual(jsi.call_function('x'), [20, 20, 30, 40, 50])
  82. def test_call(self):
  83. jsi = JSInterpreter('''
  84. function x() { return 2; }
  85. function y(a) { return x() + a; }
  86. function z() { return y(3); }
  87. ''')
  88. self.assertEqual(jsi.call_function('z'), 5)
  89. if __name__ == '__main__':
  90. unittest.main()