feat: partially created a test for EventDeserialzer, still needs some work

This commit is contained in:
Yannik Bretschneider 2021-05-17 20:10:42 +02:00
parent f3744ef225
commit 1bd4e8a9c5

View File

@ -9,10 +9,11 @@ import static org.assertj.core.api.Assertions.*;
import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.*;
import uulm.teamname.marvelous.gamelibrary.entities.Character;
import uulm.teamname.marvelous.gamelibrary.events.Event;
import uulm.teamname.marvelous.gamelibrary.events.EventType;
import uulm.teamname.marvelous.gamelibrary.events.GamestateEvent;
import uulm.teamname.marvelous.gamelibrary.events.NotificationEvent;
import uulm.teamname.marvelous.gamelibrary.events.*;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.ThreadLocalRandom;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;
@ -174,7 +175,8 @@ class EventDeserializerTest {
{"entityID": "P2", "ID": 1},
{"entityID": "P2", "ID": 2},
{"entityID": "P2", "ID": 3},
{"entityID": "P2", "ID": 4}
{"entityID": "P2", "ID": 4},
{"entityID": "P2", "ID": 5}
],
"mapSize": [15, 300],
"activeCharacter": {"entityID": "P2", "ID": 3},
@ -192,4 +194,102 @@ class EventDeserializerTest {
.isEqualTo(gamestate);
}
@Test
void GameEventsTest() {
Iterator<Integer> randomIntegers = ThreadLocalRandom.current().ints().iterator();
int repetitions = 3;
// Keys in a Game Event are:
// message, roundCount, turnCount, playerWon, timeLeft, characterOrder, nextCharacter
EventType type;
final StringBuilder message = new StringBuilder();
final int messageLength = 200;
final int minMessageLength = 5;
int roundCount;
int turnCount;
int playerWon;
int timeLeft;
EntityID[] characterOrder;
final HashSet<EntityID> usedCharacters = new HashSet<>();
EntityID nextCharacter;
for (int i = 0; i < repetitions; i++) {
// Get a random type of GameEvent
switch (Math.abs(randomIntegers.next()) % 8) {
case 0 -> type = EventType.TurnEvent;
case 1 -> type = EventType.WinEvent;
case 2 -> type = EventType.TurnTimeoutEvent;
case 3 -> type = EventType.TimeoutWarningEvent;
case 4 -> type = EventType.TimeoutEvent;
case 5 -> type = EventType.PauseStartEvent;
case 6 -> type = EventType.PauseStopEvent;
case 7 -> type = EventType.DisconnectEvent;
default -> throw new IllegalStateException("Unexpected value: " + Math.abs(randomIntegers.next()) % 8);
}
// Get a random message string
int nameLength = Math.abs(randomIntegers.next() % 12);
for (int j = 0; j < (nameLength) + 3; j++) {
// append char between A and Z, and make lowercase 50% of the time
message.append((char) (
(65 + Math.abs(randomIntegers.next() % 26))
+ 32 * Math.abs(randomIntegers.next() % 2)
));
}
// Get (realistic-seeming) random values for the int variables
roundCount = Math.abs(randomIntegers.next()) % 1000;
turnCount = Math.abs(randomIntegers.next()) % 6000;
playerWon = Math.abs(randomIntegers.next()) % 2 + 1;
timeLeft = Math.abs(randomIntegers.next()) % 600 + 1; // zero time left wouldn't make sense
// Get character order
characterOrder = new EntityID[12];
for (int j = 0; j < 12; j++) {
EntityID entityID;
do { // generate new ids until id was not added before. Inefficient, but whatever.
entityID = new EntityID(
Math.abs(randomIntegers.next()) % 2 == 1 ? EntityType.P1 : EntityType.P2,
Math.abs(randomIntegers.next()) % 6);
} while (!usedCharacters.add(entityID));
characterOrder[i] = entityID;
}
nextCharacter = characterOrder[Math.abs(randomIntegers.next()) % 12];
var gameEvent = new GameEvent();
gameEvent.type = type;
gameEvent.message = message.toString();
gameEvent.nextCharacter = nextCharacter;
gameEvent.timeLeft = timeLeft;
gameEvent.playerWon = playerWon;
gameEvent.characterOrder = characterOrder;
gameEvent.turnCount = turnCount;
gameEvent.roundCount = roundCount;
var jsonRepresentingGameEvent = """
{
"eventType": "%s",
}
""";
message.setLength(0);
usedCharacters.clear();
}
}
// TODO: Write tests for the other event types
// NOTE: it is not required to check all, only the categories (like GamestateEvent or CharacterEvent)
// because of unchecked event building in the deserializer
}