Ensure stable entities over serialization
This commit is contained in:
parent
762e845ae0
commit
5ebf44a729
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Reference in New Issue