From 41eb2341f7c90646b6b5cd5933009ff4d1f6eb2b Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Wed, 27 Nov 2019 22:43:21 +0600 Subject: [PATCH] hotfix and test adding --- .../sdi/utils/JsonAdaptations.kt | 2 +- .../com/insanusmokrassar/sdi/ListTest.kt | 54 +++++++++++++++++++ .../{DeserializationTest.kt => SimpleTest.kt} | 18 +++---- 3 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 src/commonTest/kotlin/com/insanusmokrassar/sdi/ListTest.kt rename src/commonTest/kotlin/com/insanusmokrassar/sdi/{DeserializationTest.kt => SimpleTest.kt} (65%) diff --git a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt index b37d303..de4aedd 100644 --- a/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt +++ b/src/commonMain/kotlin/com/insanusmokrassar/sdi/utils/JsonAdaptations.kt @@ -72,7 +72,7 @@ internal fun createModuleBasedOnConfigRoot(jsonObject: JsonObject): Json { DependencyResolver(this, kclass) { caches.getValue(it).invoke() } - } catch (e: SerializationException) { + } catch (e: SerializerAlreadyRegisteredException) { // here we are thinking that already registered } } diff --git a/src/commonTest/kotlin/com/insanusmokrassar/sdi/ListTest.kt b/src/commonTest/kotlin/com/insanusmokrassar/sdi/ListTest.kt new file mode 100644 index 0000000..c93983f --- /dev/null +++ b/src/commonTest/kotlin/com/insanusmokrassar/sdi/ListTest.kt @@ -0,0 +1,54 @@ +package com.insanusmokrassar.sdi + +import kotlinx.serialization.* +import kotlinx.serialization.json.Json +import kotlin.test.* + +interface List_ParentalAPI { + val services: List +} +interface List_ChildAPI { + val names: List +} + +@Serializable +class List_Parent(override val services: List<@ContextualSerialization List_ChildAPI>) : List_ParentalAPI +@Serializable +class List_Child(override val names: List) : List_ChildAPI + +@ImplicitReflectionSerializer +class ListTest { + val servicesNum = 10 + + @Test + fun test_that_simple_config_correctly_work() { + val names = (0 until servicesNum).map { + "service$it" to arrayOf("nameOne$it", "nameTwo$it") + } + val controllerName = "parent" + val input = """ + { + ${names.joinToString { (title, currentNames) -> """ + "$title": [ + "${List_Child::class.qualifiedName}", + { + "names": ${currentNames.joinToString(prefix = "[", postfix = "]") { "\"$it\"" }} + } + ] + """ }}, + "$controllerName": [ + "${List_Parent::class.qualifiedName}", + { + "services": ${names.joinToString(prefix = "[", postfix = "]") { "\"${it.first}\"" }} + } + ] + } + """.trimIndent() + val module = Json.plain.parse(Module.serializer(), input) + (module[controllerName] as List_ParentalAPI) + val controller = (module[controllerName] as List_Parent) + controller.services.forEachIndexed { i, service -> + assertEquals(names[i].second.toList(), service.names) + } + } +} diff --git a/src/commonTest/kotlin/com/insanusmokrassar/sdi/DeserializationTest.kt b/src/commonTest/kotlin/com/insanusmokrassar/sdi/SimpleTest.kt similarity index 65% rename from src/commonTest/kotlin/com/insanusmokrassar/sdi/DeserializationTest.kt rename to src/commonTest/kotlin/com/insanusmokrassar/sdi/SimpleTest.kt index 487d86c..7866ec0 100644 --- a/src/commonTest/kotlin/com/insanusmokrassar/sdi/DeserializationTest.kt +++ b/src/commonTest/kotlin/com/insanusmokrassar/sdi/SimpleTest.kt @@ -4,24 +4,24 @@ import kotlinx.serialization.* import kotlinx.serialization.json.Json import kotlin.test.* -interface ControllerAPI { +interface Simple_ControllerAPI { fun showUp() } -interface ServiceAPI { +interface Simple_ServiceAPI { val names: List } @Serializable -class Controller(@ContextualSerialization val service: ServiceAPI) : ControllerAPI { +class Simple_Controller(@ContextualSerialization val service: Simple_ServiceAPI) : Simple_ControllerAPI { override fun showUp() { println("Inited with name \"${service.names}\"") } } @Serializable -class BusinessService(override val names: List) : ServiceAPI +class Simple_BusinessService(override val names: List) : Simple_ServiceAPI @ImplicitReflectionSerializer -class DeserializationTest { +class SimpleTest { @Test fun test_that_simple_config_correctly_work() { val names = arrayOf("nameOne", "nameTwo") @@ -29,13 +29,13 @@ class DeserializationTest { val input = """ { "service": [ - "${BusinessService::class.qualifiedName}", + "${Simple_BusinessService::class.qualifiedName}", { "names": ${names.joinToString(prefix = "[", postfix = "]") { "\"$it\"" }} } ], "$controllerName": [ - "${Controller::class.qualifiedName}", + "${Simple_Controller::class.qualifiedName}", { "service": "service" } @@ -43,8 +43,8 @@ class DeserializationTest { } """.trimIndent() val module = Json.plain.parse(Module.serializer(), input) - (module[controllerName] as ControllerAPI) - val controller = (module["controller"] as Controller) + (module[controllerName] as Simple_ControllerAPI) + val controller = (module["controller"] as Simple_Controller) assertEquals(names.toList(), controller.service.names) } }