diff --git a/js/js.tests/test/org/jetbrains/k2js/test/semantics/RTTITest.java b/js/js.tests/test/org/jetbrains/k2js/test/semantics/RTTITest.java index aea6d567ae7..459309f4e56 100644 --- a/js/js.tests/test/org/jetbrains/k2js/test/semantics/RTTITest.java +++ b/js/js.tests/test/org/jetbrains/k2js/test/semantics/RTTITest.java @@ -43,4 +43,12 @@ public class RTTITest extends SingleFileTranslationTest { public void testNotIsOtherClass() throws Exception { fooBoxTest(); } + + public void testCollectionClassesIsCheck() throws Exception { + checkFooBoxIsOk(); + } + + public void testStdlibEmptyListClass() throws Exception { + checkFooBoxIsOk(); + } } diff --git a/js/js.translator/testData/rtti/cases/collectionClassesIsCheck.kt b/js/js.translator/testData/rtti/cases/collectionClassesIsCheck.kt new file mode 100644 index 00000000000..1e09f0fea12 --- /dev/null +++ b/js/js.translator/testData/rtti/cases/collectionClassesIsCheck.kt @@ -0,0 +1,97 @@ +// KT-2468 ArrayList is List or HashSet is Set fails in generated JS code +package foo + +import java.util.* + +data class A + +fun checkAbstractList(obj: Any) { + assertTrue(obj is AbstractList<*>, "checkAbstractList: is AbstractList") + assertTrue(obj is MutableList<*>, "checkAbstractList: is MutableList") + assertTrue(obj is List<*>, "checkAbstractList: is List") + assertTrue(obj is AbstractCollection<*>, "checkAbstractList: is AbstractCollection") + assertTrue(obj is MutableCollection<*>, "checkAbstractList: is MutableCollection") + assertTrue(obj is Collection<*>, "checkAbstractList: is Collection") + assertTrue(obj is MutableIterable<*>, "checkAbstractList: is MutableIterable") + assertTrue(obj is Iterable<*>, "checkAbstractList: is Iterable") + assertTrue((obj as Iterable<*>).iterator() is Iterator, "checkAbstractList: iterator() is Iterator") +} + +fun checkArrayList(obj: Any) { + assertTrue(obj is ArrayList<*>, "checkArrayList: is ArrayList") + assertTrue((obj as Iterable<*>).iterator() is MutableIterator, "checkAbstractList: iterator() is MutableIterator") + checkAbstractList(obj) +} + +fun checkHashSet(obj: Any) { + assertTrue(obj is HashSet<*>, "checkHashSet: is HashSet") + assertTrue(obj is AbstractCollection<*>, "checkHashSet: is AbstractCollection") + assertTrue(obj is MutableCollection<*>, "checkHashSet: is MutableCollection") + assertTrue(obj is Collection<*>, "checkHashSet: is Collection") + assertTrue(obj is MutableIterable<*>, "checkHashSet: is MutableIterable") + assertTrue(obj is Iterable<*>, "checkHashSet: is Iterable") + assertTrue(obj is MutableSet<*>, "checkHashSet: is MutableSet") + assertTrue(obj is Set<*>, "checkHashSet: is Set") + assertTrue((obj as Set<*>).iterator() is Iterator, "checkHashSet: iterator() is Iterator") + assertTrue((obj as Set<*>).iterator() is MutableIterator, "checkHashSet: iterator() is MutableIterator") +} + +fun checkLinkedHashSet(obj: Any) { + assertTrue(obj is LinkedHashSet<*>, "checkLinkedHashSet: is LinkedHashSet") + checkHashSet(obj) +} + +fun checkHashMap(obj: Any) { + assertTrue(obj is HashMap<*, *>, "checkHashMap: is HashMap") + assertTrue(obj is MutableMap<*, *>, "checkHashMap: is MutableMap") + assertTrue(obj is Map<*, *>, "checkHashMap: is Map") + assertTrue((obj as Map<*, *>).values() is Collection, "checkHashMap: values() is Collection") + assertTrue((obj as Map<*, *>).keySet() is Set, "checkHashMap: keySet() is Set") +} + +fun checkLinkedHashMap(obj: Any) { + assertTrue(obj is LinkedHashMap<*, *>, "checkLinkedHashMap: is LinkedHashMap") + checkHashMap(obj) +} + +fun box(): String { + + checkArrayList(ArrayList()) + checkArrayList(arrayListOf(1, 2, 3)) + + checkArrayList(ArrayList()) + checkArrayList(arrayListOf("first", "second")) + + checkArrayList(ArrayList()) + checkArrayList(arrayListOf(A())) + + checkHashSet(HashSet()) + checkHashSet(hashSetOf(1)) + checkLinkedHashSet(LinkedHashSet(0)) + + checkHashSet(HashSet()) + checkHashSet(hashSetOf(1.0)) + checkLinkedHashSet(LinkedHashSet(0)) + + checkHashSet(HashSet()) + checkHashSet(hashSetOf("test")) + checkLinkedHashSet(LinkedHashSet(0)) + + checkHashSet(HashSet()) + checkHashSet(hashSetOf(A())) + checkLinkedHashSet(LinkedHashSet(0)) + + checkHashMap(HashMap()) + checkHashMap(hashMapOf(1 to "test")) + checkLinkedHashMap(LinkedHashMap()) + + checkHashMap(HashMap()) + checkHashMap(hashMapOf(1.0 to "test")) + checkLinkedHashMap(LinkedHashMap()) + + checkHashMap(HashMap()) + checkHashMap(HashMap()) + checkLinkedHashMap(LinkedHashMap()) + + return "OK" +} \ No newline at end of file diff --git a/js/js.translator/testData/rtti/cases/stdlibEmptyListClass.kt b/js/js.translator/testData/rtti/cases/stdlibEmptyListClass.kt new file mode 100644 index 00000000000..ab230987b97 --- /dev/null +++ b/js/js.translator/testData/rtti/cases/stdlibEmptyListClass.kt @@ -0,0 +1,19 @@ +// KT-5192 JS compiler fails to generate correct code for List implementation +package foo + +import java.util.ArrayList + +class stdlib_emptyListClass : List by ArrayList() {} + +fun box(): String { + + assertTrue(stdlib_emptyListClass() is List<*>, "stdlib_emptyListClass() is List<*> #1") + assertTrue((stdlib_emptyListClass(): Any) is List<*>, "stdlib_emptyListClass() is List<*> #2") + assertTrue((stdlib_emptyListClass(): Any) !is ArrayList<*>, "stdlib_emptyListClass() !is ArrayList<*>") + + assertTrue(stdlib_emptyListClass().isEmpty(), "stdlib_emptyListClass().isEmpty()") + assertTrue(stdlib_emptyListClass().size() == 0, "stdlib_emptyListClass().size() == 0") + + return "OK" +} + diff --git a/libraries/stdlib/test/collections/CollectionTest.kt b/libraries/stdlib/test/collections/CollectionTest.kt index feac6802b72..8b5fc3bbdc7 100644 --- a/libraries/stdlib/test/collections/CollectionTest.kt +++ b/libraries/stdlib/test/collections/CollectionTest.kt @@ -30,10 +30,9 @@ class CollectionTest { assertEquals(2, foo.size) assertEquals(arrayListOf("foo", "bar"), foo) -// TODO uncomment this when KT-2468 will be fixed -// assertTrue { -// foo is List -// } + assertTrue { + foo is List + } } test fun mapNotNull() { @@ -42,10 +41,9 @@ class CollectionTest { assertEquals(2, foo.size) assertEquals(arrayListOf(3, 3), foo) -// TODO uncomment this when KT-2468 will be fixed -// assertTrue { -// foo is List -// } + assertTrue { + foo is List + } } test fun filterIntoSet() { diff --git a/libraries/stdlib/test/collections/IterableTests.kt b/libraries/stdlib/test/collections/IterableTests.kt index 85d24879b46..dc6ff39d5bb 100644 --- a/libraries/stdlib/test/collections/IterableTests.kt +++ b/libraries/stdlib/test/collections/IterableTests.kt @@ -105,8 +105,7 @@ abstract class IterableTests>(val data: T, val empty: T) { Test fun filter() { val foo = data.filter { it.startsWith("f") } - // TODO uncomment this when KT-2468 will be fixed - //expect(true) { foo is List } + expect(true) { foo is List } expect(true) { foo.all { it.startsWith("f") } } expect(1) { foo.size } assertEquals(listOf("foo"), foo) @@ -114,8 +113,7 @@ abstract class IterableTests>(val data: T, val empty: T) { Test fun filterNot() { val notFoo = data.filterNot { it.startsWith("f") } - // TODO uncomment this when KT-2468 will be fixed - //expect(true) { notFoo is List } + expect(true) { notFoo is List } expect(true) { notFoo.none { it.startsWith("f") } } expect(1) { notFoo.size } assertEquals(listOf("bar"), notFoo)