fix #77 Instant.toJson
This commit is contained in:
parent
a31befef0b
commit
080eac2e1a
@ -21,6 +21,8 @@ import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.datetime.isDistantFuture
|
||||
import kotlinx.datetime.isDistantPast
|
||||
import kotlinx.serialization.json.JsonElement
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
import net.sergeych.lyng.Scope
|
||||
import net.sergeych.lynon.LynonDecoder
|
||||
import net.sergeych.lynon.LynonEncoder
|
||||
@ -89,6 +91,9 @@ class ObjInstant(val instant: Instant,val truncateMode: LynonSettings.InstantTru
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun toJson(scope: Scope): JsonElement = JsonPrimitive(instant.toString())
|
||||
|
||||
|
||||
companion object {
|
||||
val distantFuture by lazy {
|
||||
ObjInstant(Instant.DISTANT_FUTURE)
|
||||
|
||||
@ -23,6 +23,8 @@ import kotlinx.coroutines.flow.toList
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.withTimeout
|
||||
import kotlinx.datetime.Clock
|
||||
import kotlinx.datetime.Instant
|
||||
import kotlinx.serialization.Serializable
|
||||
import net.sergeych.lyng.*
|
||||
import net.sergeych.lyng.obj.*
|
||||
@ -3842,6 +3844,25 @@ class ScriptTest {
|
||||
assertEquals(JSTest1("bar", 1, true), x.decodeSerializable<JSTest1>())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testJsonTime() = runTest {
|
||||
val now = Clock.System.now()
|
||||
val x = eval("""
|
||||
import lyng.time
|
||||
Instant.now().truncateToSecond()
|
||||
""".trimIndent()).decodeSerializable<Instant>()
|
||||
println(x)
|
||||
assertIs<Instant>(x)
|
||||
assertTrue( (now - x).absoluteValue < 2.seconds)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testJsonNull() = runTest {
|
||||
val x = eval("""null""".trimIndent()).decodeSerializable<Instant?>()
|
||||
println(x)
|
||||
assertNull(x)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testInstanceVars() = runTest {
|
||||
var x = eval(
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user