Add multi-platform support
Converts the unit tests to use kotlin-test, allowing them to target both the jvm and js platforms.
This commit is contained in:
parent
416c0950b2
commit
9ddac98e0c
39
build.gradle
39
build.gradle
@ -7,15 +7,16 @@ buildscript {
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
|
||||
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokkaVersion"
|
||||
classpath "org.junit.platform:junit-platform-gradle-plugin:$junitPlatformVersion"
|
||||
classpath "net.researchgate:gradle-release:$gradleReleaseVersion"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin'
|
||||
plugins {
|
||||
id 'com.github.ben-manes.versions' version '0.17.0'
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin-platform-common'
|
||||
apply plugin: 'maven-publish'
|
||||
apply plugin: 'org.jetbrains.dokka'
|
||||
apply plugin: 'org.junit.platform.gradle.plugin'
|
||||
apply plugin: 'net.researchgate.release'
|
||||
|
||||
description = 'A Result monad for modelling success or failure operations.'
|
||||
@ -25,31 +26,9 @@ repositories {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib-jre8:$kotlinVersion"
|
||||
testCompile "com.natpryce:hamkrest:$hamkrestVersion"
|
||||
testCompile "org.junit.jupiter:junit-jupiter-params:$junitVersion"
|
||||
testCompile "org.junit.jupiter:junit-jupiter-api:$junitVersion"
|
||||
testRuntime "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
kotlinOptions.jvmTarget = "1.8"
|
||||
}
|
||||
|
||||
compileTestKotlin {
|
||||
kotlinOptions.jvmTarget = "1.8"
|
||||
}
|
||||
|
||||
dokka {
|
||||
outputFormat = 'javadoc'
|
||||
outputDirectory = "$buildDir/docs"
|
||||
}
|
||||
|
||||
task javadocJar(type: Jar, dependsOn: dokka) {
|
||||
group = LifecycleBasePlugin.BUILD_GROUP
|
||||
description = 'Assembles a jar archive containing the Javadoc API documentation.'
|
||||
classifier = 'javadoc'
|
||||
from dokka.outputDirectory
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlinVersion"
|
||||
testCompile "org.jetbrains.kotlin:kotlin-test-annotations-common:$kotlinVersion"
|
||||
testCompile "org.jetbrains.kotlin:kotlin-test-common:$kotlinVersion"
|
||||
}
|
||||
|
||||
task sourcesJar(type: Jar) {
|
||||
@ -62,8 +41,6 @@ task sourcesJar(type: Jar) {
|
||||
publishing {
|
||||
publications {
|
||||
mavenJava(MavenPublication) {
|
||||
from components.java
|
||||
artifact javadocJar
|
||||
artifact sourcesJar
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,5 @@ version=1.0.3-SNAPSHOT
|
||||
dokkaVersion=0.9.15
|
||||
gradleReleaseVersion=2.6.0
|
||||
hamkrestVersion=1.4.2.0
|
||||
junitVersion=5.0.1
|
||||
junitPlatformVersion=1.0.0
|
||||
junitVersion=4.12
|
||||
kotlinVersion=1.2.10
|
||||
|
11
js/build.gradle
Normal file
11
js/build.gradle
Normal file
@ -0,0 +1,11 @@
|
||||
apply plugin: 'kotlin-platform-js'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlinVersion"
|
||||
expectedBy project(":")
|
||||
testCompile "org.jetbrains.kotlin:kotlin-test-js:$kotlinVersion"
|
||||
}
|
35
jvm/build.gradle
Normal file
35
jvm/build.gradle
Normal file
@ -0,0 +1,35 @@
|
||||
apply plugin: 'kotlin-platform-jvm'
|
||||
apply plugin: 'org.jetbrains.dokka'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
|
||||
expectedBy project(":")
|
||||
testCompile "junit:junit:$junitVersion"
|
||||
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlinVersion"
|
||||
testCompile "org.jetbrains.kotlin:kotlin-test:$kotlinVersion"
|
||||
}
|
||||
|
||||
dokka {
|
||||
outputFormat = 'javadoc'
|
||||
outputDirectory = "$buildDir/docs"
|
||||
}
|
||||
|
||||
task javadocJar(type: Jar, dependsOn: dokka) {
|
||||
group = LifecycleBasePlugin.BUILD_GROUP
|
||||
description = 'Assembles a jar archive containing the Javadoc API documentation.'
|
||||
classifier = 'javadoc'
|
||||
from dokka.outputDirectory
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
mavenJava(MavenPublication) {
|
||||
from components.java
|
||||
artifact javadocJar
|
||||
}
|
||||
}
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
rootProject.name = 'kotlin-result'
|
||||
|
||||
include 'js', 'jvm'
|
||||
|
@ -1,34 +1,45 @@
|
||||
package com.github.michaelbull.result
|
||||
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
import com.natpryce.hamkrest.sameInstance
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertSame
|
||||
|
||||
internal class AndTest {
|
||||
private object AndError
|
||||
|
||||
internal class `and` {
|
||||
@Test
|
||||
internal fun `and should return the result value if ok`() {
|
||||
val value = Ok(230).and(Ok(500)).get()
|
||||
assertThat(value, equalTo(500))
|
||||
internal fun returnsValueIfOk() {
|
||||
assertEquals(
|
||||
expected = 500,
|
||||
actual = Ok(230).and(Ok(500)).get()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `and should return the result value if not ok`() {
|
||||
val error = Ok(300).and(Err("hello world")).getError()
|
||||
assertThat(error, equalTo("hello world"))
|
||||
internal fun returnsValueIfErr() {
|
||||
assertEquals(
|
||||
expected = "hello world",
|
||||
actual = Ok(300).and(Err("hello world")).getError()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `andThen` {
|
||||
@Test
|
||||
internal fun returnsTransformedValueIfOk() {
|
||||
assertEquals(
|
||||
expected = 12,
|
||||
actual = Ok(5).andThen { Ok(it + 7) }.get()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `andThen should return the transformed result value if ok`() {
|
||||
val value = Ok(5).andThen { Ok(it + 7) }.get()
|
||||
assertThat(value, equalTo(12))
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `andThen should return the result error if not ok`() {
|
||||
val error = Ok(20).andThen { Ok(it + 43) }.andThen { Err(AndError) }.getError()!!
|
||||
assertThat(error, sameInstance(AndError))
|
||||
internal fun returnsErrorIfErr() {
|
||||
assertSame(
|
||||
expected = AndError,
|
||||
actual = Ok(20).andThen { Ok(it + 43) }.andThen { Err(AndError) }.getError()!!
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,79 +1,109 @@
|
||||
package com.github.michaelbull.result
|
||||
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNull
|
||||
|
||||
internal class GetTest {
|
||||
internal class `get` {
|
||||
@Test
|
||||
internal fun `get should return the result value if ok`() {
|
||||
val value = Ok(12).get()
|
||||
assertThat(value, equalTo(12))
|
||||
internal fun returnsValueIfOk() {
|
||||
assertEquals(
|
||||
expected = 12,
|
||||
actual = Ok(12).get()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `get should return null if not ok`() {
|
||||
val value = Err("error").get()
|
||||
assertThat(value, equalTo(null))
|
||||
internal fun returnsNullIfErr() {
|
||||
assertNull(Err("error").get())
|
||||
}
|
||||
}
|
||||
|
||||
internal class `getError` {
|
||||
@Test
|
||||
internal fun returnsNullIfOk() {
|
||||
assertNull(Ok("example").getError())
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `getError should return null if ok`() {
|
||||
val error = Ok("example").getError()
|
||||
assertThat(error, equalTo(null))
|
||||
internal fun returnsErrorIfErr() {
|
||||
assertEquals(
|
||||
expected = "example",
|
||||
actual = Err("example").getError()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `getOr` {
|
||||
@Test
|
||||
internal fun returnsValueIfOk() {
|
||||
assertEquals(
|
||||
expected = "hello",
|
||||
actual = Ok("hello").getOr("world")
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `getError should return the result error if not ok`() {
|
||||
val error = Err("example").getError()
|
||||
assertThat(error, equalTo("example"))
|
||||
internal fun returnsDefaultValueIfErr() {
|
||||
assertEquals(
|
||||
expected = "default",
|
||||
actual = Err("error").getOr("default")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `getErrorOr` {
|
||||
@Test
|
||||
internal fun returnsDefaultValueIfOk() {
|
||||
assertEquals(
|
||||
expected = "world",
|
||||
actual = Ok("hello").getErrorOr("world")
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `getOr should return the result value if ok`() {
|
||||
val value = Ok("hello").getOr("world")
|
||||
assertThat(value, equalTo("hello"))
|
||||
internal fun returnsErrorIfErr() {
|
||||
assertEquals(
|
||||
expected = "hello",
|
||||
actual = Err("hello").getErrorOr("world")
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `getOrElse` {
|
||||
@Test
|
||||
internal fun returnsValueIfOk() {
|
||||
assertEquals(
|
||||
expected = "hello",
|
||||
actual = Ok("hello").getOrElse { "world" }
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `getOr should return default value if not ok`() {
|
||||
val value = Err("error").getOr("default")
|
||||
assertThat(value, equalTo("default"))
|
||||
internal fun returnsTransformedErrorIfErr() {
|
||||
assertEquals(
|
||||
expected = "world",
|
||||
actual = Err("hello").getOrElse { "world" }
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `getErrorOrElse` {
|
||||
@Test
|
||||
internal fun returnsTransformedValueIfOk() {
|
||||
assertEquals(
|
||||
expected = "world",
|
||||
actual = Ok("hello").getErrorOrElse { "world" }
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `getErrorOr should return the default value if ok`() {
|
||||
val error = Ok("hello").getErrorOr("world")
|
||||
assertThat(error, equalTo("world"))
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `getErrorOr should return the result error if not ok`() {
|
||||
val error = Err("hello").getErrorOr("world")
|
||||
assertThat(error, equalTo("hello"))
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `getOrElse should return the result value if ok`() {
|
||||
val value = Ok("hello").getOrElse { "world" }
|
||||
assertThat(value, equalTo("hello"))
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `getOrElse should return the transformed result error if ok`() {
|
||||
val value = Err("hello").getOrElse { "world" }
|
||||
assertThat(value, equalTo("world"))
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `getErrorOrElse should return the transformed result value if ok`() {
|
||||
val error = Ok("hello").getErrorOrElse { "world" }
|
||||
assertThat(error, equalTo("world"))
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `getErrorOrElse should return the result error if not ok`() {
|
||||
val error = Err("hello").getErrorOrElse { "world" }
|
||||
assertThat(error, equalTo("hello"))
|
||||
internal fun returnsErrorIfErr() {
|
||||
assertEquals(
|
||||
expected = "hello",
|
||||
actual = Err("hello").getErrorOrElse { "world" }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,8 @@
|
||||
package com.github.michaelbull.result
|
||||
|
||||
import com.natpryce.hamkrest.Matcher
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
import com.natpryce.hamkrest.sameInstance
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertSame
|
||||
|
||||
internal class IterableTest {
|
||||
private sealed class IterableError {
|
||||
@ -12,12 +10,9 @@ internal class IterableTest {
|
||||
object IterableError2 : IterableError()
|
||||
}
|
||||
|
||||
private fun sameError(error: IterableError): Matcher<IterableError> {
|
||||
return sameInstance(error)
|
||||
}
|
||||
|
||||
internal class `fold` {
|
||||
@Test
|
||||
internal fun `fold should return the accumulated value if ok`() {
|
||||
internal fun returnAccumulatedValueIfOk() {
|
||||
val result = listOf(20, 30, 40, 50).fold(
|
||||
initial = 10,
|
||||
operation = { a, b -> Ok(a + b) }
|
||||
@ -25,11 +20,14 @@ internal class IterableTest {
|
||||
|
||||
result as Ok
|
||||
|
||||
assertThat(result.value, equalTo(150))
|
||||
assertEquals(
|
||||
expected = 150,
|
||||
actual = result.value
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `fold should return the first error if not ok`() {
|
||||
internal fun returnsFirstErrorIfErr() {
|
||||
val result: Result<Int, IterableError> = listOf(5, 10, 15, 20, 25).fold(
|
||||
initial = 1,
|
||||
operation = { a, b ->
|
||||
@ -43,12 +41,16 @@ internal class IterableTest {
|
||||
|
||||
result as Err
|
||||
|
||||
val matcher: Matcher<IterableError> = sameInstance(IterableError.IterableError1)
|
||||
assertThat(result.error, matcher)
|
||||
assertSame(
|
||||
expected = IterableError.IterableError1,
|
||||
actual = result.error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `foldRight` {
|
||||
@Test
|
||||
internal fun `foldRight should return the accumulated value if ok`() {
|
||||
internal fun returnsAccumulatedValueIfOk() {
|
||||
val result = listOf(2, 5, 10, 20).foldRight(
|
||||
initial = 100,
|
||||
operation = { a, b -> Ok(b - a) }
|
||||
@ -56,11 +58,14 @@ internal class IterableTest {
|
||||
|
||||
result as Ok
|
||||
|
||||
assertThat(result.value, equalTo(63))
|
||||
assertEquals(
|
||||
expected = 63,
|
||||
actual = result.value
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `foldRight should return the last error if not ok`() {
|
||||
internal fun returnsLastErrorIfErr() {
|
||||
val result = listOf(2, 5, 10, 20, 40).foldRight(
|
||||
initial = 38500,
|
||||
operation = { a, b ->
|
||||
@ -74,25 +79,45 @@ internal class IterableTest {
|
||||
|
||||
result as Err
|
||||
|
||||
assertThat(result.error, sameError(IterableError.IterableError2))
|
||||
assertSame(
|
||||
expected = IterableError.IterableError2,
|
||||
actual = result.error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `combine` {
|
||||
@Test
|
||||
internal fun `combine should return the combined list of values if results are ok`() {
|
||||
internal fun returnsValuesIfAllOk() {
|
||||
val values = combine(
|
||||
Ok(10),
|
||||
Ok(20),
|
||||
Ok(30)
|
||||
).get()!!
|
||||
|
||||
assertThat(values.size, equalTo(3))
|
||||
assertThat(values[0], equalTo(10))
|
||||
assertThat(values[1], equalTo(20))
|
||||
assertThat(values[2], equalTo(30))
|
||||
assertEquals(
|
||||
expected = 3,
|
||||
actual = values.size
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expected = 10,
|
||||
actual = values[0]
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expected = 20,
|
||||
actual = values[1]
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expected = 30,
|
||||
actual = values[2]
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `combine should return the first error if results are not ok`() {
|
||||
internal fun returnsFirstErrorIfErr() {
|
||||
val result = combine(
|
||||
Ok(20),
|
||||
Ok(40),
|
||||
@ -104,11 +129,16 @@ internal class IterableTest {
|
||||
|
||||
result as Err
|
||||
|
||||
assertThat(result.error, sameError(IterableError.IterableError1))
|
||||
assertSame(
|
||||
expected = IterableError.IterableError1,
|
||||
actual = result.error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `getAll` {
|
||||
@Test
|
||||
internal fun `getAll should return all of the result values`() {
|
||||
internal fun returnsAllValues() {
|
||||
val values = getAll(
|
||||
Ok("hello"),
|
||||
Ok("big"),
|
||||
@ -118,15 +148,36 @@ internal class IterableTest {
|
||||
Ok("world")
|
||||
)
|
||||
|
||||
assertThat(values.size, equalTo(4))
|
||||
assertThat(values[0], equalTo("hello"))
|
||||
assertThat(values[1], equalTo("big"))
|
||||
assertThat(values[2], equalTo("wide"))
|
||||
assertThat(values[3], equalTo("world"))
|
||||
assertEquals(
|
||||
expected = 4,
|
||||
actual = values.size
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expected = "hello",
|
||||
actual = values[0]
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expected = "big",
|
||||
actual = values[1]
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expected = "wide",
|
||||
actual = values[2]
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expected = "world",
|
||||
actual = values[3]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `getAllErrors` {
|
||||
@Test
|
||||
internal fun `getAllErrors should return all of the result errors`() {
|
||||
internal fun returnsAllErrors() {
|
||||
val errors = getAllErrors(
|
||||
Err(IterableError.IterableError2),
|
||||
Ok("haskell"),
|
||||
@ -139,16 +190,41 @@ internal class IterableTest {
|
||||
Err(IterableError.IterableError2)
|
||||
)
|
||||
|
||||
assertThat(errors.size, equalTo(5))
|
||||
assertThat(errors[0], sameError(IterableError.IterableError2))
|
||||
assertThat(errors[1], sameError(IterableError.IterableError2))
|
||||
assertThat(errors[2], sameError(IterableError.IterableError1))
|
||||
assertThat(errors[3], sameError(IterableError.IterableError1))
|
||||
assertThat(errors[4], sameError(IterableError.IterableError2))
|
||||
assertEquals(
|
||||
expected = 5,
|
||||
actual = errors.size
|
||||
)
|
||||
|
||||
assertSame(
|
||||
expected = IterableError.IterableError2,
|
||||
actual = errors[0]
|
||||
)
|
||||
|
||||
assertSame(
|
||||
expected = IterableError.IterableError2,
|
||||
actual = errors[1]
|
||||
)
|
||||
|
||||
assertSame(
|
||||
expected = IterableError.IterableError1,
|
||||
actual = errors[2]
|
||||
)
|
||||
|
||||
assertSame(
|
||||
expected = IterableError.IterableError1,
|
||||
actual = errors[3]
|
||||
)
|
||||
|
||||
assertSame(
|
||||
expected = IterableError.IterableError2,
|
||||
actual = errors[4]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `partition` {
|
||||
@Test
|
||||
internal fun `partition should return a pair of all the result values and errors`() {
|
||||
internal fun returnsPairOfValuesAndErrors() {
|
||||
val pairs = partition(
|
||||
Err(IterableError.IterableError2),
|
||||
Ok("haskell"),
|
||||
@ -162,18 +238,63 @@ internal class IterableTest {
|
||||
)
|
||||
|
||||
val values = pairs.first
|
||||
assertThat(values.size, equalTo(4))
|
||||
assertThat(values[0], equalTo("haskell"))
|
||||
assertThat(values[1], equalTo("f#"))
|
||||
assertThat(values[2], equalTo("elm"))
|
||||
assertThat(values[3], equalTo("clojure"))
|
||||
|
||||
assertEquals(
|
||||
expected = 4,
|
||||
actual = values.size
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expected = "haskell",
|
||||
actual = values[0]
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expected = "f#",
|
||||
actual = values[1]
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expected = "elm",
|
||||
actual = values[2]
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expected = "clojure",
|
||||
actual = values[3]
|
||||
)
|
||||
|
||||
val errors = pairs.second
|
||||
assertThat(errors.size, equalTo(5))
|
||||
assertThat(errors[0], sameError(IterableError.IterableError2))
|
||||
assertThat(errors[1], sameError(IterableError.IterableError2))
|
||||
assertThat(errors[2], sameError(IterableError.IterableError1))
|
||||
assertThat(errors[3], sameError(IterableError.IterableError1))
|
||||
assertThat(errors[4], sameError(IterableError.IterableError2))
|
||||
|
||||
assertEquals(
|
||||
expected = 5,
|
||||
actual = errors.size
|
||||
)
|
||||
|
||||
assertSame(
|
||||
expected = IterableError.IterableError2,
|
||||
actual = errors[0]
|
||||
)
|
||||
|
||||
assertSame(
|
||||
expected = IterableError.IterableError2,
|
||||
actual = errors[1]
|
||||
)
|
||||
|
||||
assertSame(
|
||||
expected = IterableError.IterableError1,
|
||||
actual = errors[2]
|
||||
)
|
||||
|
||||
assertSame(
|
||||
expected = IterableError.IterableError1,
|
||||
actual = errors[3]
|
||||
)
|
||||
|
||||
assertSame(
|
||||
expected = IterableError.IterableError2,
|
||||
actual = errors[4]
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,10 +1,8 @@
|
||||
package com.github.michaelbull.result
|
||||
|
||||
import com.natpryce.hamkrest.Matcher
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
import com.natpryce.hamkrest.sameInstance
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertSame
|
||||
|
||||
internal class MapTest {
|
||||
private sealed class MapError(val reason: String) {
|
||||
@ -13,34 +11,42 @@ internal class MapTest {
|
||||
class CustomError(reason: String) : MapError(reason)
|
||||
}
|
||||
|
||||
private fun sameError(error: MapError): Matcher<MapError> {
|
||||
return sameInstance(error)
|
||||
}
|
||||
|
||||
internal class `map` {
|
||||
@Test
|
||||
internal fun `map should return the transformed result value if ok`() {
|
||||
val value = Ok(10).map { it + 20 }.get()
|
||||
assertThat(value, equalTo(30))
|
||||
internal fun returnsTransformedValueIfOk() {
|
||||
assertEquals(
|
||||
expected = 30,
|
||||
actual = Ok(10).map { it + 20 }.get()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Suppress("UNREACHABLE_CODE")
|
||||
internal fun `map should return the result error if not ok`() {
|
||||
internal fun returnsErrorIfErr() {
|
||||
val result = Err(MapError.HelloError).map { "hello $it" }
|
||||
|
||||
result as Err
|
||||
|
||||
assertThat(result.error, sameError(MapError.HelloError))
|
||||
assertSame(
|
||||
expected = MapError.HelloError,
|
||||
actual = result.error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `mapError` {
|
||||
@Test
|
||||
internal fun `mapError should return the result value if ok`() {
|
||||
internal fun returnsValueIfOk() {
|
||||
val value = Ok(55).map { it + 15 }.mapError { MapError.WorldError }.get()
|
||||
assertThat(value, equalTo(70))
|
||||
|
||||
assertEquals(
|
||||
expected = 70,
|
||||
actual = value
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `mapError should return the transformed result error if not ok`() {
|
||||
internal fun returnsErrorIfErr() {
|
||||
val result: Result<String, MapError> = Ok("let")
|
||||
.map { "$it me" }
|
||||
.andThen {
|
||||
@ -53,34 +59,47 @@ internal class MapTest {
|
||||
|
||||
result as Err
|
||||
|
||||
assertThat(result.error.reason, equalTo("let me let me get what i want"))
|
||||
assertEquals(
|
||||
expected = "let me let me get what i want",
|
||||
actual = result.error.reason
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `mapBoth` {
|
||||
@Test
|
||||
@Suppress("UNREACHABLE_CODE")
|
||||
internal fun `mapBoth should return the transformed result value if ok`() {
|
||||
internal fun returnsTransformedValueIfOk() {
|
||||
val value = Ok("there is").mapBoth(
|
||||
success = { "$it a light" },
|
||||
failure = { "$it that never" }
|
||||
)
|
||||
|
||||
assertThat(value, equalTo("there is a light"))
|
||||
assertEquals(
|
||||
expected = "there is a light",
|
||||
actual = value
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
@Suppress("UNREACHABLE_CODE")
|
||||
internal fun `mapBoth should return the transformed result error if not ok`() {
|
||||
internal fun returnsTransformedErrorIfErr() {
|
||||
val error = Err(MapError.CustomError("this")).mapBoth(
|
||||
success = { "$it charming" },
|
||||
failure = { "${it.reason} man" }
|
||||
)
|
||||
|
||||
assertThat(error, equalTo("this man"))
|
||||
assertEquals(
|
||||
expected = "this man",
|
||||
actual = error
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `mapEither` {
|
||||
@Test
|
||||
@Suppress("UNREACHABLE_CODE")
|
||||
internal fun `mapEither should return the transformed result value if ok`() {
|
||||
internal fun returnsTransformedValueIfOk() {
|
||||
val result = Ok(500).mapEither(
|
||||
success = { it + 500 },
|
||||
failure = { MapError.CustomError("$it") }
|
||||
@ -88,11 +107,14 @@ internal class MapTest {
|
||||
|
||||
result as Ok
|
||||
|
||||
assertThat(result.value, equalTo(1000))
|
||||
assertEquals(
|
||||
expected = 1000,
|
||||
actual = result.value
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `mapEither should return the transformed result error if not ok`() {
|
||||
internal fun returnsTransformedErrorIfErr() {
|
||||
val result = Err("the reckless").mapEither(
|
||||
success = { "the wild youth" },
|
||||
failure = { MapError.CustomError("the truth") }
|
||||
@ -100,6 +122,10 @@ internal class MapTest {
|
||||
|
||||
result as Err
|
||||
|
||||
assertThat(result.error.reason, equalTo("the truth"))
|
||||
assertEquals(
|
||||
expected = "the truth",
|
||||
actual = result.error.reason
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,38 +1,61 @@
|
||||
package com.github.michaelbull.result
|
||||
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
internal class OnTest {
|
||||
object CounterError
|
||||
class Counter(var count: Int)
|
||||
|
||||
internal class `onSuccess` {
|
||||
@Test
|
||||
internal fun `onSuccess should invoke the callback when result is ok`() {
|
||||
internal fun invokesCallbackIfOk() {
|
||||
val counter = Counter(50)
|
||||
|
||||
Ok(counter).onSuccess { it.count += 50 }
|
||||
assertThat(counter.count, equalTo(100))
|
||||
|
||||
assertEquals(
|
||||
expected = 100,
|
||||
actual = counter.count
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `onSuccess should not invoke the callback when result is not ok`() {
|
||||
internal fun invokesNothingIfErr() {
|
||||
val counter = Counter(200)
|
||||
|
||||
Err(CounterError).onSuccess { counter.count -= 50 }
|
||||
assertThat(counter.count, equalTo(200))
|
||||
|
||||
assertEquals(
|
||||
expected = 200,
|
||||
actual = counter.count
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `onFailure` {
|
||||
@Test
|
||||
internal fun `onFailure should invoke the callback when result is not ok`() {
|
||||
internal fun invokesCallbackIfErr() {
|
||||
val counter = Counter(555)
|
||||
|
||||
Err(CounterError).onFailure { counter.count += 100 }
|
||||
assertThat(counter.count, equalTo(655))
|
||||
|
||||
assertEquals(
|
||||
expected = 655,
|
||||
actual = counter.count
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `onFailure should not invoke the callback when result is ok`() {
|
||||
internal fun invokesNothingIfOk() {
|
||||
val counter = Counter(1020)
|
||||
|
||||
Ok("hello").onFailure { counter.count = 1030 }
|
||||
assertThat(counter.count, equalTo(1020))
|
||||
|
||||
assertEquals(
|
||||
expected = 1020,
|
||||
actual = counter.count
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,33 +1,44 @@
|
||||
package com.github.michaelbull.result
|
||||
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
|
||||
internal class OrTest {
|
||||
private object OrError
|
||||
|
||||
internal class `or` {
|
||||
@Test
|
||||
internal fun `or should return the result value if ok`() {
|
||||
val value = Ok(500).or(Ok(1000)).get()
|
||||
assertThat(value, equalTo(500))
|
||||
internal fun returnsValueIfOk() {
|
||||
assertEquals(
|
||||
expected = 500,
|
||||
actual = Ok(500).or(Ok(1000)).get()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `or should return the default value if not ok`() {
|
||||
val value = Err(OrError).or(Ok(5000)).get()
|
||||
assertThat(value, equalTo(5000))
|
||||
internal fun returnsDefaultValueIfErr() {
|
||||
assertEquals(
|
||||
expected = 5000,
|
||||
actual = Err(OrError).or(Ok(5000)).get()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `orElse` {
|
||||
@Test
|
||||
internal fun returnsValueIfOk() {
|
||||
assertEquals(
|
||||
expected = 3000,
|
||||
actual = Ok(3000).orElse { Ok(4000) }.get()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `orElse should return the result value if ok`() {
|
||||
val value = Ok(3000).orElse { Ok(4000) }.get()
|
||||
assertThat(value, equalTo(3000))
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `orElse should return the transformed value if not ok`() {
|
||||
val value = Err(4000).orElse { Ok(2000) }.get()
|
||||
assertThat(value, equalTo(2000))
|
||||
internal fun returnsTransformedValueIfErr() {
|
||||
assertEquals(
|
||||
expected = 2000,
|
||||
actual = Err(4000).orElse { Ok(2000) }.get()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,69 +1,65 @@
|
||||
package com.github.michaelbull.result
|
||||
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.*
|
||||
|
||||
internal class ResultIteratorTest {
|
||||
internal class `hasNext` {
|
||||
@Test
|
||||
internal fun `hasNext should return true if unyielded and result is ok`() {
|
||||
internal fun returnsTrueIfUnyieldedAndOk() {
|
||||
val iterator = Ok("hello").iterator()
|
||||
assertThat(iterator.hasNext(), equalTo(true))
|
||||
assertTrue { iterator.hasNext() }
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `hasNext should return false if result is not ok`() {
|
||||
internal fun returnsFalseIfErr() {
|
||||
val iterator = Err("hello").iterator()
|
||||
assertThat(iterator.hasNext(), equalTo(false))
|
||||
assertFalse { iterator.hasNext() }
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `hasNext should return false if yielded`() {
|
||||
internal fun returnsFalseIfYielded() {
|
||||
val iterator = Ok("hello").iterator()
|
||||
iterator.next()
|
||||
assertThat(iterator.hasNext(), equalTo(false))
|
||||
assertFalse { iterator.hasNext() }
|
||||
}
|
||||
}
|
||||
|
||||
internal class `next` {
|
||||
@Test
|
||||
internal fun returnsValueIfUnyieldedAndOk() {
|
||||
assertEquals(
|
||||
expected = "hello",
|
||||
actual = Ok("hello").iterator().next()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `next should return the result value if unyielded and result is ok`() {
|
||||
val value = Ok("hello").iterator().next()
|
||||
assertThat(value, equalTo("hello"))
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `next should throw NoSuchElementException if unyielded and result is not ok`() {
|
||||
internal fun throwsExceptionIfUnyieldedAndErr() {
|
||||
val iterator = Err("hello").iterator()
|
||||
|
||||
assertThrows(NoSuchElementException::class.java) {
|
||||
iterator.next()
|
||||
}
|
||||
assertFailsWith<NoSuchElementException> { iterator.next() }
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `next should throw NoSuchElementException if yielded and result is ok`() {
|
||||
internal fun throwsExceptionIfYieldedAndOk() {
|
||||
val iterator = Ok("hello").iterator()
|
||||
iterator.next()
|
||||
|
||||
assertThrows(NoSuchElementException::class.java) {
|
||||
iterator.next()
|
||||
assertFailsWith<NoSuchElementException> { iterator.next() }
|
||||
}
|
||||
}
|
||||
|
||||
internal class `remove` {
|
||||
@Test
|
||||
internal fun `remove should make hasNext return false`() {
|
||||
internal fun makesHasNextReturnFalse() {
|
||||
val iterator = Ok("hello").mutableIterator()
|
||||
iterator.remove()
|
||||
assertThat(iterator.hasNext(), equalTo(false))
|
||||
assertFalse { iterator.hasNext() }
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `remove should make next throw NoSuchElementException`() {
|
||||
internal fun makesNextThrowException() {
|
||||
val iterator = Ok("hello").mutableIterator()
|
||||
iterator.remove()
|
||||
|
||||
assertThrows(NoSuchElementException::class.java) {
|
||||
iterator.next()
|
||||
assertFailsWith<NoSuchElementException> { iterator.next() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,25 +1,31 @@
|
||||
package com.github.michaelbull.result
|
||||
|
||||
import com.natpryce.hamkrest.Matcher
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
import com.natpryce.hamkrest.sameInstance
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertSame
|
||||
|
||||
internal class ResultTest {
|
||||
internal class `of` {
|
||||
@Test
|
||||
internal fun `of should return ok if invocation did not throw anything`() {
|
||||
internal fun returnsOkIfInvocationSuccessful() {
|
||||
val callback = { "example" }
|
||||
val value = Result.of(callback).get()
|
||||
assertThat(value, equalTo("example"))
|
||||
|
||||
assertEquals(
|
||||
expected = "example",
|
||||
actual = Result.of(callback).get()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `of should return error if invocation threw something`() {
|
||||
internal fun returnsErrIfInvocationFails() {
|
||||
val throwable = IllegalArgumentException("throw me")
|
||||
val callback = { throw throwable }
|
||||
val error = Result.of(callback).getError()!!
|
||||
val matcher: Matcher<Throwable> = sameInstance(throwable)
|
||||
assertThat(error, matcher)
|
||||
|
||||
assertSame(
|
||||
expected = throwable,
|
||||
actual = error
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,76 +1,83 @@
|
||||
package com.github.michaelbull.result
|
||||
|
||||
import com.natpryce.hamkrest.assertion.assertThat
|
||||
import com.natpryce.hamkrest.equalTo
|
||||
import org.junit.jupiter.api.Assertions.assertThrows
|
||||
import org.junit.jupiter.api.Test
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFailsWith
|
||||
|
||||
internal class UnwrapTest {
|
||||
internal class `unwrap` {
|
||||
@Test
|
||||
internal fun `unwrap should return the result value if ok`() {
|
||||
val value = Ok(5000).unwrap()
|
||||
assertThat(value, equalTo(5000))
|
||||
internal fun returnsValueIfOk() {
|
||||
assertEquals(
|
||||
expected = 5000,
|
||||
actual = Ok(5000).unwrap()
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `unwrap should throw an UnwrapException if not ok`() {
|
||||
val throwable = assertThrows(UnwrapException::class.java) {
|
||||
internal fun throwsExceptionIfErr() {
|
||||
assertFailsWith<UnwrapException>("called Result.wrap on an Err value 5000") {
|
||||
Err(5000).unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assertThat(throwable.message, equalTo("called Result.wrap on an Err value 5000"))
|
||||
internal class `expect` {
|
||||
@Test
|
||||
internal fun returnsValueIfOk() {
|
||||
assertEquals(
|
||||
expected = 1994,
|
||||
actual = Ok(1994).expect { "the year should be" }
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `expect should return the result value if ok`() {
|
||||
val value = Ok(1994).expect { "the year should be" }
|
||||
assertThat(value, equalTo(1994))
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `expect should throw an UnwrapException with a specified message if not ok`() {
|
||||
internal fun throwsExceptionIfErr() {
|
||||
val message = object {
|
||||
override fun toString() = "the year should be"
|
||||
}
|
||||
|
||||
val throwable = assertThrows(UnwrapException::class.java) {
|
||||
assertFailsWith<UnwrapException>("the year should be 1994") {
|
||||
Err(1994).expect { message }
|
||||
}
|
||||
|
||||
assertThat(throwable.message, equalTo("the year should be 1994"))
|
||||
}
|
||||
}
|
||||
|
||||
internal class `unwrapError` {
|
||||
@Test
|
||||
internal fun `unwrapError should throw an UnwrapException if ok`() {
|
||||
val throwable = assertThrows(UnwrapException::class.java) {
|
||||
internal fun throwsExceptionIfOk() {
|
||||
assertFailsWith<UnwrapException>("called Result.unwrapError on an Ok value example") {
|
||||
Ok("example").unwrapError()
|
||||
}
|
||||
|
||||
assertThat(throwable.message, equalTo("called Result.unwrapError on an Ok value example"))
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `unwrapError should return the result error if not ok`() {
|
||||
val error = Err("example").unwrapError()
|
||||
assertThat(error, equalTo("example"))
|
||||
internal fun returnsErrorIfErr() {
|
||||
assertEquals(
|
||||
expected = "example",
|
||||
actual = Err("example").unwrapError()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
internal class `expectError` {
|
||||
@Test
|
||||
internal fun `expectError should throw an UnwrapException with a specified message if ok`() {
|
||||
internal fun throwsExceptionIfOk() {
|
||||
val message = object {
|
||||
override fun toString() = "the year should be"
|
||||
}
|
||||
|
||||
val throwable = assertThrows(UnwrapException::class.java) {
|
||||
assertFailsWith<UnwrapException>("the year should be 2010") {
|
||||
Ok(2010).expectError { message }
|
||||
}
|
||||
|
||||
assertThat(throwable.message, equalTo("the year should be 2010"))
|
||||
}
|
||||
|
||||
@Test
|
||||
internal fun `expectError should return the result error if not ok`() {
|
||||
val error = Err(2010).expectError { "the year should be" }
|
||||
assertThat(error, equalTo(2010))
|
||||
internal fun returnsErrorIfErr() {
|
||||
assertEquals(
|
||||
expected = 2010,
|
||||
actual = Err(2010).expectError { "the year should be" }
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user