test: Added some random test
And did some other minor changes
This commit is contained in:
@@ -59,5 +59,11 @@
|
|||||||
<version>2.0.17</version>
|
<version>2.0.17</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
package cz.jzitnik.common.socket.messages;
|
|
||||||
|
|
||||||
import cz.jzitnik.common.socket.SocketMessage;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test message for socket communication.
|
|
||||||
*
|
|
||||||
* @author Jakub Žitník (jzitnik)
|
|
||||||
*/
|
|
||||||
public class Test implements SocketMessage {
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,76 @@
|
|||||||
|
package cz.jzitnik.common.models.coordinates;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class RoomCordsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructor() {
|
||||||
|
RoomCords cords = new RoomCords(10, 20);
|
||||||
|
assertEquals(10, cords.getX());
|
||||||
|
assertEquals(20, cords.getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateCordsWithInts() {
|
||||||
|
RoomCords cords = new RoomCords(0, 0);
|
||||||
|
cords.updateCords(5, 10);
|
||||||
|
assertEquals(5, cords.getX());
|
||||||
|
assertEquals(10, cords.getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testUpdateCordsWithRoomCords() {
|
||||||
|
RoomCords cords = new RoomCords(0, 0);
|
||||||
|
RoomCords newCords = new RoomCords(15, 25);
|
||||||
|
cords.updateCords(newCords);
|
||||||
|
assertEquals(15, cords.getX());
|
||||||
|
assertEquals(25, cords.getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCalculateDistance() {
|
||||||
|
RoomCords cords1 = new RoomCords(0, 0);
|
||||||
|
RoomCords cords2 = new RoomCords(3, 4);
|
||||||
|
double distance = cords1.calculateDistance(cords2);
|
||||||
|
assertEquals(5.0, distance, 0.0001);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCalculateDistanceToSelf() {
|
||||||
|
RoomCords cords = new RoomCords(5, 5);
|
||||||
|
double distance = cords.calculateDistance(cords);
|
||||||
|
assertEquals(0.0, distance, 0.0001);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testCalculateDistanceThrowsOnNull() {
|
||||||
|
RoomCords cords = new RoomCords(0, 0);
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> cords.calculateDistance(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testClone() {
|
||||||
|
RoomCords original = new RoomCords(7, 8);
|
||||||
|
RoomCords cloned = original.clone();
|
||||||
|
assertEquals(original.getX(), cloned.getX());
|
||||||
|
assertEquals(original.getY(), cloned.getY());
|
||||||
|
assertNotSame(original, cloned);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource({
|
||||||
|
"0, 0, 10, 10",
|
||||||
|
"5, 5, 15, 15",
|
||||||
|
"-5, -5, 0, 0"
|
||||||
|
})
|
||||||
|
void testUpdateCordsWithValues(int x, int y, int expectedX, int expectedY) {
|
||||||
|
RoomCords cords = new RoomCords(x, y);
|
||||||
|
assertEquals(x, cords.getX());
|
||||||
|
assertEquals(y, cords.getY());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package cz.jzitnik.common.models.coordinates;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class RoomPartTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructor() {
|
||||||
|
RoomCords start = new RoomCords(0, 0);
|
||||||
|
RoomCords end = new RoomCords(10, 10);
|
||||||
|
RoomPart roomPart = new RoomPart(start, end);
|
||||||
|
assertEquals(start, roomPart.getStart());
|
||||||
|
assertEquals(end, roomPart.getEnd());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsWithinReturnsTrueForPointInside() {
|
||||||
|
RoomPart roomPart = new RoomPart(new RoomCords(0, 0), new RoomCords(10, 10));
|
||||||
|
assertTrue(roomPart.isWithin(new RoomCords(5, 5)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsWithinReturnsTrueForPointOnEdge() {
|
||||||
|
RoomPart roomPart = new RoomPart(new RoomCords(0, 0), new RoomCords(10, 10));
|
||||||
|
assertTrue(roomPart.isWithin(new RoomCords(0, 0)));
|
||||||
|
assertTrue(roomPart.isWithin(new RoomCords(10, 10)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsWithinReturnsFalseForPointOutside() {
|
||||||
|
RoomPart roomPart = new RoomPart(new RoomCords(0, 0), new RoomCords(10, 10));
|
||||||
|
assertFalse(roomPart.isWithin(new RoomCords(15, 5)));
|
||||||
|
assertFalse(roomPart.isWithin(new RoomCords(5, 15)));
|
||||||
|
assertFalse(roomPart.isWithin(new RoomCords(-1, 5)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsOverlappingReturnsTrueForOverlappingParts() {
|
||||||
|
RoomPart part1 = new RoomPart(new RoomCords(0, 0), new RoomCords(10, 10));
|
||||||
|
RoomPart part2 = new RoomPart(new RoomCords(5, 5), new RoomCords(15, 15));
|
||||||
|
assertTrue(part1.isOverlapping(part2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsOverlappingReturnsTrueForIdenticalParts() {
|
||||||
|
RoomPart part1 = new RoomPart(new RoomCords(0, 0), new RoomCords(10, 10));
|
||||||
|
RoomPart part2 = new RoomPart(new RoomCords(0, 0), new RoomCords(10, 10));
|
||||||
|
assertTrue(part1.isOverlapping(part2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsOverlappingReturnsFalseForNonOverlappingParts() {
|
||||||
|
RoomPart part1 = new RoomPart(new RoomCords(0, 0), new RoomCords(10, 10));
|
||||||
|
RoomPart part2 = new RoomPart(new RoomCords(20, 20), new RoomCords(30, 30));
|
||||||
|
assertFalse(part1.isOverlapping(part2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsOverlappingReturnsTrueForAdjacentParts() {
|
||||||
|
RoomPart part1 = new RoomPart(new RoomCords(0, 0), new RoomCords(10, 10));
|
||||||
|
RoomPart part2 = new RoomPart(new RoomCords(10, 0), new RoomCords(20, 10));
|
||||||
|
assertTrue(part1.isOverlapping(part2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIsOverlappingWithOnePartInsideAnother() {
|
||||||
|
RoomPart part1 = new RoomPart(new RoomCords(0, 0), new RoomCords(20, 20));
|
||||||
|
RoomPart part2 = new RoomPart(new RoomCords(5, 5), new RoomCords(15, 15));
|
||||||
|
assertTrue(part1.isOverlapping(part2));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -123,13 +123,6 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter</artifactId>
|
<artifactId>junit-jupiter</artifactId>
|
||||||
<version>6.0.2</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.junit.jupiter</groupId>
|
|
||||||
<artifactId>junit-jupiter-api</artifactId>
|
|
||||||
<version>6.0.2</version>
|
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
|||||||
@@ -3,5 +3,10 @@ package cz.jzitnik.client.events;
|
|||||||
import cz.jzitnik.client.game.objects.DroppedItem;
|
import cz.jzitnik.client.game.objects.DroppedItem;
|
||||||
import cz.jzitnik.client.utils.events.Event;
|
import cz.jzitnik.client.utils.events.Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event triggered when a dropped item in the game world needs to be rerendered.
|
||||||
|
*
|
||||||
|
* @param droppedItem the dropped item to rerender
|
||||||
|
*/
|
||||||
public record DroppedItemRerender(DroppedItem droppedItem) implements Event {
|
public record DroppedItemRerender(DroppedItem droppedItem) implements Event {
|
||||||
}
|
}
|
||||||
@@ -2,5 +2,8 @@ package cz.jzitnik.client.events;
|
|||||||
|
|
||||||
import cz.jzitnik.client.utils.events.Event;
|
import cz.jzitnik.client.utils.events.Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event triggered when the entire screen needs to be fully redrawn.
|
||||||
|
*/
|
||||||
public class FullRedrawEvent implements Event {
|
public class FullRedrawEvent implements Event {
|
||||||
}
|
}
|
||||||
@@ -4,6 +4,10 @@ import cz.jzitnik.client.utils.events.Event;
|
|||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event triggered when the current room needs to be fully drawn.
|
||||||
|
* Can optionally trigger a full rerender of all elements.
|
||||||
|
*/
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
@Getter
|
@Getter
|
||||||
public class FullRoomDraw implements Event {
|
public class FullRoomDraw implements Event {
|
||||||
|
|||||||
@@ -2,5 +2,8 @@ package cz.jzitnik.client.events;
|
|||||||
|
|
||||||
import cz.jzitnik.client.utils.events.Event;
|
import cz.jzitnik.client.utils.events.Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event triggered when the player's inventory display needs to be rerendered.
|
||||||
|
*/
|
||||||
public class InventoryRerender implements Event {
|
public class InventoryRerender implements Event {
|
||||||
}
|
}
|
||||||
@@ -2,5 +2,8 @@ package cz.jzitnik.client.events;
|
|||||||
|
|
||||||
import cz.jzitnik.client.utils.events.Event;
|
import cz.jzitnik.client.utils.events.Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event triggered when the player stats display needs to be rerendered.
|
||||||
|
*/
|
||||||
public class RenderStats implements Event {
|
public class RenderStats implements Event {
|
||||||
}
|
}
|
||||||
@@ -6,6 +6,11 @@ import cz.jzitnik.client.utils.events.Event;
|
|||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event triggered when specific parts of the screen need to be rerendered.
|
||||||
|
*
|
||||||
|
* @param parts the screen parts that need to be rerendered
|
||||||
|
*/
|
||||||
public record RerenderScreen(ScreenPart[] parts) implements Event {
|
public record RerenderScreen(ScreenPart[] parts) implements Event {
|
||||||
public RerenderScreen(ScreenPart part) {
|
public RerenderScreen(ScreenPart part) {
|
||||||
this(new ScreenPart[]{part});
|
this(new ScreenPart[]{part});
|
||||||
|
|||||||
@@ -3,5 +3,10 @@ package cz.jzitnik.client.events;
|
|||||||
import cz.jzitnik.client.events.handlers.FullRoomDrawHandler;
|
import cz.jzitnik.client.events.handlers.FullRoomDrawHandler;
|
||||||
import cz.jzitnik.client.utils.events.Event;
|
import cz.jzitnik.client.utils.events.Event;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event triggered when the player changes to a different room.
|
||||||
|
*
|
||||||
|
* @param door the door position from which the player entered the new room
|
||||||
|
*/
|
||||||
public record RoomChangeEvent(FullRoomDrawHandler.DoorPosition door) implements Event {
|
public record RoomChangeEvent(FullRoomDrawHandler.DoorPosition door) implements Event {
|
||||||
}
|
}
|
||||||
@@ -3,5 +3,10 @@ package cz.jzitnik.client.events;
|
|||||||
import cz.jzitnik.client.utils.events.Event;
|
import cz.jzitnik.client.utils.events.Event;
|
||||||
import cz.jzitnik.common.socket.SocketMessage;
|
import cz.jzitnik.common.socket.SocketMessage;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Event triggered when a socket message needs to be sent to the server.
|
||||||
|
*
|
||||||
|
* @param message the socket message to send
|
||||||
|
*/
|
||||||
public record SendSocketMessageEvent(SocketMessage message) implements Event {
|
public record SendSocketMessageEvent(SocketMessage message) implements Event {
|
||||||
}
|
}
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
package cz.jzitnik.client.socket.events;
|
|
||||||
|
|
||||||
import cz.jzitnik.client.annotations.SocketEventHandler;
|
|
||||||
import cz.jzitnik.client.socket.AbstractSocketEventHandler;
|
|
||||||
import cz.jzitnik.common.socket.messages.Test;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Handler for the Test socket event.
|
|
||||||
*
|
|
||||||
* @author Jakub Žitník (jzitnik)
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
@SocketEventHandler(Test.class)
|
|
||||||
public class TestHandler extends AbstractSocketEventHandler<Test> {
|
|
||||||
/**
|
|
||||||
* Handles the Test event.
|
|
||||||
*
|
|
||||||
* @param event The Test event
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void handle(Test event) {
|
|
||||||
log.debug("Got test: {}", event);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,6 @@ import cz.jzitnik.client.annotations.ui.KeyboardPressHandler;
|
|||||||
import cz.jzitnik.client.annotations.ui.UI;
|
import cz.jzitnik.client.annotations.ui.UI;
|
||||||
import cz.jzitnik.client.events.*;
|
import cz.jzitnik.client.events.*;
|
||||||
import cz.jzitnik.client.utils.events.EventManager;
|
import cz.jzitnik.client.utils.events.EventManager;
|
||||||
import cz.jzitnik.common.socket.messages.Test;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* UI component for global keyboard shortcuts.
|
* UI component for global keyboard shortcuts.
|
||||||
@@ -58,17 +57,4 @@ public class GlobalShortcuts {
|
|||||||
eventManager.emitEvent(new PlayerMoveEvent(event.getKeyStroke()));
|
eventManager.emitEvent(new PlayerMoveEvent(event.getKeyStroke()));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Sends a test socket message.
|
|
||||||
*
|
|
||||||
* @param event The keyboard event
|
|
||||||
* @return true
|
|
||||||
*/
|
|
||||||
@KeyboardPressHandler(character = 'p')
|
|
||||||
public boolean debugSocket(KeyboardPressEvent event) {
|
|
||||||
eventManager.emitEvent(new SendSocketMessageEvent(new Test()));
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package cz.jzitnik.client.game;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class RequirementTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructor() {
|
||||||
|
Requirement requirement = new Requirement("key");
|
||||||
|
assertEquals("key", requirement.itemType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testItemTypeCanBeNull() {
|
||||||
|
Requirement requirement = new Requirement(null);
|
||||||
|
assertNull(requirement.itemType());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testItemTypeEquality() {
|
||||||
|
Requirement req1 = new Requirement("sword");
|
||||||
|
Requirement req2 = new Requirement("sword");
|
||||||
|
assertEquals(req1, req2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDifferentItemTypesAreNotEqual() {
|
||||||
|
Requirement req1 = new Requirement("key");
|
||||||
|
Requirement req2 = new Requirement("sword");
|
||||||
|
assertNotEquals(req1, req2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testToString() {
|
||||||
|
Requirement requirement = new Requirement("gold");
|
||||||
|
assertTrue(requirement.toString().contains("gold"));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package cz.jzitnik.common.socket.messages.player;
|
||||||
|
|
||||||
|
import cz.jzitnik.common.models.coordinates.RoomCords;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class PlayerMoveTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructor() {
|
||||||
|
RoomCords cords = new RoomCords(10, 20);
|
||||||
|
PlayerMove move = new PlayerMove(cords, PlayerRotation.RIGHT);
|
||||||
|
|
||||||
|
assertEquals(cords, move.newCords());
|
||||||
|
assertEquals(PlayerRotation.RIGHT, move.playerRotation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testWithNullCords() {
|
||||||
|
PlayerMove move = new PlayerMove(null, PlayerRotation.FRONT);
|
||||||
|
assertNull(move.newCords());
|
||||||
|
assertEquals(PlayerRotation.FRONT, move.playerRotation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testWithNullRotation() {
|
||||||
|
RoomCords cords = new RoomCords(5, 5);
|
||||||
|
PlayerMove move = new PlayerMove(cords, null);
|
||||||
|
assertEquals(cords, move.newCords());
|
||||||
|
assertNull(move.playerRotation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAllRotations() {
|
||||||
|
RoomCords cords = new RoomCords(0, 0);
|
||||||
|
|
||||||
|
for (PlayerRotation rotation : PlayerRotation.values()) {
|
||||||
|
PlayerMove move = new PlayerMove(cords, rotation);
|
||||||
|
assertEquals(rotation, move.playerRotation());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEquality() {
|
||||||
|
RoomCords cords1 = new RoomCords(10, 20);
|
||||||
|
RoomCords cords2 = new RoomCords(10, 20);
|
||||||
|
|
||||||
|
PlayerMove move1 = new PlayerMove(cords1, PlayerRotation.LEFT);
|
||||||
|
PlayerMove move2 = new PlayerMove(cords2, PlayerRotation.LEFT);
|
||||||
|
|
||||||
|
assertEquals(move1, move2);
|
||||||
|
}
|
||||||
|
}
|
||||||
5
pom.xml
5
pom.xml
@@ -45,6 +45,11 @@
|
|||||||
<mainClass>none</mainClass>
|
<mainClass>none</mainClass>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>3.5.2</version>
|
||||||
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -106,5 +106,11 @@
|
|||||||
<artifactId>reflections</artifactId>
|
<artifactId>reflections</artifactId>
|
||||||
<version>0.10.2</version>
|
<version>0.10.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
72
server/src/test/java/cz/jzitnik/server/game/GameTest.java
Normal file
72
server/src/test/java/cz/jzitnik/server/game/GameTest.java
Normal file
@@ -0,0 +1,72 @@
|
|||||||
|
package cz.jzitnik.server.game;
|
||||||
|
|
||||||
|
import cz.jzitnik.common.socket.messages.items.ItemTookFromChest;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class GameTest {
|
||||||
|
|
||||||
|
private Game game;
|
||||||
|
private static final String PASSWORD = "test123";
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
game = new Game(PASSWORD, new ArrayList<>());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructor() {
|
||||||
|
assertEquals(PASSWORD, game.getPassword());
|
||||||
|
assertNotNull(game.getPlayers());
|
||||||
|
assertTrue(game.getPlayers().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetItemModifiers() {
|
||||||
|
assertNotNull(game.getItemModifiers());
|
||||||
|
assertTrue(game.getItemModifiers().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testItemModifiersAreIndependent() {
|
||||||
|
Game game1 = new Game("pass", new ArrayList<>());
|
||||||
|
Game game2 = new Game("pass", new ArrayList<>());
|
||||||
|
|
||||||
|
game1.getItemModifiers().add(new ItemTookFromChest("room1", 1));
|
||||||
|
|
||||||
|
assertTrue(game2.getItemModifiers().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPasswordIsStored() {
|
||||||
|
String customPassword = "secret";
|
||||||
|
Game customGame = new Game(customPassword, new ArrayList<>());
|
||||||
|
assertEquals(customPassword, customGame.getPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEmptyPassword() {
|
||||||
|
Game emptyPasswordGame = new Game("", new ArrayList<>());
|
||||||
|
assertEquals("", emptyPasswordGame.getPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testNullPassword() {
|
||||||
|
Game nullPasswordGame = new Game(null, new ArrayList<>());
|
||||||
|
assertNull(nullPasswordGame.getPassword());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPlayersListCanBeModifiedExternally() {
|
||||||
|
List<Client> players = new ArrayList<>();
|
||||||
|
Game game = new Game("pass", players);
|
||||||
|
|
||||||
|
assertDoesNotThrow(() -> players.add(new Client(null, null, null)));
|
||||||
|
assertEquals(1, game.getPlayers().size());
|
||||||
|
}
|
||||||
|
}
|
||||||
87
server/src/test/java/cz/jzitnik/server/game/PlayerTest.java
Normal file
87
server/src/test/java/cz/jzitnik/server/game/PlayerTest.java
Normal file
@@ -0,0 +1,87 @@
|
|||||||
|
package cz.jzitnik.server.game;
|
||||||
|
|
||||||
|
import cz.jzitnik.common.models.coordinates.RoomCords;
|
||||||
|
import cz.jzitnik.common.models.coordinates.RoomPart;
|
||||||
|
import cz.jzitnik.common.models.player.PlayerCreation;
|
||||||
|
import cz.jzitnik.common.socket.messages.player.PlayerRotation;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class PlayerTest {
|
||||||
|
|
||||||
|
private PlayerCreation playerCreation;
|
||||||
|
private RoomCords cords;
|
||||||
|
private RoomPart collider;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
cords = new RoomCords(100, 200);
|
||||||
|
collider = new RoomPart(new RoomCords(95, 195), new RoomCords(105, 205));
|
||||||
|
playerCreation = new PlayerCreation(cords, collider);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testConstructor() {
|
||||||
|
Player player = new Player(playerCreation);
|
||||||
|
assertEquals(playerCreation.getId(), player.getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGetCords() {
|
||||||
|
Player player = new Player(playerCreation);
|
||||||
|
assertNotNull(player.getCords());
|
||||||
|
assertEquals(cords.getX(), player.getCords().getX());
|
||||||
|
assertEquals(cords.getY(), player.getCords().getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testDefaultRotation() {
|
||||||
|
Player player = new Player(playerCreation);
|
||||||
|
assertEquals(PlayerRotation.FRONT, player.getPlayerRotation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSetPlayerRotation() {
|
||||||
|
Player player = new Player(playerCreation);
|
||||||
|
player.setPlayerRotation(PlayerRotation.LEFT);
|
||||||
|
assertEquals(PlayerRotation.LEFT, player.getPlayerRotation());
|
||||||
|
|
||||||
|
player.setPlayerRotation(PlayerRotation.RIGHT);
|
||||||
|
assertEquals(PlayerRotation.RIGHT, player.getPlayerRotation());
|
||||||
|
|
||||||
|
player.setPlayerRotation(PlayerRotation.BACK);
|
||||||
|
assertEquals(PlayerRotation.BACK, player.getPlayerRotation());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testSetCurrentRoom() {
|
||||||
|
Player player = new Player(playerCreation);
|
||||||
|
player.setCurrentRoom("room1");
|
||||||
|
assertEquals("room1", player.getCurrentRoom());
|
||||||
|
|
||||||
|
player.setCurrentRoom("room2");
|
||||||
|
assertEquals("room2", player.getCurrentRoom());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testToPlayerCreation() {
|
||||||
|
Player player = new Player(playerCreation);
|
||||||
|
PlayerCreation result = player.toPlayerCreation();
|
||||||
|
|
||||||
|
assertNotNull(result);
|
||||||
|
assertEquals(player.getCords().getX(), result.getPlayerCords().getX());
|
||||||
|
assertEquals(player.getCords().getY(), result.getPlayerCords().getY());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testIdIsFinal() {
|
||||||
|
Player player = new Player(playerCreation);
|
||||||
|
int originalId = player.getId();
|
||||||
|
|
||||||
|
playerCreation.setId(999);
|
||||||
|
|
||||||
|
assertEquals(originalId, player.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user