-
Notifications
You must be signed in to change notification settings - Fork 396
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
haveResources w/testing and checkAIPlayer
Adding haveResources and checkAIPlayer to RulesAttachment. Included is RulesAttachmentTest which uses "VICTORY_TEST" to test haveResources. checkAIPlayer is not tested because of internal testing for boolean values. Cheers...
- Loading branch information
Showing
2 changed files
with
274 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
163 changes: 163 additions & 0 deletions
163
game-app/game-core/src/test/java/games/strategy/triplea/attachments/RulesAttachmentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package games.strategy.triplea.attachments; | ||
|
||
import static games.strategy.triplea.Constants.PUS; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import games.strategy.engine.data.GameData; | ||
import games.strategy.engine.data.GamePlayer; | ||
import games.strategy.engine.data.gameparser.GameParseException; | ||
import java.security.SecureRandom; | ||
import java.util.List; | ||
|
||
import games.strategy.triplea.delegate.GameDataTestUtil; | ||
import games.strategy.triplea.xml.TestMapGameData; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class RulesAttachmentTest { | ||
|
||
/** | ||
* "Victory" map is just a branch/mod of Pact of Steel 2. POS2 is an actual game with good gameplay | ||
* that we don't want to mess with, so "Victory" is more of an xml purely for testing purposes, and | ||
* probably should never be played. | ||
*/ | ||
private final GameData gameData = TestMapGameData.VICTORY_TEST.getGameData(); | ||
private final RulesAttachment attachment = new RulesAttachment("Test attachment", null, gameData); | ||
|
||
@Nested | ||
class HaveResources { | ||
|
||
private final GamePlayer italians = GameDataTestUtil.italians(gameData); | ||
private final GamePlayer germans = GameDataTestUtil.germans(gameData); | ||
private final String FUEL = "Fuel"; | ||
private final String ORE = "Ore"; | ||
private final String addString = "add"; | ||
private final String sumString ="SUM"; | ||
|
||
/* Length test for haveResources */ | ||
@Test | ||
void setHaveResourcesInvalidLength() { | ||
assertThrows(GameParseException.class, () -> attachment.setHaveResources("")); | ||
assertThrows(GameParseException.class, () -> attachment.setHaveResources(":add")); | ||
|
||
assertThrows(GameParseException.class, () -> attachment.setHaveResources("a")); | ||
assertThrows(GameParseException.class, () -> attachment.setHaveResources("a:add")); | ||
} | ||
|
||
/* Invalid arguments for haveResources */ | ||
@Test | ||
void setHaveResourcesInvalidArgs() { | ||
/* Not a number (NAN) test */ | ||
assertThrows( | ||
IllegalArgumentException.class, | ||
() -> attachment.setHaveResources("NAN:PUs")); | ||
assertThrows( | ||
IllegalArgumentException.class, | ||
() -> attachment.setHaveResources("NAN:add:PUs")); | ||
/* -1 value test */ | ||
assertThrows( | ||
GameParseException.class, | ||
() -> attachment.setHaveResources("0:PUs")); | ||
assertThrows( | ||
GameParseException.class, | ||
() -> attachment.setHaveResources("0:add:PUs")); | ||
/* Not a resource test */ | ||
assertThrows( | ||
GameParseException.class, | ||
() -> attachment.setHaveResources("1:NOT A RESOURCE")); | ||
assertThrows( | ||
GameParseException.class, | ||
() -> attachment.setHaveResources("1:Sum:NOT A RESOURCE")); | ||
assertThrows( | ||
GameParseException.class, () -> attachment.setHaveResources("0:w")); | ||
assertThrows( | ||
GameParseException.class, () -> attachment.setHaveResources("0:w:e")); | ||
assertThrows( | ||
GameParseException.class, () -> attachment.setHaveResources("0:add:w")); | ||
assertThrows( | ||
GameParseException.class, () -> attachment.setHaveResources("0:add:w:e")); | ||
} | ||
|
||
/* Testing stored values with getHaveResources */ | ||
@Test | ||
void setHaveResourcesTest() throws Exception { | ||
final SecureRandom rand = new SecureRandom(); | ||
final String random1 = Integer.toString(Math.abs(rand.nextInt())); | ||
final String[] expected1 = new String[] {random1, PUS}; | ||
|
||
attachment.setHaveResources( | ||
concatWithColon(random1, addString, PUS)); | ||
assertEquals( | ||
expected1[0], | ||
attachment.getHaveResources()[0]); | ||
assertEquals( | ||
expected1[1], | ||
attachment.getHaveResources()[2]); | ||
} | ||
|
||
/* Testing checkHaveResources */ | ||
@Test | ||
void testCheckHaveResources() throws Exception { | ||
final int italianFuelAmount = italians.getResources().getQuantity(FUEL); | ||
final int italianPuAmount = italians.getResources().getQuantity(PUS); | ||
final int italianOreAmount = italians.getResources().getQuantity(ORE); | ||
final int germanFuelAmount = germans.getResources().getQuantity(FUEL); | ||
final int germanPuAmount = germans.getResources().getQuantity(PUS); | ||
final int germanOreAmount = germans.getResources().getQuantity(ORE); | ||
|
||
final int testItalianPU = italianPuAmount; | ||
final int testItalianResources = italianOreAmount + italianFuelAmount + italianPuAmount; | ||
final int testPUs = testItalianPU + germanPuAmount; | ||
final int testResources = testItalianResources + germanPuAmount + germanFuelAmount + germanOreAmount; | ||
|
||
/* testing with 1 player */ | ||
final List<GamePlayer> players = List.of(italians); | ||
attachment.setHaveResources( | ||
concatWithColon(String.valueOf(testItalianPU), PUS)); | ||
assertTrue( | ||
attachment.checkHaveResources(players)); | ||
attachment.setHaveResources( | ||
concatWithColon(String.valueOf(testItalianResources), addString, PUS)); | ||
assertFalse( | ||
attachment.checkHaveResources(players)); | ||
attachment.setHaveResources( | ||
concatWithColon(String.valueOf(testItalianResources), addString, PUS, FUEL)); | ||
assertFalse( | ||
attachment.checkHaveResources(players)); | ||
attachment.setHaveResources( | ||
concatWithColon(String.valueOf(testItalianResources), addString, PUS, FUEL, ORE)); | ||
assertTrue( | ||
attachment.checkHaveResources(players)); | ||
|
||
/* testing with 2 players */ | ||
final List<GamePlayer> players1 = List.of(italians, germans); | ||
attachment.setHaveResources( | ||
concatWithColon(String.valueOf(testPUs), sumString, PUS)); | ||
assertTrue( | ||
attachment.checkHaveResources(players1)); | ||
attachment.setHaveResources( | ||
concatWithColon(String.valueOf(testResources), sumString, PUS)); | ||
assertFalse( | ||
attachment.checkHaveResources(players1)); | ||
attachment.setHaveResources( | ||
concatWithColon(String.valueOf(testResources), sumString, PUS, FUEL)); | ||
assertFalse( | ||
attachment.checkHaveResources(players1)); | ||
attachment.setHaveResources( | ||
concatWithColon(String.valueOf(testResources), sumString, PUS, FUEL, ORE)); | ||
assertTrue( | ||
attachment.checkHaveResources(players1)); | ||
|
||
} | ||
@Test | ||
private String concatWithColon(final String... args) { | ||
return String.join(":", args); | ||
} | ||
} | ||
} |