From f5d9f29c90f3915d818424c01b04e9e7aa363178 Mon Sep 17 00:00:00 2001 From: Danielle Voznyy Date: Tue, 30 Apr 2024 00:20:27 -0400 Subject: [PATCH] fix(prefabs): Lots of incorrect behaviour with EntityObservers tests(prefabs): Add test for EntityObservers component in config --- .../components/EntityObservers.kt | 6 +- .../observers/ConfigEntityObserversTests.kt | 74 +++++++++++++++++++ .../geary/serialization/formats/Format.kt | 7 ++ .../geary/serialization/formats/YamlFormat.kt | 7 +- 4 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 addons/geary-prefabs/src/jvmTest/kotlin/com/mineinabyss/geary/prefabs/observers/ConfigEntityObserversTests.kt diff --git a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt index 6e67756c..e5845481 100644 --- a/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt +++ b/addons/geary-prefabs/src/commonMain/kotlin/com/mineinabyss/geary/prefabs/configuration/components/EntityObservers.kt @@ -28,7 +28,9 @@ class EventBind( class CachedEvent(val componentId: ComponentId, val data: Any?) @Transient - val emitEvents = emit.map { - if (using != null) ReEmitEvent(SerializableComponentId(using.id), it) else it + val emitEvents = emit.flatMap { emitter -> + emitter.map { component -> + if (using != null) ReEmitEvent(SerializableComponentId(using.id), component) else component + } }.map { CachedEvent(componentId(it::class), it) } } diff --git a/addons/geary-prefabs/src/jvmTest/kotlin/com/mineinabyss/geary/prefabs/observers/ConfigEntityObserversTests.kt b/addons/geary-prefabs/src/jvmTest/kotlin/com/mineinabyss/geary/prefabs/observers/ConfigEntityObserversTests.kt new file mode 100644 index 00000000..6a96e7d9 --- /dev/null +++ b/addons/geary-prefabs/src/jvmTest/kotlin/com/mineinabyss/geary/prefabs/observers/ConfigEntityObserversTests.kt @@ -0,0 +1,74 @@ +package com.mineinabyss.geary.prefabs.observers + +import com.mineinabyss.geary.modules.TestEngineModule +import com.mineinabyss.geary.modules.geary +import com.mineinabyss.geary.prefabs.Prefabs +import com.mineinabyss.geary.prefabs.configuration.components.EntityObservers +import com.mineinabyss.geary.serialization.dsl.serialization +import com.mineinabyss.geary.serialization.dsl.withCommonComponentNames +import com.mineinabyss.geary.serialization.formats.YamlFormat +import com.mineinabyss.geary.serialization.serializableComponents +import com.mineinabyss.geary.serialization.serializers.GearyEntitySerializer +import com.mineinabyss.geary.systems.builders.observeWithData +import com.mineinabyss.idofront.di.DI +import io.kotest.assertions.print.Print +import io.kotest.matchers.shouldBe +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.builtins.serializer +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test + +class ConfigEntityObserversTests { + @Serializable + @SerialName("geary:print") + data class Print(val string: String) + + @Serializable + @SerialName("geary:my_comp") + class MyComp() + + @BeforeEach + fun createEngine() { + DI.clear() + geary(TestEngineModule) { + install(Prefabs) + + serialization { + withCommonComponentNames() + + components { + component(String.serializer()) + component(Print.serializer()) + component(EntityObservers.serializer()) + component(MyComp.serializer()) + } + } + } + geary.pipeline.runStartupTasks() + } + + @Test + fun `should correctly add temporary and persisting components with CopyToInstances`() { + // arrange + val entityDef = """ + geary:observe: + - event: geary:onSet + involving: [ geary:myComp ] + emit: + - geary:print: + string: "Hello World" + """.trimIndent() + + val format = YamlFormat(serializableComponents.serializers.module) + val entity = format.decodeFromString(GearyEntitySerializer, entityDef) + val printed = mutableListOf() + geary.observeWithData().exec { printed += event.string } + + // act + entity.set(MyComp()) + + // assert + printed shouldBe listOf("Hello World") + } +} diff --git a/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/formats/Format.kt b/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/formats/Format.kt index 302e9cd3..c90e3144 100644 --- a/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/formats/Format.kt +++ b/addons/geary-serialization/src/commonMain/kotlin/com/mineinabyss/geary/serialization/formats/Format.kt @@ -8,6 +8,13 @@ import okio.Path interface Format { val ext: String + fun decodeFromString( + deserializer: DeserializationStrategy, + string: String, + overrideSerializersModule: SerializersModule? = null, + configType: ConfigType = ConfigType.REGULAR, + ): T + fun decodeFromFile( deserializer: DeserializationStrategy, path: Path, diff --git a/addons/geary-serialization/src/jvmMain/kotlin/com/mineinabyss/geary/serialization/formats/YamlFormat.kt b/addons/geary-serialization/src/jvmMain/kotlin/com/mineinabyss/geary/serialization/formats/YamlFormat.kt index 2feca5f3..c0ab4a82 100644 --- a/addons/geary-serialization/src/jvmMain/kotlin/com/mineinabyss/geary/serialization/formats/YamlFormat.kt +++ b/addons/geary-serialization/src/jvmMain/kotlin/com/mineinabyss/geary/serialization/formats/YamlFormat.kt @@ -52,11 +52,12 @@ class YamlFormat( return Yaml(module, config).decodeFromStream(deserializer, inputStream()) } - fun decodeFromString( + + override fun decodeFromString( deserializer: DeserializationStrategy, @Language("yaml") string: String, - overrideSerializersModule: SerializersModule? = null, - configType: ConfigType = ConfigType.REGULAR, + overrideSerializersModule: SerializersModule?, + configType: ConfigType ): T { return decodeFromStream(deserializer, overrideSerializersModule, configType) { string.byteInputStream() } }