Gamelib/src/test/java/uulm/teamname/marvelous/gamelibrary/events/EventBuilderTest.java

398 lines
14 KiB
Java

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)
)
};
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");
}
@Test
void buildWrongEntityEvent() {
EventBuilder eb = new EventBuilder(EventType.DestroyedEntityEvent);
//the target entity is not set
EntityEvent des2 = eb
.withTargetField(new IntVector2(1,1))
.buildEntityEvent();
//the target entity is not set --> check() return false
boolean b = des2.check();
assertEquals(b, false);
}
@Test
void buildWrongCharacterEvent() {
EventBuilder eb = new EventBuilder(EventType.MeleeAttackEvent);
//the target entity is not set
CharacterEvent des2 = eb
.withTargetField(new IntVector2(1,1))
.buildCharacterEvent();
//the target entity, the origin and the target entity are not set --> check() return false
boolean b = des2.check();
assertEquals(b, false);
}
@Test
void buildEntityEvent() {
//testing EntityEvent class
assertThat(new EventBuilder(EventType.DestroyedEntityEvent)
.withTargetField(new IntVector2(1,1))
.withTargetEntity(new EntityID(EntityType.P1,1))
.buildEntityEvent().check()).isTrue();
EntityEvent des = new EntityEvent();
des.type = EventType.DestroyedEntityEvent;
des.targetEntity= new EntityID(EntityType.P1,1);
des.targetField= new IntVector2(1,1);
EventBuilder eb = new EventBuilder(EventType.DestroyedEntityEvent);
EntityEvent des2 = eb
.withTargetField(new IntVector2(1,1))
.withTargetEntity(new EntityID(EntityType.P1,1))
.buildEntityEvent();
//testing EventBuilder and EntityEvent class
assertEquals(des.type, des2.type);
assertEquals(des.targetEntity, des2.targetEntity);
assertEquals(des.targetField, des2.targetField);
// EntityEvent des2 = new EntityEvent(eb.);
assertEquals(des.equals(des2), true);
assertEquals(des.hashCode(), des2.hashCode());
des.targetField = new IntVector2(3,3);
assertEquals(des.equals(des2), false);
assertNotEquals(des.hashCode(), des2.hashCode());
}
@Test
void buildCharacterEvent() {
//testing CharacterEvent class
assertThat(new EventBuilder(EventType.MeleeAttackEvent)
.withOriginField(new IntVector2(2,2))
.withTargetField(new IntVector2(1,1))
.withOriginEntity(new EntityID(EntityType.P2,2))
.withTargetEntity(new EntityID(EntityType.P1,1))
.buildEntityEvent().check()).isTrue();
CharacterEvent des = new CharacterEvent();
des.type = EventType.MeleeAttackEvent;
des.originField= new IntVector2(2,2);
des.targetEntity= new EntityID(EntityType.P1,1);
des.originEntity = new EntityID(EntityType.P2,2);
des.targetField= new IntVector2(1,1);
EventBuilder eb = new EventBuilder(EventType.MeleeAttackEvent);
CharacterEvent des2 = eb
.withOriginField(new IntVector2(2,2))
.withTargetField(new IntVector2(1,1))
.withOriginEntity(new EntityID(EntityType.P2,2))
.withTargetEntity(new EntityID(EntityType.P1,1))
.buildCharacterEvent();
//testing EventBuilder and CharacterEvent class
assertEquals(des.type, des2.type);
assertEquals(des.targetEntity, des2.targetEntity);
assertEquals(des.targetField, des2.targetField);
assertEquals(des.originEntity, des2.originEntity);
assertEquals(des.originField, des2.originField);
assertEquals(des.equals(des2), true);
assertEquals(des.hashCode(), des2.hashCode());
des.targetField = new IntVector2(3,3);
assertEquals(des.equals(des2), false);
assertNotEquals(des.hashCode(), des2.hashCode());
}
@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)
.withMapSize(new IntVector2(42, 24))
.withActiveCharacter(turn)
.withStoneCooldowns(new Integer[] {6, 5, 4, 3, 2, 1})
.withWinCondition(false)
.buildGameStateEvent()
.check()).isTrue();
}
@Test
void buildCustomEvent() {
//testing CustomEvent class
assertThat(new EventBuilder(EventType.CustomEvent)
.withCustomContent(new HashMap<String, Object>())
.buildCustomEvent().check()).isTrue();
CustomEvent des = new CustomEvent();
des.type = EventType.CustomEvent;
des.customContent= new HashMap<String, Object>();
EventBuilder eb = new EventBuilder(EventType.CustomEvent);
CustomEvent des2 = eb
.withCustomContent(new HashMap<String, Object>())
.buildCustomEvent();
//testing EventBuilder and CustomEvent class
assertEquals(des.type, des2.type);
assertEquals(des.customContent, des2.customContent);
assertEquals(des.equals(des2), true);
assertEquals(des.hashCode(), des2.hashCode());
Object o = new Object();
des.customContent = new HashMap<String, Object>(1,1);
des.customContent.put("a",o);
assertEquals(des.equals(des2), false);
assertNotEquals(des.hashCode(), des2.hashCode());
}
}