Skip to content

Commit

Permalink
Add test for interaction of placement restrictions and stacking limit.
Browse files Browse the repository at this point in the history
  • Loading branch information
asvitkine committed Jul 28, 2023
1 parent a7e90df commit 2f66bcb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class AbstractDelegateTestCase extends AbstractClientSettingTest
protected Territory sfeSeaZone = gameData.getMap().getTerritory("Soviet Far East Sea Zone");
protected Territory brazil = gameData.getMap().getTerritory("Brazil");
protected Territory westCanada = gameData.getMap().getTerritory("West Canada");
protected Territory westCanadaSeaZone = gameData.getMap().getTerritory("West Canada Sea Zone");
protected Territory germany = gameData.getMap().getTerritory("Germany");
protected Territory syria = gameData.getMap().getTerritory("Syria Jordan");
protected Territory manchuria = gameData.getMap().getTerritory("Manchuria");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package games.strategy.triplea.delegate;

import static games.strategy.triplea.delegate.Matches.unitIsOfType;
import static games.strategy.triplea.delegate.MockDelegateBridge.newDelegateBridge;
import static games.strategy.triplea.delegate.remote.IAbstractPlaceDelegate.BidMode.NOT_BID;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

Expand All @@ -11,9 +15,12 @@
import games.strategy.engine.delegate.IDelegateBridge;
import games.strategy.triplea.delegate.data.PlaceableUnits;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.triplea.java.collections.CollectionUtils;

class PlaceDelegateTest extends AbstractDelegateTestCase {
private PlaceDelegate delegate;
Expand All @@ -27,7 +34,7 @@ void setupPlaceDelegate() {
delegate.start();
}

private Collection<Unit> create(GamePlayer player, UnitType unitType, int quantity) {
private List<Unit> create(GamePlayer player, UnitType unitType, int quantity) {
var units = unitType.create(quantity, player);
player.getUnitCollection().addAll(units);
return units;
Expand Down Expand Up @@ -199,4 +206,30 @@ void testPlayerAttachmentStackingLimit() {
units.addAll(create(british, carrier, 1));
assertError(delegate.canUnitsBePlaced(northSea, units, british));
}

@Test
void testStackingLimitFilteringHappensAfterPlacementRestrictions() {
// Note: battleship is marked as not placeable in "West Canada Sea Zone" on the test map.

// Add a carrier to the sea zone.
westCanadaSeaZone.getUnitCollection().addAll(create(british, carrier, 1));

// if we filter list of 2 battleships and 2 carriers, the 2 carriers should be selected.
List<Unit> units = create(british, battleship, 2);
units.addAll(create(british, carrier, 2));
// First, we can't place all of them (expected).
assertError(delegate.canUnitsBePlaced(westCanadaSeaZone, units, british));

PlaceableUnits response = delegate.getPlaceableUnits(units, westCanadaSeaZone);
assertThat(response.getUnits(), hasSize(2));
assertThat(response.getUnits(), is(CollectionUtils.getMatches(units, unitIsOfType(carrier))));

// Check that it's the case even if we shuffle the list a few times.
for (int i = 0; i < 5; i++) {
Collections.shuffle(units);
response = delegate.getPlaceableUnits(units, westCanadaSeaZone);
assertThat(response.getUnits(), hasSize(2));
assertThat(response.getUnits(), is(CollectionUtils.getMatches(units, unitIsOfType(carrier))));
}
}
}
14 changes: 10 additions & 4 deletions game-app/game-core/src/test/resources/DelegateTest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,8 @@
<option name="attack" value="4"/>
<option name="defense" value="4"/>
<option name="canBombard" value="true"/>
<!-- For PlaceDelegateTest.testStackingLimitFilteringHappensAfterPlacementRestrictions() -->
<option name="unitPlacementRestrictions" value="West Canada Sea Zone"/>
</attachment>
<attachment name="unitAttachment"
attachTo="carrier"
Expand Down Expand Up @@ -1078,19 +1080,23 @@
<propertyList>

<!-- OPTIONAL PROPERTIES -->
<property name="Place in Any Territory" value="true" editable="false">
<property name="Place in Any Territory" value="true">
<boolean/>
</property>

<property name="Unit Placement Per Territory Restricted" value="true" editable="false">
<property name="Unit Placement Restrictions" value="true">
<boolean/>
</property>

<property name="neutralCharge" value="3" editable="false">
<property name="Unit Placement Per Territory Restricted" value="true">
<boolean/>
</property>

<property name="neutralCharge" value="3">
<number min="0" max="9999999"/>
</property>

<property name="Heavy Bomber Dice Rolls" value="3" editable="false">
<property name="Heavy Bomber Dice Rolls" value="3">
<number min="1" max="10"/>
</property>

Expand Down

0 comments on commit 2f66bcb

Please sign in to comment.