Skip to content

Commit ed6ee9b

Browse files
committed
GROOVY-4721: add testcases
1 parent 1ad9b50 commit ed6ee9b

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

src/test/groovy/bugs/Groovy4721.groovy

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,92 @@ final class Groovy4721 {
157157

158158
@Test
159159
void testAccessingVariableInFinallyBlock_7() {
160+
def declareClass = { String lang ->
161+
"""
162+
import java.util.List;
163+
import java.util.ArrayList;
164+
import java.util.function.Function;
165+
public class TryFinally${lang}Test {
166+
public String test() {
167+
String result = "result: ";
168+
try {
169+
String x = "foo";
170+
result += x;
171+
return result;
172+
} finally {
173+
String y = "bar";
174+
result += y;
175+
return result;
176+
}
177+
}
178+
}
179+
"""
180+
}
181+
182+
JavaShell js = new JavaShell()
183+
final mcn = "tests.TryFinallyJavaTest"
184+
js.compile(mcn, "package tests;\n${declareClass('Java')}")
185+
186+
new GroovyShell(js.getClassLoader()).evaluate """\
187+
package tests;
188+
import tests.TryFinallyJavaTest
189+
190+
${declareClass('Groovy')}
191+
192+
final groovyResult = new TryFinallyGroovyTest().test()
193+
assert 'result: foobar' == groovyResult
194+
assert new TryFinallyJavaTest().test() == groovyResult
195+
"""
196+
}
197+
198+
@Test
199+
void testAccessingVariableInFinallyBlock_8() {
200+
def declareClass = { String lang ->
201+
"""
202+
import java.util.List;
203+
import java.util.ArrayList;
204+
import java.util.function.Function;
205+
public class TryFinally${lang}Test {
206+
public List<String> test() {
207+
List<String> resultList = new ArrayList<>();
208+
String result = "result: ";
209+
try {
210+
String x = "foo";
211+
result += x;
212+
resultList.add(result);
213+
Function<String, List<String>> f = (String r) -> {
214+
resultList.add(r);
215+
return resultList;
216+
};
217+
return f.apply(result);
218+
} finally {
219+
String y = "bar";
220+
result += y;
221+
resultList.add(result);
222+
}
223+
}
224+
}
225+
"""
226+
}
227+
228+
JavaShell js = new JavaShell()
229+
final mcn = "tests.TryFinallyJavaTest"
230+
js.compile(mcn, "package tests;\n${declareClass('Java')}")
231+
232+
new GroovyShell(js.getClassLoader()).evaluate """\
233+
package tests;
234+
import tests.TryFinallyJavaTest
235+
236+
${declareClass('Groovy')}
237+
238+
final groovyResult = new TryFinallyGroovyTest().test()
239+
assert ['result: foo', 'result: foo', 'result: foobar'] == groovyResult
240+
assert new TryFinallyJavaTest().test() == groovyResult
241+
"""
242+
}
243+
244+
@Test
245+
void testAccessingVariableInFinallyBlock_9() {
160246
def declareClass = { String lang ->
161247
"""
162248
import java.util.List;
@@ -179,6 +265,7 @@ final class Groovy4721 {
179265
String y = "bar";
180266
result += y;
181267
resultList.add(result);
268+
return resultList;
182269
}
183270
}
184271
}

0 commit comments

Comments
 (0)