Reduce verbosity of test case assertions

This commit is contained in:
Jaculabilis 2022-03-28 15:05:04 -07:00
parent 609e8cc60c
commit ff64b459ca
7 changed files with 176 additions and 64 deletions

View File

@ -6,6 +6,11 @@ public abstract class BinaryAdjudicationDecision : AdjudicationDecision
public override bool Resolved => this.Outcome != null;
public override string ToString()
{
return $"{this.GetType().Name}={this.Outcome}";
}
public bool Update(bool outcome)
{
if (this.Outcome == null)

View File

@ -1,6 +1,7 @@
using MultiversalDiplomacy.Adjudicate;
using MultiversalDiplomacy.Adjudicate.Decision;
using MultiversalDiplomacy.Model;
using MultiversalDiplomacy.Orders;
using NUnit.Framework;
@ -17,9 +18,9 @@ public class DATC_A
setup["England"]
.Fleet("North Sea").MovesTo("Picardy").GetReference(out var order);
// Order should fail.
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(order.Validation, Is.Invalid(ValidationReason.UnreachableDestination));
Assert.That(order, Is.Invalid(ValidationReason.UnreachableDestination));
}
[Test]
@ -27,6 +28,7 @@ public class DATC_A
{
TestCaseBuilder setup = new TestCaseBuilder(StandardEmpty);
// Order should fail.
Assert.That(
() =>
{
@ -41,6 +43,7 @@ public class DATC_A
{
TestCaseBuilder setup = new TestCaseBuilder(StandardEmpty);
// Order should fail.
Assert.That(
() =>
{
@ -57,9 +60,9 @@ public class DATC_A
setup["Germany"]
.Fleet("Kiel").MovesTo("Kiel").GetReference(out var order);
// Program should not crash.
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(order.Validation, Is.Invalid(ValidationReason.DestinationMatchesOrigin));
Assert.That(order, Is.Invalid(ValidationReason.DestinationMatchesOrigin));
}
[Test]
@ -72,15 +75,20 @@ public class DATC_A
.Army("Yorkshire").MovesTo("Yorkshire").GetReference(out var orderYor)
.Army("Liverpool").Supports.Army("Yorkshire").MoveTo("Yorkshire")
["Germany"]
.Fleet("London").MovesTo("Yorkshire")
.Fleet("London").MovesTo("Yorkshire").GetReference(out var orderLon)
.Army("Wales").Supports.Fleet("London").MoveTo("Yorkshire");
// The move of the army in Yorkshire is illegal. This makes the support of Liverpool also illegal.
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(orderLon, Is.Valid);
Assert.That(orderNth, Is.Invalid(ValidationReason.DestinationMatchesOrigin));
Assert.That(orderYor, Is.Invalid(ValidationReason.DestinationMatchesOrigin));
var orderYorRepl = orderYor.GetReplacementReference<HoldOrder>();
Assert.That(orderNth.Validation, Is.Invalid(ValidationReason.DestinationMatchesOrigin));
Assert.That(orderYor.Validation, Is.Invalid(ValidationReason.DestinationMatchesOrigin));
// TODO assert dislodge
// Without the support, the Germans have a stronger force. The army in London dislodges the army in Yorkshire.
setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(orderLon, Is.Victorious);
Assert.That(orderYorRepl, Is.Dislodged);
}
[Test]
@ -91,9 +99,9 @@ public class DATC_A
["Germany"]
.Fleet("London", powerName: "England").MovesTo("North Sea").GetReference(out var order);
// Order should fail.
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(order.Validation, Is.Invalid(ValidationReason.InvalidUnitForPower));
Assert.That(order, Is.Invalid(ValidationReason.InvalidUnitForPower));
}
[Test]
@ -105,9 +113,9 @@ public class DATC_A
.Fleet("London").MovesTo("Belgium")
.Fleet("North Sea").Convoys.Army("London").To("Belgium").GetReference(out var order);
// Move from London to Belgium should fail.
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(order.Validation, Is.Invalid(ValidationReason.InvalidOrderTypeForUnit));
Assert.That(order, Is.Invalid(ValidationReason.InvalidOrderTypeForUnit));
}
[Test]
@ -122,15 +130,12 @@ public class DATC_A
.Fleet("Trieste").Supports.Fleet("Trieste").Hold().GetReference(out var orderTri);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(orderTri.Validation, Is.Invalid(ValidationReason.NoSelfSupport));
Assert.That(orderTri, Is.Invalid(ValidationReason.NoSelfSupport));
var orderTriRepl = orderTri.GetReplacementReference<HoldOrder>();
// The army in Trieste should be dislodged.
var adjudications = setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
// The order reference captures the invalidated order, but the unit is the same.
var dislodgeTri = adjudications
.OfType<IsDislodged>()
.Single(adj => adj.Order.Unit == orderTri.Order.Unit);
Assert.That(dislodgeTri.Outcome, Is.True, "Expected F Tri to be dislodged");
setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(orderTriRepl, Is.Dislodged);
}
[Test]
@ -141,9 +146,9 @@ public class DATC_A
["Italy"]
.Fleet("Rome").MovesTo("Venice").GetReference(out var order);
// Move fails. An army can go from Rome to Venice, but a fleet can not.
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(order.Validation, Is.Invalid(ValidationReason.UnreachableDestination));
Assert.That(order, Is.Invalid(ValidationReason.UnreachableDestination));
}
[Test]
@ -160,12 +165,11 @@ public class DATC_A
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
// The support of Rome is illegal, because Venice can not be reached from Rome by a fleet.
Assert.That(orderRom.Validation, Is.Invalid(ValidationReason.UnreachableSupport));
Assert.That(orderRom, Is.Invalid(ValidationReason.UnreachableSupport));
// Venice is not dislodged.
setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(orderVen.Adjudications.OfType<IsDislodged>().Count(), Is.EqualTo(1));
Assert.That(orderVen.Adjudications.OfType<IsDislodged>().First().Outcome, Is.False);
Assert.That(orderVen, Is.NotDislodged);
}
[Test]
@ -179,15 +183,15 @@ public class DATC_A
.Army("Venice").MovesTo("Tyrolia").GetReference(out var orderVen);
setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(orderVie.Validation, Is.Valid);
Assert.That(orderVen.Validation, Is.Valid);
Assert.That(orderVie, Is.Valid);
Assert.That(orderVen, Is.Valid);
var adjudications = setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
// The two units bounce.
Assert.That(orderVie.Adjudications.OfType<DoesMove>().Count(), Is.EqualTo(1));
Assert.That(orderVie.Adjudications.OfType<DoesMove>().First().Outcome, Is.False);
Assert.That(orderVen.Adjudications.OfType<DoesMove>().Count(), Is.EqualTo(1));
Assert.That(orderVen.Adjudications.OfType<DoesMove>().First().Outcome, Is.False);
var adjudications = setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(orderVie, Is.Repelled);
Assert.That(orderVie, Is.NotDislodged);
Assert.That(orderVen, Is.Repelled);
Assert.That(orderVen, Is.NotDislodged);
}
[Test]
@ -203,17 +207,17 @@ public class DATC_A
.Army("Venice").MovesTo("Tyrolia").GetReference(out var orderVen);
var validations = setup.ValidateOrders(MovementPhaseAdjudicator.Instance);
Assert.That(orderVie.Validation, Is.Valid);
Assert.That(orderMun.Validation, Is.Valid);
Assert.That(orderVen.Validation, Is.Valid);
Assert.That(orderVie, Is.Valid);
Assert.That(orderMun, Is.Valid);
Assert.That(orderVen, Is.Valid);
var adjudications = setup.AdjudicateOrders(MovementPhaseAdjudicator.Instance);
// The three units bounce.
Assert.That(orderVie.Adjudications.OfType<DoesMove>().Count(), Is.EqualTo(1));
Assert.That(orderVie.Adjudications.OfType<DoesMove>().First().Outcome, Is.False);
Assert.That(orderMun.Adjudications.OfType<DoesMove>().Count(), Is.EqualTo(1));
Assert.That(orderMun.Adjudications.OfType<DoesMove>().First().Outcome, Is.False);
Assert.That(orderVen.Adjudications.OfType<DoesMove>().Count(), Is.EqualTo(1));
Assert.That(orderVen.Adjudications.OfType<DoesMove>().First().Outcome, Is.False);
Assert.That(orderVie, Is.Repelled);
Assert.That(orderVie, Is.NotDislodged);
Assert.That(orderMun, Is.Repelled);
Assert.That(orderMun, Is.NotDislodged);
Assert.That(orderVen, Is.Repelled);
Assert.That(orderVen, Is.NotDislodged);
}
}

