From 6b816881bff92c5f4985cb91eac8a2c1bbedda96 Mon Sep 17 00:00:00 2001 From: swdevsm <13076163+swdevsm@users.noreply.github.com> Date: Thu, 16 Feb 2023 20:33:43 +0300 Subject: [PATCH] Provide test coverage for Option.kt (#2894) --- .../kotlin/arrow/core/OptionTest.kt | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) diff --git a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/OptionTest.kt b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/OptionTest.kt index 2bbb47fa773..2a7c1dc44f1 100755 --- a/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/OptionTest.kt +++ b/arrow-libs/core/arrow-core/src/commonTest/kotlin/arrow/core/OptionTest.kt @@ -14,6 +14,7 @@ import io.kotest.property.arbitrary.boolean import io.kotest.property.arbitrary.int import io.kotest.property.arbitrary.long import io.kotest.property.arbitrary.orNull +import io.kotest.property.arbitrary.string import io.kotest.property.checkAll class OptionTest : StringSpec({ @@ -423,6 +424,105 @@ class OptionTest : StringSpec({ val exception = Exception("Boom!") Option.catch { throw exception } shouldBe None } + + "invoke operator should return Some" { + checkAll(Arb.int()) { a: Int -> + Option(a) shouldBe Some(a) + } + } + + "lift" { + val f: (Int) -> String = { a -> a.toString() } + val lifted = Option.lift(f) + checkAll(Arb.int()) { a: Int -> + lifted(Some(a)) shouldBe Some(a.toString()) + } + } + + "isNone should return true if None and false if Some" { + none.isNone() shouldBe true + none.isSome() shouldBe false + } + + "isSome should return true if Some and false if None" { + some.isSome() shouldBe true + some.isNone() shouldBe false + } + + "getOrNull" { + none.getOrNull() shouldBe null + some.getOrNull() shouldBe "kotlin" + } + + "isSome with predicate" { + some.isSome { it.startsWith('k') } shouldBe true + some.isSome { it.startsWith('j') } shouldBe false + none.isSome { it.startsWith('k') } shouldBe false + } + + "flatten" { + checkAll(Arb.int()) { a: Int -> + Some(Some(a)).flatten() shouldBe Some(a) + Some(None).flatten() shouldBe None + } + } + + "unzip Some values" { + checkAll(Arb.int(), Arb.string()) { a: Int, b: String -> + val op: Option> = Pair(a, b).toOption() + op.unzip() shouldBe Pair(Option(a), Option(b)) + } + } + + "unzip None values" { + val op: Option> = None + op.unzip() shouldBe Pair(None, None) + } + + "unzip with function" { + val f: (Int) -> Pair = { c -> Pair(c.toString(), c.toLong()) } + checkAll(Arb.int()) { c: Int -> + Option(c).unzip(f) shouldBe Pair(Option(c.toString()), Option(c.toLong())) + } + } + + "widen" { + checkAll(Arb.string()) { a: String -> + val widen: Option = Option(a).widen() + widen.map { it.length } shouldBe Some(a.length) + } + } + + "compareTo with Some values" { + checkAll(Arb.int(), Arb.int()) { a: Int, b: Int -> + val opA = Option(a) + val opB = Option(b) + (opA > opB) shouldBe (a > b) + (opA >= opB) shouldBe (a >= b) + (opA < opB) shouldBe (a < b) + (opA <= opB) shouldBe (a <= b) + (opA == opB) shouldBe (a == b) + (opA != opB) shouldBe (a != b) + } + } + + "compareTo with None values" { + val opA = Option(1) + val opB = None + (opA > opB) shouldBe true + (opA >= opB) shouldBe true + (opA < opB) shouldBe false + (opA <= opB) shouldBe false + (opA == opB) shouldBe false + (opA != opB) shouldBe true + + (none > some) shouldBe false + (none >= some) shouldBe false + (none < some) shouldBe true + (none <= some) shouldBe true + (none == some) shouldBe false + (none != some) shouldBe true + } }) // Utils