Skip to content

Commit

Permalink
Merge pull request #43 from cs3217-2324/fix-tests
Browse files Browse the repository at this point in the history
Test/fix tests after ECS refactor
  • Loading branch information
jasonqiu212 committed Mar 28, 2024
2 parents 25daed0 + 9a8a420 commit 5794b02
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
Expand Up @@ -12,9 +12,16 @@ import XCTest
final class SpriteComponentTests: XCTestCase {
func testEqual_initComponent() {
let player = createPlayerEntity()
let spriteComponent = SpriteComponent(entityId: player.id, image: "", textureAtlas: "", size: .zero)
XCTAssertEqual(spriteComponent.size, .zero, "Size should be initialized")
let textureSet = createTextureSet()
let spriteComponent = SpriteComponent(
entityId: player.id,
image: "",
textureSet: textureSet,
textureAtlas: "",
size: .zero)
XCTAssertEqual(spriteComponent.image, "", "Image should be initialized")
XCTAssertEqual(spriteComponent.textureSet?.run, textureSet.run, "TextureSet should be initialized")
XCTAssertEqual(spriteComponent.textureAtlas, "", "TextureAtlas should be initialized")
XCTAssertEqual(spriteComponent.size, .zero, "Size should be initialized")
}
}
23 changes: 12 additions & 11 deletions star-dash/star-dashTests/GameEngine/EntityManagerTests.swift
Expand Up @@ -14,30 +14,30 @@ final class EntityManagerTests: XCTestCase {
let entityManager = createEntityManager()
let player = createPlayerEntity()
entityManager.add(entity: player)
XCTAssertEqual(entityManager.entityMap.count, 1, "Entity should be added to the entityMap")
XCTAssertEqual(entityManager.entities.count, 1, "Entity should be added to the entityMap")
}

func testEqual_addSameEntity() {
let entityManager = createEntityManager()
let player = createPlayerEntity()
entityManager.add(entity: player)
entityManager.add(entity: player)
XCTAssertEqual(entityManager.entityMap.count, 1, "Repeated entity should not be added to the entityMap")
XCTAssertEqual(entityManager.entities.count, 1, "Repeated entity should not be added to the entityMap")
}

func testEqual_removeEntity() {
let entityManager = createEntityManager()
let player = createPlayerEntity()
entityManager.add(entity: player)
entityManager.remove(entity: player)
XCTAssertEqual(entityManager.entityMap.count, 0, "Entity should be removed from the entityMap")
XCTAssertEqual(entityManager.entities.count, 0, "Entity should be removed from the entityMap")
}

func testEqual_removeEntityNotInList() {
let entityManager = createEntityManager()
let player = createPlayerEntity()
entityManager.remove(entity: player)
XCTAssertEqual(entityManager.entityMap.count, 0, "Entity should not be removed from the entityMap")
XCTAssertEqual(entityManager.entities.count, 0, "Entity should not be removed from the entityMap")
}

func testNotNil_getEntity() {
Expand All @@ -51,27 +51,28 @@ final class EntityManagerTests: XCTestCase {
func testNotNil_getComponent() {
let entityManager = createEntityManager()
let player = createPlayerEntity()
let positionComponent = PositionComponent(entityId: player.id, position: .zero, rotation: .zero)
entityManager.add(entity: player)
player.setUpAndAdd(to: entityManager)
let positionComponent = entityManager.component(ofType: PositionComponent.self, of: player.id)
XCTAssertNotNil(positionComponent, "Component should be retrieved from the componentMap")
entityManager.add(component: positionComponent)
let retrievedComponent = entityManager.component(ofType: PositionComponent.self, of: player.id)
XCTAssertNotNil(retrievedComponent, "Component should be retrieved from the componentMap")
}

func testNil_getComponentOfRemovedEntity() {
let entityManager = createEntityManager()
let player = createPlayerEntity()
let positionComponent = PositionComponent(entityId: player.id, position: .zero, rotation: .zero)
entityManager.add(entity: player)
player.setUpAndAdd(to: entityManager)
entityManager.add(component: positionComponent)
entityManager.remove(entity: player)
let positionComponent = entityManager.component(ofType: PositionComponent.self, of: player.id)
XCTAssertNil(positionComponent, "Component should not be retrieved from the componentMap if entity is removed")
let retrievedComponent = entityManager.component(ofType: PositionComponent.self, of: player.id)
XCTAssertNil(retrievedComponent, "Component should not be retrieved from the componentMap if entity is removed")
}

func testNil_getEntityOfRemovedEntity() {
let entityManager = createEntityManager()
let player = createPlayerEntity()
entityManager.add(entity: player)
player.setUpAndAdd(to: entityManager)
entityManager.remove(entity: player)
let entity = entityManager.entity(with: player.id)
XCTAssertNil(entity, "Entity should not be retrieved from the entityMap if entity is removed")
Expand Down
6 changes: 5 additions & 1 deletion star-dash/star-dashTests/Util.swift
Expand Up @@ -9,9 +9,13 @@ import Foundation
@testable import star_dash

func createPlayerEntity() -> Player {
Player(position: CGPoint(x: 0, y: 0), playerSprite: .RedNose)
Player(id: UUID())
}

func createEntityManager() -> EntityManager {
EntityManager()
}

func createTextureSet() -> TextureSet {
TextureSet(run: "")
}

0 comments on commit 5794b02

Please sign in to comment.