feat: partially finished test for EventBuilder

This commit is contained in:
Yannik Bretschneider 2021-05-02 04:14:28 +02:00
parent c190111de1
commit 3e36947d3e

View File

@ -1,22 +1,88 @@
package uulm.teamname.marvelous.gamelibrary.events; package uulm.teamname.marvelous.gamelibrary.events;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*; import static org.assertj.core.api.Assertions.*;
import uulm.teamname.marvelous.gamelibrary.IntVector2; import uulm.teamname.marvelous.gamelibrary.IntVector2;
import uulm.teamname.marvelous.gamelibrary.entities.*; import uulm.teamname.marvelous.gamelibrary.entities.*;
import uulm.teamname.marvelous.gamelibrary.entities.Character;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class EventBuilderTest { class EventBuilderTest {
EntityID[] turns;
EntityID turn;
Entity[] entities;
Entity entity;
EventBuilder filled;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
EventBuilder builder = new EventBuilder() turns = new EntityID[]{
.withType(EventType.CustomEvent) new EntityID(EntityType.P1, 0),
new EntityID(EntityType.P1, 1),
new EntityID(EntityType.P1, 2),
new EntityID(EntityType.P1, 3),
new EntityID(EntityType.P1, 4),
new EntityID(EntityType.P1, 5),
new EntityID(EntityType.P2, 5),
new EntityID(EntityType.P2, 4),
new EntityID(EntityType.P2, 3),
new EntityID(EntityType.P2, 2),
new EntityID(EntityType.P2, 1),
new EntityID(EntityType.P2, 0),
};
turn = new EntityID(EntityType.P1, 0);
entities = new Entity[]{
new Character(
new EntityID(EntityType.P1, 4),
new IntVector2(4, 16),
"This is a name",
15,
14,
13,
12,
11,
10),
new Character(
new EntityID(EntityType.P2, 2),
new IntVector2(4, 16),
"This is a name",
16,
17,
18,
19,
21,
25),
new NPC(
new EntityID(EntityType.NPC, 1),
new IntVector2(11, 14),
new ArrayList<>()
)
};
entity = new Character(
new EntityID(EntityType.P2, 2),
new IntVector2(4, 16),
"This is a name",
16,
17,
18,
19,
21,
25);
filled = new EventBuilder(EventType.CustomEvent)
// .withType(EventType.CustomEvent)
.withTargetEntity(new EntityID(EntityType.P1, 1)) .withTargetEntity(new EntityID(EntityType.P1, 1))
.withTargetField(new IntVector2(11, 13)) .withTargetField(new IntVector2(11, 13))
.withAmount(15) .withAmount(15)
@ -25,7 +91,7 @@ class EventBuilderTest {
.withStoneType(StoneType.MindStone) .withStoneType(StoneType.MindStone)
.withRoundCount(3) .withRoundCount(3)
.withTurnCount(4) .withTurnCount(4)
.withCharacterOrder(new EntityID[] { .withCharacterOrder(new EntityID[]{
new EntityID(EntityType.P1, 4), new EntityID(EntityType.P1, 4),
new EntityID(EntityType.P1, 1), new EntityID(EntityType.P1, 1),
new EntityID(EntityType.P2, 2), new EntityID(EntityType.P2, 2),
@ -36,8 +102,8 @@ class EventBuilderTest {
.withPlayerWon(2) .withPlayerWon(2)
.withMessage("Some message") .withMessage("Some message")
.withTimeLeft(11) .withTimeLeft(11)
.withEntities(new Entity[] {}) .withEntities(new Entity[]{})
.withTurnOrder(new EntityID[] { .withTurnOrder(new EntityID[]{
new EntityID(EntityType.P1, 4), new EntityID(EntityType.P1, 4),
new EntityID(EntityType.P1, 1), new EntityID(EntityType.P1, 1),
new EntityID(EntityType.P2, 2), new EntityID(EntityType.P2, 2),
@ -51,27 +117,141 @@ class EventBuilderTest {
} }
@Test @Test
void buildGameEvent() { void buildGameEventUncheckedActualEvents() {
var gameEvent = new EventBuilder() // System.out.println("Checks for mistakes in GameEvent creation");
.withType(EventType.DisconnectEvent) var roundSetupEvent = new EventBuilder(EventType.RoundSetupEvent)
.buildEntityEvent(); .withRoundCount(4)
.withCharacterOrder(turns)
.buildGameEvent();
assertThat(gameEvent).isEqualTo() var roundSetupEventBaseline = new GameEvent();
roundSetupEventBaseline.type = EventType.RoundSetupEvent;
roundSetupEventBaseline.roundCount = 4;
roundSetupEventBaseline.characterOrder = turns;
assertThat(roundSetupEvent)
.isEqualTo(roundSetupEventBaseline)
.withFailMessage("RoundSetupEvent built improperly");
var turnEvent = new EventBuilder(EventType.TurnEvent)
.withNextCharacter(turn)
.withRoundCount(5)
.buildGameEventUnchecked();
var turnEventBaseline = new GameEvent();
turnEventBaseline.type = EventType.TurnEvent;
turnEventBaseline.nextCharacter = turn;
turnEventBaseline.roundCount = 5;
assertThat(turnEvent)
.isEqualTo(turnEventBaseline)
.withFailMessage("TurnEvent was built improperly");
} }
@Test
void buildGameEventsUncheckedTest() {
// System.out.println("Checks for unsafe (direct) event creation with non-standard GameEvents");
var gameEvent = new EventBuilder(EventType.DisconnectEvent) // this event is invalid, but can be tested against
.withRoundCount(4112)
.withTurnCount(2113)
.withCharacterOrder(turns)
.withNextCharacter(turn)
.withPlayerWon(5912)
.withMessage("This message is very much not useful at all")
.withTimeLeft(-144)
.buildGameEventUnchecked();
var baseline = new GameEvent();
baseline.type = EventType.DisconnectEvent;
baseline.roundCount = 4112;
baseline.turnCount = 2113;
baseline.characterOrder = turns;
baseline.nextCharacter = turn;
baseline.playerWon = 5912;
baseline.message = "This message is very much not useful at all";
baseline.timeLeft = -144;
assertThat(gameEvent)
.isEqualTo(baseline)
.withFailMessage("GameEvent was built improperly");
}
@Test
void buildGameStateEventsUncheckedTest() {
// System.out.println("Checks for unsafe (direct) event creation with non-standard GameStateEvents");
var gameStateEvent = new EventBuilder(EventType.ConsumedAPEvent)
.withEntities(entities)
.withTurnOrder(turns)
.withActiveCharacter(turn)
.withWinCondition(true)
.buildGameStateEventUnchecked();
var baseline = new GameStateEvent();
baseline.type = EventType.ConsumedAPEvent;
baseline.entities = entities;
baseline.turnOrder = turns;
baseline.activeCharacter = turn;
baseline.winCondition = true;
assertThat(gameStateEvent)
.isEqualTo(baseline)
.withFailMessage("GameStateEvent was built improperly");
}
// TODO: implement tests for unchecked CharacterEvents and EntityEvents
@Test @Test
void buildEntityEvent() { void buildEntityEvent() {
// TODO: check all entityEvent type validations for correctness
} }
@Test @Test
void buildCharacterEvent() { void buildCharacterEvent() {
// TODO: check all characterEvent type validations for correctness
} }
@Test @Test
void buildGamestateEvent() { void buildGameStateEventWithTooManyProperties() {
assertThatNoException()
.isThrownBy(() -> new EventBuilder(EventType.Ack) // too many properties is fine
.withAmount(15) // also properties of different EventTypes, they just get ignored
.withEntities(entities) // properties belonging to the same eventType get incorporated into
.withWinCondition(false) // the final event, so they have to be ignored
.buildGameStateEvent()); // by the programmer later on
}
@Test
void buildGameStateEvent() {
assertThatNoException()
.isThrownBy(() -> new EventBuilder(EventType.Ack) // needs no properties
.buildGameStateEvent());
assertThatNoException()
.isThrownBy(() -> new EventBuilder(EventType.Nack).buildGameStateEvent());
assertThatNoException()
.isThrownBy(() -> new EventBuilder(EventType.Req).buildGameStateEvent());
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> new EventBuilder(EventType.GameStateEvent) // if properties missing throw exception
.withTurnOrder(turns)
.withActiveCharacter(turn)
.buildGameStateEvent());
assertThatNoException()
.isThrownBy(() -> new EventBuilder(EventType.GameStateEvent) // no exception if all properties present
.withEntities(entities)
.withTurnOrder(turns)
.withActiveCharacter(turn)
.withWinCondition(false)
.buildGameStateEvent());
} }
@Test @Test
void buildCustomEvent() { void buildCustomEvent() {
// TODO: check CustomEvent validation for correctness
} }
} }