Files
oops/src/test/java/oving2/LocationTest.java
Andreas Omholt Olsen c7c4780535 Add oving 2
2026-01-19 11:53:18 +01:00

108 lines
1.8 KiB
Java

package oving2;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class LocationTest {
private Location loc;
/**
* Check that the position of {@link #loc} is equal to the parameters.
*
* @param x Expected x position
* @param y Expected y position
*/
private void checkPos(int x, int y) {
assertEquals(x, loc.getX(), "Wrong x coordinate");
assertEquals(y, loc.getY(), "Wrong y coordinate");
}
@BeforeEach
public void beforeEach() {
loc = new Location();
}
@Test
@DisplayName("Private fields")
public void testPrivateFields() {
TestHelper.checkIfFieldsPrivate(Location.class);
}
@Test
@DisplayName("Constructor")
public void testConstructor() {
this.checkPos(0, 0);
}
@Test
@DisplayName("Move up")
public void testUp() {
loc.up();
this.checkPos(0, -1);
loc.up();
this.checkPos(0, -2);
}
@Test
@DisplayName("Move down")
public void testDown() {
loc.down();
this.checkPos(0, 1);
loc.down();
this.checkPos(0, 2);
}
@Test
@DisplayName("Move left")
public void testLeft() {
loc.left();
this.checkPos(-1, 0);
loc.left();
this.checkPos(-2, 0);
}
@Test
@DisplayName("Move right")
public void testRight() {
loc.right();
this.checkPos(1, 0);
loc.right();
this.checkPos(2, 0);
}
@Test
@DisplayName("Move multiple directions")
public void testComplexMovement() {
loc.right();
this.checkPos(1, 0);
loc.down();
this.checkPos(1, 1);
loc.right();
this.checkPos(2, 1);
loc.down();
this.checkPos(2, 2);
loc.left();
this.checkPos(1, 2);
loc.up();
this.checkPos(1, 1);
loc.up();
this.checkPos(1, 0);
loc.left();
this.checkPos(0, 0);
}
}