diff --git a/katz/src/main/kotlin/katz/Option.kt b/katz/src/main/kotlin/katz/Option.kt index ab49d8a1efa..c6c7d13ebe7 100644 --- a/katz/src/main/kotlin/katz/Option.kt +++ b/katz/src/main/kotlin/katz/Option.kt @@ -38,11 +38,11 @@ sealed class Option { * Returns a $some containing the result of applying $f to this $option's * value if this $option is nonempty. Otherwise return $none. * - * @note This is similar to `flatMap` except here, - * $f does not need to wrap its result in an $option. + * @note This is similar to `flatMap` except here, + * $f does not need to wrap its result in an $option. * - * @param f the function to apply - * @see flatMap + * @param f the function to apply + * @see flatMap */ inline fun map(f: (A) -> B): Option = fold({ None }, { a -> Some(f(a)) }) @@ -112,7 +112,7 @@ sealed class Option { */ inline fun forall(p: (A) -> Boolean): Boolean = exists(p) - class Some(val value: A) : Option() { + data class Some(val value: A) : Option() { override val isEmpty = false } diff --git a/katz/src/test/kotlin/katz/KatsTests.kt b/katz/src/test/kotlin/katz/KatsTests.kt deleted file mode 100644 index 762abce1a7b..00000000000 --- a/katz/src/test/kotlin/katz/KatsTests.kt +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (C) 2017 The Katz Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package katz - -import io.kotlintest.KTestJUnitRunner -import org.junit.Test -import org.junit.runner.RunWith - -/** - * - */ -@RunWith(KTestJUnitRunner::class) -class KatzTests : UnitSpec() { - init { - "String.length" should "return the length of the string" { - "Katz".length shouldBe 4 - "".length shouldBe 0 - } - } -} diff --git a/katz/src/test/kotlin/katz/OptionTest.kt b/katz/src/test/kotlin/katz/OptionTest.kt new file mode 100644 index 00000000000..3407d8f5851 --- /dev/null +++ b/katz/src/test/kotlin/katz/OptionTest.kt @@ -0,0 +1,70 @@ +/* + * Copyright (C) 2017 The Kats Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package katz + +import io.kotlintest.KTestJUnitRunner +import katz.Option.Some +import katz.Option.None +import org.junit.runner.RunWith + +@RunWith(KTestJUnitRunner::class) +class OptionTest : UnitSpec() { + init { + + "map" should "modify value" { + Some(12).map { "flower" } shouldBe Some("flower") + None.map { "flower" } shouldBe None + } + + "flatMap" should "modify entity" { + Some(1).flatMap { None } shouldBe None + Some(1).flatMap { Some("something") } shouldBe Some("something") + None.flatMap { Some("something") } shouldBe None + } + + "getOrElse" should "return value" { + Some(12).getOrElse { 17 } shouldBe 12 + None.getOrElse { 17 } shouldBe 17 + } + + "exits" should "evaluate value" { + val none: Option = None + + Some(12).exists { it > 10 } shouldBe true + Some(7).exists { it > 10 } shouldBe false + none.exists { it > 10 } shouldBe false + } + + "fold" should "return default value on None" { + val exception = Exception() + val result: Option = None + result.fold( + { exception }, + { fail("Some should not be called") } + ) shouldBe exception + } + + "fold" should "call function on Some" { + val value = "Some value" + val result: Option = Some(value) + result.fold( + { fail("None should not be called") }, + { value } + ) shouldBe value + } + } +} \ No newline at end of file