added testApplyMapDiff(Removed, Added, Changed) and refactor

This commit is contained in:
akkkihi 2023-05-12 22:36:59 +03:00
parent aeb2ff191c
commit 34d8c18a46

View File

@ -10,92 +10,119 @@ class MapDiffUtilsTests {
@Test
fun testMapDiffRemoved() {
val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)
val map2 = mapOf("a" to 1, "c" to 3, "d" to 4)
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val newMap = mapOf("a" to 1, "c" to 3, "d" to 4)
val diff = map1.diff(map2)
val diff = oldMap.diff(newMap)
assertEquals(mapOf("b" to 2), diff.removed)
assertEquals(
mapOf("b" to 2),
diff.removed
)
}
@Test
fun testMapDiffAdded() {
val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)
val map2 = mapOf("a" to 1, "c" to 3, "d" to 4)
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val newMap = mapOf("a" to 1, "c" to 3, "d" to 4)
val diff = map1.diff(map2)
val diff = oldMap.diff(newMap)
assertEquals(mapOf("d" to 4), diff.added)
assertEquals(
mapOf("d" to 4),
diff.added
)
}
@Test
fun testMapDiffChanged() {
val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)
val map2 = mapOf("a" to 1, "c" to 5, "d" to 4)
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val newMap = mapOf("a" to 1, "c" to 5, "d" to 4)
val diff = map1.diff(map2)
val diff = oldMap.diff(newMap)
assertEquals(mapOf("c" to (3 to 5)), diff.changed)
assertEquals(
mapOf("c" to (3 to 5)),
diff.changed
)
}
@Test
fun testMapDiffRemovedWithCompareFun() {
val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)
val map2 = mapOf("a" to 1, "c" to 3, "d" to 4)
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val newMap = mapOf("a" to 1, "c" to 3, "d" to 4)
val diff = map1.diff(map2, compareFun())
val diff = oldMap.diff(newMap, compareFun())
assertEquals(mapOf("b" to 2), diff.removed)
assertEquals(
mapOf("b" to 2),
diff.removed
)
}
@Test
fun testMapDiffAddedWithCompareFun() {
val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)
val map2 = mapOf("a" to 1, "c" to 3, "d" to 4)
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val newMap = mapOf("a" to 1, "c" to 3, "d" to 4)
val diff = map1.diff(map2, compareFun())
val diff = oldMap.diff(newMap, compareFun())
assertEquals(mapOf("d" to 4), diff.added)
assertEquals(
mapOf("d" to 4),
diff.added
)
}
@Test
fun testMapDiffChangedWithCompareFun() {
val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)
val map2 = mapOf("a" to 1, "c" to 5, "d" to 4)
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val newMap = mapOf("a" to 1, "c" to 5, "d" to 4)
val diff = map1.diff(map2) { _, v1, v2 -> v1 == v2 }
val diff = oldMap.diff(newMap) { _, v1, v2 -> v1 == v2 }
assertEquals(mapOf("c" to (3 to 5)), diff.changed)
assertEquals(
mapOf("c" to (3 to 5)),
diff.changed
)
}
@Test
fun testMapDiffRemovedStrictComparison() {
val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)
val map2 = mapOf("a" to 1, "c" to 3, "d" to 4)
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val newMap = mapOf("a" to 1, "c" to 3, "d" to 4)
val diff = map1.diff(map2, true)
val diff = oldMap.diff(newMap, true)
assertEquals(mapOf("b" to 2), diff.removed)
assertEquals(
mapOf("b" to 2),
diff.removed
)
}
@Test
fun testMapDiffAddedStrictComparison() {
val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)
val map2 = mapOf("a" to 1, "c" to 3, "d" to 4)
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val newMap = mapOf("a" to 1, "c" to 3, "d" to 4)
val diff = map1.diff(map2, true)
val diff = oldMap.diff(newMap, true)
assertEquals(mapOf("d" to 4), diff.added)
assertEquals(
mapOf("d" to 4),
diff.added
)
}
@Test
fun testMapDiffChangedStrictComparison() {
val map1 = mapOf("a" to 1, "b" to 2, "c" to 3)
val map2 = mapOf("a" to 1, "c" to 5, "d" to 4)
val oldMap = mapOf("a" to 1, "b" to 2, "c" to 3)
val newMap = mapOf("a" to 1, "c" to 5, "d" to 4)
val diff = map1.diff(map2, true)
val diff = oldMap.diff(newMap, true)
assertEquals(mapOf("c" to (3 to 5)), diff.changed)
assertEquals(
mapOf("c" to (3 to 5)),
diff.changed
)
}
@Test
@ -103,8 +130,26 @@ class MapDiffUtilsTests {
fun testApplyMapDiffRemoved() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3)
val diff = MapDiff(mapOf("b" to 2), emptyMap(), emptyMap())
originalMap.applyDiff(diff)
assertEquals(mapOf("a" to 1, "c" to 3), originalMap)
assertEquals(
mapOf("a" to 1, "c" to 3),
originalMap
)
}
@Test
@OptIn(Warning::class)
fun testApplyMapDiffAdded() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3)
val diff = MapDiff(emptyMap(), emptyMap(), mapOf("d" to 4))
originalMap.applyDiff(diff)
assertEquals(
mapOf("a" to 1, "b" to 2, "c" to 3, "d" to 4),
originalMap
)
}
@Test
@ -112,17 +157,58 @@ class MapDiffUtilsTests {
fun testApplyMapDiffChanged() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3)
val diff = MapDiff(emptyMap(), mapOf("b" to (2 to 4)), emptyMap())
originalMap.applyDiff(diff)
assertEquals(mapOf("a" to 1, "b" to 4, "c" to 3), originalMap)
assertEquals(
mapOf("a" to 1, "b" to 4, "c" to 3),
originalMap
)
}
@Test
@OptIn(Warning::class)
fun testApplyMapDiffAdded() {
fun testApplyMapDiffRemovedWithCompareFun() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3)
val diff = MapDiff(emptyMap(), emptyMap(), mapOf("d" to 4))
originalMap.applyDiff(diff)
assertEquals(mapOf("a" to 1, "b" to 2, "c" to 3, "d" to 4), originalMap)
val oldMap = mutableMapOf<String, Int>()
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(
remove,
diff.removed
)
}
@Test
fun testApplyMapDiffAddedWithCompareFun() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3)
val apply = mapOf("d" to 4)
val diff = originalMap.applyDiff(apply, compareFun())
assertEquals(
apply,
diff.added
)
}
@Test
fun testApplyMapDiffChangedWithCompareFun() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3)
val applyDiff = originalMap.applyDiff(mapOf("b" to 4), compareFun())
val changed = mapOf("b" to (2 to 4))
assertEquals(
changed,
applyDiff.changed
)
}
@Test
@ -130,8 +216,13 @@ class MapDiffUtilsTests {
fun testApplyMapDiffMixed() {
val originalMap = mutableMapOf("a" to 1, "b" to 2, "c" to 3)
val diff = MapDiff(mapOf("b" to 2), mapOf("c" to (3 to 5)), mapOf("d" to 4))
originalMap.applyDiff(diff)
assertEquals(mapOf("a" to 1, "c" to 5, "d" to 4), originalMap)
assertEquals(
mapOf("a" to 1, "c" to 5, "d" to 4),
originalMap
)
}
@Test