tests: improved testable dialogs with better/colored logs and asserts (part of #13643, #13638);

This commit is contained in:
Oleg Agafonov 2025-06-14 21:10:32 +04:00
parent d43e96eaf2
commit a98f72649a
17 changed files with 123 additions and 83 deletions

View file

@ -11,19 +11,19 @@ public class AmountTestableResult extends BaseTestableResult {
int amount = 0;
public void save(boolean status, List<String> info, int amount) {
this.save(status, info);
public void onFinish(boolean status, List<String> info, int amount) {
this.onFinish(status, info);
this.amount = amount;
}
@Override
public boolean isOk() {
return true;
public Boolean getResAssert() {
return null; // TODO: implement
}
@Override
public void clear() {
super.clear();
public void onClear() {
super.onClear();
this.amount = 0;
}
}

View file

@ -42,7 +42,7 @@ class AnnounceXTestableDialog extends BaseTestableDialog {
List<String> res = new ArrayList<>();
res.add(getGroup() + " - " + this.getName() + " selected " + chooseRes);
((AmountTestableResult) this.getResult()).save(true, res, chooseRes);
((AmountTestableResult) this.getResult()).onFinish(true, res, chooseRes);
}
static public void register(TestableDialogsRunner runner) {

View file

@ -44,13 +44,13 @@ abstract class BaseTestableDialog implements TestableDialog {
@Override
public void prepare() {
this.result.clear();
this.result.onClear();
}
@Override
final public void showResult(Player player, Game game) {
// show message with result
game.informPlayer(player, String.join("<br>", getResult().getInfo()));
game.informPlayer(player, String.join("<br>", getResult().getResDetails()));
// reset game and gui (in most use cases it must return to player's priority)
game.firePriorityEvent(player.getId());
}

View file

@ -10,41 +10,41 @@ import java.util.List;
*/
public class BaseTestableResult implements TestableResult {
boolean saved = false;
boolean status = false;
List<String> info = new ArrayList<>();
boolean isFinished = false;
boolean resStatus = false;
List<String> resInfo = new ArrayList<>();
@Override
public boolean getStatus() {
return this.status;
public boolean getResStatus() {
return this.resStatus;
}
@Override
public List<String> getInfo() {
return this.info;
public List<String> getResDetails() {
return this.resInfo;
}
@Override
public void save(boolean status, List<String> info) {
this.saved = true;
this.status = status;
this.info = info;
public Boolean getResAssert() {
return null; // TODO: implement
}
@Override
public boolean isOk() {
return true;
public void onFinish(boolean resStatus, List<String> resDetails) {
this.isFinished = true;
this.resStatus = resStatus;
this.resInfo = resDetails;
}
@Override
public boolean isSaved() {
return this.saved;
public boolean isFinished() {
return this.isFinished;
}
@Override
public void clear() {
this.saved = false;
this.status = false;
this.info.clear();
public void onClear() {
this.isFinished = false;
this.resStatus = false;
this.resInfo.clear();
}
}

View file

@ -11,19 +11,19 @@ public class ChoiceTestableResult extends BaseTestableResult {
String choice = null;
public void save(boolean status, List<String> info, String choice) {
this.save(status, info);
public void onFinish(boolean status, List<String> info, String choice) {
this.onFinish(status, info);
this.choice = choice;
}
@Override
public boolean isOk() {
return true;
public Boolean getResAssert() {
return null; // TODO: implement
}
@Override
public void clear() {
super.clear();
public void onClear() {
super.onClear();
this.choice = null;
}
}

View file

@ -54,7 +54,7 @@ class ChooseAmountTestableDialog extends BaseTestableDialog {
Targets.printDebugTargets(getGroup() + " - " + this.getName() + " - " + "FALSE", new Targets(choosingTarget), source, game, res);
}
((TargetTestableResult) this.getResult()).save(chooseRes, res, choosingTarget);
((TargetTestableResult) this.getResult()).onFinish(chooseRes, res, choosingTarget);
}
static public void register(TestableDialogsRunner runner) {

View file

@ -70,7 +70,7 @@ class ChooseCardsTestableDialog extends BaseTestableDialog {
Targets.printDebugTargets(getGroup() + " - " + this.getName() + " - " + "FALSE", new Targets(choosingTarget), source, game, res);
}
((TargetTestableResult) this.getResult()).save(chooseRes, res, choosingTarget);
((TargetTestableResult) this.getResult()).onFinish(chooseRes, res, choosingTarget);
}
static public void register(TestableDialogsRunner runner) {

View file

@ -52,7 +52,7 @@ class ChooseChoiceTestableDialog extends BaseTestableDialog {
res.add(String.format("* selected value: %s", choice));
}
((ChoiceTestableResult) this.getResult()).save(chooseRes, res, choice);
((ChoiceTestableResult) this.getResult()).onFinish(chooseRes, res, choice);
}
static public void register(TestableDialogsRunner runner) {

View file

@ -56,7 +56,7 @@ class ChoosePileTestableDialog extends BaseTestableDialog {
res.add(getGroup() + " - " + this.getName() + " - " + (chooseRes ? "TRUE" : "FALSE"));
res.add(" * selected pile: " + (chooseRes ? "pile 1" : "pile 2"));
this.getResult().save(chooseRes, res);
this.getResult().onFinish(chooseRes, res);
}
static public void register(TestableDialogsRunner runner) {

View file

@ -74,14 +74,14 @@ class ChooseTargetTestableDialog extends BaseTestableDialog {
Targets.printDebugTargets(getGroup() + " - " + this.getName() + " - " + "FALSE", new Targets(choosingTarget), source, game, res);
}
((TargetTestableResult) this.getResult()).save(chooseRes, res, choosingTarget);
((TargetTestableResult) this.getResult()).onFinish(chooseRes, res, choosingTarget);
}
private ChooseTargetTestableDialog aiMustChoose(boolean status, int count) {
private ChooseTargetTestableDialog aiMustChoose(boolean resStatus, int targetsCount) {
TargetTestableResult res = ((TargetTestableResult) this.getResult());
res.aiAssert = true;
res.aiMustChooseStatus = status;
res.aiMustChooseTargetsCount = count;
res.aiAssertEnabled = true;
res.aiAssertResStatus = resStatus;
res.aiAssertTargetsCount = targetsCount;
return this;
}

View file

@ -60,7 +60,7 @@ class ChooseUseTestableDialog extends BaseTestableDialog {
List<String> res = new ArrayList<>();
res.add(chooseRes ? "TRUE" : "FALSE");
this.getResult().save(chooseRes, res);
this.getResult().onFinish(chooseRes, res);
}
static public void register(TestableDialogsRunner runner) {

View file

@ -44,7 +44,7 @@ class GetAmountTestableDialog extends BaseTestableDialog {
List<String> res = new ArrayList<>();
res.add(getGroup() + " - " + this.getName() + " selected " + chooseRes);
((AmountTestableResult) this.getResult()).save(true, res, chooseRes);
((AmountTestableResult) this.getResult()).onFinish(true, res, chooseRes);
}
static public void register(TestableDialogsRunner runner) {

View file

@ -82,7 +82,7 @@ class GetMultiAmountTestableDialog extends BaseTestableDialog {
}
res.add("total selected: " + selectedTotal);
((MultiAmountTestableResult) this.getResult()).save(true, res, chooseRes);
((MultiAmountTestableResult) this.getResult()).onFinish(true, res, chooseRes);
}
static public void register(TestableDialogsRunner runner) {

View file

@ -12,19 +12,19 @@ public class MultiAmountTestableResult extends BaseTestableResult {
List<Integer> values = new ArrayList<>();
public void save(boolean status, List<String> info, List<Integer> values) {
this.save(status, info);
public void onFinish(boolean status, List<String> info, List<Integer> values) {
this.onFinish(status, info);
this.values = values;
}
@Override
public boolean isOk() {
return true;
public Boolean getResAssert() {
return null; // TODO: implement
}
@Override
public void clear() {
super.clear();
public void onClear() {
super.onClear();
this.values.clear();
}
}

View file

@ -13,33 +13,33 @@ public class TargetTestableResult extends BaseTestableResult {
Target target = null;
boolean aiAssert = false;
boolean aiMustChooseStatus = false;
int aiMustChooseTargetsCount = 0;
boolean aiAssertEnabled = false;
boolean aiAssertResStatus = false;
int aiAssertTargetsCount = 0;
public void save(boolean status, List<String> info, Target target) {
this.save(status, info);
public void onFinish(boolean status, List<String> info, Target target) {
this.onFinish(status, info);
this.target = target;
}
@Override
public boolean isOk() {
if (!this.aiAssert) {
return true;
public Boolean getResAssert() {
if (!this.aiAssertEnabled) {
return null;
}
// not finish
// not finished
if (this.target == null) {
return false;
return null;
}
// wrong choose
if (this.getStatus() != this.aiMustChooseStatus) {
if (this.getResStatus() != this.aiAssertResStatus) {
return false;
}
// wrong targets
if (this.target.getTargets().size() != this.aiMustChooseTargetsCount) {
if (this.target.getTargets().size() != this.aiAssertTargetsCount) {
return false;
}
@ -48,8 +48,8 @@ public class TargetTestableResult extends BaseTestableResult {
}
@Override
public void clear() {
super.clear();
public void onClear() {
super.onClear();
this.target = null;
}
}

View file

@ -9,21 +9,25 @@ import java.util.List;
*/
public interface TestableResult {
boolean getStatus();
/**
* Dialog's result
*/
boolean getResStatus();
List<String> getInfo();
/**
* Dialog's detail result
*/
List<String> getResDetails();
/**
* Save new result after show dialog
*
* @param status result of choice dialog call
* @param info detail result to show in GUI
*/
void save(boolean status, List<String> info);
void onFinish(boolean resStatus, List<String> resDetails);
boolean isSaved();
boolean isFinished();
void clear();
void onClear();
boolean isOk();
Boolean getResAssert();
}

View file

@ -8,6 +8,7 @@ import mage.constants.Zone;
import mage.utils.testers.TestableDialog;
import mage.utils.testers.TestableDialogsRunner;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
import org.mage.test.player.TestPlayer;
import org.mage.test.serverside.base.CardTestPlayerBaseWithAIHelps;
@ -19,7 +20,7 @@ import java.util.stream.Collectors;
/**
* Try to test all possible game dialogs by TestableDialogsRunner
*
* <p>
* TODO: fill ai results for all generated dialogs
*
* @author JayDi85
@ -50,7 +51,7 @@ public class TestableDialogsTest extends CardTestPlayerBaseWithAIHelps {
setStopAt(1, PhaseStep.END_TURN);
execute();
printRunnerResults(false);
printRunnerResults(false, false);
}
@Test
@ -71,10 +72,11 @@ public class TestableDialogsTest extends CardTestPlayerBaseWithAIHelps {
setStopAt(1, PhaseStep.END_TURN);
execute();
printRunnerResults(false);
printRunnerResults(false, true);
}
@Test
@Ignore // TODO: enable and fix all failed dialogs
public void test_RunAll_AI() {
// it's impossible to setup 700+ dialogs, so all choices made by AI
// current AI uses only simple choices in dialogs, not simulations
@ -103,7 +105,7 @@ public class TestableDialogsTest extends CardTestPlayerBaseWithAIHelps {
setStopAt(1, PhaseStep.END_TURN);
execute();
printRunnerResults(true);
printRunnerResults(true, true);
}
private List<TestableDialog> findDialogs(TestableDialogsRunner runner, String byGroup, String byName) {
@ -113,14 +115,14 @@ public class TestableDialogsTest extends CardTestPlayerBaseWithAIHelps {
.collect(Collectors.toList());
}
private void printRunnerResults(boolean showFullList) {
private void printRunnerResults(boolean showFullList, boolean assertGoodResults) {
// print text table with full dialogs list and results
// found table sizes
int maxGroupLength = "Group".length();
int maxNameLength = "Name".length();
for (TestableDialog dialog : runner.getDialogs()) {
if (!showFullList && !dialog.getResult().isSaved()) {
if (!showFullList && !dialog.getResult().isFinished()) {
continue;
}
maxGroupLength = Math.max(maxGroupLength, dialog.getGroup().length());
@ -142,8 +144,11 @@ public class TestableDialogsTest extends CardTestPlayerBaseWithAIHelps {
// data
String prevGroup = "";
int totalDialogs = 0;
int totalGood = 0;
int totalBad = 0;
int totalUnknown = 0;
for (TestableDialog dialog : runner.getDialogs()) {
if (!showFullList && !dialog.getResult().isSaved()) {
if (!showFullList && !dialog.getResult().isFinished()) {
continue;
}
totalDialogs++;
@ -151,7 +156,17 @@ public class TestableDialogsTest extends CardTestPlayerBaseWithAIHelps {
System.out.println(horizontalBorder);
}
prevGroup = dialog.getGroup();
String status = dialog.getResult().isOk() ? "OK" : "FAIL";
String status;
if (dialog.getResult().getResAssert() == null) {
status = asYellow("?");
totalUnknown++;
} else if (dialog.getResult().getResAssert()) {
totalGood++;
status = asGreen("OK");
} else {
totalBad++;
status = asRed("FAIL");
}
System.out.printf(rowFormat, dialog.getGroup(), dialog.getName(), status);
}
System.out.println(horizontalBorder);
@ -160,7 +175,28 @@ public class TestableDialogsTest extends CardTestPlayerBaseWithAIHelps {
System.out.printf("| %-" + (maxGroupLength + maxNameLength + maxResultLength + 6) + "s |%n",
"Total dialogs: " + totalDialogs);
System.out.printf("| %-" + (maxGroupLength + maxNameLength + maxResultLength + 6) + "s |%n",
"Total results: TODO");
String.format("Total results: %s good, %s bad, %s unknown",
asGreen(String.valueOf(totalGood)),
asRed(String.valueOf(totalBad)),
asYellow(String.valueOf(totalUnknown))
)
);
System.out.println(horizontalBorder);
if (assertGoodResults && totalBad > 0) {
Assert.fail(String.format("Testable dialogs has %d bad results, try to fix it", totalBad));
}
}
private String asRed(String text) {
return "\u001B[31m" + text + "\u001B[0m";
}
private String asGreen(String text) {
return "\u001B[32m" + text + "\u001B[0m";
}
private String asYellow(String text) {
return "\u001B[33m" + text + "\u001B[0m";
}
}