Test framework: added dynamic code support in unit tests (command: runCode - use it same way as real time checks);

This commit is contained in:
Oleg Agafonov 2020-12-19 04:46:19 +04:00
parent 384ff2e7ac
commit 3cccad2138
4 changed files with 68 additions and 1 deletions

View file

@ -2,6 +2,7 @@ package org.mage.test.player;
import mage.constants.PhaseStep;
import mage.game.Game;
import org.mage.test.serverside.base.CardTestCodePayload;
/**
* @author BetaSteward_at_googlemail.com
@ -12,12 +13,18 @@ public class PlayerAction {
private final int turnNum;
private final PhaseStep step;
private final String action;
private final CardTestCodePayload codePayload; // special code to execute (e.g. on dynamic check)
public PlayerAction(String actionName, int turnNum, PhaseStep step, String action) {
this(actionName, turnNum, step, action, null);
}
public PlayerAction(String actionName, int turnNum, PhaseStep step, String action, CardTestCodePayload codePayload) {
this.actionName = actionName;
this.turnNum = turnNum;
this.step = step;
this.action = action;
this.codePayload = codePayload;
}
public int getTurnNum() {
@ -33,7 +40,11 @@ public class PlayerAction {
}
public String getActionName() {
return this.actionName;
return actionName;
}
public CardTestCodePayload getCodePayload() {
return codePayload;
}
/**

View file

@ -738,6 +738,18 @@ public class TestPlayer implements Player {
}
Assert.fail("Unknow ai command: " + command);
} else if (action.getAction().startsWith(RUN_PREFIX)) {
String command = action.getAction();
command = command.substring(command.indexOf(RUN_PREFIX) + RUN_PREFIX.length());
// custom code execute
if (command.equals(RUN_COMMAND_CODE)) {
action.getCodePayload().run(action.getActionName(), computerPlayer, game);
actions.remove(action);
return true;
}
Assert.fail("Unknow run command: " + command);
} else if (action.getAction().startsWith(CHECK_PREFIX)) {
String command = action.getAction();
command = command.substring(command.indexOf(CHECK_PREFIX) + CHECK_PREFIX.length());

View file

@ -0,0 +1,21 @@
package org.mage.test.serverside.base;
import mage.game.Game;
import mage.players.Player;
/**
* @author JayDi85
*/
@FunctionalInterface
public interface CardTestCodePayload {
/**
* Run dynamic code in unit tests on player's priority.
*
* @param info
* @param player activate player who would execute the code on their priority
* @param game
*/
void run(String info, Player player, Game game);
}

View file

@ -34,6 +34,7 @@ import org.junit.Before;
import org.mage.test.player.PlayerAction;
import org.mage.test.player.TestPlayer;
import org.mage.test.serverside.base.CardTestAPI;
import org.mage.test.serverside.base.CardTestCodePayload;
import org.mage.test.serverside.base.MageTestPlayerBase;
import java.io.FileNotFoundException;
@ -57,6 +58,7 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
public static final String CHECK_PREFIX = "check:"; // prefix for all check commands
public static final String SHOW_PREFIX = "show:"; // prefix for all show commands
public static final String AI_PREFIX = "ai:"; // prefix for all ai commands
public static final String RUN_PREFIX = "run:"; // prefix for all run commands
static {
// aliases can be used in check commands, so all prefixes and delimeters must be unique
@ -77,6 +79,9 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
public static final String AI_COMMAND_PLAY_PRIORITY = "play priority";
public static final String AI_COMMAND_PLAY_STEP = "play step";
// commands for run
public static final String RUN_COMMAND_CODE = "code";
static {
// cards can be played/casted by activate ability command too
Assert.assertTrue("musts contains activate ability part", ACTIVATE_PLAY.startsWith(ACTIVATE_ABILITY));
@ -375,6 +380,24 @@ public abstract class CardTestPlayerAPIImpl extends MageTestPlayerBase implement
addPlayerAction(player, checkName, turnNum, step, res);
}
/**
* Execute custom code under player's priority. You can stop debugger on it.
* <p>
* Example 1: check some conditions in the middle of the test
* Example 2: make game modifications (if you don't want to use custom abilities)
* Example 3: stop debugger in the middle of the game
*
* @param info
* @param turnNum
* @param step
* @param player
* @param codePayload code to execute
*/
public void runCode(String info, int turnNum, PhaseStep step, TestPlayer player, CardTestCodePayload codePayload) {
PlayerAction playerAction = new PlayerAction(info, turnNum, step, RUN_PREFIX + RUN_COMMAND_CODE, codePayload);
addPlayerAction(player, playerAction);
}
public void checkPT(String checkName, int turnNum, PhaseStep step, TestPlayer player, String permanentName, Integer power, Integer toughness) {
//Assert.assertNotEquals("", permanentName);
check(checkName, turnNum, step, player, CHECK_COMMAND_PT, permanentName, power.toString(), toughness.toString());