# HG changeset patch # User IBBoard # Date 1322600194 0 # Node ID 0762766bd3f2cfd6137e46ad8547d44c70d1cc95 # Parent cd2aeab2357f7b46f999b96fe730d00b9bae1ce8 Re #350: Add requirement to allow N of unit for specific other units * Remove static access to mock race - it broke tests * Add first tests for new requirement diff -r cd2aeab2357f -r 0762766bd3f2 API/Objects/Requirement/RequiresAtLeastNUnitsRequirementTest.cs --- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirementTest.cs Sun Nov 13 20:53:55 2011 +0000 +++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirementTest.cs Tue Nov 29 20:56:34 2011 +0000 @@ -22,7 +22,7 @@ [TestFixtureSetUp()] public void SetupRace() { - mockRace = MockRace.GetMockRace(); + mockRace = new MockRace(); unitType1 = new MockUnitType("type1", "Unit Type 1"); mockRace.AddUnitType(unitType1); unitType2 = new MockUnitType("type2", "Unit Type 2"); diff -r cd2aeab2357f -r 0762766bd3f2 API/Objects/Requirement/RequiresNUnitsForMUnitsRequirementTest.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/API/Objects/Requirement/RequiresNUnitsForMUnitsRequirementTest.cs Tue Nov 29 20:56:34 2011 +0000 @@ -0,0 +1,120 @@ +// This file (UnitRequiresNUnitsForMUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 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 IBBoard.WarFoundry.API.Objects.Mock; +using NUnit.Framework.SyntaxHelpers; +using System.Reflection; +using IBBoard.NUnit; +using IBBoard.WarFoundry.API.Factories; + +namespace IBBoard.WarFoundry.API.Objects.Requirement +{ + [TestFixture()] + public class RequiresNUnitsForMUnitsRequirementTest : AbstractEqualityTest + { + private MockRace mockRace; + private UnitType unitType1; + private UnitType unitType2; + private UnitType unitType3; + + [TestFixtureSetUp()] + public void SetupRace() + { + mockRace = MockRace.GetMockRace(); + unitType1 = new MockUnitType("type1", "Unit Type 1"); + mockRace.AddUnitType(unitType1); + unitType2 = new MockUnitType("type2", "Unit Type 2"); + mockRace.AddUnitType(unitType2); + unitType3 = new MockUnitType("type3", "Unit Type 3"); + mockRace.AddUnitType(unitType3); + } + + [Test()] + public void TestAddingUnrelatedUnitWithNoUnitsAndOneUnitTypeRequiredIsNA() + { + Army army = new Army(mockRace, "Test", 1000); + RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2); + Unit unit = CreateUnitOfType(unitType3, army); + Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.NotApplicable)); + } + + [Test()] + public void TestAddingOneUnitWithOneUnitTypeRequiredAndOneUnitOfTypePasses() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType2, army); + RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2); + Unit unit = CreateUnitOfType(unitType1, army); + Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed)); + } + + [Test()] + public void TestAddingOneUnitWithOneUnitTypeRequiredAndNoUnitOfTypeFails() + { + Army army = new Army(mockRace, "Test", 1000); + RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2); + Unit unit = CreateUnitOfType(unitType1, army); + Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed)); + } + + [Test()] + public void TestAddingTwoUnitsWithOneUnitTypeRequiredAndOneUnitOfTypeFails() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType1, army); + RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2); + Unit unit = CreateUnitOfType(unitType1, army); + Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed)); + } + + [Test()] + public void TestAddingTwoUnitsWithTwoOfUnitTypeAllowedForOneUnitTypeRequiredAndOneUnitOfTypePasses() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType1, army); + RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2, 1, 2); + Unit unit = CreateUnitOfType(unitType1, army); + Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed)); + } + + 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)); + } + + public override RequiresNUnitsForMUnitsRequirement GetObject() + { + RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType3); + req.AddUnitTypeRequirement(unitType1, 2, 3); + return req; + } + + public override RequiresNUnitsForMUnitsRequirement GetSameObject() + { + RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType3); + req.AddUnitTypeRequirement(unitType1, 2, 3); + return req; + } + + public override RequiresNUnitsForMUnitsRequirement GetDifferentObject() + { + RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType3); + DummyWarFoundryFactory factory = new DummyWarFoundryFactory(); + GameSystem gameSystem = new GameSystem("system", "system", factory); + Race race = new Race("race", "race", gameSystem, factory); + req.AddUnitTypeRequirement(new UnitType("id2", "Type 2", race), 2, 3); + return req; + } + } +} + diff -r cd2aeab2357f -r 0762766bd3f2 IBBoard.WarFoundry.API.Tests.csproj --- a/IBBoard.WarFoundry.API.Tests.csproj Sun Nov 13 20:53:55 2011 +0000 +++ b/IBBoard.WarFoundry.API.Tests.csproj Tue Nov 29 20:56:34 2011 +0000 @@ -113,6 +113,7 @@ +