This is an automated email from the ASF dual-hosted git repository.

sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new ed6ee9bc20 GROOVY-4721: add testcases
ed6ee9bc20 is described below

commit ed6ee9bc203f236f437af71bea34d8e6f22665b9
Author: Daniel Sun <[email protected]>
AuthorDate: Sat Apr 19 23:20:05 2025 +0900

    GROOVY-4721: add testcases
---
 src/test/groovy/bugs/Groovy4721.groovy | 87 ++++++++++++++++++++++++++++++++++
 1 file changed, 87 insertions(+)

diff --git a/src/test/groovy/bugs/Groovy4721.groovy 
b/src/test/groovy/bugs/Groovy4721.groovy
index b75dafd955..ab17599169 100644
--- a/src/test/groovy/bugs/Groovy4721.groovy
+++ b/src/test/groovy/bugs/Groovy4721.groovy
@@ -157,6 +157,92 @@ final class Groovy4721 {
 
     @Test
     void testAccessingVariableInFinallyBlock_7() {
+        def declareClass = { String lang ->
+            """
+            import java.util.List;
+            import java.util.ArrayList;
+            import java.util.function.Function;
+            public class TryFinally${lang}Test {
+                public String test() {
+                    String result = "result: ";
+                    try {
+                        String x = "foo";
+                        result += x;
+                        return result;
+                    } finally {
+                        String y = "bar";
+                        result += y;
+                        return result;
+                    }
+                }
+            }
+            """
+        }
+
+        JavaShell js = new JavaShell()
+        final mcn = "tests.TryFinallyJavaTest"
+        js.compile(mcn, "package tests;\n${declareClass('Java')}")
+
+        new GroovyShell(js.getClassLoader()).evaluate """\
+            package tests;
+            import tests.TryFinallyJavaTest
+
+            ${declareClass('Groovy')}
+
+            final groovyResult = new TryFinallyGroovyTest().test()
+            assert 'result: foobar' == groovyResult
+            assert new TryFinallyJavaTest().test() == groovyResult
+        """
+    }
+
+    @Test
+    void testAccessingVariableInFinallyBlock_8() {
+        def declareClass = { String lang ->
+            """
+            import java.util.List;
+            import java.util.ArrayList;
+            import java.util.function.Function;
+            public class TryFinally${lang}Test {
+                public List<String> test() {
+                    List<String> resultList = new ArrayList<>();
+                    String result = "result: ";
+                    try {
+                        String x = "foo";
+                        result += x;
+                        resultList.add(result);
+                        Function<String, List<String>> f = (String r) -> {
+                            resultList.add(r);
+                            return resultList;
+                        };
+                        return f.apply(result);
+                    } finally {
+                        String y = "bar";
+                        result += y;
+                        resultList.add(result);
+                    }
+                }
+            }
+            """
+        }
+
+        JavaShell js = new JavaShell()
+        final mcn = "tests.TryFinallyJavaTest"
+        js.compile(mcn, "package tests;\n${declareClass('Java')}")
+
+        new GroovyShell(js.getClassLoader()).evaluate """\
+            package tests;
+            import tests.TryFinallyJavaTest
+
+            ${declareClass('Groovy')}
+
+            final groovyResult = new TryFinallyGroovyTest().test()
+            assert ['result: foo', 'result: foo', 'result: foobar'] == 
groovyResult
+            assert new TryFinallyJavaTest().test() == groovyResult
+        """
+    }
+
+    @Test
+    void testAccessingVariableInFinallyBlock_9() {
         def declareClass = { String lang ->
             """
             import java.util.List;
@@ -179,6 +265,7 @@ final class Groovy4721 {
                         String y = "bar";
                         result += y;
                         resultList.add(result);
+                        return resultList;
                     }
                 }
             }

Reply via email to