diff --git a/js/js.translator/testData/maps.js b/js/js.translator/testData/maps.js index 50c95ffd123..ffed41daa11 100644 --- a/js/js.translator/testData/maps.js +++ b/js/js.translator/testData/maps.js @@ -17,6 +17,27 @@ (function () { "use strict"; + /** + * @class + * @constructor + * @param {K} key + * @param {V} value + * @template K, V + */ + function Entry(key, value) { + this.key = key; + this.value = value; + } + + Entry.prototype.getKey = function () { + return this.key; + }; + + Entry.prototype.getValue = function () { + return this.value; + }; + + /** @const */ var FUNCTION = "function"; var arrayRemoveAt = (typeof Array.prototype.splice == FUNCTION) ? @@ -400,26 +421,36 @@ } return res; }; + + this.entrySet = function () { + var result = new Kotlin.ComplexHashSet(); + var entries = this._entries(); + var i = entries.length; + while (i--) { + var entry = entries[i]; + result.add_za3rmp$(new Entry(entry[0], entry[1])); + } + + return result; + } }; Kotlin.HashTable = Hashtable; -})(); -/** - * @interface - * @template Key, Value - */ -Kotlin.Map = Kotlin.createClassNow(); + /** + * @interface + * @template Key, Value + */ + Kotlin.Map = Kotlin.createClassNow(); -Kotlin.HashMap = Kotlin.createClassNow(Kotlin.Map, - function () { - Kotlin.HashTable.call(this); - } -); + Kotlin.HashMap = Kotlin.createClassNow(Kotlin.Map, + function () { + Kotlin.HashTable.call(this); + } + ); -Kotlin.ComplexHashMap = Kotlin.HashMap; + Kotlin.ComplexHashMap = Kotlin.HashMap; -(function () { /** * @class * @implements Kotlin.Iterator. @@ -530,6 +561,17 @@ Kotlin.ComplexHashMap = Kotlin.HashMap; } } }, + entrySet: function () { + var result = new Kotlin.ComplexHashSet(); + var map = this.map; + for (var key in map) { + if (map.hasOwnProperty(key)) { + result.add_za3rmp$(new Entry(key, map[key])); + } + } + + return result; + }, keySet: function () { var result = new Kotlin.PrimitiveHashSet(); var map = this.map; diff --git a/libraries/stdlib/test/js/MapJsTest.kt b/libraries/stdlib/test/js/MapJsTest.kt index cac15665612..88c76b9db22 100644 --- a/libraries/stdlib/test/js/MapJsTest.kt +++ b/libraries/stdlib/test/js/MapJsTest.kt @@ -6,12 +6,14 @@ import java.util.* import org.junit.Test as test class ComplexMapJsTest : MapJsTest() { + override fun > Collection.toNormalizedList(): List = this.toSortedList() // hashMapOf returns ComlpexHashMap because it is Generic - override fun emptyMutableMap(): MutableMap = hashMapOf() + override fun emptyMutableMap(): MutableMap = hashMapOf() } class PrimitiveMapJsTest : MapJsTest() { - override fun emptyMutableMap(): MutableMap = HashMap() + override fun > Collection.toNormalizedList(): List = this.toSortedList() + override fun emptyMutableMap(): MutableMap = HashMap() } abstract class MapJsTest { @@ -102,12 +104,26 @@ abstract class MapJsTest { test fun mapValues() { val map = createTestMap() - assertEquals(VALUES, map.values().toSortedList()) + assertEquals(VALUES.toNormalizedList(), map.values().toNormalizedList()) } test fun mapKeySet() { val map = createTestMap() - assertEquals(KEYS.toSortedList(), map.keySet().toSortedList()) + assertEquals(KEYS.toNormalizedList(), map.keySet().toNormalizedList()) + } + + test fun mapEntrySet() { + val map = createTestMap() + + val actualKeys = ArrayList() + val actualValues = ArrayList() + for (e in map.entrySet()) { + actualKeys.add(e.getKey()) + actualValues.add(e.getValue()) + } + + assertEquals(KEYS.toNormalizedList(), actualKeys.toNormalizedList()) + assertEquals(VALUES.toNormalizedList(), actualValues.toNormalizedList()) } test fun mapContainsKey() { @@ -196,6 +212,35 @@ abstract class MapJsTest { assertEquals(2, map.get("b")) } + test fun mapIteratorImplicitly() { + val map = createTestMap() + + val actualKeys = ArrayList() + val actualValues = ArrayList() + for (e in map) { + actualKeys.add(e.getKey()) + actualValues.add(e.getValue()) + } + + assertEquals(KEYS.toNormalizedList(), actualKeys.toNormalizedList()) + assertEquals(VALUES.toNormalizedList(), actualValues.toNormalizedList()) + } + + test fun mapIteratorExplicitly() { + val map = createTestMap() + + val actualKeys = ArrayList() + val actualValues = ArrayList() + val iterator = map.iterator() + for (e in iterator) { + actualKeys.add(e.getKey()) + actualValues.add(e.getValue()) + } + + assertEquals(KEYS.toNormalizedList(), actualKeys.toNormalizedList()) + assertEquals(VALUES.toNormalizedList(), actualValues.toNormalizedList()) + } + /* test fun createLinkedMap() { val map = linkedMapOf("c" to 3, "b" to 2, "a" to 1) @@ -312,7 +357,10 @@ abstract class MapJsTest { // Helpers - fun emptyMap(): Map = HashMap() + abstract fun > Collection.toNormalizedList(): List + + fun emptyMap(): Map = emptyMutableMap() + abstract fun emptyMutableMap(): MutableMap fun createTestMap(): Map = createTestMutableMap()