Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide test coverage for Option.kt (#2894) #2934

Merged
merged 3 commits into from
Feb 21, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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<Int, String>> = Pair(a, b).toOption()
op.unzip() shouldBe Pair(Option(a), Option(b))
}
}

"unzip None values" {
val op: Option<Pair<Int, String>> = None
op.unzip() shouldBe Pair(None, None)
}

"unzip with function" {
val f: (Int) -> Pair<String, Long> = { 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<CharSequence> = 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
Expand Down