Skip to content

Commit

Permalink
refactor(#9)!: replace asSet property by toSet function in NotEmptySet
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Jan 3, 2023
1 parent 0f3a83d commit 05547d8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
Expand Up @@ -35,7 +35,7 @@ public data class NotEmptyMap<K, out V> internal constructor(
*/
public val asMap: Map<K, V> by lazy {
tail?.let {
val tail: Array<Pair<K, V>> = it.entries.asSet
val tail: Array<Pair<K, V>> = it.entries.toSet()
.map(Map.Entry<K, V>::toPair)
.toTypedArray()
mapOf(head, *tail)
Expand Down
18 changes: 9 additions & 9 deletions src/commonMain/kotlin/kotools/types/collection/NotEmptySet.kt
Expand Up @@ -28,19 +28,19 @@ public data class NotEmptySet<out E> internal constructor(
/** All elements of this set except [the first one][head]. */
public val tail: NotEmptySet<E>? = null
) {
/** Returns all elements of this set as a [Set] of type [E]. */
public val asSet: Set<E> by lazy {
val elements: Set<E> = setOf(head)
tail?.let { elements + it.asSet } ?: elements
}

/** The size of this set. */
public val size: StrictlyPositiveInt by lazy(
asSet.size.toStrictlyPositiveInt()::getOrThrow
toSet().size.toStrictlyPositiveInt()::getOrThrow
)

/** Returns all elements of this set as a [Set] of type [E]. */
public fun toSet(): Set<E> {
val elements: Set<E> = setOf(head)
return tail?.let { elements + it.toSet() } ?: elements
}

/** Returns the string representation of this set. */
override fun toString(): String = "$asSet"
override fun toString(): String = "${toSet()}"
}

/**
Expand Down Expand Up @@ -77,7 +77,7 @@ internal class NotEmptySetSerializer<E>(elementSerializer: KSerializer<E>) :
}

override fun serialize(encoder: Encoder, value: NotEmptySet<E>): Unit =
encoder.encodeSerializableValue(delegate, value.asSet)
encoder.encodeSerializableValue(delegate, value.toSet())

override fun deserialize(decoder: Decoder): NotEmptySet<E> = decoder
.decodeSerializableValue(delegate)
Expand Down
3 changes: 2 additions & 1 deletion src/commonMain/kotlin/kotools/types/number/NonZeroInt.kt
Expand Up @@ -32,7 +32,8 @@ public sealed interface NonZeroInt : AnyInt {
min.toInt()..StrictlyNegativeInt.max.toInt(),
StrictlyPositiveInt.min.toInt()..max.toInt()
)
return ranges.asSet.random()
return ranges.toSet()
.random()
.random()
.toNonZeroInt()
.getOrThrow()
Expand Down
Expand Up @@ -17,7 +17,7 @@ class NotEmptyMapTest {
"c" to Random.nextInt()
)
assertEquals(
actual = notEmptyMap.entries.asSet,
actual = notEmptyMap.entries.toSet(),
expected = notEmptyMap.asMap.entries
)
}
Expand All @@ -30,7 +30,7 @@ class NotEmptyMapTest {
"c" to Random.nextInt()
)
assertEquals(
actual = notEmptyMap.keys.asSet,
actual = notEmptyMap.keys.toSet(),
expected = notEmptyMap.asMap.keys
)
}
Expand Down
19 changes: 10 additions & 9 deletions src/commonTest/kotlin/kotools/types/collection/NotEmptySetTest.kt
Expand Up @@ -20,7 +20,7 @@ class NotEmptySetTest {
.asNotEmptySet
.getOrThrow()
val result: Int = elements.head
result shouldEqual elements.asSet.first()
result shouldEqual elements.toSet().first()
}

@Test
Expand All @@ -29,7 +29,8 @@ class NotEmptySetTest {
.asNotEmptySet
.getOrThrow()
val result: NotEmptySet<Int>? = elements.tail
result.shouldBeNotNull().asSet contentShouldEqual elements.asSet.drop(1)
val expected: List<Int> = elements.toSet().drop(1)
result.shouldBeNotNull().toSet() contentShouldEqual expected
}

@Test
Expand All @@ -42,7 +43,7 @@ class NotEmptySetTest {
fun asSet_should_return_all_elements_as_a_Set() {
val elements: Set<Int> = List(3) { Random.nextInt() }
.toSet()
val result: Set<Int> = elements.asNotEmptySet.getOrThrow().asSet
val result: Set<Int> = elements.asNotEmptySet.getOrThrow().toSet()
result contentShouldEqual elements
}

Expand All @@ -52,22 +53,22 @@ class NotEmptySetTest {
.asNotEmptySet
.getOrThrow()
val result: StrictlyPositiveInt = elements.size
result.toInt() shouldEqual elements.asSet.size
result.toInt() shouldEqual elements.toSet().size
}

@Test
fun toString_should_behave_like_a_Set() {
val elements: NotEmptySet<Int> = List(3) { Random.nextInt() }
.asNotEmptySet
.getOrThrow()
"$elements" shouldEqual "${elements.asSet}"
"$elements" shouldEqual "${elements.toSet()}"
}

@Test
fun collection_asNotEmptySet_should_pass_with_a_not_empty_Collection() {
val elements: List<Int> = List(3) { Random.nextInt() }
val result: Result<NotEmptySet<Int>> = elements.asNotEmptySet
result.getOrThrow().asSet contentShouldEqual elements
result.getOrThrow().toSet() contentShouldEqual elements
}

@Test
Expand All @@ -84,7 +85,7 @@ class NotEmptySetTest {
val tail: Array<Int> = List(2) { Random.nextInt() }
.toTypedArray()
val result: NotEmptySet<Int> = notEmptySetOf(head, *tail)
result.asSet contentShouldEqual listOf(head) + tail
result.toSet() contentShouldEqual listOf(head) + tail
}
}

Expand All @@ -104,15 +105,15 @@ class NotEmptySetSerializerTest {
.asNotEmptySet
.getOrThrow()
val result: String = Json.encodeToString(elements)
result shouldEqual Json.encodeToString(elements.asSet)
result shouldEqual Json.encodeToString(elements.toSet())
}

@Test
fun deserialization_should_pass_with_a_not_empty_Collection() {
val collection: Collection<Int> = List(3) { Random.nextInt() }
val encoded: String = Json.encodeToString(collection)
val result: NotEmptySet<Int> = Json.decodeFromString(encoded)
result.asSet contentShouldEqual collection
result.toSet() contentShouldEqual collection
}

@Test
Expand Down

0 comments on commit 05547d8

Please sign in to comment.