View File

@ -1,12 +1,25 @@
using MultiversalDiplomacy.Adjudicate;
using MultiversalDiplomacy.Adjudicate.Decision;
namespace MultiversalDiplomacyTests;
public class Is : NUnit.Framework.Is
{
public static OrderValidationConstraint Valid
=> new OrderValidationConstraint(true, ValidationReason.Valid);
=> new(true, ValidationReason.Valid);
public static OrderValidationConstraint Invalid(ValidationReason expected)
=> new OrderValidationConstraint(false, expected);
=> new(false, expected);
public static OrderBinaryAdjudicationConstraint<IsDislodged> Dislodged
=> new(true);
public static OrderBinaryAdjudicationConstraint<IsDislodged> NotDislodged
=> new(false);
public static OrderBinaryAdjudicationConstraint<DoesMove> Victorious
=> new(true);
public static OrderBinaryAdjudicationConstraint<DoesMove> Repelled
=> new(false);
}

View File

@ -1,9 +0,0 @@
using MultiversalDiplomacy.Model;
using MultiversalDiplomacy.Orders;
namespace MultiversalDiplomacyTests;
public class NullOrder : Order
{
public NullOrder(Power power) : base(power) {}
}

View File

@ -0,0 +1,32 @@
using MultiversalDiplomacy.Adjudicate;
using MultiversalDiplomacy.Adjudicate.Decision;
using NUnit.Framework.Constraints;
namespace MultiversalDiplomacyTests;
public class OrderBinaryAdjudicationConstraint<DecisionType> : Constraint
where DecisionType : BinaryAdjudicationDecision
{
private bool expectedOutcome;
public override string Description
{
get => $"{typeof(DecisionType).Name}={expectedOutcome}";
}
public OrderBinaryAdjudicationConstraint(bool outcome)
{
this.expectedOutcome = outcome;
}
public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
if (actual is OrderReference orderRef)
{
DecisionType decision = orderRef.GetDecision<DecisionType>();
return new ConstraintResult(this, decision, decision.Outcome == this.expectedOutcome);
}
return new ConstraintResult(this, actual, false);
}
}

