mourning-ember / src / test / java / dev / llan / model / board / pathfinding / GridHeuristicTest.java
GridHeuristicTest.java
Raw
package dev.llan.model.board.pathfinding;

import dev.llan.model.Game;
import dev.llan.model.GameContainer;
import dev.llan.model.board.Grid;
import dev.llan.model.board.Point;
import dev.llan.model.board.Tile.TileType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

class GridHeuristicTest {
  private Game game;
  private Grid grid;

  @BeforeEach
  void setUp() {
    GameContainer.getInstance().initGame();
    game = GameContainer.getInstance().getCurrentGame();
    grid = game.getMap().getGrid();
  }

  @Test
  void findValidPath() {
    PathFinder pathFinder = new GridHeuristic(grid);

    Optional<Path> path = pathFinder.findPath(new Point(1, 1), new Point(5, 5));

    assertTrue(path.isPresent());
    assertEquals(4, path.get().getPath().size());
  }

  @Test
  void findValidPathWithObstacles() {
    PathFinder pathFinder = new GridHeuristic(grid);

    grid.tileAt(new Point(2, 3)).setType(TileType.UNMOVABLE);
    grid.tileAt(new Point(3, 2)).setType(TileType.UNMOVABLE);
    grid.tileAt(new Point(3, 3)).setType(TileType.UNMOVABLE);
    Optional<Path> path = pathFinder.findPath(new Point(1, 1), new Point(5, 5));

    assertTrue(path.isPresent());
    assertEquals(6, path.get().getPath().size());
  }

  @Test
  void findPathToInteractable() {
    PathFinder pathFinder = new GridHeuristic(grid);

    grid.tileAt(new Point(5, 5)).setType(TileType.INTERACTABLE);
    Optional<Path> path = pathFinder.findPath(new Point(1, 1), new Point(5, 5));

    assertTrue(path.isPresent());
  }

  @Test
  void findInvalidPath() {
    PathFinder pathFinder = new GridHeuristic(grid);

    grid.tileAt(new Point(5, 5)).setType(TileType.UNMOVABLE);
    Optional<Path> path = pathFinder.findPath(new Point(1, 1), new Point(5, 5));

    assertFalse(path.isPresent());
  }
}