From 3e36947d3e13036cbf701f5be56311c3c9c7726e Mon Sep 17 00:00:00 2001 From: Yannik Bretschneider Date: Sun, 2 May 2021 04:14:28 +0200 Subject: [PATCH] feat: partially finished test for EventBuilder --- .../gamelibrary/events/EventBuilderTest.java | 202 +++++++++++++++++- 1 file changed, 191 insertions(+), 11 deletions(-) diff --git a/src/test/java/uulm/teamname/marvelous/gamelibrary/events/EventBuilderTest.java b/src/test/java/uulm/teamname/marvelous/gamelibrary/events/EventBuilderTest.java index f138822..31d0783 100644 --- a/src/test/java/uulm/teamname/marvelous/gamelibrary/events/EventBuilderTest.java +++ b/src/test/java/uulm/teamname/marvelous/gamelibrary/events/EventBuilderTest.java @@ -1,22 +1,88 @@ package uulm.teamname.marvelous.gamelibrary.events; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; + 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 java.util.ArrayList; import java.util.HashMap; import static org.mockito.Mockito.*; import static org.junit.jupiter.api.Assertions.*; class EventBuilderTest { + EntityID[] turns; + EntityID turn; + Entity[] entities; + Entity entity; + EventBuilder filled; @BeforeEach void setUp() { - EventBuilder builder = new EventBuilder() - .withType(EventType.CustomEvent) + turns = new EntityID[]{ + 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)) .withTargetField(new IntVector2(11, 13)) .withAmount(15) @@ -25,7 +91,7 @@ class EventBuilderTest { .withStoneType(StoneType.MindStone) .withRoundCount(3) .withTurnCount(4) - .withCharacterOrder(new EntityID[] { + .withCharacterOrder(new EntityID[]{ new EntityID(EntityType.P1, 4), new EntityID(EntityType.P1, 1), new EntityID(EntityType.P2, 2), @@ -36,8 +102,8 @@ class EventBuilderTest { .withPlayerWon(2) .withMessage("Some message") .withTimeLeft(11) - .withEntities(new Entity[] {}) - .withTurnOrder(new EntityID[] { + .withEntities(new Entity[]{}) + .withTurnOrder(new EntityID[]{ new EntityID(EntityType.P1, 4), new EntityID(EntityType.P1, 1), new EntityID(EntityType.P2, 2), @@ -51,27 +117,141 @@ class EventBuilderTest { } @Test - void buildGameEvent() { - var gameEvent = new EventBuilder() - .withType(EventType.DisconnectEvent) - .buildEntityEvent(); + void buildGameEventUncheckedActualEvents() { +// System.out.println("Checks for mistakes in GameEvent creation"); + var roundSetupEvent = new EventBuilder(EventType.RoundSetupEvent) + .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 void buildEntityEvent() { + // TODO: check all entityEvent type validations for correctness } @Test void buildCharacterEvent() { + // TODO: check all characterEvent type validations for correctness } @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 void buildCustomEvent() { + // TODO: check CustomEvent validation for correctness } + } \ No newline at end of file