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 1ad9b50d7d GROOVY-4721: add test cases
1ad9b50d7d is described below
commit 1ad9b50d7d1907eb4854d7355eaafa7a3b4d48ee
Author: Daniel Sun <[email protected]>
AuthorDate: Sat Apr 19 22:48:56 2025 +0900
GROOVY-4721: add test cases
---
src/test/groovy/bugs/Groovy4721.groovy | 86 ++++++++++++++++++++++++++++++++++
1 file changed, 86 insertions(+)
diff --git a/src/test/groovy/bugs/Groovy4721.groovy
b/src/test/groovy/bugs/Groovy4721.groovy
index 8e720f6f83..b75dafd955 100644
--- a/src/test/groovy/bugs/Groovy4721.groovy
+++ b/src/test/groovy/bugs/Groovy4721.groovy
@@ -18,6 +18,7 @@
*/
package bugs
+import org.apache.groovy.util.JavaShell
import org.junit.jupiter.api.Test
import static groovy.test.GroovyAssert.assertScript
@@ -114,4 +115,89 @@ final class Groovy4721 {
assert 'foo' == new MyClass().myMethod()
'''
}
+
+ @Test
+ void testAccessingVariableInFinallyBlock_6() {
+ 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;
+ }
+ }
+ }
+ """
+ }
+
+ 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' == groovyResult
+ assert new TryFinallyJavaTest().test() == groovyResult
+ """
+ }
+
+ @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 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
+ """
+ }
}