-
Notifications
You must be signed in to change notification settings - Fork 3
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
Akkihi
wants to merge
10
commits into
InsanusMokrassar:master
Choose a base branch
from
Akkihi:add_test_mapdiff
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Tests for MapDiff #255
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
aeb2ff1
added class MapDiffUtilsTests for testing MapDiff
Akkihi 34d8c18
added testApplyMapDiff(Removed, Added, Changed) and refactor
Akkihi 61ea85a
refactor and add mixed
Akkihi ac34050
refactor
Akkihi 3573b2c
Update MapDiffUtilsTests.kt
InsanusMokrassar 119009d
fix in several new tests
InsanusMokrassar fb48aa8
rewrite and refactor
Akkihi 8820da3
refactor
Akkihi 99982db
refactor
Akkihi f03de8c
micro refactor
Akkihi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
377 changes: 377 additions & 0 deletions
377
common/src/commonTest/kotlin/dev/inmo/micro_utils/common/MapDiffUtilsTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
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 | ||
) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
===
) comparison===
) equal to the values from newMap by the same keys