Spawn Task Tests - UQcsse3200/2024-studio-2 GitHub Wiki
Test Cases
shouldTriggerSpawnEvent()
: Verifies that the SpawnTask triggers the "spawnChicken" event when started
Expected Result: The "spawnChicken" event is triggered
@Test
void shouldTriggerSpawnEvent() {
Vector2 target = new Vector2(10f, 10f);
TaskRunner taskRunner = mock(TaskRunner.class);
Entity entity = mock(Entity.class);
EventHandler events = mock(EventHandler.class);
when(taskRunner.getEntity()).thenReturn(entity);
when(entity.getEvents()).thenReturn(events);
SpawnTask spawn = new SpawnTask(target, 1f);
spawn.setOwner((TaskRunner) taskRunner);
spawn.start();
verify(entity.getEvents()).trigger("spawnChicken");
}
getSpawnPositionReturnsCorrectValue
: Tests that the SpawnTask returns the correct spawn position.
Expected Result: The spawn position matches the expected value.
@Test
void getSpawnPositionReturnsCorrectValue() {
Vector2 expectedSpawnPosition = new Vector2(10f, 10f);
SpawnTask spawn = new SpawnTask(expectedSpawnPosition, 1f);
Vector2 actualSpawnPosition = spawn.getSpawnPosition();
assertEquals(expectedSpawnPosition, actualSpawnPosition);
}
shouldBeActiveAfterStart
: Verifies that the SpawnTask transitions to the ACTIVE status after starting.
Expected Result: The status is ACTIVE after starting.
@Test
void shouldBeActiveAfterStart(){
Vector2 target = new Vector2(0f,0f);
TaskRunner taskRunner = mock(TaskRunner.class);
Entity entity = mock(Entity.class);
EventHandler events = mock(EventHandler.class);
when(taskRunner.getEntity()).thenReturn(entity);
when(entity.getEvents()).thenReturn(events);
SpawnTask spawn = new SpawnTask(target,1f);
spawn.setOwner((TaskRunner) taskRunner);
spawn.start();
assertEquals(Status.ACTIVE, spawn.getStatus());
}
shouldBeInactiveOutsideStartCall
: Tests that the SpawnTask remains INACTIVE outside of the start method call.
Expected Result: The status is INACTIVE outside of the start method call.
@Test
void shouldBeInactiveOutsideStartCall(){
Vector2 target = new Vector2(0f,0f);
SpawnTask spawn = new SpawnTask(target,1f);
assertEquals(Status.INACTIVE, spawn.getStatus());
spawn.stop();
assertEquals(Status.INACTIVE, spawn.getStatus());
}
constructorInitialisesCorrectly
: Verifies that the SpawnTask constructor initializes the object correctly.
Expected Result: The spawn position and duration are set to the expected values.
@Test
void constructorInitialisesCorrectly() {
Vector2 target = new Vector2(10f, 10f);
SpawnTask spawn = new SpawnTask(target, 1f);
assertEquals(target, spawn.getSpawnPosition());
assertEquals(1f, spawn.getSpawnDuration());
}
getPriorityReturnsCorrectValue
: Tests that the SpawnTask returns the correct priority value.
Expected Result: The priority value is 0.
@Test
void getPriorityReturnsCorrectValue() {
SpawnTask spawn = new SpawnTask(new Vector2(0f, 0f), 1f);
assertEquals(0, spawn.getPriority()); // Verify default priority
// Test with different spawn durations
spawn = new SpawnTask(new Vector2(0f, 0f), 2f);
assertEquals(0, spawn.getPriority()); // Verify priority remains the same
// Test with different spawn positions
spawn = new SpawnTask(new Vector2(10f, 10f), 1f);
assertEquals(0, spawn.getPriority()); // Verify priority remains the same
}
updatesElapsedTime
: Tests that the SpawnTask updates its elapsed time correctly.
Expected Result: The elapsed time is updated correctly.
@Test
void updatesElapsedTime() {
TaskRunner taskRunner = mock(TaskRunner.class);
Entity entity = mock(Entity.class);
EventHandler events = mock(EventHandler.class);
when(taskRunner.getEntity()).thenReturn(entity);
when(entity.getEvents()).thenReturn(events);
SpawnTask spawn = new SpawnTask(new Vector2(0f, 0f), 1f);
spawn.setOwner(taskRunner);
spawn.start();
spawn.update();
assertEquals(0, spawn.getElapsedTime());
}
completeTaskStopped
: Verifies that the SpawnTask completes and stops correctly.
Expected Result: The task is stopped and the status is INACTIVE.
@Test
void completeTaskStopped() {
TaskRunner taskRunner = mock(TaskRunner.class);
EventHandler events = mock(EventHandler.class);
Entity entity = mock(Entity.class);
when(taskRunner.getEntity()).thenReturn(entity);
when(entity.getEvents()).thenReturn(events);
SpawnTask spawn = new SpawnTask(new Vector2(0f, 0f), 1f);
spawn.setOwner(taskRunner);
spawn.start();
spawn.completeTask();
assertNotNull(spawn.getStatus());
assertEquals(Status.INACTIVE, spawn.getStatus());
}
}