Skip to content

Commit

Permalink
t add Test for TargetGridElementEvaluatorImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominic authored and Dominic committed Dec 23, 2020
1 parent d5731a4 commit 8a5d14e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Optional;
import java.util.function.Predicate;

import com.myownb3.piranha.annotation.Visible4Testing;
import com.myownb3.piranha.core.battle.belligerent.Belligerent;
import com.myownb3.piranha.core.battle.belligerent.party.BelligerentParty;
import com.myownb3.piranha.core.battle.weapon.gun.projectile.Projectile;
Expand Down Expand Up @@ -43,6 +44,7 @@ private Predicate<? super GridElement> isGridElementDetected(Position detectorPo
return gridElement -> gridElement.isDetectedBy(detectorPos, detector);
}

@Visible4Testing
boolean isGridElementEnemy(GridElement gridElement) {
return (gridElement instanceof Belligerent) && belligerentParty.isEnemyParty(((Belligerent) gridElement).getBelligerentParty());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.myownb3.piranha.core.battle.weapon.target;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Mockito.mock;

import org.junit.jupiter.api.Test;

import com.myownb3.piranha.core.battle.belligerent.party.BelligerentPartyConst;
import com.myownb3.piranha.core.battle.destruction.DestructionHelper;
import com.myownb3.piranha.core.battle.weapon.target.TargetGridElementEvaluatorImpl.TargetGridElementEvaluatorBuilder;
import com.myownb3.piranha.core.detector.IDetector;
import com.myownb3.piranha.core.grid.Grid;
import com.myownb3.piranha.core.grid.gridelement.GridElement;
import com.myownb3.piranha.core.grid.gridelement.evaluator.GridElementEvaluator;
import com.myownb3.piranha.core.grid.gridelement.obstacle.ObstacleImpl;
import com.myownb3.piranha.core.grid.gridelement.obstacle.ObstacleImpl.ObstacleBuilder;
import com.myownb3.piranha.core.grid.gridelement.shape.dimension.DimensionInfoImpl.DimensionInfoBuilder;
import com.myownb3.piranha.core.grid.gridelement.shape.position.PositionShape.PositionShapeBuilder;
import com.myownb3.piranha.core.grid.position.Positions;

class TargetGridElementEvaluatorImplTest {

@Test
void testIsGridElementEnemy_NotBelligerentAtAll() {

// Given
GridElement gridElement = mock(GridElement.class);
TargetGridElementEvaluatorImpl targetGridElementEvaluator = (TargetGridElementEvaluatorImpl) TargetGridElementEvaluatorBuilder.builder()
.withBelligerentParty(BelligerentPartyConst.GALACTIC_EMPIRE)
.withDetector(mock(IDetector.class))
.withGridElementEvaluator(mock(GridElementEvaluator.class))
.build();
// When
boolean isActualGridElementEnemy = targetGridElementEvaluator.isGridElementEnemy(gridElement);

// Then
assertThat(isActualGridElementEnemy, is(false));
}

@Test
void testIsGridElementEnemy_Enemy() {

// Given
ObstacleImpl obstacleImpl = ObstacleBuilder.builder()
.withBelligerentParty(BelligerentPartyConst.REBEL_ALLIANCE)
.withDestructionHelper(mock(DestructionHelper.class))
.withDimensionInfo(DimensionInfoBuilder.getDefaultDimensionInfo(1))
.withShape(PositionShapeBuilder.builder()
.withPosition(Positions.of(5, 5))
.build())
.withGrid(mock(Grid.class))
.build();
TargetGridElementEvaluatorImpl targetGridElementEvaluator = (TargetGridElementEvaluatorImpl) TargetGridElementEvaluatorBuilder.builder()
.withBelligerentParty(BelligerentPartyConst.GALACTIC_EMPIRE)
.withDetector(mock(IDetector.class))
.withGridElementEvaluator(mock(GridElementEvaluator.class))
.build();
// When
boolean isActualGridElementEnemy = targetGridElementEvaluator.isGridElementEnemy(obstacleImpl);

// Then
assertThat(isActualGridElementEnemy, is(true));
}

@Test
void testIsGridElementEnemy_Friend() {

// Given
ObstacleImpl obstacleImpl = ObstacleBuilder.builder()
.withBelligerentParty(BelligerentPartyConst.GALACTIC_EMPIRE)
.withDestructionHelper(mock(DestructionHelper.class))
.withDimensionInfo(DimensionInfoBuilder.getDefaultDimensionInfo(1))
.withShape(PositionShapeBuilder.builder()
.withPosition(Positions.of(5, 5))
.build())
.withGrid(mock(Grid.class))
.build();
TargetGridElementEvaluatorImpl targetGridElementEvaluator = (TargetGridElementEvaluatorImpl) TargetGridElementEvaluatorBuilder.builder()
.withBelligerentParty(BelligerentPartyConst.GALACTIC_EMPIRE)
.withDetector(mock(IDetector.class))
.withGridElementEvaluator(mock(GridElementEvaluator.class))
.build();
// When
boolean isActualGridElementEnemy = targetGridElementEvaluator.isGridElementEnemy(obstacleImpl);

// Then
assertThat(isActualGridElementEnemy, is(false));
}

}

0 comments on commit 8a5d14e

Please sign in to comment.