Skip to content

Commit 89205f6

Browse files
l2hyunwooserras
andauthored
Provide test coverage for NonEmptyList (#3135)
* [feature/add_non_empty_list_test] Add Method Tests - lastOrNull - extract * add nonemptylist test (operator plus function) * add coflatmap test * add foldLeft test * fix lint * fix foldLeft unit test logic --------- Co-authored-by: Alejandro Serrano <[email protected]>
1 parent fa93b17 commit 89205f6

File tree

1 file changed

+53
-4
lines changed

1 file changed

+53
-4
lines changed

arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/NonEmptyListTest.kt

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ import io.kotest.matchers.booleans.shouldBeTrue
1212
import io.kotest.matchers.nulls.shouldNotBeNull
1313
import io.kotest.matchers.shouldBe
1414
import io.kotest.property.Arb
15-
import io.kotest.property.arbitrary.boolean
16-
import io.kotest.property.arbitrary.int
17-
import io.kotest.property.arbitrary.negativeInt
18-
import io.kotest.property.arbitrary.pair
15+
import io.kotest.property.arbitrary.*
1916
import io.kotest.property.checkAll
2017
import kotlin.math.max
2118
import kotlin.math.min
@@ -445,4 +442,56 @@ class NonEmptyListTest : StringSpec({
445442
}
446443
}
447444
}
445+
446+
"lastOrNull" {
447+
checkAll(
448+
Arb.nonEmptyList(Arb.int())
449+
) { a ->
450+
val result = a.lastOrNull()
451+
val expected = a.last()
452+
result shouldBe expected
453+
}
454+
}
455+
456+
"extract" {
457+
checkAll(
458+
Arb.nonEmptyList(Arb.int())
459+
) { a ->
460+
val result = a.extract()
461+
val expected = a.head
462+
result shouldBe expected
463+
}
464+
}
465+
466+
"plus" {
467+
checkAll(
468+
Arb.nonEmptyList(Arb.int()),
469+
Arb.int()
470+
) { a, b ->
471+
val result = a + b
472+
val expected = a.all + b
473+
result shouldBe expected
474+
}
475+
}
476+
477+
"coflatMap should retain the same length as the original list" {
478+
checkAll(
479+
Arb.nonEmptyList(Arb.int())
480+
) { a ->
481+
val result = a.coflatMap { it.all }
482+
val expected = a.all
483+
result.size shouldBe expected.size
484+
}
485+
}
486+
487+
"foldLeft should sum up correctly for addition" {
488+
checkAll(
489+
Arb.nonEmptyList(Arb.int()),
490+
Arb.int()
491+
) { list, initial ->
492+
val result = list.foldLeft(initial) { acc, i -> acc + i }
493+
val expected = initial + list.all.sum()
494+
result shouldBe expected
495+
}
496+
}
448497
})

0 commit comments

Comments
 (0)