-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine accessData and accessDataOrNull
- Loading branch information
Showing
2 changed files
with
86 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package dev.pmpuro.tila.api | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T> DataMap.accessData(id: DataId): T = get(id) as T | ||
fun <T> DataMap.accessData(id: DataId): T = | ||
get(id).let { it as T } ?: error("data $id does not exists") | ||
|
||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun <T> DataMap.accessDataOrNull(id: DataId): T? = getOrElse(id) { null } as T? |
83 changes: 83 additions & 0 deletions
83
tila-core/src/commonTest/kotlin/dev/pmpuro/tila/AccessTest.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,83 @@ | ||
package dev.pmpuro.tila | ||
|
||
import dev.pmpuro.tila.api.DataId | ||
import dev.pmpuro.tila.api.DataMap | ||
import dev.pmpuro.tila.api.MutableDataMap | ||
import dev.pmpuro.tila.api.accessData | ||
import dev.pmpuro.tila.api.accessDataOrNull | ||
import kotlinx.coroutines.test.runTest | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.assertNull | ||
import kotlin.test.fail | ||
|
||
|
||
class AccessTest { | ||
|
||
@Test | ||
fun `should access data of correct type`() = runTest { | ||
createData() | ||
.accessData<Int>(dataA) | ||
.let { | ||
assertEquals(it, value1) | ||
} | ||
|
||
createData() | ||
.accessDataOrNull<Int>(dataB) | ||
.let { | ||
assertEquals(it, value2) | ||
} | ||
} | ||
|
||
@Test | ||
fun `should throw for incorrect type`() = runTest { | ||
assertFailsWith<ClassCastException> { | ||
createData() | ||
.accessData<String>(dataA) | ||
.let { | ||
fail("wrong type access should fail") | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `should return null for incorrect type`() = runTest { | ||
assertFailsWith<ClassCastException> { | ||
createData() | ||
.accessDataOrNull<String>(dataB) | ||
.let { | ||
fail("wrong type access should fail") | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `should throw for non existent data`() = runTest { | ||
assertFailsWith<IllegalStateException> { | ||
createData() | ||
.accessData<Int>(dataC) | ||
.let { | ||
fail("data does not exists") | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `should return null for non existent data`() = runTest { | ||
createData() | ||
.accessDataOrNull<Int>(dataC) | ||
.let { | ||
assertNull(it, "data does not exists") | ||
} | ||
} | ||
|
||
private val dataA = DataId("a") | ||
private val dataB = DataId("b") | ||
private val dataC = DataId("c") | ||
private val value1 = 1 | ||
private val value2 = 2 | ||
private val value3 = 3 | ||
private val map: MutableDataMap = mutableMapOf(dataA to value1, dataB to value2) | ||
private fun createData(): DataMap = map | ||
} |