BAEL-892: Example of Sealed Classes (#2373)

This commit is contained in:
Graham Cox 2017-08-04 19:52:00 +01:00 committed by Grzegorz Piwowarek
parent b5835c574f
commit 6a9e13cc03
2 changed files with 103 additions and 0 deletions

View File

@ -0,0 +1,19 @@
package com.baeldung.kotlin
sealed class Result<out S, out F> {
abstract fun <R> map(func: (S) -> R) : Result<R, F>
abstract fun <R> mapFailure(func: (F) -> R) : Result<S, R>
abstract fun get() : S?
}
data class Success<out S, out F>(val success: S) : Result<S, F>() {
override fun <R> map(func: (S) -> R) : Result<R, F> = Success(func(success))
override fun <R> mapFailure(func: (F) -> R): Result<S, R> = Success(success)
override fun get(): S? = success
}
data class Failure<out S, out F>(val failure: F) : Result<S, F>() {
override fun <R> map(func: (S) -> R) : Result<R, F> = Failure(failure)
override fun <R> mapFailure(func: (F) -> R): Result<S, R> = Failure(func(failure))
override fun get(): S? = null
}

View File

@ -0,0 +1,84 @@
package com.baeldung.kotlin
import org.junit.Assert
import org.junit.Test
class SealedTest {
fun divide(a: Int, b: Int) : Result<Float, String> = when (b) {
0 -> Failure("Division by zero")
else -> Success(a.toFloat() / b)
}
@Test
fun testSuccess() {
val result = divide(10, 5)
Assert.assertEquals(Success<Float, String>(2.0f), result)
}
@Test
fun testError() {
val result = divide(10, 0)
Assert.assertEquals(Failure<Float, String>("Division by zero"), result)
}
@Test
fun testMatchOnSuccess() {
val result = divide(10, 5)
when (result) {
is Success -> {
// Expected
}
is Failure -> Assert.fail("Expected Success")
}
}
@Test
fun testMatchOnError() {
val result = divide(10, 0)
when (result) {
is Failure -> {
// Expected
}
}
}
@Test
fun testGetSuccess() {
val result = divide(10, 5)
Assert.assertEquals(2.0f, result.get())
}
@Test
fun testGetError() {
val result = divide(10, 0)
Assert.assertNull(result.get())
}
@Test
fun testMapOnSuccess() {
val result = divide(10, 5)
.map { "Result: $it" }
Assert.assertEquals(Success<String, String>("Result: 2.0"), result)
}
@Test
fun testMapOnError() {
val result = divide(10, 0)
.map { "Result: $it" }
Assert.assertEquals(Failure<Float, String>("Division by zero"), result)
}
@Test
fun testMapFailureOnSuccess() {
val result = divide(10, 5)
.mapFailure { "Failure: $it" }
Assert.assertEquals(Success<Float, String>(2.0f), result)
}
@Test
fun testMapFailureOnError() {
val result = divide(10, 0)
.mapFailure { "Failure: $it" }
Assert.assertEquals(Failure<Float, String>("Failure: Division by zero"), result)
}
}