Add validation constraint
This commit is contained in:
parent
c50dbf6b46
commit
8c828661e2
|
@ -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})";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue