Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for MapDiff #255

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,377 @@
package dev.inmo.micro_utils.common

import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class MapDiffUtilsTests {

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 newMapChangedComparable = Pair(
newMapChanged.first,
mapOf(
"a" to (1 to 1),
"b" to (2 to 2),
)
)
private val newMapMixed = Pair(
mapOf(
"a" to 1,
"c" to 5,
"d" to 4,
),
MapDiff(
newMapRemoved.second,
newMapChanged.second,
newMapAdded.second,
)
)
private val compareFunWasCalled = booleanArrayOf(false)

private fun <K, V> compareFun(
wasCalled: BooleanArray
): (K, V, V) -> Boolean = { _, a, b ->
wasCalled[0] = true
a != b
}

@Test
fun testMapDiffRemoved() {
val diff = originalMap.diff(newMapRemoved.first)

assertEquals(
newMapRemoved.second,
diff.removed
)
}

@Test
fun testMapDiffAdded() {
val diff = originalMap.diff(newMapAdded.first)

assertEquals(
newMapAdded.second,
diff.added
)
}

@Test
fun testMapDiffChanged() {
val diff = originalMap.diff(newMapChanged.first)

assertEquals(
newMapChanged.second,
diff.changed
)
}

@Test
fun testMapDiffRemovedWithCompareFun() {
val wasCalled = compareFunWasCalled
val diff = originalMap.diff(newMapRemoved.first, compareFun(wasCalled))

assertTrue(wasCalled[0])
assertEquals(
newMapRemoved.second,
diff.removed
)
}

@Test
fun testMapDiffAddedWithCompareFun() {
val wasCalled = compareFunWasCalled
val diff = originalMap.diff(newMapAdded.first, compareFun(wasCalled))

assertTrue(wasCalled[0])
assertEquals(
newMapAdded.second,
diff.added
)
}

@Test
fun testMapDiffChangedWithCompareFun() {
val wasCalled = compareFunWasCalled
val diff = originalMap.diff(newMapChangedComparable.first, compareFun(wasCalled))

assertTrue(wasCalled[0])
assertEquals(
newMapChangedComparable.second,
diff.changed
)
}

@Test
fun testMapDiffRemovedStrictComparison() {
val diff = originalMap.diff(newMapRemoved.first, true)

assertEquals(
newMapRemoved.second,
diff.removed
)
}

@Test
fun testMapDiffAddedStrictComparison() {
val diff = originalMap.diff(newMapAdded.first, true)

assertEquals(
newMapAdded.second,
diff.added
)
}

@Test
fun testMapDiffChangedStrictComparison() {
val diff = originalMap.diff(newMapChanged.first, true)

assertEquals(
newMapChanged.second,
diff.changed
)
}

@Test
fun testApplyMapDiffRemoved() {
val originalMap = originalMap.toMutableMap()
val mapDiff = MapDiff.empty<String, Int>().copy(
removed = newMapRemoved.first
)

originalMap.applyDiff(mapDiff)

assertEquals(
newMapRemoved.second,
originalMap
)
}
@Test
fun testApplyMapDiffAdded() {
val originalMap = originalMap.toMutableMap()
val mapDiff = MapDiff.empty<String, Int>().copy(
added = newMapAdded.first
)

originalMap.applyDiff(mapDiff)

assertEquals(
newMapAdded.first,
originalMap
)
}

@Test
fun testApplyMapDiffChanged() {
val originalMap = originalMap.toMutableMap()
val mapDiff = MapDiff.empty<String, Int>().copy(
changed = newMapChanged.second
)

originalMap.applyDiff(mapDiff)

assertEquals(
newMapChanged.first,
originalMap
)
}

@Test
fun testApplyMapDiffRemovedStrictComparison() {
val originalMap = originalMap.toMutableMap()
val diff = originalMap.applyDiff(newMapRemoved.first, true)

assertEquals(
newMapRemoved.second,
diff.removed
)
}

@Test
fun testApplyMapDiffAddedStrictComparison() {
val originalMap = originalMap.toMutableMap()
val diff = originalMap.applyDiff(newMapAdded.first, true)

assertEquals(
newMapAdded.second,
diff.added
)
}

@Test
fun testApplyMapDiffChangedStrictComparison() {
val originalMap = originalMap.toMutableMap()
val diff = originalMap.applyDiff(newMapChanged.first, true)

assertEquals(
newMapChanged.second,
diff.changed
)
}

@Test
fun testApplyMapDiffRemovedWithCompareFun() {
val wasCalled = compareFunWasCalled
val oldMap = originalMap.toMutableMap()
val diff = oldMap.applyDiff(newMapRemoved.first, compareFun(wasCalled))

assertTrue(wasCalled[0])
assertEquals(
newMapRemoved.second,
diff.removed
)
}
@Test
fun testApplyMapDiffAddedWithCompareFun() {
val wasCalled = compareFunWasCalled
val oldMap = originalMap.toMutableMap()
val diff = oldMap.applyDiff(newMapAdded.first, compareFun(wasCalled))

assertTrue(wasCalled[0])
assertEquals(
newMapAdded.second,
diff.added
)
}

@Test
fun testApplyMapDiffChangedWithCompareFun() {
val wasCalled = compareFunWasCalled
val oldMap = originalMap.toMutableMap()
val diff = oldMap.applyDiff(newMapChangedComparable.first, compareFun(wasCalled))

assertTrue(wasCalled[0])
assertEquals(
newMapChangedComparable.second,
diff.changed
)
}

@Test
fun testMapDiffMixed() {
val oldMap = originalMap.toMutableMap()
val diff = oldMap.applyDiff(newMapMixed.first)

assertEquals(
newMapMixed.second.removed,
diff.removed
)
assertEquals(
newMapMixed.second.changed,
diff.changed
)
assertEquals(
newMapMixed.second.added,
diff.added
)
}

@Test
fun testApplyMapDiffMixed() {
val oldMap = originalMap.toMutableMap()
val diff = oldMap.applyDiff(newMapMixed.first)

assertEquals(
newMapMixed.second.removed,
diff.removed
)
assertEquals(
newMapMixed.second.changed,
diff.changed
)
assertEquals(
newMapMixed.second.added,
diff.added
)
}

@Test
fun testMapDiffWorksWithEqualValuesStrictComparison() {
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.diff(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
)
}

@Test
fun testApplyMapDiffWorksWithEqualValuesStrictComparison() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you should check that:

  1. Values from old map (before changes) presented in diffs with strict (===) comparison
  2. Values from old map absent in the oldMap after applyDiff (with strict check too)
  3. Values in oldMap after applyDiff strictly (===) equal to the values from newMap by the same keys

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
)
}
}