-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(queries): Allow iterating over entities gathered ahead of time, …
…allows some degree of modifying archetypes while iterating a query
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
geary-core/src/jvmTest/kotlin/com/mineinabyss/geary/queries/QueryForEachMutatingTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.mineinabyss.geary.queries | ||
|
||
import com.mineinabyss.geary.annotations.optin.UnsafeAccessors | ||
import com.mineinabyss.geary.helpers.entity | ||
import com.mineinabyss.geary.systems.query.query | ||
import com.mineinabyss.geary.test.GearyTest | ||
import io.kotest.assertions.throwables.shouldThrowAny | ||
import io.kotest.matchers.collections.shouldBeEmpty | ||
import io.kotest.matchers.collections.shouldHaveSize | ||
import org.junit.jupiter.api.BeforeEach | ||
import kotlin.test.Test | ||
|
||
class QueryForEachMutatingTest : GearyTest() { | ||
@BeforeEach | ||
fun setup() { | ||
resetEngine() | ||
repeat(10) { | ||
entity { set<Int>(it) } | ||
} | ||
} | ||
|
||
@Test | ||
fun `forEach should fail when modifying unsafeEntity's archetype`() { | ||
val query = queryManager.trackQuery(query<Int>()) | ||
shouldThrowAny { | ||
@OptIn(UnsafeAccessors::class) | ||
query.forEach { | ||
it.unsafeEntity.toGeary().remove<Int>() | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
fun `forEachEntity should allow modifying archetypes while iterating`() { | ||
val query = queryManager.trackQuery(query<Int>()) | ||
val postQuery = queryManager.trackQuery(query<Long>()) | ||
|
||
@OptIn(UnsafeAccessors::class) | ||
query.forEachMutating { entity, (integer) -> | ||
entity.remove<Int>() | ||
entity.set<Long>(integer.toLong()) | ||
} | ||
|
||
query.entities().shouldBeEmpty() | ||
postQuery.entities().shouldHaveSize(10) | ||
} | ||
} |