Skip to content

Commit

Permalink
refactor(#12): update serialization and tests of AnyInt
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Dec 29, 2022
1 parent 9ba16f8 commit 14e766a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/commonMain/kotlin/kotools/types/number/AnyInt.kt
Expand Up @@ -44,9 +44,8 @@ internal sealed interface AnyIntSerializer<I : AnyInt> : KSerializer<I> {

fun deserialize(value: Int): I

override fun deserialize(decoder: Decoder): I = deserialize(
decoder.decodeInt()
)
override fun deserialize(decoder: Decoder): I = decoder.decodeInt()
.let(::deserialize)
}

internal object AnyIntSerializerImplementation : AnyIntSerializer<AnyInt> {
Expand Down
18 changes: 7 additions & 11 deletions src/commonTest/kotlin/kotools/types/number/AnyIntTest.kt
Expand Up @@ -5,16 +5,16 @@ import kotlinx.serialization.KSerializer
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotools.types.Package
import kotools.types.shouldEqual
import kotlin.random.Random
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class AnyIntTest {
@Test
fun compareTo_should_return_zero_with_another_AnyInt_having_the_same_value() {
val result: Int = ZeroInt.compareTo(ZeroInt)
assertEquals(0, result)
result shouldEqual ZeroInt.asInt
}

@Test
Expand All @@ -26,7 +26,7 @@ class AnyIntTest {
.asStrictlyPositiveInt
.getOrThrow()
val result: Int = x.compareTo(y)
assertTrue { result < 0 }
assertTrue { result < ZeroInt.asInt }
}

@Test
Expand All @@ -38,7 +38,7 @@ class AnyIntTest {
.asStrictlyNegativeInt
.getOrThrow()
val result: Int = x.compareTo(y)
assertTrue { result > 0 }
assertTrue { result > ZeroInt.asInt }
}
}

Expand All @@ -48,26 +48,22 @@ class AnyIntSerializerTest {
@ExperimentalSerializationApi
@Test
fun descriptor_should_have_the_qualified_name_of_AnyInt_as_serial_name(): Unit =
assertEquals(
"${Package.number}.AnyInt",
serializer.descriptor.serialName
)
serializer.descriptor.serialName shouldEqual "${Package.number}.AnyInt"

@Test
fun serialize_should_behave_like_an_Int() {
val x: AnyInt = strictlyPositiveIntRange.random()
.asStrictlyPositiveInt
.getOrThrow()
val result: String = Json.encodeToString(serializer, x)
val expected: String = Json.encodeToString(x.asInt)
assertEquals(expected, result)
result shouldEqual Json.encodeToString(x.asInt)
}

@Test
fun deserialize_should_pass_with_an_Int() {
val value: Int = Random.nextInt()
val encoded: String = Json.encodeToString(value)
val result: AnyInt = Json.decodeFromString(serializer, encoded)
assertEquals(value, result.asInt)
result.asInt shouldEqual value
}
}

0 comments on commit 14e766a

Please sign in to comment.