Skip to content

Commit c7cf957

Browse files
committed
GROOVY-11769: add test case
1 parent 04c5855 commit c7cf957

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/test/groovy/groovy/transform/stc/CoercionSTCTest.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ class CoercionSTCTest extends StaticTypeCheckingTestCase {
211211
'''
212212
}
213213

214+
void testCastObjectToInterface() {
215+
assertScript '''
216+
List<Object> m(object) {
217+
if (object == null) return new ArrayList<>()
218+
if (object instanceof Collection) return new ArrayList<Object>((Collection) object)
219+
return [object]
220+
}
221+
def item = 0
222+
def list = [1,2]
223+
assert m(null).size() == 0
224+
assert m(item).size() == 1
225+
assert m(list).size() == 2
226+
'''
227+
}
228+
214229
void testCastInterfaceToSubclass() {
215230
assertScript '''
216231
interface A {

src/test/groovy/org/codehaus/groovy/classgen/asm/sc/CoercionStaticCompileTests.groovy

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,12 @@ import groovy.transform.stc.CoercionSTCTest
2424
* Unit tests for static compilation : coercions.
2525
*/
2626
final class CoercionStaticCompileTests extends CoercionSTCTest implements StaticCompilationTestSupport {
27+
28+
@Override // GROOVY-11769
29+
void testCastObjectToInterface() {
30+
super.testCastObjectToInterface()
31+
String bytecode = astTrees.values()[0][1]
32+
bytecode = bytecode.substring(bytecode.indexOf('m(Ljava/lang/Object;)'))
33+
assert bytecode.count('CHECKCAST') == 1 // guarded typecast isn't groovy
34+
}
2735
}

0 commit comments

Comments
 (0)