Add tests

This commit is contained in:
Christian Treffs 2020-10-20 14:40:01 +02:00
parent 0d66a30544
commit d751359ea0
No known key found for this signature in database
GPG Key ID: 49A4B4B460BE3ED4
3 changed files with 77 additions and 1 deletions

View File

@ -10,6 +10,29 @@ import FirebladeECS
class EmptyComponent: Component {
}
final class Optionals: Component, DefaultInitializable {
var int: Int?
var float: Float?
var string: String?
convenience init() {
self.init(nil, nil, nil)
}
init(_ int: Int?, _ float: Float?, _ string: String?) {
self.int = int
self.float = float
self.string = string
}
}
extension Optionals: Equatable {
static func == (lhs: Optionals, rhs: Optionals) -> Bool {
lhs.int == rhs.int &&
lhs.float == rhs.float &&
lhs.string == rhs.string
}
}
final class Name: Component, DefaultInitializable {
var name: String
init(name: String) {

View File

@ -34,7 +34,7 @@ class EntityTests: XCTestCase {
entity.assign(name, vel)
let expectedComponents: [Component] = [pos, name, vel]
let allComponents = entity.allComponents()
let allComponents = Array(entity.makeComponentsIterator())
XCTAssertTrue(allComponents.elementsEqualUnordered(expectedComponents) { $0 === $1 })
}
@ -117,6 +117,56 @@ class EntityTests: XCTestCase {
entity[Position.self] = nil // remove position component
XCTAssertNil(entity[Position.self])
let opts = Optionals(1, 2, "hello")
entity[Optionals.self] = opts
XCTAssertEqual(entity[Optionals.self], opts)
entity[\Optionals.float] = nil
XCTAssertEqual(entity[\Optionals.float], nil)
XCTAssertEqual(entity[\Optionals.int], 1)
XCTAssertEqual(entity[\Optionals.string], "hello")
entity[Optionals.self] = nil
XCTAssertNil(entity[Optionals.self])
entity[\Optionals.string] = "world"
XCTAssertEqual(entity[\Optionals.string], "world")
entity.assign(Comp1(12))
XCTAssertEqual(entity[\Comp1.value], 12)
}
func testComponentsIteration() {
let nexus = Nexus()
let entity = nexus.createEntity()
XCTAssertTrue(Array(entity.makeComponentsIterator()).isEmpty)
entity.assign(Position())
XCTAssertEqual(Array(entity.makeComponentsIterator()).count, 1)
}
func testEntityCreationIntrinsic() {
let nexus = Nexus()
let entity = nexus.createEntity()
let secondEntity = entity.createEntity()
XCTAssertNotEqual(secondEntity, entity)
let thirdEntity = secondEntity.createEntity()
XCTAssertNotEqual(secondEntity, thirdEntity)
XCTAssertNotEqual(entity, thirdEntity)
let entityWithComponents = entity.createEntity(with: Position(), Name())
XCTAssertTrue(entityWithComponents.has(Position.self))
XCTAssertTrue(entityWithComponents.has(Name.self))
XCTAssertEqual(nexus.numEntities, 4)
XCTAssertEqual(nexus.numComponents, 2)
}
func testEntityDescriptions() {
let nexus = Nexus()
let entt = nexus.createEntity()
XCTAssertFalse(entt.description.isEmpty)
XCTAssertFalse(entt.debugDescription.isEmpty)
}
}

View File

@ -140,6 +140,9 @@ extension EntityTests {
// to regenerate.
static let __allTests__EntityTests = [
("testAllComponentsOfEntity", testAllComponentsOfEntity),
("testComponentsIteration", testComponentsIteration),
("testEntityCreationIntrinsic", testEntityCreationIntrinsic),
("testEntityDescriptions", testEntityDescriptions),
("testEntityEquality", testEntityEquality),
("testEntityIdentifierAndIndex", testEntityIdentifierAndIndex),
("testEntityIdGenerator", testEntityIdGenerator),