Skip to content

Commit

Permalink
fix(prefabs): Lots of incorrect behaviour with EntityObservers
Browse files Browse the repository at this point in the history
tests(prefabs): Add test for EntityObservers component in config
  • Loading branch information
0ffz committed Apr 30, 2024
1 parent 989d435 commit f5d9f29
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
}
Original file line number Diff line number Diff line change
@@ -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<String>()
geary.observeWithData<Print>().exec { printed += event.string }

// act
entity.set(MyComp())

// assert
printed shouldBe listOf("Hello World")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import okio.Path
interface Format {
val ext: String

fun <T> decodeFromString(
deserializer: DeserializationStrategy<T>,
string: String,
overrideSerializersModule: SerializersModule? = null,
configType: ConfigType = ConfigType.REGULAR,
): T

fun <T> decodeFromFile(
deserializer: DeserializationStrategy<T>,
path: Path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ class YamlFormat(
return Yaml(module, config).decodeFromStream(deserializer, inputStream())
}

fun <T> decodeFromString(

override fun <T> decodeFromString(
deserializer: DeserializationStrategy<T>,
@Language("yaml") string: String,
overrideSerializersModule: SerializersModule? = null,
configType: ConfigType = ConfigType.REGULAR,
overrideSerializersModule: SerializersModule?,
configType: ConfigType
): T {
return decodeFromStream(deserializer, overrideSerializersModule, configType) { string.byteInputStream() }
}
Expand Down

0 comments on commit f5d9f29

Please sign in to comment.