about summary refs log tree commit diff
path: root/test
diff options
context:
space:
mode:
authordf <fieldhouse@gmx.net>2021-11-02 11:18:39 +0000
committerdirkf <fieldhouse@gmx.net>2022-01-30 00:05:54 +0000
commit96f87aaa3b34d80bc72097a7475d8093849091fc (patch)
treed15de84dff0d793a460b68bdcdbdf9ad539f3f41 /test
parent5f5de51a499f732a6e687f32037e130cbdc50c8f (diff)
downloadyoutube-dl-96f87aaa3b34d80bc72097a7475d8093849091fc.tar.gz
youtube-dl-96f87aaa3b34d80bc72097a7475d8093849091fc.tar.xz
youtube-dl-96f87aaa3b34d80bc72097a7475d8093849091fc.zip
Back-port JS interpreter upgrade from yt-dlp PR #1437
Diffstat (limited to 'test')
-rw-r--r--test/test_jsinterp.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/test/test_jsinterp.py b/test/test_jsinterp.py
index c24b8ca74..4d05ea610 100644
--- a/test/test_jsinterp.py
+++ b/test/test_jsinterp.py
@@ -112,6 +112,57 @@ class TestJSInterpreter(unittest.TestCase):
         ''')
         self.assertEqual(jsi.call_function('z'), 5)
 
+    def test_for_loop(self):
+        # function x() { a=0; for (i=0; i-10; i++) {a++} a }
+        jsi = JSInterpreter('''
+        function x() { a=0; for (i=0; i-10; i = i + 1) {a++} a }
+        ''')
+        self.assertEqual(jsi.call_function('x'), 10)
+
+    def test_switch(self):
+        jsi = JSInterpreter('''
+        function x(f) { switch(f){
+            case 1:f+=1;
+            case 2:f+=2;
+            case 3:f+=3;break;
+            case 4:f+=4;
+            default:f=0;
+        } return f }
+        ''')
+        self.assertEqual(jsi.call_function('x', 1), 7)
+        self.assertEqual(jsi.call_function('x', 3), 6)
+        self.assertEqual(jsi.call_function('x', 5), 0)
+
+    def test_try(self):
+        jsi = JSInterpreter('''
+        function x() { try{return 10} catch(e){return 5} }
+        ''')
+        self.assertEqual(jsi.call_function('x'), 10)
+
+    def test_for_loop_continue(self):
+        jsi = JSInterpreter('''
+        function x() { a=0; for (i=0; i-10; i++) { continue; a++ } a }
+        ''')
+        self.assertEqual(jsi.call_function('x'), 0)
+
+    def test_for_loop_break(self):
+        jsi = JSInterpreter('''
+        function x() { a=0; for (i=0; i-10; i++) { break; a++ } a }
+        ''')
+        self.assertEqual(jsi.call_function('x'), 0)
+
+    def test_literal_list(self):
+        jsi = JSInterpreter('''
+        function x() { [1, 2, "asdf", [5, 6, 7]][3] }
+        ''')
+        self.assertEqual(jsi.call_function('x'), [5, 6, 7])
+
+    def test_comma(self):
+        jsi = JSInterpreter('''
+        function x() { a=5; a -= 1, a+=3; return a }
+        ''')
+        self.assertEqual(jsi.call_function('x'), 7)
+
 
 if __name__ == '__main__':
     unittest.main()