Add JS targets for Kotlin Multiplatform (#36)

This commit is contained in:
Jan Müller 2020-11-29 22:34:08 +01:00 committed by GitHub
parent 0fdd0f2c2b
commit 0f90bb8b90
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 158 additions and 4 deletions

View File

@ -62,6 +62,11 @@ subprojects {
artifact(javadocJar.get())
}
}
js {
browser()
nodejs()
}
}
}

View File

@ -33,6 +33,12 @@ kotlin {
implementation(kotlin("test"))
}
}
val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}
}
}

View File

@ -26,7 +26,7 @@ class SuspendableBindingTest {
return Ok(2)
}
runBlockingTest {
return runBlockingTest {
val result = binding<Int, BindingError> {
val x = provideX().bind()
val y = provideY().bind()
@ -53,7 +53,7 @@ class SuspendableBindingTest {
return Ok(x + 2)
}
runBlockingTest {
return runBlockingTest {
val result = binding<Int, BindingError> {
val x = provideX().bind()
val y = provideY(x.toInt()).bind()
@ -85,7 +85,7 @@ class SuspendableBindingTest {
return Ok(2)
}
runBlockingTest {
return runBlockingTest {
val result = binding<Int, BindingError> {
val x = provideX().bind()
val y = provideY().bind()
@ -160,7 +160,7 @@ class SuspendableBindingTest {
return Ok(2)
}
runBlockingTest {
return runBlockingTest {
val result = binding<Int, BindingError> {
val x = provideX().bind()
val y = provideY().bind()

View File

@ -0,0 +1,10 @@
package com.github.michaelbull.result.coroutines
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.promise
import kotlin.coroutines.CoroutineContext
actual fun runBlockingTest(context: CoroutineContext, testBody: suspend CoroutineScope.() -> Unit): dynamic =
GlobalScope.promise(context) { testBody(this) }

View File

@ -0,0 +1,122 @@
package com.github.michaelbull.result.coroutines.binding
import com.github.michaelbull.result.Err
import com.github.michaelbull.result.Ok
import com.github.michaelbull.result.Result
import com.github.michaelbull.result.coroutines.runBlockingTest
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class AsyncSuspendableBindingTest {
private sealed class BindingError {
object BindingErrorA : BindingError()
object BindingErrorB : BindingError()
}
@Test
fun returnsOkIfAllBindsSuccessful(): dynamic {
suspend fun provideX(): Result<Int, BindingError> {
delay(100)
return Ok(1)
}
suspend fun provideY(): Result<Int, BindingError> {
delay(100)
return Ok(2)
}
return runBlockingTest {
val result = binding<Int, BindingError> {
val x = async { provideX().bind() }
val y = async { provideY().bind() }
x.await() + y.await()
}
assertTrue(result is Ok)
assertEquals(
expected = 3,
actual = result.value
)
}
}
@Test
fun returnsFirstErrIfBindingFailed(): dynamic {
suspend fun provideX(): Result<Int, BindingError> {
delay(10)
return Ok(1)
}
suspend fun provideY(): Result<Int, BindingError.BindingErrorA> {
delay(20)
return Err(BindingError.BindingErrorA)
}
suspend fun provideZ(): Result<Int, BindingError.BindingErrorB> {
delay(1)
return Err(BindingError.BindingErrorB)
}
return runBlockingTest {
val result = binding<Int, BindingError> {
val x = async { provideX().bind() }
val y = async { provideY().bind() }
val z = async { provideZ().bind() }
x.await() + y.await() + z.await()
}
assertTrue(result is Err)
assertEquals(
expected = BindingError.BindingErrorB,
actual = result.error
)
}
}
@Test
fun returnsStateChangedForOnlyTheFirstAsyncBindFailWhenEagerlyCancellingBinding(): dynamic {
var xStateChange = false
var yStateChange = false
var zStateChange = false
suspend fun provideX(): Result<Int, BindingError> {
delay(20)
xStateChange = true
return Ok(1)
}
suspend fun provideY(): Result<Int, BindingError.BindingErrorA> {
delay(10)
yStateChange = true
return Err(BindingError.BindingErrorA)
}
suspend fun provideZ(): Result<Int, BindingError.BindingErrorB> {
delay(1)
zStateChange = true
return Err(BindingError.BindingErrorB)
}
return runBlockingTest {
val result = binding<Int, BindingError> {
val x = async { provideX().bind() }
val y = async { provideY().bind() }
val z = async { provideZ().bind() }
x.await() + y.await() + z.await()
}
assertTrue(result is Err)
assertEquals(
expected = BindingError.BindingErrorB,
actual = result.error
)
assertFalse(xStateChange)
assertFalse(yStateChange)
assertTrue(zStateChange)
}
}
}

View File

@ -60,6 +60,14 @@ kotlin {
implementation("org.jetbrains.kotlinx:kotlinx.benchmark.runtime:${Versions.kotlinBenchmark}")
}
}
val jsMain by getting
val jsTest by getting {
dependencies {
implementation(kotlin("test-js"))
}
}
}
}

View File

@ -0,0 +1,3 @@
package com.github.michaelbull.result
internal actual object BindException : Exception()