added opt tool
This commit is contained in:
parent
967558aed3
commit
bc726eada9
@ -10,7 +10,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "net.sergeych"
|
group = "net.sergeych"
|
||||||
version = "0.4.0-SNAPSHOT"
|
version = "0.4.1-SNAPSHOT"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
25
src/commonMain/kotlin/net.sergeych.tools/Opt.kt
Normal file
25
src/commonMain/kotlin/net.sergeych.tools/Opt.kt
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
package net.sergeych.tools
|
||||||
|
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
class Opt<T>(val value: T?) {
|
||||||
|
override fun hashCode(): Int {
|
||||||
|
return value.hashCode()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun equals(other: Any?): Boolean {
|
||||||
|
if (other !is Opt<*>) return false
|
||||||
|
if (value == null && other.value == null) return true
|
||||||
|
if( value == null || other.value == null) return false
|
||||||
|
return value == other.value
|
||||||
|
}
|
||||||
|
|
||||||
|
val isNull by lazy { value == null }
|
||||||
|
val isNotNull by lazy { value != null }
|
||||||
|
|
||||||
|
fun orElse(f: ()->T) = value ?: f()
|
||||||
|
|
||||||
|
override fun toString(): String = "Opt[$value]"
|
||||||
|
}
|
||||||
|
|
@ -4,17 +4,25 @@ import assertThrows
|
|||||||
import io.ktor.server.engine.*
|
import io.ktor.server.engine.*
|
||||||
import io.ktor.server.netty.*
|
import io.ktor.server.netty.*
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
import net.sergeych.mp_logger.Log
|
import net.sergeych.mp_logger.Log
|
||||||
|
import net.sergeych.tools.Opt
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
internal class WsServerKtTest {
|
internal class WsServerKtTest {
|
||||||
|
|
||||||
data class TestSession(var buzz: String = "BuZZ"): WithAdapter()
|
data class TestSession(var buzz: String = "BuZZ"): WithAdapter()
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
data class TestStruct(val foobar: String)
|
||||||
|
|
||||||
object TestApiServer: CommandHost<TestSession>() {
|
object TestApiServer: CommandHost<TestSession>() {
|
||||||
val foo by command<String,String>()
|
val foo by command<String,String>()
|
||||||
val ping by command<Unit,String>()
|
val ping by command<Unit,String>()
|
||||||
|
val nullable1 by command<Boolean, Opt<TestStruct>>()
|
||||||
|
val nullable2 by command<Boolean,String?>()
|
||||||
}
|
}
|
||||||
object TestApiClient: CommandHost<WithAdapter>() {
|
object TestApiClient: CommandHost<WithAdapter>() {
|
||||||
val bar by command<String,String>()
|
val bar by command<String,String>()
|
||||||
@ -45,6 +53,46 @@ internal class WsServerKtTest {
|
|||||||
client.close()
|
client.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
fun testWsServerNullables() {
|
||||||
|
|
||||||
|
embeddedServer(Netty, port = 8081) {
|
||||||
|
parsec3TransportServer(
|
||||||
|
TestApiServer,
|
||||||
|
) {
|
||||||
|
newSession { TestSession() }
|
||||||
|
on(api.foo) {
|
||||||
|
it + buzz + "-foo"
|
||||||
|
}
|
||||||
|
on(api.nullable2) {
|
||||||
|
if( it ) null else "not null"
|
||||||
|
}
|
||||||
|
on(api.nullable1) {
|
||||||
|
if( it ) Opt(null) else Opt(TestStruct("not null"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start(wait = false)
|
||||||
|
|
||||||
|
val client = Parsec3WSClient("ws://localhost:8081/api/p3", TestApiClient) {}
|
||||||
|
|
||||||
|
runBlocking {
|
||||||
|
val x = TestApiServer.foo.invoke(client.adapter(), "*great*")
|
||||||
|
assertEquals("*great*BuZZ-foo", x)
|
||||||
|
assertEquals(TestApiServer.nullable2.invoke(client.adapter(), true),null)
|
||||||
|
assertEquals(TestApiServer.nullable2.invoke(client.adapter(), false),"not null")
|
||||||
|
assertEquals(TestApiServer.nullable1.invoke(client.adapter(), true),Opt<TestStruct>(null))
|
||||||
|
|
||||||
|
assertTrue { TestApiServer.nullable1.invoke(client.adapter(), true).isNull }
|
||||||
|
assertTrue { TestApiServer.nullable1.invoke(client.adapter(), false).isNotNull }
|
||||||
|
assertEquals(TestApiServer.nullable1.invoke(client.adapter(), false),Opt(TestStruct("not null")))
|
||||||
|
assertTrue { TestApiServer.nullable1.invoke(client.adapter(), false).value == TestStruct("not null") }
|
||||||
|
client.close()
|
||||||
|
|
||||||
|
assertEquals("bar", Opt<String>("bar").orElse { "foobar" } )
|
||||||
|
assertEquals("foobar", Opt<String>(null).orElse { "foobar" } )
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@Test
|
@Test
|
||||||
fun testWsServerReconnect() {
|
fun testWsServerReconnect() {
|
||||||
|
Loading…
Reference in New Issue
Block a user