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