# HG changeset patch # User IBBoard # Date 1301173338 0 # Node ID c6562eb62d0489825fab15e427bb57c21ea9efc4 # Parent a3bc8174299f835175c59b0266c1222a83ec7c1f Re #27: Define unit requirements * Refactor out common code * Simplify tests to try with one unit type required at first * Add test where requirement is satisfied diff -r a3bc8174299f -r c6562eb62d04 API/Objects/Requirements/UnitRequiresAtLeastNUnitsRequirementTest.cs --- a/API/Objects/Requirements/UnitRequiresAtLeastNUnitsRequirementTest.cs Sat Mar 26 17:04:09 2011 +0000 +++ b/API/Objects/Requirements/UnitRequiresAtLeastNUnitsRequirementTest.cs Sat Mar 26 21:02:18 2011 +0000 @@ -10,20 +10,49 @@ { [TestFixture()] public class UnitRequiresAtLeastNUnitsRequirementTest - { - [Test()] - public void TestWithNoUnits() + { + private MockRace mockRace; + private UnitType unitType1; + private UnitType unitType2; + + [TestFixtureSetUp()] + public void SetupRace() { - MockRace mockRace = MockRace.GetMockRace(); - UnitType unitType1 = new MockUnitType("type1", "Unit Type 1"); + mockRace = MockRace.GetMockRace(); + unitType1 = new MockUnitType("type1", "Unit Type 1"); mockRace.AddUnitType(unitType1); - UnitType unitType2 = new MockUnitType("type2", "Unit Type 2"); + unitType2 = new MockUnitType("type2", "Unit Type 2"); mockRace.AddUnitType(unitType2); + } + + [Test()] + public void TestWithNoUnitsAndOneUnitTypeRequired() + { Army army = new Army(mockRace, "Test", 1000); - UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1, unitType2); - Unit unit = new Unit(unitType1, army.GetCategory(unitType1.MainCategory)); + Unit unit = CreateUnitOfType(unitType1, army); + UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType2); Assert.That(req.CanAddToArmy(unit, army), Is.False); + } + + [Test()] + public void TestWithOneUnitAndOneUnitTypeRequired() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType2, army); + Unit unit = CreateUnitOfType(unitType1, army); + UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType2); + Assert.That(req.CanAddToArmy(unit, army), Is.True); + } + + private static void AddUnitOfTypeToArmy(UnitType unitType, Army army) + { + army.AddUnit(CreateUnitOfType(unitType, army)); } + + private static Unit CreateUnitOfType(UnitType unitType, Army army) + { + return new Unit(unitType, army.GetCategory(unitType.MainCategory)); + } } }