Ensure stable entities over serialization

This commit is contained in:
Christian Treffs 2020-08-21 10:50:54 +02:00
parent 762e845ae0
commit 5ebf44a729
No known key found for this signature in database
GPG Key ID: 49A4B4B460BE3ED4
1 changed files with 19 additions and 0 deletions

View File

@ -11,9 +11,18 @@ import FirebladeECS
public final class SerializationTests: XCTestCase {
func testSerialization() throws {
let nexus = Nexus()
let e1 = nexus.createEntity()
let e2 = nexus.createEntity()
nexus.createEntity(with: Position(x: 1, y: 4), Name(name: "myName"))
let e3 = nexus.createEntity()
nexus.createEntity(with: Position(x: 5, y: 18), Name(name: "yourName"))
// Fragment entities
nexus.destroy(entity: e2)
nexus.destroy(entity: e3)
nexus.destroy(entity: e1)
let encoder = JSONEncoder()
let data = try encoder.encode(nexus)
@ -29,8 +38,16 @@ public final class SerializationTests: XCTestCase {
func testDeserialization() throws {
let nexus = Nexus()
let e1 = nexus.createEntity()
let e2 = nexus.createEntity()
let firstEntity = nexus.createEntity(with: Name(name: "myName"), Position(x: 1, y: 2))
let e3 = nexus.createEntity()
let secondEntity = nexus.createEntity(with: Velocity(a: 3.14), Party(partying: true))
// Fragment entities
nexus.destroy(entity: e2)
nexus.destroy(entity: e3)
nexus.destroy(entity: e1)
let encoder = JSONEncoder()
let data = try encoder.encode(nexus)
@ -39,6 +56,7 @@ public final class SerializationTests: XCTestCase {
let nexus2: Nexus = try decoder.decode(Nexus.self, from: data)
let firstEntity2 = nexus2.get(entity: firstEntity.identifier)!
XCTAssertEqual(firstEntity2.identifier, firstEntity.identifier)
XCTAssertTrue(firstEntity2.has(Name.self))
XCTAssertTrue(firstEntity2.has(Position.self))
XCTAssertEqual(firstEntity2.get(component: Name.self)?.name, "myName")
@ -46,6 +64,7 @@ public final class SerializationTests: XCTestCase {
XCTAssertEqual(firstEntity2.get(component: Position.self)?.y, 2)
let secondEntity2 = nexus2.get(entity: secondEntity.identifier)!
XCTAssertEqual(secondEntity2.identifier, secondEntity.identifier)
XCTAssertTrue(secondEntity2.has(Velocity.self))
XCTAssertTrue(secondEntity2.has(Party.self))
XCTAssertEqual(secondEntity2.get(component: Velocity.self)?.a, 3.14)