# HG changeset patch # User IBBoard # Date 1343572183 -3600 # Node ID 99248d05ba9cb13fc0974ef4fcb422120379d950 # Parent fdebdeb52a408903111d4e20a6fb65448ee085bc Re #410: Create "N units per M models in parent unit" requirement * Add "add unit" test helper method that sets a unit name * Add validation tests diff -r fdebdeb52a40 -r 99248d05ba9c API/Objects/Requirement/AbstractUnitRequirementTest.cs --- a/API/Objects/Requirement/AbstractUnitRequirementTest.cs Sun Jul 29 14:17:21 2012 +0100 +++ b/API/Objects/Requirement/AbstractUnitRequirementTest.cs Sun Jul 29 15:29:43 2012 +0100 @@ -78,6 +78,14 @@ return unit; } + protected static Unit AddUnitOfTypeToArmy(UnitType unitType, Army army, string name) + { + Unit unit = AddUnitOfTypeToArmy(unitType, army); + unit.Name = name; + return unit; + + } + protected static Unit CreateUnitOfType(UnitType unitType, Army army) { return new Unit(unitType, army.GetCategory(unitType.MainCategory)); diff -r fdebdeb52a40 -r 99248d05ba9c API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs --- a/API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs Sun Jul 29 14:17:21 2012 +0100 +++ b/API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs Sun Jul 29 15:29:43 2012 +0100 @@ -76,6 +76,73 @@ Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 5 × " + unitType1.Name + " as sub-units of each " + unitType2.Name + ", would have 6"); } + [Test()] + public void TestValidatingUnitTypeWithNoParentAndOneUnitTypeRequiredIsAllowed() + { + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + AddUnitOfTypeToArmy(unitType1, army); + Assert_That__NotApplicable(req, army); + } + + [Test()] + public void TestValidatingUnitTypeWithParentAndOneUnitTypeRequiredIsAllowed() + { + Army army = new Army(mockRace, "Test", 1000); + Unit parent = AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType1, army, parent); + UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert_That__ValidationPasses(req, army); + } + + [Test()] + public void TestValidatingUnitTypeWithWrongParentAndOneUnitTypeRequiredIsNotApplicable() + { + Army army = new Army(mockRace, "Test", 1000); + Unit parent = AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType1, army, parent); + UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType3); + Assert_That__NotApplicable(req, army); + } + + [Test()] + public void TestValidatingTooManyUnitTypesWithParentAndOneUnitTypeRequiredFails() + { + Army army = new Army(mockRace, "Test", 1000); + Unit parent = AddUnitOfTypeToArmy(unitType3, army, "TheParent"); + AddUnitsOfTypeToArmy(2, unitType1, army, parent); + UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType3); + Assert_That__ValidationFails(req, army, "Army can only contain 1 × " + unitType1.Name + " for every 1 model in a parent unit of " + unitType3.Name + ": TheParent has 2 for 1."); + } + + [Test()] + public void TestValidatingTooManyUnitTypesWithParentAndFiveUnitTypeRequiredFails() + { + Army army = new Army(mockRace, "Test", 1000); + Unit parent = AddUnitOfTypeToArmy(unitType2, army, "TheParent"); + AddUnitsOfTypeToArmy(6, unitType1, army, parent); + UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert_That__ValidationFails(req, army, "Army can only contain 1 × " + unitType1.Name + " for every 1 model in a parent unit of " + unitType2.Name + ": TheParent has 6 for 5."); + } + + [Test()] + public void TestValidatingTooManyUnitsAtThreshold() + { + Army army = new Army(mockRace, "Test", 1000); + Unit parent = AddUnitOfTypeToArmy(unitType2, army, "TheParent"); + AddUnitsOfTypeToArmy(5, unitType1, army, parent); + UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert_That__ValidationPasses(req, army); + AddUnitOfTypeToArmy(unitType1, army, parent); + Assert_That__ValidationFails(req, army, "Army can only contain 1 × " + unitType1.Name + " for every 1 model in a parent unit of " + unitType2.Name + ": TheParent has 6 for 5."); + } + protected override UnitRequiresNParentModelsForMUnitsRequirement CreateRequirement(UnitType requirementOn) { return new UnitRequiresNParentModelsForMUnitsRequirement(requirementOn);