View File

@ -8,16 +8,21 @@ using NUnit.Framework;
namespace MultiversalDiplomacyTests;
/// <summary>
/// An object that provides a view into an order's fate during a test case.
/// An object that provides a view into an order's fate during a test case. This object is
/// stateless and provides data by encapsulating queries on the state of the origin test case
/// builder.
/// </summary>
public class OrderReference<OrderType> where OrderType : Order
public abstract class OrderReference
{
private TestCaseBuilder Builder { get; }
protected TestCaseBuilder Builder { get; }
/// <summary>
/// The order.
/// </summary>
public OrderType Order { get; }
protected Order Order { get; }
public OrderReference(TestCaseBuilder builder, Order order)
{
this.Builder = builder;
this.Order = order;
}
/// <summary>
/// The validation result for the order. Throws if validation has not occurred.
@ -63,6 +68,28 @@ public class OrderReference<OrderType> where OrderType : Order
}
}
/// <summary>
/// Returns an <see cref="OrderReference"/> for the order that replaced this order.
/// </summary>
public OrderReference<ReplacementOrderType> GetReplacementReference<ReplacementOrderType>()
where ReplacementOrderType : Order
{
if (this.Replacement == null)
{
throw new InvalidOperationException("This order was not replaced");
}
Assert.That(
this.Replacement.Order,
Is.AssignableTo(typeof(ReplacementOrderType)),
"Unexpected replacement order type");
ReplacementOrderType replacementOrder = (ReplacementOrderType)this.Replacement.Order;
return new(this.Builder, replacementOrder);
}
/// <summary>
/// A list of all adjudication decisions related to this order. Throws if adjudication has not
/// occurred.
/// </summary>
public List<AdjudicationDecision> Adjudications
{
get
@ -76,15 +103,35 @@ public class OrderReference<OrderType> where OrderType : Order
IsDislodged dislodged => dislodged.Order == this.Order,
DoesMove moves => moves.Order == this.Order,
GivesSupport supports => supports.Order == this.Order,
HasPath path => path.Order == this.Order,
AttackStrength attack => attack.Order == this.Order,
DefendStrength defend => defend.Order == this.Order,
PreventStrength prevent => prevent.Order == this.Order,
HoldStrength hold => this.Order is UnitOrder unitOrder
? hold.Province == unitOrder.Unit.Location.Province
: false,
_ => false,
}).ToList();
return adjudications;
}
}
/// <summary>
/// Returns an adjudication decision of a specified type for this order. Throws if adjudication
/// has not occurred.
/// </summary>
public DecisionType GetDecision<DecisionType>()
where DecisionType : AdjudicationDecision
{
var typeAdjudications = this.Adjudications.OfType<DecisionType>();
Assert.That(typeAdjudications.Any(), Is.True, $"No {typeof(DecisionType)} decision found");
return typeAdjudications.Single();
}
/// <summary>
/// If this order is a unit order and the unit was dislodged, the <see cref="RetreatingUnit"/>
/// representing the retreat. Throws if adjudication has not occurred.
/// </summary>
public RetreatingUnit? Retreat
{
get
@ -105,10 +152,23 @@ public class OrderReference<OrderType> where OrderType : Order
return null;
}
}
}
/// <summary>
/// An object that provides a view into an order's fate during a test case. This object is
/// stateless and provides data by encapsulating queries on the state of the origin test case
/// builder.
/// </summary>
public class OrderReference<OrderType> : OrderReference where OrderType : Order
{
/// <summary>
/// The order.
/// </summary>
new public OrderType Order { get; }
public OrderReference(TestCaseBuilder builder, OrderType order)
: base(builder, order)
{
this.Builder = builder;
this.Order = order;
}
}
}

View File

@ -22,9 +22,16 @@ public class OrderValidationConstraint : Constraint
public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
bool success = actual is OrderValidation validation
&& validation.Valid == this.valid
&& validation.Reason == this.expectedReason;
bool success = actual switch
{
OrderReference reference
=> reference.Validation.Valid == this.valid
&& reference.Validation.Reason == this.expectedReason,
OrderValidation validation
=> validation.Valid == this.valid
&& validation.Reason == this.expectedReason,
_ => false,
};
return new ConstraintResult(this, actual, success);
}
}