added some tests for Events, Requests and their Builders

This commit is contained in:
C H 2021-06-01 09:40:03 +02:00
parent 6c06beb366
commit fe7687bc21
2 changed files with 52 additions and 6 deletions

View File

@ -216,20 +216,34 @@ class EventBuilderTest {
.withFailMessage("GameStateEvent was built improperly"); .withFailMessage("GameStateEvent was built improperly");
} }
// TODO: implement tests for unchecked CharacterEvents and EntityEvents
@Test @Test
void buildWrongEntityEvent1() { void buildWrongEntityEvent() {
EventBuilder eb = new EventBuilder(EventType.DestroyedEntityEvent); EventBuilder eb = new EventBuilder(EventType.DestroyedEntityEvent);
//the target entity is not set //the target entity is not set
EntityEvent des2 = eb EntityEvent des2 = eb
.withTargetField(new IntVector2(1,1)) .withTargetField(new IntVector2(1,1))
.buildEntityEvent(); .buildEntityEvent();
//the target entity is not set --> check() return false //the target entity is not set --> check() return false
boolean b = des2.check(); boolean b = des2.check();
assertEquals(b, false); 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 @Test
void buildEntityEvent() { void buildEntityEvent() {
//testing EntityEvent class //testing EntityEvent class
@ -249,12 +263,20 @@ class EventBuilderTest {
.withTargetEntity(new EntityID(EntityType.P1,1)) .withTargetEntity(new EntityID(EntityType.P1,1))
.buildEntityEvent(); .buildEntityEvent();
//testing EventBuilder //testing EventBuilder and EntityEvent class
assertEquals(des.type, des2.type); assertEquals(des.type, des2.type);
assertEquals(des.targetEntity, des2.targetEntity); assertEquals(des.targetEntity, des2.targetEntity);
assertEquals(des.targetField, des2.targetField); assertEquals(des.targetField, des2.targetField);
// EntityEvent des2 = new EntityEvent(eb.); // 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());
} }
@ -282,13 +304,23 @@ class EventBuilderTest {
.withOriginEntity(new EntityID(EntityType.P2,2)) .withOriginEntity(new EntityID(EntityType.P2,2))
.withTargetEntity(new EntityID(EntityType.P1,1)) .withTargetEntity(new EntityID(EntityType.P1,1))
.buildCharacterEvent(); .buildCharacterEvent();
//testing EventBuilder
//testing EventBuilder and CharacterEvent class
assertEquals(des.type, des2.type); assertEquals(des.type, des2.type);
assertEquals(des.targetEntity, des2.targetEntity); assertEquals(des.targetEntity, des2.targetEntity);
assertEquals(des.targetField, des2.targetField); assertEquals(des.targetField, des2.targetField);
assertEquals(des.originEntity, des2.originEntity); assertEquals(des.originEntity, des2.originEntity);
assertEquals(des.originField, des2.originField); 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 @Test
@ -346,9 +378,20 @@ class EventBuilderTest {
.withCustomContent(new HashMap<String, Object>()) .withCustomContent(new HashMap<String, Object>())
.buildCustomEvent(); .buildCustomEvent();
//testing EventBuilder //testing EventBuilder and CustomEvent class
assertEquals(des.type, des2.type); assertEquals(des.type, des2.type);
assertEquals(des.customContent, des2.customContent); 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());
} }
} }

View File

@ -17,7 +17,7 @@ import java.util.HashMap;
class RequestBuilderTest { class RequestBuilderTest {
@Test @Test
void testEqualsCharacterRequest() { void testEqualsAndHashCodeCharacterRequest() {
CharacterRequest cr = new CharacterRequest(); CharacterRequest cr = new CharacterRequest();
cr.type = RequestType.MoveRequest; cr.type = RequestType.MoveRequest;
@ -55,6 +55,9 @@ class RequestBuilderTest {
assertEquals(cr.equals(cr2), true); assertEquals(cr.equals(cr2), true);
assertEquals(cr.equals(cr3), false); assertEquals(cr.equals(cr3), false);
assertEquals(cr.hashCode(), cr2.hashCode());
assertNotEquals(cr.hashCode(), cr3.hashCode());
} }
@Test @Test