Worker Movement Testing - UQdeco2800/2022-studio-3 GitHub Wiki

Overview

These JUnit test cases aim to check if the WorkerMovementTask passes the following functionality tests:

  1. shouldMoveOnStart: when the start method is called on the task, the entity should be moving towards the assigned target.
void shouldMoveOnStart() {
        Vector2 target = new Vector2(10f, 10f);
        WorkerMovementTask task = new WorkerMovementTask(target);
        Entity entity = new Entity().addComponent(new PhysicsComponent());
        PhysicsMovementComponent movementComponent = new PhysicsMovementComponent();
        entity.addComponent(movementComponent);
        entity.create();

        task.create(() -> entity);
        task.start();
        assertTrue(movementComponent.getMoving());
        assertEquals(target, movementComponent.getTarget());
        assertEquals(Task.Status.ACTIVE, task.getStatus());
    }
  1. shouldStopWhenClose: when the task is started and the position of the entity comes within stopping distance of the target (0.01f by default), then the entity should stop moving.
void shouldStopWhenClose() {
        WorkerMovementTask task = new WorkerMovementTask(new Vector2(10f, 10f));
        task.setStopDistance(1f); // Stopping distance for entity
        Entity entity = new Entity().addComponent(new PhysicsComponent());
        PhysicsMovementComponent movementComponent = new PhysicsMovementComponent();
        entity.addComponent(movementComponent);
        entity.setPosition(5f, 5f);
        entity.create();

        task.create(() -> entity);
        task.start();
        task.update();
        assertTrue(movementComponent.getMoving());
        assertEquals(Task.Status.ACTIVE, task.getStatus());

        entity.setPosition(10f, 9f);
        task.update();
        assertFalse(movementComponent.getMoving());
        assertEquals(Task.Status.FINISHED, task.getStatus());
    }
  1. shouldMoveToNewTarget: when the task is started and the entity is moving towards its first target then, if the target is changed before the entity stops, it should move towards and stop at the second target.
void shouldMoveToNewTarget() {
        Vector2 firstTarget = new Vector2(10f, 10f); // First entity target
        WorkerMovementTask task = new WorkerMovementTask(firstTarget);
        task.setStopDistance(1f); // Stopping distance for entity
        Entity entity = new Entity().addComponent(new PhysicsComponent());
        PhysicsMovementComponent movementComponent = new PhysicsMovementComponent();
        entity.addComponent(movementComponent);
        entity.setPosition(5f, 5f);
        entity.create();

        // Create WorkerMovementTask and assign entity
        task.create(() -> entity);
        task.start();
        task.update();
        assertTrue(movementComponent.getMoving());
        assertEquals(Task.Status.ACTIVE, task.getStatus());

        // Change target of entity
        Vector2 secondTarget = new Vector2(-10f, -10f);
        task.setTarget(secondTarget);

        // Move entity next to first target; should still be moving
        entity.setPosition(10f, 9f);
        task.update();
        assertTrue(movementComponent.getMoving());
        assertEquals(Task.Status.ACTIVE, task.getStatus());

        // Move entity next to second target; should stop moving
        entity.setPosition(-10f, -9f);
        task.update();
        assertFalse(movementComponent.getMoving());
        assertEquals(Task.Status.FINISHED, task.getStatus());
    }