diff src/testdir/test_vim9_class.vim @ 34618:7ff3c277377f v9.1.0198

patch 9.1.0198: Vim9: compound operators broken for lambdas in an object Commit: https://github.com/vim/vim/commit/d990bf08d85d83e14fc51fd99a66ebe2f36d2fcd Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Fri Mar 22 19:56:17 2024 +0100 patch 9.1.0198: Vim9: compound operators broken for lambdas in an object Problem: Vim9: compound operators broken for lambdas in an object (girishji) Solution: When using an object from the outer scope, use the LOADOUTER instruction to load the object (Yegappan Lakshmanan). fixes: #14236 closes: #14266 Signed-off-by: Yegappan Lakshmanan <yegappan@yahoo.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Fri, 22 Mar 2024 20:00:06 +0100
parents d7b7fa7edb3b
children 5b25ec43f208
line wrap: on
line diff
--- a/src/testdir/test_vim9_class.vim
+++ b/src/testdir/test_vim9_class.vim
@@ -10349,4 +10349,75 @@ def Test_Ref_Class_Within_Same_Class()
   v9.CheckScriptFailure(lines, 'E1347: Not a valid interface: A', 3)
 enddef
 
+" Test for using a compound operator from a lambda function in an object method
+def Test_compound_op_in_objmethod_lambda()
+  # Test using the "+=" operator
+  var lines =<< trim END
+    vim9script
+    class A
+      var n: number = 10
+      def Foo()
+        var Fn = () => {
+          this.n += 1
+        }
+        Fn()
+      enddef
+    endclass
+
+    var a = A.new()
+    a.Foo()
+    assert_equal(11, a.n)
+  END
+  v9.CheckScriptSuccess(lines)
+
+  # Test using the "..=" operator
+  lines =<< trim END
+    vim9script
+    class A
+      var s: string = "a"
+      def Foo()
+        var Fn = () => {
+          this.s ..= "a"
+        }
+        Fn()
+      enddef
+    endclass
+
+    var a = A.new()
+    a.Foo()
+    a.Foo()
+    assert_equal("aaa", a.s)
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
+" call a lambda function in one object from another object
+def Test_lambda_invocation_across_classes()
+  var lines =<< trim END
+    vim9script
+    class A
+      var s: string = "foo"
+      def GetFn(): func
+        var Fn = (): string => {
+          return this.s
+        }
+        return Fn
+      enddef
+    endclass
+
+    class B
+      var s: string = "bar"
+      def GetFn(): func
+        var a = A.new()
+        return a.GetFn()
+      enddef
+    endclass
+
+    var b = B.new()
+    var Fn = b.GetFn()
+    assert_equal("foo", Fn())
+  END
+  v9.CheckScriptSuccess(lines)
+enddef
+
 " vim: ts=8 sw=2 sts=2 expandtab tw=80 fdm=marker