# HG changeset patch # User IBBoard # Date 1343417620 -3600 # Node ID 5e03b68dd21483801b1e30155cc19e42a7c50e7c # Parent d6883a522c70a6f497d87f674d93573544444d77 Re #410: Create "N units per M models in parent unit" requirement * Add required usage of context objects * Add tests for new requirement * Add some extras to test unit type setup that the new requirement needs diff -r d6883a522c70 -r 5e03b68dd214 API/Factories/Requirement/Mock/MockRequirement.cs --- a/API/Factories/Requirement/Mock/MockRequirement.cs Wed Jul 11 20:54:12 2012 +0100 +++ b/API/Factories/Requirement/Mock/MockRequirement.cs Fri Jul 27 20:33:40 2012 +0100 @@ -4,6 +4,7 @@ using System; using IBBoard.WarFoundry.API.Objects.Requirement; using IBBoard.WarFoundry.API.Objects; +using IBBoard.WarFoundry.API.Objects.Requirement.Context; namespace IBBoard.WarFoundry.API.Factories.Requirement.Mock { @@ -36,7 +37,7 @@ throw new NotImplementedException(); } - public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy) + public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy, AddingContext context) { throw new NotImplementedException(); } diff -r d6883a522c70 -r 5e03b68dd214 API/Objects/Requirement/AbstractUnitRequirementTest.cs --- a/API/Objects/Requirement/AbstractUnitRequirementTest.cs Wed Jul 11 20:54:12 2012 +0100 +++ b/API/Objects/Requirement/AbstractUnitRequirementTest.cs Fri Jul 27 20:33:40 2012 +0100 @@ -5,6 +5,7 @@ using NUnit.Framework; using NUnit.Framework.SyntaxHelpers; using IBBoard.NUnit; +using System.Collections.Generic; namespace IBBoard.WarFoundry.API.Objects.Requirement { @@ -58,17 +59,23 @@ Assert.That(req.GetValidationMessage(army), Is.EqualTo(message)); } - protected static void AddUnitsOfTypeToArmy(int count, UnitType unitType, Army army) + protected static ICollection AddUnitsOfTypeToArmy(int count, UnitType unitType, Army army) { + ICollection units = new List(); + for (int i = 0; i < count; i++) { - AddUnitOfTypeToArmy(unitType, army); + units.Add(AddUnitOfTypeToArmy(unitType, army)); } + + return units; } - protected static void AddUnitOfTypeToArmy(UnitType unitType, Army army) + protected static Unit AddUnitOfTypeToArmy(UnitType unitType, Army army) { - army.AddUnit(CreateUnitOfType(unitType, army)); + Unit unit = CreateUnitOfType(unitType, army); + army.AddUnit(unit); + return unit; } protected static Unit CreateUnitOfType(UnitType unitType, Army army) diff -r d6883a522c70 -r 5e03b68dd214 API/Objects/Requirement/AbstractUnitTypeUnitRequirementTest.cs --- a/API/Objects/Requirement/AbstractUnitTypeUnitRequirementTest.cs Wed Jul 11 20:54:12 2012 +0100 +++ b/API/Objects/Requirement/AbstractUnitTypeUnitRequirementTest.cs Fri Jul 27 20:33:40 2012 +0100 @@ -19,10 +19,15 @@ { mockRace = new MockRace(); unitType1 = new MockUnitType("type1", "Unit Type 1"); + unitType1.MinSize = 1; mockRace.AddUnitType(unitType1); unitType2 = new MockUnitType("type2", "Unit Type 2"); + unitType2.MinSize = 5; + unitType2.AddContainedUnitType(unitType1); mockRace.AddUnitType(unitType2); unitType3 = new MockUnitType("type3", "Unit Type 3"); + unitType3.MinSize = 1; + unitType3.AddContainedUnitType(unitType1); mockRace.AddUnitType(unitType3); } diff -r d6883a522c70 -r 5e03b68dd214 API/Objects/Requirement/Mock/AbstractFixedRequirement.cs --- a/API/Objects/Requirement/Mock/AbstractFixedRequirement.cs Wed Jul 11 20:54:12 2012 +0100 +++ b/API/Objects/Requirement/Mock/AbstractFixedRequirement.cs Fri Jul 27 20:33:40 2012 +0100 @@ -2,6 +2,7 @@ // // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. using System; +using IBBoard.WarFoundry.API.Objects.Requirement.Context; namespace IBBoard.WarFoundry.API.Objects.Requirement.Mock { @@ -34,7 +35,7 @@ return (result.Equals(Validation.Failed)) ? "Validation failed" : ""; } - public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy) + public string GetAllowsAddingMessage(IWarFoundryObject toAdd, Army toArmy, AddingContext context) { return (result.Equals(Validation.Failed)) ? "Validation failed" : ""; } diff -r d6883a522c70 -r 5e03b68dd214 API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs Fri Jul 27 20:33:40 2012 +0100 @@ -0,0 +1,91 @@ +// This file (UnitRequiresNoMoreThanNUnitsPerMModelsInParentUnitRequirementTests.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2012 IBBoard +// +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. +using System; +using NUnit.Framework; +using NUnit.Framework.SyntaxHelpers; +using IBBoard.WarFoundry.API.Objects.Requirement.Context; + +namespace IBBoard.WarFoundry.API.Objects.Requirement +{ + [TestFixture()] + public class UnitRequiresNParentModelsForMUnitsRequirementTests: AbstractUnitTypeUnitRequirementTest + { + [Test()] + public void TestAddingUnitTypeWithNoParentAndOneUnitTypeRequiredIsAllowed() + { + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert_That__AddingNotApplicable(req, unitType1, army); + } + + [Test()] + public void TestAddingUnitTypeWithParentAndOneUnitTypeRequiredIsAllowed() + { + Army army = new Army(mockRace, "Test", 1000); + Unit parent = AddUnitOfTypeToArmy(unitType2, army); + UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert_That__PassesAdding(req, unitType1, parent, army); + } + + [Test()] + public void TestAddingUnitTypeWithWrongParentAndOneUnitTypeRequiredIsNotApplicable() + { + Army army = new Army(mockRace, "Test", 1000); + Unit parent = AddUnitOfTypeToArmy(unitType2, army); + UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType3); + Assert_That__AddingNotApplicable(req, unitType1, parent, army); + } + + [Test()] + public void TestAddingTooManyUnitTypesWithParentAndOneUnitTypeRequiredFails() + { + Army army = new Army(mockRace, "Test", 1000); + Unit parent = AddUnitOfTypeToArmy(unitType3, army); + Unit firstChild = AddUnitOfTypeToArmy(unitType1, army); + parent.AddContainedUnit(firstChild); + UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType3); + Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 1 × " + unitType1.Name + " as a sub-unit of each " + unitType3.Name + ", would have 2"); + } + + protected override UnitRequiresNParentModelsForMUnitsRequirement CreateRequirement(UnitType requirementOn) + { + return new UnitRequiresNParentModelsForMUnitsRequirement(requirementOn); + } + + protected static void Assert_That__PassesAdding(AbstractUnitRequirement req, UnitType unitType, Unit parent, Army army) + { + AddingToParentContext ctx = new AddingToParentContext(parent); + Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.Passed)); + Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.Empty); + Unit unit = CreateUnitOfType(unitType, army); + Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Passed)); + Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.Empty); + } + + protected static void Assert_That__AddingNotApplicable(AbstractUnitRequirement req, UnitType unitType, Unit parent, Army army) + { + AddingToParentContext ctx = new AddingToParentContext(parent); + Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.NotApplicable)); + Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.Empty); + Unit unit = CreateUnitOfType(unitType, army); + Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.NotApplicable)); + Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.Empty); + } + + protected static void Assert_That__FailsAdding(AbstractUnitRequirement req, UnitType unitType, Unit parent, Army army, string message) + { + AddingToParentContext ctx = new AddingToParentContext(parent); + Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.Failed)); + Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.EqualTo(message)); + Unit unit = CreateUnitOfType(unitType, army); + Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Failed)); + Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.EqualTo(message)); + } + } +} + diff -r d6883a522c70 -r 5e03b68dd214 IBBoard.WarFoundry.API.Tests.csproj --- a/IBBoard.WarFoundry.API.Tests.csproj Wed Jul 11 20:54:12 2012 +0100 +++ b/IBBoard.WarFoundry.API.Tests.csproj Fri Jul 27 20:33:40 2012 +0100 @@ -127,6 +127,7 @@ +