Implement assert moves/no-move

This commit is contained in:
Tim Van Baak 2024-09-03 04:25:37 +00:00
parent e9c9999268
commit 80f340c0b2
3 changed files with 59 additions and 12 deletions

View File

@ -1,6 +1,7 @@
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using MultiversalDiplomacy.Adjudicate; using MultiversalDiplomacy.Adjudicate;
using MultiversalDiplomacy.Adjudicate.Decision;
using MultiversalDiplomacy.Model; using MultiversalDiplomacy.Model;
using MultiversalDiplomacy.Orders; using MultiversalDiplomacy.Orders;
@ -9,6 +10,7 @@ namespace MultiversalDiplomacy.Script;
public class AdjudicationQueryScriptHandler( public class AdjudicationQueryScriptHandler(
Action<string> WriteLine, Action<string> WriteLine,
List<OrderValidation> validations, List<OrderValidation> validations,
List<AdjudicationDecision> adjudications,
World world, World world,
IPhaseAdjudicator adjudicator) IPhaseAdjudicator adjudicator)
: IScriptHandler : IScriptHandler
@ -17,6 +19,8 @@ public class AdjudicationQueryScriptHandler(
public List<OrderValidation> Validations { get; } = validations; public List<OrderValidation> Validations { get; } = validations;
public List<AdjudicationDecision> Adjudications { get; } = adjudications;
public World World { get; private set; } = world; public World World { get; private set; } = world;
public ScriptResult HandleInput(string input) public ScriptResult HandleInput(string input)
@ -122,10 +126,42 @@ public class AdjudicationQueryScriptHandler(
// Assert a unit was dislodged // Assert a unit was dislodged
case "moves": case "moves":
// Assert a unit successfully moved
case "no-move": case "no-move":
// Assert a unit did not move re = new(World);
prov = new($"^{re.FullLocation}$", RegexOptions.IgnoreCase);
match = prov.Match(args[1]);
if (!match.Success) return ScriptResult.Fail($"Could not parse province from \"{args[1]}\"", this);
timeline = match.Groups[1].Length > 0
? match.Groups[1].Value
: Season.First.Timeline;
seasonsInTimeline = World.Timelines.Seasons.Where(season => season.Timeline == timeline);
if (!seasonsInTimeline.Any()) return ScriptResult.Fail($"No seasons in timeline {timeline}", this);
turn = match.Groups[4].Length > 0
? int.Parse(match.Groups[4].Value)
// If turn is unspecified, use the second-latest turn in the timeline,
// since we want to assert against the subjects of the orders just adjudicated,
// and adjudication created a new set of seasons.
: seasonsInTimeline.Max(season => season.Turn) - 1;
season = new(timeline, turn);
province = World.Map.Provinces.Single(province => province.Is(match.Groups[2].Value));
var matchingMoves = Adjudications.Where(adj
=> adj is DoesMove moves
&& moves.Order.Unit.Season == season
&& World.Map.GetLocation(moves.Order.Unit.Location).ProvinceName == province.Name);
if (!matchingMoves.Any()) return ScriptResult.Fail("No matching movement decisions");
var doesMove = matchingMoves.Cast<DoesMove>().First();
if (args[0] == "moves" && doesMove.Outcome != true) {
return ScriptResult.Fail($"Adjudication {doesMove} is false");
}
if (args[0] == "no-move" && doesMove.Outcome != false) {
return ScriptResult.Fail($"Adjudication {doesMove} is true");
}
return ScriptResult.Succeed(this);
case "supports": case "supports":
// Assert a unit's support was given // Assert a unit's support was given

View File

@ -39,7 +39,7 @@ public class GameScriptHandler(
var adjudication = adjudicator.AdjudicateOrders(World, validOrders); var adjudication = adjudicator.AdjudicateOrders(World, validOrders);
var newWorld = adjudicator.UpdateWorld(World, adjudication); var newWorld = adjudicator.UpdateWorld(World, adjudication);
return ScriptResult.Succeed(new AdjudicationQueryScriptHandler( return ScriptResult.Succeed(new AdjudicationQueryScriptHandler(
WriteLine, validation, newWorld, adjudicator)); WriteLine, validation, adjudication, newWorld, adjudicator));
} }
// "===" submits the orders and moves immediately to taking the next set of orders // "===" submits the orders and moves immediately to taking the next set of orders

View File

@ -124,16 +124,18 @@ public class ReplTest
--- ---
"""); """);
// Assertion should pass for a season's past // Expected past
repl.Execute("assert has-past a1>a0"); repl.Execute("assert has-past a1>a0");
// Assertion should fail for an incorrect past // Incorrect past
repl.AssertFails("assert has-past a0>a1"); repl.AssertFails("assert has-past a0>a1");
repl.AssertFails("assert has-past a1>a1");
// Missing season
repl.AssertFails("assert has-past a2>a1");
} }
[Test] [Test]
public void AssertHolds() public void AssertMovement()
{ {
Assert.Ignore();
var repl = StandardRepl(); var repl = StandardRepl();
repl.ExecuteAll(""" repl.ExecuteAll("""
@ -144,10 +146,19 @@ public class ReplTest
--- ---
"""); """);
// Assertion should pass for a repelled move // Movement fails
repl.Execute("assert holds Tyr"); repl.Execute("assert no-move Mun");
// Assertion should fail for a repelled move repl.AssertFails("assert moves Mun");
repl.Execute("assert dislodged Tyr");
repl.ExecuteAll("""
---
Germany Mun - Boh
---
""");
// Movement succeeds
repl.Execute("assert moves Mun");
repl.AssertFails("assert no-move Mun");
} }
[Test] [Test]