summaryrefslogtreecommitdiff
path: root/runtime/commonTest/src/kotlinx/serialization/json/JsonTransientTest.kt
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/commonTest/src/kotlinx/serialization/json/JsonTransientTest.kt')
-rw-r--r--runtime/commonTest/src/kotlinx/serialization/json/JsonTransientTest.kt57
1 files changed, 57 insertions, 0 deletions
diff --git a/runtime/commonTest/src/kotlinx/serialization/json/JsonTransientTest.kt b/runtime/commonTest/src/kotlinx/serialization/json/JsonTransientTest.kt
new file mode 100644
index 00000000..8da3e3a9
--- /dev/null
+++ b/runtime/commonTest/src/kotlinx/serialization/json/JsonTransientTest.kt
@@ -0,0 +1,57 @@
+/*
+ * Copyright 2017-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
+ */
+@file:Suppress("EqualsOrHashCode")
+
+package kotlinx.serialization.json
+
+import kotlinx.serialization.*
+import kotlin.test.*
+
+class JsonTransientTest : JsonTestBase() {
+
+ @Serializable
+ class Data(val a: Int = 0, @Transient var b: Int = 42, val e: Boolean = false) {
+ var c = "Hello"
+
+ @Transient
+ val d: String
+ get() = "hello"
+
+ override fun equals(other: Any?): Boolean {
+ if (this === other) return true
+ if (other == null || this::class != other::class) return false
+
+ other as Data
+
+ if (a != other.a) return false
+ if (b != other.b) return false
+ if (c != other.c) return false
+ if (d != other.d) return false
+
+ return true
+ }
+
+ override fun toString(): String {
+ return "Data(a=$a, b=$b, e=$e, c='$c', d='$d')"
+ }
+ }
+
+ @Test
+ fun testAll() = parametrizedTest { useStreaming ->
+ assertEquals("{a:0,e:false,c:Hello}", unquoted.stringify(Data.serializer(), Data(), useStreaming))
+ }
+
+ @Test
+ fun testMissingOptionals() = parametrizedTest { useStreaming ->
+ assertEquals(strict.parse(Data.serializer(), "{a:0,c:Hello}", useStreaming), Data())
+ assertEquals(strict.parse(Data.serializer(), "{a:0}", useStreaming), Data())
+ }
+
+ @Test
+ fun testThrowTransient() = parametrizedTest { useStreaming ->
+ assertFailsWith(JsonUnknownKeyException::class) {
+ strict.parse(Data.serializer(), "{a:0,b:100500,c:Hello}", useStreaming)
+ }
+ }
+}