package uulm.teamname.marvelous.gamelibrary.events; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.*; import static org.junit.jupiter.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; class EventBuilderTest { EntityID[] turns; EntityID turn; Entity[] entities; Entity entity; EventBuilder filled; @BeforeEach void setUp() { 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 = turns[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) .withTargetEntity(new EntityID(EntityType.P1, 1)) .withTargetField(new IntVector2(11, 13)) .withAmount(15) .withOriginEntity(new EntityID(EntityType.P2, 4)) .withOriginField(new IntVector2(15, 3)) .withStoneType(StoneType.MindStone) .withRoundCount(3) .withTurnCount(4) .withCharacterOrder(new EntityID[]{ new EntityID(EntityType.P1, 4), new EntityID(EntityType.P1, 1), new EntityID(EntityType.P2, 2), new EntityID(EntityType.P2, 4), new EntityID(EntityType.P1, 3), new EntityID(EntityType.P2, 5),}) .withNextCharacter(new EntityID(EntityType.P2, 2)) .withPlayerWon(2) .withMessage("Some message") .withTimeLeft(11) .withEntities(new Entity[]{}) .withTurnOrder(new EntityID[]{ new EntityID(EntityType.P1, 4), new EntityID(EntityType.P1, 1), new EntityID(EntityType.P2, 2), new EntityID(EntityType.P2, 4), new EntityID(EntityType.P1, 3), new EntityID(EntityType.P2, 5),}) .withActiveCharacter(new EntityID(EntityType.P1, 1)) .withWinCondition(false) .withTeamIdentifier("Team25") .withCustomContent(new HashMap<>()); } @Test void builderToStringTest() { assertEquals("EventBuilder(non-null) {\ntype = TurnEvent\nroundCount = 5\n}", new EventBuilder(EventType.TurnEvent) .withRoundCount(5) .notNullToString()); } @Test void buildGameEventUncheckedActualEvents() { // System.out.println("Checks for mistakes in GameEvent creation"); var roundSetupEvent = new EventBuilder(EventType.RoundSetupEvent) .withRoundCount(4) .withCharacterOrder(turns) .buildGameEvent(); assertThat(roundSetupEvent.check()) .isTrue() .withFailMessage("RoundSetupEvent failed check"); 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) .withTurnCount(5) .buildGameEvent(); assertThat(turnEvent.check()) .isTrue() .withFailMessage("TurnEvent failed check"); var turnEventBaseline = new GameEvent(); turnEventBaseline.type = EventType.TurnEvent; turnEventBaseline.nextCharacter = turn; turnEventBaseline.turnCount = 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) .buildGameEvent(); assertThat(gameEvent.check()) .isTrue() .withFailMessage("GameEvent failed check"); 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) .buildGameStateEvent(); assertThat(gameStateEvent.check()) .isTrue() .withFailMessage("GameStateEvent failed check"); 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 buildGameStateEventWithTooManyProperties() { assertThat(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 .check()).isTrue(); } @Test void buildGameStateEvent() { assertThat(new EventBuilder(EventType.Ack) // needs no properties .buildGameStateEvent() .check()).isTrue(); assertThat(new EventBuilder(EventType.Nack).buildGameStateEvent().check()).isTrue(); assertThat(new EventBuilder(EventType.Req).buildGameStateEvent().check()).isTrue(); assertThat(new EventBuilder(EventType.GameStateEvent) // if properties missing throw exception .withTurnOrder(turns) .withActiveCharacter(turn) .buildGameStateEvent() .check()).isFalse(); assertThat(new EventBuilder(EventType.GameStateEvent) // no exception if all properties present .withEntities(entities) .withTurnOrder(turns) .withActiveCharacter(turn) .withWinCondition(false) .buildGameStateEvent() .check()).isTrue(); } @Test void buildCustomEvent() { // TODO: check CustomEvent validation for correctness } }