Add validation constraint

This commit is contained in:
Jaculabilis 2022-03-22 22:11:14 -07:00
parent c50dbf6b46
commit 8c828661e2
4 changed files with 63 additions and 46 deletions

View File

@ -27,6 +27,16 @@ public class OrderValidation
this.Order = order;
this.Valid = valid;
this.Reason = reason;
if (this.Valid != (this.Reason == ValidationReason.Valid))
{
throw new ArgumentException("Only valid orders should have the Valid reason");
}
}
public override string ToString()
{
return this.Valid ? "Valid" : $"Invalid ({this.Reason})";
}
}

View File

@ -0,0 +1,12 @@
using MultiversalDiplomacy.Adjudicate;
namespace MultiversalDiplomacyTests;
public class Is : NUnit.Framework.Is
{
public static OrderValidationConstraint Valid
=> new OrderValidationConstraint(true, ValidationReason.Valid);
public static OrderValidationConstraint Invalid(ValidationReason expected)
=> new OrderValidationConstraint(false, expected);
}

View File

@ -17,15 +17,8 @@ public class MovementAdjudicatorTest
setup.ValidateOrders(new MovementPhaseAdjudicator());
Assert.Multiple(() =>
{
Assert.That(order.Validation.Valid, Is.True, "Unexpected validation result");
Assert.That(
order.Validation.Reason,
Is.EqualTo(ValidationReason.Valid),
"Unexpected validation reason");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
});
Assert.That(order.Validation, Is.Valid, "Unexpected validation result");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
}
[Test]
@ -37,15 +30,8 @@ public class MovementAdjudicatorTest
setup.ValidateOrders(new MovementPhaseAdjudicator());
Assert.Multiple(() =>
{
Assert.That(order.Validation.Valid, Is.True, "Unexpected validation result");
Assert.That(
order.Validation.Reason,
Is.EqualTo(ValidationReason.Valid),
"Unexpected validation reason");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
});
Assert.That(order.Validation, Is.Valid, "Unexpected validation result");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
}
[Test]
@ -57,15 +43,8 @@ public class MovementAdjudicatorTest
setup.ValidateOrders(new MovementPhaseAdjudicator());
Assert.Multiple(() =>
{
Assert.That(order.Validation.Valid, Is.True, "Unexpected validation result");
Assert.That(
order.Validation.Reason,
Is.EqualTo(ValidationReason.Valid),
"Unexpected validation reason");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
});
Assert.That(order.Validation, Is.Valid, "Unexpected validation result");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
}
[Test]
@ -77,15 +56,8 @@ public class MovementAdjudicatorTest
setup.ValidateOrders(new MovementPhaseAdjudicator());
Assert.Multiple(() =>
{
Assert.That(order.Validation.Valid, Is.True, "Unexpected validation result");
Assert.That(
order.Validation.Reason,
Is.EqualTo(ValidationReason.Valid),
"Unexpected validation reason");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
});
Assert.That(order.Validation, Is.Valid, "Unexpected validation result");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
}
[Test]
@ -97,14 +69,7 @@ public class MovementAdjudicatorTest
setup.ValidateOrders(new MovementPhaseAdjudicator());
Assert.Multiple(() =>
{
Assert.That(order.Validation.Valid, Is.True, "Unexpected validation result");
Assert.That(
order.Validation.Reason,
Is.EqualTo(ValidationReason.Valid),
"Unexpected validation reason");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
});
Assert.That(order.Validation, Is.Valid, "Unexpected validation result");
Assert.That(order.Replacement, Is.Null, "Unexpected order replacement");
}
}
}

View File

@ -0,0 +1,30 @@
using MultiversalDiplomacy.Adjudicate;
using NUnit.Framework.Constraints;
namespace MultiversalDiplomacyTests;
public class OrderValidationConstraint : Constraint
{
private bool valid;
private ValidationReason expectedReason;
public override string Description
{
get => this.valid ? "Valid" : $"Invalid ({this.expectedReason})";
}
public OrderValidationConstraint(bool valid, ValidationReason expected)
{
this.valid = valid;
this.expectedReason = expected;
}
public override ConstraintResult ApplyTo<TActual>(TActual actual)
{
bool success = actual is OrderValidation validation
&& validation.Valid == this.valid
&& validation.Reason == this.expectedReason;
return new ConstraintResult(this, actual, success);
}
}