add test for EntityStateMachine could be deallocated

This commit is contained in:
Igor Kravchenko 2020-10-02 13:17:31 +03:00
parent 1aae190fc1
commit 1c8c063e01
2 changed files with 22 additions and 1 deletions

View File

@ -354,7 +354,7 @@ public class EntityStateMachine<StateName: Hashable> {
toAdd = newState.providers
}
for (_, provider) in toAdd {
for (_, provider) in toAdd {
entity.assign(provider.getComponent())
}
currentState = newState

View File

@ -413,6 +413,27 @@ class EntityStateMachineTests: XCTestCase {
XCTAssertTrue(entity.get(component: MockComponent.self) === component1)
XCTAssertTrue(entity.get(component: MockComponent2.self) === component2)
}
func testGetsDeinitedWhileBeingStronglyReferencedByComponentAssignedToEntity() {
class Marker: Component {
let fsm: EntityStateMachine<String>
init(fsm: EntityStateMachine<String>) {
self.fsm = fsm
}
}
let nexus = Nexus()
var entity = nexus.createEntity()
var markerComponent = Marker(fsm: EntityStateMachine<String>(entity: entity))
entity.assign(markerComponent)
weak var weakMarker = markerComponent
weak var weakFsm = markerComponent.fsm
nexus.destroy(entity: entity)
entity = nexus.createEntity()
markerComponent = .init(fsm: .init(entity: entity))
XCTAssertNil(weakMarker)
XCTAssertNil(weakFsm)
}
class MockComponent: ComponentInitializable {
let value: Int