Fix null values being treated as missing in 'put' method of a specialized string-keyed map

This commit is contained in:
Ilya Gorbunov
2017-04-17 21:03:04 +03:00
parent 8fdb611e3b
commit 2db8714fc5
2 changed files with 22 additions and 2 deletions
@@ -23,6 +23,10 @@ import kotlin.collections.MutableMap.MutableEntry
/**
* A simple wrapper around JavaScript Map for key type is string.
*
* Though this map is instantiated only with K=String, the K type is not fixed to String statically,
* because we want to have it erased to Any? in order not to generate type-safe override bridges for
* [get], [contains], [remove] etc, if they ever are generated.
*/
internal class InternalStringMap<K, V>(override val equality: EqualityComparator) : InternalMap<K, V> {
@@ -55,7 +59,7 @@ internal class InternalStringMap<K, V>(override val equality: EqualityComparator
val oldValue = backingMap[key]
backingMap[key] = value
if (oldValue == undefined) {
if (oldValue === undefined) {
size++
// structureChanged(host)
return null
@@ -99,10 +103,12 @@ internal class InternalStringMap<K, V>(override val equality: EqualityComparator
override fun next(): MutableEntry<K, V> {
val key = iterator.next()
lastKey = key
@Suppress("UNCHECKED_CAST")
return newMapEntry(key as K)
}
override fun remove() {
@Suppress("UNCHECKED_CAST")
this@InternalStringMap.remove(checkNotNull(lastKey) as K)
}
}
+15 -1
View File
@@ -45,7 +45,7 @@ class PrimitiveMapJsTest : MapJsTest() {
override fun emptyMutableMapWithNullableKeyValue(): MutableMap<String?, Int?> = HashMap()
@Test fun compareBehavior() {
val specialJsStringMap = HashMap<String, Any>()
val specialJsStringMap = stringMapOf<Any>()
specialJsStringMap.put("k1", "v1")
compare(genericHashMapOf("k1" to "v1"), specialJsStringMap) { mapBehavior() }
@@ -53,6 +53,20 @@ class PrimitiveMapJsTest : MapJsTest() {
specialJsNumberMap.put(5, "v5")
compare(genericHashMapOf(5 to "v5"), specialJsNumberMap) { mapBehavior() }
}
@Test fun putNull() {
val map = stringMapOf("k" to null)
assertEquals(1, map.size)
map.put("k", null)
assertEquals(1, map.size)
map["k"] = null
assertEquals(1, map.size)
map.remove("k")
assertEquals(0, map.size)
}
}
class LinkedHashMapJsTest : MapJsTest() {