-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from connorwyatt/result
Add Result type
- Loading branch information
Showing
4 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
dependencies { | ||
testImplementation(testingLibraries.jUnit.jupiter) | ||
testImplementation(testingLibraries.strikt.core) | ||
|
||
testRuntimeOnly(testingLibraries.jUnit.jupiter.engine) | ||
} |
13 changes: 13 additions & 0 deletions
13
result/src/main/kotlin/io/connorwyatt/common/result/Result.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.connorwyatt.common.result | ||
|
||
sealed interface Result<out TSuccess, out TFailure> { | ||
fun getOrThrow(): TSuccess = | ||
when (this) { | ||
is Success -> value | ||
is Failure -> throw Exception("Result was a failure: $error") | ||
} | ||
|
||
data class Success<TSuccess>(val value: TSuccess) : Result<TSuccess, Nothing> | ||
|
||
data class Failure<TFailure>(val error: TFailure) : Result<Nothing, TFailure> | ||
} |
26 changes: 26 additions & 0 deletions
26
result/src/test/kotlin/io/connorwyatt/common/result/ResultTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.connorwyatt.common.result | ||
|
||
import org.junit.jupiter.api.Test | ||
import strikt.api.expectThat | ||
import strikt.api.expectThrows | ||
import strikt.assertions.isEqualTo | ||
|
||
class ResultTests { | ||
@Test | ||
fun `getOrThrow returns the value when Result is a success`() { | ||
val value = "value" | ||
|
||
val result = Result.Success(value) | ||
|
||
expectThat(result.getOrThrow()).isEqualTo(value) | ||
} | ||
|
||
@Test | ||
fun `getOrThrow throws when Result is a failure`() { | ||
val value = "value" | ||
|
||
val result = Result.Failure(value) | ||
|
||
expectThrows<Exception> { result.getOrThrow() } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters