rewrite and refactor

This commit is contained in:
akkkihi 2023-05-14 19:29:01 +03:00
parent 119009d3fb
commit fb48aa84f2

View File

@ -8,346 +8,285 @@ class MapDiffUtilsTests {
private fun <K, V> compareFun(): (K, V, V) -> Boolean = { _, a, b -> a == b } private fun <K, V> compareFun(): (K, V, V) -> Boolean = { _, a, b -> a == b }
private val originalMap = mapOf(
"a" to 1,
"b" to 2,
"c" to 3,
)
private val newMapRemoved = Pair(
mapOf(
"a" to 1,
"c" to 3,
),
mapOf("b" to 2)
)
private val newMapAdded = Pair(
mapOf(
"a" to 1,
"b" to 2,
"c" to 3,
"d" to 4,
),
mapOf("d" to 4)
)
private val newMapChanged = Pair(
mapOf(
"a" to 1,
"b" to 2,
"c" to 5,
),
mapOf("c" to (3 to 5))
)
private val newMapMixed = Pair(
mapOf(
"a" to 1,
"c" to 5,
"d" to 4,
),
MapDiff(
newMapRemoved.second,
newMapChanged.second,
newMapAdded.second,
)
)
@Test @Test
fun testMapDiffRemoved() { fun testMapDiffRemoved() {
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) val diff = originalMap.diff(newMapRemoved.first)
val newMap = mapOf("a" to 1, "c" to 3)
val diff = oldMap.diff(newMap)
assertEquals( assertEquals(
mapOf("b" to 2), newMapRemoved.second,
diff.removed diff.removed
) )
} }
@Test @Test
fun testMapDiffAdded() { fun testMapDiffAdded() {
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) val diff = originalMap.diff(newMapAdded.first)
val newMap = mapOf("a" to 1, "b" to 2, "c" to 3, "d" to 4)
val diff = oldMap.diff(newMap)
assertEquals( assertEquals(
mapOf("d" to 4), newMapAdded.second,
diff.added diff.added
) )
} }
@Test @Test
fun testMapDiffChanged() { fun testMapDiffChanged() {
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) val diff = originalMap.diff(newMapChanged.first)
val newMap = mapOf("a" to 1, "b" to 2, "c" to 5)
val diff = oldMap.diff(newMap)
assertEquals( assertEquals(
mapOf("c" to (3 to 5)), newMapChanged.second,
diff.changed diff.changed
) )
} }
@Test @Test
fun testMapDiffRemovedWithCompareFun() { fun testMapDiffRemovedWithCompareFun() {
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) val diff = originalMap.diff(newMapRemoved.first, compareFun())
val newMap = mapOf("a" to 1, "c" to 3, "d" to 4)
val diff = oldMap.diff(newMap, compareFun())
assertEquals( assertEquals(
mapOf("b" to 2), newMapRemoved.second,
diff.removed diff.removed
) )
} }
@Test @Test
fun testMapDiffAddedWithCompareFun() { fun testMapDiffAddedWithCompareFun() {
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) val diff = originalMap.diff(newMapAdded.first, compareFun())
val newMap = mapOf("a" to 1, "c" to 3, "d" to 4)
val diff = oldMap.diff(newMap, compareFun())
assertEquals( assertEquals(
mapOf("d" to 4), newMapAdded.second,
diff.added diff.added
) )
} }
@Test @Test
fun testMapDiffChangedWithCompareFun() { fun testMapDiffChangedWithCompareFun() {
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) val diff = originalMap.diff(newMapChanged.first, compareFun())
val newMap = mapOf("a" to 1, "c" to 5, "d" to 4)
val diff = oldMap.diff(newMap) { _, v1, v2 -> v1 == v2 }
assertEquals( assertEquals(
mapOf("c" to (3 to 5)), newMapChanged.second,
diff.changed diff.changed
) )
} }
@Test @Test
fun testMapDiffRemovedStrictComparison() { fun testMapDiffRemovedStrictComparison() {
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) val diff = originalMap.diff(newMapRemoved.first, true)
val newMap = mapOf("a" to 1, "c" to 3, "d" to 4)
val diff = oldMap.diff(newMap, true)
assertEquals( assertEquals(
mapOf("b" to 2), newMapRemoved.second,
diff.removed diff.removed
) )
} }
@Test @Test
fun testMapDiffAddedStrictComparison() { fun testMapDiffAddedStrictComparison() {
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) val diff = originalMap.diff(newMapAdded.first, true)
val newMap = mapOf("a" to 1, "c" to 3, "d" to 4)
val diff = oldMap.diff(newMap, true)
assertEquals( assertEquals(
mapOf("d" to 4), newMapAdded.second,
diff.added diff.added
) )
} }
@Test @Test
fun testMapDiffChangedStrictComparison() { fun testMapDiffChangedStrictComparison() {
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3) val diff = originalMap.diff(newMapChanged.first, true)
val newMap = mapOf("a" to 1, "c" to 5, "d" to 4)
val diff = oldMap.diff(newMap, true)
assertEquals( assertEquals(
mapOf("c" to (3 to 5)), newMapChanged.second,
diff.changed diff.changed
) )
} }
@Test @Test
@OptIn(Warning::class)
fun testApplyMapDiffRemoved() { fun testApplyMapDiffRemoved() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val originalMap = originalMap.toMutableMap()
val diff = MapDiff(mapOf("b" to 2), emptyMap(), emptyMap()) val mapDiff = MapDiff.empty<String, Int>().copy(
removed = newMapRemoved.first
)
originalMap.applyDiff(diff) originalMap.applyDiff(mapDiff)
assertEquals( assertEquals(
mapOf("a" to 1, "c" to 3), newMapRemoved.second,
originalMap originalMap
) )
} }
@Test @Test
@OptIn(Warning::class)
fun testApplyMapDiffAdded() { fun testApplyMapDiffAdded() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val originalMap = originalMap.toMutableMap()
val diff = MapDiff(emptyMap(), emptyMap(), mapOf("d" to 4)) val mapDiff = MapDiff.empty<String, Int>().copy(
added = newMapAdded.first
)
originalMap.applyDiff(diff) originalMap.applyDiff(mapDiff)
assertEquals( assertEquals(
mapOf("a" to 1, "b" to 2, "c" to 3, "d" to 4), newMapAdded.first,
originalMap originalMap
) )
} }
@Test @Test
@OptIn(Warning::class)
fun testApplyMapDiffChanged() { fun testApplyMapDiffChanged() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val originalMap = originalMap.toMutableMap()
val diff = MapDiff(emptyMap(), mapOf("b" to (2 to 4)), emptyMap()) val mapDiff = MapDiff.empty<String, Int>().copy(
changed = newMapChanged.second
)
originalMap.applyDiff(diff) originalMap.applyDiff(mapDiff)
assertEquals( assertEquals(
mapOf("a" to 1, "b" to 4, "c" to 3), newMapChanged.first,
originalMap originalMap
) )
} }
@Test @Test
fun testApplyMapDiffRemovedStrictComparison() { fun testApplyMapDiffRemovedStrictComparison() {
val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val originalMap = originalMap.toMutableMap()
val newMap = mutableMapOf("a" to 1, "c" to 3, "d" to 4) val diff = originalMap.applyDiff(newMapRemoved.first, true)
val diff = oldMap.applyDiff(newMap, true)
assertEquals( assertEquals(
mapOf("b" to 2), newMapRemoved.second,
diff.removed diff.removed
) )
} }
@Test @Test
fun testApplyMapDiffAddedStrictComparison() { fun testApplyMapDiffAddedStrictComparison() {
val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val originalMap = originalMap.toMutableMap()
val newMap = mutableMapOf("a" to 1, "c" to 3, "d" to 4) val diff = originalMap.applyDiff(newMapAdded.first, true)
val diff = oldMap.applyDiff(newMap, true)
assertEquals( assertEquals(
mapOf("d" to 4), newMapAdded.second,
diff.added diff.added
) )
} }
@Test @Test
fun testApplyMapDiffChangedStrictComparison() { fun testApplyMapDiffChangedStrictComparison() {
val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val originalMap = originalMap.toMutableMap()
val newMap = mutableMapOf("a" to 1, "c" to 5, "d" to 4) val diff = originalMap.applyDiff(newMapChanged.first, true)
val diff = oldMap.applyDiff(newMap, true)
assertEquals( assertEquals(
mapOf("c" to (3 to 5)), newMapChanged.second,
diff.changed diff.changed
) )
} }
@Test @Test
fun testApplyMapDiffRemovedWithCompareFun() { fun testApplyMapDiffRemovedWithCompareFun() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val oldMap = originalMap.toMutableMap()
val oldMap = mutableMapOf<String, Int>() val diff = oldMap.applyDiff(newMapRemoved.first, compareFun())
val newMap = mutableMapOf<String, Int>()
oldMap.putAll(originalMap)
newMap.putAll(originalMap)
val remove = mapOf("b" to 2)
newMap.remove(remove.keys.first())
val diff = oldMap.applyDiff(newMap, compareFun())
assertEquals( assertEquals(
remove, newMapRemoved.second,
diff.removed diff.removed
) )
} }
@Test @Test
fun testApplyMapDiffAddedWithCompareFun() { fun testApplyMapDiffAddedWithCompareFun() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val oldMap = originalMap.toMutableMap()
val apply = mapOf("d" to 4) val diff = oldMap.applyDiff(newMapAdded.first, compareFun())
val diff = originalMap.applyDiff(apply, compareFun())
assertEquals( assertEquals(
apply, newMapAdded.second,
diff.added diff.added
) )
} }
@Test @Test
fun testApplyMapDiffChangedWithCompareFun() { fun testApplyMapDiffChangedWithCompareFun() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val oldMap = originalMap.toMutableMap()
val diff = oldMap.applyDiff(newMapChanged.first, compareFun())
val applyDiff = originalMap.applyDiff(mapOf("b" to 4), compareFun())
val changed = mapOf("b" to (2 to 4))
assertEquals( assertEquals(
changed, newMapChanged.second,
applyDiff.changed diff.changed
) )
} }
@Test @Test
fun testMapDiffMixed() { fun testMapDiffMixed() {
val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val oldMap = originalMap.toMutableMap()
val newMap = mutableMapOf("a" to 1, "c" to 5, "d" to 4) val diff = oldMap.applyDiff(newMapMixed.first)
val mapDiff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4))
val diff = oldMap.diff(newMap)
assertEquals( assertEquals(
mapDiff.removed, newMapMixed.second.removed,
diff.removed diff.removed
) )
assertEquals( assertEquals(
mapDiff.changed, newMapMixed.second.changed,
diff.changed diff.changed
) )
assertEquals( assertEquals(
mapDiff.added, newMapMixed.second.added,
diff.added diff.added
) )
} }
@Test @Test
fun testApplyMapDiffMixed() { fun testApplyMapDiffMixed() {
val oldMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3) val oldMap = originalMap.toMutableMap()
val newMap = mutableMapOf("a" to 1, "c" to 5, "d" to 4) val diff = oldMap.applyDiff(newMapMixed.first)
val mapDiff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4))
val diff = oldMap.applyDiff(newMap)
assertEquals( assertEquals(
mapDiff.removed, newMapMixed.second.removed,
diff.removed diff.removed
) )
assertEquals( assertEquals(
mapDiff.changed, newMapMixed.second.changed,
diff.changed diff.changed
) )
assertEquals( assertEquals(
mapDiff.added, newMapMixed.second.added,
diff.added diff.added
) )
} }
@Test
@OptIn(Warning::class)
fun testMapDiffWorksStrictComparison() {
val oldMap = mutableMapOf<Int, String>()
val newMap = mutableMapOf<Int, String>()
for (i in 0 until 10) {
oldMap[i] = "old$i"
newMap[i] = "new$i"
}
oldMap[0] = "old0"
oldMap[2] = "old2"
oldMap[4] = "old4"
newMap[0] = "new0"
newMap[2] = "new2"
newMap[4] = "new4"
newMap[10] = "new10"
newMap[11] = "new11"
newMap.remove(1)
newMap.remove(3)
val mapDiff = oldMap.diff(newMap, true)
val removed = mapOf(
1 to "old1",
3 to "old3"
)
val added = mapOf(
10 to "new10",
11 to "new11"
)
val changed = oldMap.keys.intersect(newMap.keys).associateWith {
oldMap[it] to newMap[it]
}
val equalMapDiff = MapDiff(removed, changed, added)
assertEquals(
added,
mapDiff.added
)
assertEquals(
equalMapDiff.removed,
mapDiff.removed
)
assertEquals(
equalMapDiff.changed,
mapDiff.changed
)
}
@Test @Test
fun testMapDiffWorksWithEqualValuesStrictComparison() { fun testMapDiffWorksWithEqualValuesStrictComparison() {
val oldMap = mutableMapOf<String, Int>() val oldMap = mutableMapOf<String, Int>()
@ -378,4 +317,35 @@ class MapDiffUtilsTests {
mapDiff.added mapDiff.added
) )
} }
@Test
fun testApplyMapDiffWorksWithEqualValuesStrictComparison() {
val oldMap = mutableMapOf<String, Int>()
val newMap = mutableMapOf<String, Int>()
for (i in 0 until 10) {
val value = Random.nextInt()
oldMap[i.toString()] = value
newMap[i.toString()] = value
}
val mapDiff = oldMap.applyDiff(newMap, true)
val mergedValues = oldMap.keys.intersect(newMap.keys).associateWith {
oldMap[it] to newMap[it]
}
assertEquals(
mergedValues,
mapDiff.changed,
)
assertEquals(
emptyMap(),
mapDiff.removed
)
assertEquals(
emptyMap(),
mapDiff.added
)
}
} }