Make adjudicator reusable to decrease verbosity

This commit is contained in:
Jaculabilis 2022-03-30 08:04:44 -07:00
parent 604dda95e8
commit b679558d9c
3 changed files with 58 additions and 48 deletions

View File

@ -11,11 +11,11 @@ public class MovementAdjudicatorTest
[Test]
public void Validation_ValidHold()
{
TestCaseBuilder setup = new TestCaseBuilder(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
setup["Germany"]
.Army("Mun").Holds().GetReference(out var order);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
Assert.That(order, Is.Valid, "Unexpected validation result");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
@ -24,11 +24,11 @@ public class MovementAdjudicatorTest
[Test]
public void Validation_ValidMove()
{
TestCaseBuilder setup = new TestCaseBuilder(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
setup["Germany"]
.Army("Mun").MovesTo("Tyr").GetReference(out var order);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
Assert.That(order, Is.Valid, "Unexpected validation result");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
@ -37,11 +37,11 @@ public class MovementAdjudicatorTest
[Test]
public void Validation_ValidConvoy()
{
TestCaseBuilder setup = new TestCaseBuilder(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
setup["Germany"]
.Fleet("Nth").Convoys.Army("Hol").To("Lon").GetReference(out var order);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
Assert.That(order, Is.Valid, "Unexpected validation result");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
@ -50,11 +50,11 @@ public class MovementAdjudicatorTest
[Test]
public void Validation_ValidSupportHold()
{
TestCaseBuilder setup = new TestCaseBuilder(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
setup["Germany"]
.Army("Mun").Supports.Army("Kie").Hold().GetReference(out var order);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
Assert.That(order, Is.Valid, "Unexpected validation result");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
@ -63,11 +63,11 @@ public class MovementAdjudicatorTest
[Test]
public void Validation_ValidSupportMove()
{
TestCaseBuilder setup = new TestCaseBuilder(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
setup["Germany"]
.Army("Mun").Supports.Army("Kie").MoveTo("Ber").GetReference(out var order);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
Assert.That(order, Is.Valid, "Unexpected validation result");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
@ -76,11 +76,11 @@ public class MovementAdjudicatorTest
[Test]
public void Adjudication_Hold()
{
TestCaseBuilder setup = new TestCaseBuilder(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
setup["Germany"]
.Army("Mun").Holds().GetReference(out var order);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
var adjMun = order.Adjudications;
@ -96,12 +96,12 @@ public class MovementAdjudicatorTest
[Test]
public void Adjudication_Move()
{
TestCaseBuilder setup = new TestCaseBuilder(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
setup["Germany"]
.Army("Mun").MovesTo("Tyr").GetReference(out var order);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
setup.AdjudicateOrders();
var adjMun = order.Adjudications;
Assert.That(adjMun.All(adj => adj.Resolved), Is.True);
@ -122,13 +122,13 @@ public class MovementAdjudicatorTest
[Test]
public void Adjudication_Support()
{
TestCaseBuilder setup = new TestCaseBuilder(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
setup["Germany"]
.Army("Mun").MovesTo("Tyr").GetReference(out var move)
.Army("Boh").Supports.Army("Mun").MoveTo("Tyr").GetReference(out var support);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
setup.AdjudicateOrders();
var adjBoh = support.Adjudications;
Assert.That(adjBoh.All(adj => adj.Resolved), Is.True);
@ -156,17 +156,17 @@ public class MovementAdjudicatorTest
[Test]
public void Update_SingleHold()
{
TestCaseBuilder setup = new(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
setup["Germany"]
.Army("Mun").Holds().GetReference(out var mun);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
Assert.That(mun, Is.Valid);
setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
setup.AdjudicateOrders();
Assert.That(mun, Is.NotDislodged);
World updated = setup.UpdateWorld(MovementPhaseAdjudicator.Instance);
World updated = setup.UpdateWorld();
// Confirm the future was created
Assert.That(updated.Seasons.Count, Is.EqualTo(2));
@ -185,18 +185,18 @@ public class MovementAdjudicatorTest
[Test]
public void Update_DoubleHold()
{
TestCaseBuilder setup = new(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
setup[(0, 0)]
.GetReference(out Season s1)
["Germany"]
.Army("Mun").Holds().GetReference(out var mun1);
Assert.That(mun1.Order.Unit.Season, Is.EqualTo(s1));
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
Assert.That(mun1, Is.Valid);
setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
setup.AdjudicateOrders();
Assert.That(mun1, Is.NotDislodged);
World updated = setup.UpdateWorld(MovementPhaseAdjudicator.Instance);
World updated = setup.UpdateWorld();
// Confirm the future was created
Season s2 = updated.GetSeason(1, 0);
@ -217,16 +217,16 @@ public class MovementAdjudicatorTest
.Army("Mun").Holds().GetReference(out var mun2);
// Validate the second set of orders
var validations = setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
var validations = setup.ValidateOrders();
Assert.That(validations.Count, Is.EqualTo(1));
Assert.That(mun2, Is.Valid);
// Adjudicate the second set of orders
var adjudications = setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
var adjudications = setup.AdjudicateOrders();
Assert.That(mun2, Is.NotDislodged);
// Update the world again
updated = setup.UpdateWorld(MovementPhaseAdjudicator.Instance);
updated = setup.UpdateWorld();
Season s3 = updated.GetSeason(s2.Turn + 1, s2.Timeline);
Unit u3 = updated.GetUnitAt("Mun", s3.Coord);
Assert.That(u3.Past, Is.EqualTo(mun2.Order.Unit));
@ -235,18 +235,18 @@ public class MovementAdjudicatorTest
[Test]
public void Update_DoubleMove()
{
TestCaseBuilder setup = new(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
setup[(0, 0)]
.GetReference(out Season s1)
["Germany"]
.Army("Mun").MovesTo("Tyr").GetReference(out var mun1);
Assert.That(mun1.Order.Unit.Season, Is.EqualTo(s1));
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
Assert.That(mun1, Is.Valid);
setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
setup.AdjudicateOrders();
Assert.That(mun1, Is.Victorious);
World updated = setup.UpdateWorld(MovementPhaseAdjudicator.Instance);
World updated = setup.UpdateWorld();
// Confirm the future was created
Season s2 = updated.GetSeason(s1.Turn + 1, s1.Timeline);
@ -267,16 +267,16 @@ public class MovementAdjudicatorTest
.Army("Tyr").MovesTo("Mun").GetReference(out var tyr2);
// Validate the second set of orders
var validations = setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
var validations = setup.ValidateOrders();
Assert.That(validations.Count, Is.EqualTo(1));
Assert.That(tyr2, Is.Valid);
// Adjudicate the second set of orders
var adjudications = setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
var adjudications = setup.AdjudicateOrders();
Assert.That(tyr2, Is.Victorious);
// Update the world again
updated = setup.UpdateWorld(MovementPhaseAdjudicator.Instance);
updated = setup.UpdateWorld();
Season s3 = updated.GetSeason(s2.Turn + 1, s2.Timeline);
Unit u3 = updated.GetUnitAt("Mun", s3.Coord);
Assert.That(u3.Past, Is.EqualTo(u2));

View File

@ -170,7 +170,7 @@ public class TestCaseBuilder
/// <summary>
/// Perform validation, adjudication, and update using the defined orders.
/// </summary>
public TestCaseBuilder Execute(IPhaseAdjudicator adjudicator);
public TestCaseBuilder Execute(IPhaseAdjudicator? adjudicator = null);
/// <summary>
/// Choose a new season to define orders for.
@ -199,6 +199,7 @@ public class TestCaseBuilder
}
public World World { get; private set; }
private IPhaseAdjudicator LastUsedAdjudicator { get; set; }
public ReadOnlyCollection<Order> Orders { get; }
private List<Order> OrderList;
public List<OrderValidation>? ValidationResults { get; private set; }
@ -207,9 +208,10 @@ public class TestCaseBuilder
/// <summary>
/// Create a test case builder that will operate on a world.
/// </summary>
public TestCaseBuilder(World world)
public TestCaseBuilder(World world, IPhaseAdjudicator? adjudicator = null)
{
this.World = world;
this.LastUsedAdjudicator = adjudicator ?? new TestAdjudicator();
this.OrderList = new();
this.Orders = new(this.OrderList);
this.ValidationResults = null;
@ -260,20 +262,24 @@ public class TestCaseBuilder
return newUnit;
}
public List<OrderValidation> ValidateOrders(IPhaseAdjudicator adjudicator)
public List<OrderValidation> ValidateOrders(IPhaseAdjudicator? adjudicator = null)
{
adjudicator ??= this.LastUsedAdjudicator;
this.LastUsedAdjudicator = adjudicator;
this.ValidationResults = adjudicator.ValidateOrders(this.World, this.Orders.ToList());
this.OrderList.Clear();
return this.ValidationResults;
}
public List<AdjudicationDecision> AdjudicateOrders(IPhaseAdjudicator adjudicator)
public List<AdjudicationDecision> AdjudicateOrders(IPhaseAdjudicator? adjudicator = null)
{
if (this.ValidationResults == null)
{
throw new InvalidOperationException("Cannot adjudicate before validation");
}
adjudicator ??= this.LastUsedAdjudicator;
this.LastUsedAdjudicator = adjudicator;
List<Order> orders = this.ValidationResults
.Where(validation => validation.Valid)
.Select(validation => validation.Order)
@ -283,13 +289,15 @@ public class TestCaseBuilder
return this.AdjudicationResults;
}
public World UpdateWorld(IPhaseAdjudicator adjudicator)
public World UpdateWorld(IPhaseAdjudicator? adjudicator = null)
{
if (this.AdjudicationResults == null)
{
throw new InvalidOperationException("Cannot update before adjudication");
}
adjudicator ??= this.LastUsedAdjudicator;
this.LastUsedAdjudicator = adjudicator;
this.World = adjudicator.UpdateWorld(this.World, this.AdjudicationResults);
this.AdjudicationResults = null;
return this.World;
@ -584,8 +592,10 @@ public class TestCaseBuilder
this.Order = order;
}
public TestCaseBuilder Execute(IPhaseAdjudicator adjudicator)
public TestCaseBuilder Execute(IPhaseAdjudicator? adjudicator = null)
{
adjudicator ??= this.Builder.LastUsedAdjudicator;
this.Builder.LastUsedAdjudicator = adjudicator;
this.Builder.ValidateOrders(adjudicator);
this.Builder.AdjudicateOrders(adjudicator);
this.Builder.UpdateWorld(adjudicator);

View File

@ -9,27 +9,27 @@ namespace MultiversalDiplomacyTests;
public class TimeTravelTest
{
[Test]
public void MoveIntoOwnPast()
public void MoveIntoOwnPastForksTimeline()
{
TestCaseBuilder setup = new(World.WithStandardMap());
TestCaseBuilder setup = new(World.WithStandardMap(), MovementPhaseAdjudicator.Instance);
// Hold to move into the future, then move back into the past.
setup[(0, 0)]
.GetReference(out Season s0)
["Germany"]
.Army("Mun").Holds().GetReference(out var mun0)
.Execute(MovementPhaseAdjudicator.Instance)
.Execute()
[(1, 0)]
.GetReference(out Season s1)
["Germany"]
.Army("Mun").MovesTo("Tyr", season: s0).GetReference(out var mun1);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
setup.ValidateOrders();
Assert.That(mun1, Is.Valid);
setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
setup.AdjudicateOrders();
Assert.That(mun1, Is.Victorious);
World world = setup.UpdateWorld(MovementPhaseAdjudicator.Instance);
World world = setup.UpdateWorld();
// Confirm that there are now three seasons: the root, a future off the root, and a fork.
Assert.That(world.Seasons.Count, Is.EqualTo(3));