changeset 188:0762766bd3f2

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
author IBBoard <dev@ibboard.co.uk>
date Tue, 29 Nov 2011 20:56:34 +0000
parents cd2aeab2357f
children eea440e5891b
files API/Objects/Requirement/RequiresAtLeastNUnitsRequirementTest.cs API/Objects/Requirement/RequiresNUnitsForMUnitsRequirementTest.cs IBBoard.WarFoundry.API.Tests.csproj
diffstat 3 files changed, 122 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- 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");
--- /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<RequiresNUnitsForMUnitsRequirement>
+	{
+		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;
+		}
+	}
+}
+
--- 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 @@
     <Compile Include="API\Objects\UnitEquipmentRatioSelectionTests.cs" />
     <Compile Include="API\Objects\UnitEquipmentNumericSelectionTests.cs" />
     <Compile Include="API\Factories\Requirement\UnitRequiresNoMoreThanNOfUnitTypeRequirementFactoryTest.cs" />
+    <Compile Include="API\Objects\Requirement\RequiresNUnitsForMUnitsRequirementTest.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="app.config" />