Skip to content

Commit

Permalink
Added tests for Option (#27)
Browse files Browse the repository at this point in the history
Made Some a data class for equals impl
  • Loading branch information
aballano authored and raulraja committed Mar 25, 2017
1 parent c2c8e15 commit 0094fd4
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 39 deletions.
10 changes: 5 additions & 5 deletions katz/src/main/kotlin/katz/Option.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ sealed class Option<out A> {
* 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 <B> map(f: (A) -> B): Option<B> = fold({ None }, { a -> Some(f(a)) })

Expand Down Expand Up @@ -112,7 +112,7 @@ sealed class Option<out A> {
*/
inline fun forall(p: (A) -> Boolean): Boolean = exists(p)

class Some<A>(val value: A) : Option<A>() {
data class Some<A>(val value: A) : Option<A>() {
override val isEmpty = false
}

Expand Down
34 changes: 0 additions & 34 deletions katz/src/test/kotlin/katz/KatsTests.kt

This file was deleted.

70 changes: 70 additions & 0 deletions katz/src/test/kotlin/katz/OptionTest.kt
Original file line number Diff line number Diff line change
@@ -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<Int> = 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<String> = 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<String> = Some(value)
result.fold(
{ fail("None should not be called") },
{ value }
) shouldBe value
}
}
}

0 comments on commit 0094fd4

Please sign in to comment.