Skip to content

Commit

Permalink
Fix AbilityTests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nightm4re94 committed Sep 11, 2023
1 parent 62aa268 commit 6b59039
Showing 1 changed file with 83 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
Expand All @@ -20,7 +21,6 @@
import de.gurkenlabs.litiengine.entities.EntityPivotType;
import de.gurkenlabs.litiengine.graphics.RenderEngine;
import de.gurkenlabs.litiengine.test.GameTestSuite;

import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Arc2D;
Expand All @@ -33,6 +33,7 @@

@ExtendWith(GameTestSuite.class)
class AbilityTests {

@BeforeEach
public void init() {
Game.init(Game.COMMANDLINE_ARG_NOGUI);
Expand Down Expand Up @@ -75,7 +76,7 @@ void isOnCooldownCooldownOver() {

AbilityExecution abExec = mock(AbilityExecution.class);
when(abExec.getExecutionTicks())
.thenReturn(Game.time().now() - (ability.getAttributes().cooldown().get() + 1));
.thenReturn(Game.time().now() - (ability.getAttributes().cooldown().get() + 1));
ability.setCurrentExecution(abExec);

// act
Expand Down Expand Up @@ -135,7 +136,7 @@ void testGetRemainingCooldownInSeconds_returnTime() {
ability.canCast();

AbilityExecution ae = mock(AbilityExecution.class);
when(ae.getExecutionTicks()).thenReturn(10l);
when(ae.getExecutionTicks()).thenReturn(10L);
ability.setCurrentExecution(ae);

// act
Expand All @@ -152,7 +153,7 @@ void testGetRemainingCooldownInSeconds_returnZero() {
ability.canCast();

AbilityExecution ae = mock(AbilityExecution.class);
when(ae.getExecutionTicks()).thenReturn(0l);
when(ae.getExecutionTicks()).thenReturn(0L);
ability.setCurrentExecution(ae);

// act
Expand All @@ -174,7 +175,7 @@ void testInitialization() {
assertEquals("I do somethin", ability.getName());
assertEquals("does somethin", ability.getDescription());
assertEquals(CastType.ONCONFIRM, ability.getCastType());
assertEquals(true, ability.isMultiTarget());
assertTrue(ability.isMultiTarget());

assertEquals(333, ability.getAttributes().cooldown().get().intValue());
assertEquals(222, ability.getAttributes().duration().get().intValue());
Expand All @@ -194,15 +195,14 @@ void testEffectInitialization() {
assertEquals(ability, effect.getAbility());
assertEquals(0, effect.getFollowUpEffects().size());
assertFalse(effect.isActive(entity));
assertArrayEquals(new EffectTarget[] {EffectTarget.ENEMY}, effect.getEffectTargets());
assertArrayEquals(new EffectTarget[]{EffectTarget.ENEMY}, effect.getEffectTargets());
}

@AbilityInfo(
impact = 150,
impactAngle = 360,
origin = EntityPivotType.DIMENSION_CENTER,
range = 100)
private class TestAbility360 extends Ability {
impact = 150,
origin = EntityPivotType.DIMENSION_CENTER,
range = 100)
private static class TestAbility360 extends Ability {

protected TestAbility360(Creature executor) {
super(executor);
Expand All @@ -222,19 +222,18 @@ void testInternalCalculateImpactArea360() {
TestAbility360 ability = new TestAbility360(new Creature());
Shape s = ability.internalCalculateImpactArea(90);
Ellipse2D e = (Ellipse2D) s;
assertTrue(e instanceof Ellipse2D);
assertEquals(-9.0, e.getX(), 0.001);
assertEquals(-59.0, e.getY(), 0.001);
assertEquals(150, e.getHeight(), 0.001);
assertEquals(150, e.getWidth(), 0.001);
assertEquals(-84.0, e.getX(), 0.001);
assertEquals(-134.0, e.getY(), 0.001);
assertEquals(300, e.getHeight(), 0.001);
assertEquals(300, e.getWidth(), 0.001);
}

@AbilityInfo(
impact = 111,
impactAngle = 180,
origin = EntityPivotType.DIMENSION_CENTER,
range = 150)
private class TestAbility180 extends Ability {
impact = 111,
impactAngle = 180,
origin = EntityPivotType.DIMENSION_CENTER,
range = 150)
private static class TestAbility180 extends Ability {

protected TestAbility180(Creature executor) {
super(executor);
Expand All @@ -254,11 +253,10 @@ void testInternalCalculateImpactArea180() {
TestAbility180 ability = new TestAbility180(new Creature());
Shape s = ability.internalCalculateImpactArea(0);
Arc2D a = (Arc2D) s;
assertTrue(a instanceof Arc2D);
assertEquals(-39.5, a.getX(), 0.001);
assertEquals(35.5, a.getY(), 0.001);
assertEquals(111, a.getHeight(), 0.001);
assertEquals(111, a.getWidth(), 0.001);
assertEquals(-95.0, a.getX(), 0.001);
assertEquals(-20, a.getY(), 0.001);
assertEquals(222, a.getHeight());
assertEquals(222, a.getWidth());
assertEquals(-180, a.getAngleStart(), 0.001);
assertEquals(180, a.getAngleExtent(), 0.001);
}
Expand Down Expand Up @@ -362,78 +360,89 @@ void testRender() {
ability.render(graphics);

// assert
verify(renderEngine, times(1)).renderShape(any(Graphics2D.class), any(Shape.class));
verify(renderEngine, times(1)).renderOutline(any(Graphics2D.class), any(Shape.class));
verify(renderEngine, times(1)).renderShape(
any(Graphics2D.class),
any(Shape.class),
eq(true));
verify(renderEngine, times(1)).renderOutline(
any(Graphics2D.class),
any(Shape.class),
eq(true));
}
}

@AbilityInfo(
castType = CastType.ONCONFIRM,
name = "I do somethin",
description = "does somethin",
cooldown = 333,
duration = 222,
impact = 111,
impactAngle = 99,
multiTarget = true,
origin = EntityPivotType.COLLISIONBOX_CENTER,
range = 444,
value = 999)
private class TestAbility extends Ability {
castType = CastType.ONCONFIRM,
name = "I do somethin",
description = "does somethin",
cooldown = 333,
duration = 222,
impact = 111,
impactAngle = 99,
multiTarget = true,
origin = EntityPivotType.COLLISIONBOX_CENTER,
range = 444,
value = 999)
private static class TestAbility extends Ability {

protected TestAbility(Creature executor) {
super(executor);
}
}

@AbilityInfo(impact = 0)
private class TestAbilityNoImpact extends Ability {
private static class TestAbilityNoImpact extends Ability {

protected TestAbilityNoImpact(Creature executor) {
super(executor);
}
}

private class TestEffect extends Effect {
private static class TestEffect extends Effect {

protected TestEffect(Ability ability, EffectTarget... targets) {
super(ability, targets);
}
}

@AbilityInfo(origin = EntityPivotType.LOCATION)
private class TestOriginLocation extends Ability {
private static class TestOriginLocation extends Ability {

protected TestOriginLocation(Creature executor) {
super(executor);
}
}

@AbilityInfo(origin = EntityPivotType.OFFSET)
private class TestOriginCustom extends Ability {
private static class TestOriginCustom extends Ability {

protected TestOriginCustom(Creature executor) {
super(executor);
}
}

@AbilityInfo(origin = EntityPivotType.COLLISIONBOX_CENTER)
private class TestOriginCollisionBox extends Ability {
private static class TestOriginCollisionBox extends Ability {

protected TestOriginCollisionBox(Creature executor) {
super(executor);
}
}

@AbilityInfo(origin = EntityPivotType.DIMENSION_CENTER)
private class TestOriginDimension extends Ability {
private static class TestOriginDimension extends Ability {

protected TestOriginDimension(Creature executor) {
super(executor);
}
}

/**
* Test getPotentialCollisionBox when the collision box of the entity is the zero rectangle and the impact of the of the
* ability is zero.
* Test getPotentialCollisionBox when the collision box of the entity is the zero rectangle and
* the impact of the of the ability is zero.
*
* <p>
* Expected: potentialImpactArea() is an ellipse with a center in the origin and a zero width and height.
* Expected: potentialImpactArea() is an ellipse with a center in the origin and a zero width and
* height.
*/
@Test
void testGetPotentialCollisionZeroBoxZeroImpact() {
Expand All @@ -448,18 +457,18 @@ void testGetPotentialCollisionZeroBoxZeroImpact() {
}

/**
* Test getPotentialCollisionBox when the collision box of the entity is non-zero and the impact of the of the ability
* is zero.
* Test getPotentialCollisionBox when the collision box of the entity is non-zero and the impact
* of the of the ability is zero.
*
* <p>
* Expected: potentialImpactArea() is an ellipse with a center corresponding to the collisionbox and a zero width and
* height.
* Expected: potentialImpactArea() is an ellipse with a center corresponding to the collisionbox
* and a zero width and height.
*/
@Test
void testGetPotentialCollisionBoxNonZeroBoxZeroImpact() {
Rectangle2D collisonBox = new Rectangle2D.Double(1, 1, 1, 1);
Ellipse2D ellipse =
new Ellipse2D.Double(collisonBox.getCenterX(), collisonBox.getCenterY(), 0, 0);
new Ellipse2D.Double(collisonBox.getCenterX(), collisonBox.getCenterY(), 0, 0);

Creature entity = mock(Creature.class);
when(entity.getCollisionBox()).thenReturn(collisonBox);
Expand All @@ -469,12 +478,12 @@ void testGetPotentialCollisionBoxNonZeroBoxZeroImpact() {
}

/**
* Test getPotentialCollisionBox when the collision box of the entity is the zero rectangle, and the impact of the of
* the ability is non-zero.
* Test getPotentialCollisionBox when the collision box of the entity is the zero rectangle, and
* the impact of the of the ability is non-zero.
*
* <p>
* Expected: potentialImpactArea() is an ellipse with a center shifted by half of the negative impact from the origin,
* and a width and height corresponding to the impact.
* Expected: potentialImpactArea() is an ellipse with a center shifted by half of the negative
* impact from the origin, and a width and height corresponding to the impact.
*/
@Test
void testGetPotentialCollisionBoxZeroBoxNonZeroImpact() {
Expand All @@ -489,12 +498,12 @@ void testGetPotentialCollisionBoxZeroBoxNonZeroImpact() {
}

/**
* Test getPotentialCollisionBox when the collision box of the entity is non-zero, and the impact of the of the ability
* is non-zero.
* Test getPotentialCollisionBox when the collision box of the entity is non-zero, and the impact
* of the of the ability is non-zero.
*
* <p>
* Expected: potentialImpactArea() is an ellipse with a center corresponding to the collisionbox shifted by half of the
* negative impact, and a width and height corresponding to the impact.
* Expected: potentialImpactArea() is an ellipse with a center corresponding to the collisionbox
* shifted by half of the negative impact, and a width and height corresponding to the impact.
*/
@Test
void testGetPotentialCollisionBoxNonZeroBoxNonZeroImpact() {
Expand All @@ -508,21 +517,23 @@ void testGetPotentialCollisionBoxNonZeroBoxNonZeroImpact() {
assertEquals(ellipse, ability.calculatePotentialImpactArea());
}

/** If the executor is dead it is not possible to cast Expected: canCast() is false. */
/**
* If the executor is dead it is not possible to cast Expected: canCast() is false.
*/
@Test
void testCanCastWhenDead() {
Creature entity = mock(Creature.class);
when(entity.isDead()).thenReturn(true);

TestAbility a = new TestAbility(entity);
assertEquals(a.getExecutor(), entity);
assertFalse(!a.getExecutor().isDead());
assertTrue(a.getExecutor().isDead());
assertFalse(a.canCast());
}

/**
* If the executor is alive and the ability has no current execution, it is possible to cast. Expected: canCast() is
* true.
* If the executor is alive and the ability has no current execution, it is possible to cast.
* Expected: canCast() is true.
*/
@Test
void testCanCastWhenNoExecution() {
Expand All @@ -533,28 +544,28 @@ void testCanCastWhenNoExecution() {
a.setCurrentExecution(null);

assertEquals(a.getExecutor(), entity);
assertTrue(!a.getExecutor().isDead());
assertFalse(a.getExecutor().isDead());
assertNull(a.getCurrentExecution());
assertTrue(a.canCast());
}

/**
* If the executor is alive and the execution has no execution ticks left, it is possible to cast. Expected: canCast()
* is true.
* If the executor is alive and the execution has no execution ticks left, it is possible to cast.
* Expected: canCast() is true.
*/
@Test
void testCanCastWhenNoExecutionticks() {
Creature entity = mock(Creature.class);
when(entity.isDead()).thenReturn(false);

AbilityExecution ae = mock(AbilityExecution.class);
when(ae.getExecutionTicks()).thenReturn(0l);
when(ae.getExecutionTicks()).thenReturn(0L);

TestAbility a = new TestAbility(entity);
a.setCurrentExecution(ae);

assertEquals(a.getExecutor(), entity);
assertTrue(!a.getExecutor().isDead());
assertFalse(a.getExecutor().isDead());
assertNotNull(a.getCurrentExecution());
assertEquals(0, a.getCurrentExecution().getExecutionTicks());
assertTrue(a.canCast());
Expand Down

0 comments on commit 6b59039

Please sign in to comment.