changeset 139:3e4864e206ea

Re #140: Equality methods * Add equality testing to RequiresNoMoreThanN requirement * Add extra different races for testing more of equality method
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Apr 2011 19:47:36 +0000
parents 11faab6c712a
children 2ae80631ec9c
files API/Objects/RaceTests.cs API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirementTest.cs
diffstat 2 files changed, 195 insertions(+), 109 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/RaceTests.cs	Tue Apr 26 19:14:34 2011 +0000
     1.2 +++ b/API/Objects/RaceTests.cs	Tue Apr 26 19:47:36 2011 +0000
     1.3 @@ -129,6 +129,20 @@
     1.4  			GameSystem gameSystem = new GameSystem("system2", "another system", factory);
     1.5  			return new Race("race2", "other race", gameSystem, factory);
     1.6  		}
     1.7 +
     1.8 +		public Race GetOtherDifferentObjectWithSubID()
     1.9 +		{
    1.10 +			DummyWarFoundryFactory factory = new DummyWarFoundryFactory();
    1.11 +			GameSystem gameSystem = new GameSystem("system", "system", factory);
    1.12 +			return new Race("race", "subid", "race", gameSystem, factory);
    1.13 +		}
    1.14 +
    1.15 +		public Race GetOtherDifferentObjectWithGameSystem()
    1.16 +		{
    1.17 +			DummyWarFoundryFactory factory = new DummyWarFoundryFactory();
    1.18 +			GameSystem gameSystem = new GameSystem("system2", "system", factory);
    1.19 +			return new Race("race", "race", gameSystem, factory);
    1.20 +		}
    1.21  	}
    1.22  }
    1.23  
     2.1 --- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirementTest.cs	Tue Apr 26 19:14:34 2011 +0000
     2.2 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirementTest.cs	Tue Apr 26 19:47:36 2011 +0000
     2.3 @@ -1,120 +1,192 @@
     2.4 -// This file (RequiresNoMoreThanNOfUnitTypeRequirementTest.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 IBBoard
     2.5 +// This file (RequiresNoMoreThanNOfUnitTypeRequirementTest.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 IBBoard
     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.
     2.8  using System;
     2.9 -using NUnit.Framework;
    2.10 -using IBBoard.WarFoundry.API.Objects.Mock;
    2.11 +using NUnit.Framework;
    2.12 +using IBBoard.WarFoundry.API.Objects.Mock;
    2.13  using NUnit.Framework.SyntaxHelpers;
    2.14 +using System.Reflection;
    2.15 +using IBBoard.NUnit;
    2.16 +using IBBoard.WarFoundry.API.Factories;
    2.17  
    2.18  namespace IBBoard.WarFoundry.API.Objects.Requirement
    2.19  {
    2.20  	[TestFixture()]
    2.21 -	public class RequiresNoMoreThanNOfUnitTypeRequirementTest
    2.22 -	{
    2.23 -		private MockRace mockRace;
    2.24 -		private UnitType unitType1;
    2.25 -		private UnitType unitType2;
    2.26 -		private UnitType unitType3;
    2.27 -
    2.28 -		[TestFixtureSetUp()]
    2.29 -		public void SetupRace()
    2.30 -		{
    2.31 -			mockRace = new MockRace();
    2.32 -			unitType1 = new MockUnitType("type1", "Unit Type 1");
    2.33 -			mockRace.AddUnitType(unitType1);
    2.34 -			unitType2 = new MockUnitType("type2", "Unit Type 2");
    2.35 -			mockRace.AddUnitType(unitType2);
    2.36 -			unitType3 = new MockUnitType("type3", "Unit Type 3");
    2.37 -			mockRace.AddUnitType(unitType3);
    2.38 -		}
    2.39 -
    2.40 -		[Test()]
    2.41 -		public void TestAddingUnitWithNoUnitsAndOneUnitTypeRequiredIsAllowed()
    2.42 -		{
    2.43 -			Army army = new Army(mockRace, "Test", 1000);
    2.44 -			Unit unit = CreateUnitOfType(unitType1, army);
    2.45 -			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
    2.46 -			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
    2.47 -		}
    2.48 -
    2.49 -		[Test()]
    2.50 -		public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredIsNotAllowed()
    2.51 -		{
    2.52 -			Army army = new Army(mockRace, "Test", 1000);
    2.53 -			AddUnitOfTypeToArmy(unitType2, army);
    2.54 -			Unit unit = CreateUnitOfType(unitType1, army);
    2.55 -			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
    2.56 -			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
    2.57 -		}
    2.58 -
    2.59 -		[Test()]
    2.60 -		public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequiredIsAllowed()
    2.61 -		{
    2.62 -			Army army = new Army(mockRace, "Test", 1000);
    2.63 -			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
    2.64 -			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
    2.65 -		}
    2.66 -
    2.67 -		[Test()]
    2.68 -		public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredIsNotAllowed()
    2.69 -		{
    2.70 -			Army army = new Army(mockRace, "Test", 1000);
    2.71 -			AddUnitOfTypeToArmy(unitType2, army);
    2.72 -			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
    2.73 -			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
    2.74 -		}
    2.75 -
    2.76 -		[Test()]
    2.77 -		public void TestAddingUnitTypeSetsLimit()
    2.78 -		{
    2.79 -			Army army = new Army(mockRace, "Test", 1000);
    2.80 -			AddUnitOfTypeToArmy(unitType2, army);
    2.81 -			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
    2.82 -			req.AddUnitTypeRequirement(unitType2, 1);
    2.83 -			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
    2.84 -		}
    2.85 -
    2.86 -		[Test()]
    2.87 -		public void TestAddingUnitTypeSetsLimitForFailure()
    2.88 -		{
    2.89 -			Army army = new Army(mockRace, "Test", 1000);
    2.90 -			AddUnitOfTypeToArmy(unitType2, army);
    2.91 -			AddUnitOfTypeToArmy(unitType2, army);
    2.92 -			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
    2.93 -			req.AddUnitTypeRequirement(unitType2, 1);
    2.94 -			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
    2.95 -		}
    2.96 -
    2.97 -		[Test()]
    2.98 -		public void TestAddingUnitTypeDefaultsToNoMoreThanZero()
    2.99 -		{
   2.100 -			Army army = new Army(mockRace, "Test", 1000);
   2.101 -			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
   2.102 -			req.AddUnitTypeRequirement(unitType2);
   2.103 -			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
   2.104 -			AddUnitOfTypeToArmy(unitType2, army);
   2.105 -			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
   2.106 -		}
   2.107 -
   2.108 -		[Test]
   2.109 -		public void TestRequirementBecomesInvalidWhenProhibitedUnitsChange()
   2.110 -		{
   2.111 -			Army army = new Army(mockRace, "Test", 1000);
   2.112 -			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
   2.113 -			req.AddUnitTypeRequirement(unitType2);
   2.114 -			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
   2.115 -			AddUnitOfTypeToArmy(unitType2, army);
   2.116 -			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
   2.117 -		}
   2.118 -
   2.119 -		private static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
   2.120 -		{
   2.121 -			army.AddUnit(CreateUnitOfType(unitType, army));
   2.122 -		}
   2.123 -
   2.124 -		private static Unit CreateUnitOfType(UnitType unitType, Army army)
   2.125 -		{
   2.126 -			return new Unit(unitType, army.GetCategory(unitType.MainCategory));
   2.127 +	public class RequiresNoMoreThanNOfUnitTypeRequirementTest : AbstractEqualityTest<RequiresNoMoreThanNOfUnitTypeRequirement>
   2.128 +	{
   2.129 +		//FIXME We shouldn't duplicate these tests, but it is broken at the moment
   2.130 +		[Test]
   2.131 +		public void TestEquality()
   2.132 +		{
   2.133 +			Assert.AreEqual(GetObject(), GetSameObject());
   2.134 +			Assert.AreEqual(GetSameObject(), GetObject());
   2.135 +		}
   2.136 +
   2.137 +		[Test]
   2.138 +		public void TestInequality()
   2.139 +		{
   2.140 +			Assert.AreNotEqual(GetObject(), GetDifferentObject());
   2.141 +			Assert.AreNotEqual(GetSameObject(), GetDifferentObject());
   2.142 +			Assert.AreNotEqual(GetDifferentObject(), GetObject());
   2.143 +			Assert.AreNotEqual(GetDifferentObject(), GetSameObject());
   2.144 +		}
   2.145 +
   2.146 +		[Test]
   2.147 +		public void TestReflexiveEquality()
   2.148 +		{
   2.149 +			Assert.AreEqual(GetObject(), GetObject());
   2.150 +			Assert.AreEqual(GetSameObject(), GetSameObject());
   2.151 +			Assert.AreEqual(GetDifferentObject(), GetDifferentObject());
   2.152 +		}
   2.153 +
   2.154 +		[Test]
   2.155 +		public void TestOtherInequality()
   2.156 +		{
   2.157 +			MethodInfo[] methodInfo = GetType().GetMethods();
   2.158 +			RequiresNoMoreThanNOfUnitTypeRequirement obj = GetObject();
   2.159 +
   2.160 +			foreach (MethodInfo method in methodInfo)
   2.161 +			{
   2.162 +				if (method.Name.StartsWith("GetOtherDifferent"))
   2.163 +				{
   2.164 +					RequiresAtLeastNUnitsRequirement otherObj = (RequiresAtLeastNUnitsRequirement)method.Invoke(this, new object[0]);
   2.165 +					Assert.AreNotEqual(obj, otherObj, "Objects equal for "+method.Name);
   2.166 +					Assert.AreNotEqual(otherObj, obj, "Objects equal for "+method.Name);
   2.167 +				}
   2.168 +			}
   2.169 +		}
   2.170 +
   2.171 +
   2.172 +
   2.173 +
   2.174 +		private MockRace mockRace;
   2.175 +		private UnitType unitType1;
   2.176 +		private UnitType unitType2;
   2.177 +		private UnitType unitType3;
   2.178 +
   2.179 +		[TestFixtureSetUp()]
   2.180 +		public void SetupRace()
   2.181 +		{
   2.182 +			mockRace = new MockRace();
   2.183 +			unitType1 = new MockUnitType("type1", "Unit Type 1");
   2.184 +			mockRace.AddUnitType(unitType1);
   2.185 +			unitType2 = new MockUnitType("type2", "Unit Type 2");
   2.186 +			mockRace.AddUnitType(unitType2);
   2.187 +			unitType3 = new MockUnitType("type3", "Unit Type 3");
   2.188 +			mockRace.AddUnitType(unitType3);
   2.189 +		}
   2.190 +
   2.191 +		[Test()]
   2.192 +		public void TestAddingUnitWithNoUnitsAndOneUnitTypeRequiredIsAllowed()
   2.193 +		{
   2.194 +			Army army = new Army(mockRace, "Test", 1000);
   2.195 +			Unit unit = CreateUnitOfType(unitType1, army);
   2.196 +			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
   2.197 +			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
   2.198 +		}
   2.199 +
   2.200 +		[Test()]
   2.201 +		public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredIsNotAllowed()
   2.202 +		{
   2.203 +			Army army = new Army(mockRace, "Test", 1000);
   2.204 +			AddUnitOfTypeToArmy(unitType2, army);
   2.205 +			Unit unit = CreateUnitOfType(unitType1, army);
   2.206 +			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
   2.207 +			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
   2.208 +		}
   2.209 +
   2.210 +		[Test()]
   2.211 +		public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequiredIsAllowed()
   2.212 +		{
   2.213 +			Army army = new Army(mockRace, "Test", 1000);
   2.214 +			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
   2.215 +			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
   2.216 +		}
   2.217 +
   2.218 +		[Test()]
   2.219 +		public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredIsNotAllowed()
   2.220 +		{
   2.221 +			Army army = new Army(mockRace, "Test", 1000);
   2.222 +			AddUnitOfTypeToArmy(unitType2, army);
   2.223 +			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
   2.224 +			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
   2.225 +		}
   2.226 +
   2.227 +		[Test()]
   2.228 +		public void TestAddingUnitTypeSetsLimit()
   2.229 +		{
   2.230 +			Army army = new Army(mockRace, "Test", 1000);
   2.231 +			AddUnitOfTypeToArmy(unitType2, army);
   2.232 +			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
   2.233 +			req.AddUnitTypeRequirement(unitType2, 1);
   2.234 +			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
   2.235 +		}
   2.236 +
   2.237 +		[Test()]
   2.238 +		public void TestAddingUnitTypeSetsLimitForFailure()
   2.239 +		{
   2.240 +			Army army = new Army(mockRace, "Test", 1000);
   2.241 +			AddUnitOfTypeToArmy(unitType2, army);
   2.242 +			AddUnitOfTypeToArmy(unitType2, army);
   2.243 +			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
   2.244 +			req.AddUnitTypeRequirement(unitType2, 1);
   2.245 +			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
   2.246 +		}
   2.247 +
   2.248 +		[Test()]
   2.249 +		public void TestAddingUnitTypeDefaultsToNoMoreThanZero()
   2.250 +		{
   2.251 +			Army army = new Army(mockRace, "Test", 1000);
   2.252 +			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
   2.253 +			req.AddUnitTypeRequirement(unitType2);
   2.254 +			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
   2.255 +			AddUnitOfTypeToArmy(unitType2, army);
   2.256 +			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
   2.257 +		}
   2.258 +
   2.259 +		[Test]
   2.260 +		public void TestRequirementBecomesInvalidWhenProhibitedUnitsChange()
   2.261 +		{
   2.262 +			Army army = new Army(mockRace, "Test", 1000);
   2.263 +			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
   2.264 +			req.AddUnitTypeRequirement(unitType2);
   2.265 +			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
   2.266 +			AddUnitOfTypeToArmy(unitType2, army);
   2.267 +			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
   2.268 +		}
   2.269 +
   2.270 +		private static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
   2.271 +		{
   2.272 +			army.AddUnit(CreateUnitOfType(unitType, army));
   2.273 +		}
   2.274 +
   2.275 +		private static Unit CreateUnitOfType(UnitType unitType, Army army)
   2.276 +		{
   2.277 +			return new Unit(unitType, army.GetCategory(unitType.MainCategory));
   2.278 +		}
   2.279 +
   2.280 +		public override RequiresNoMoreThanNOfUnitTypeRequirement GetObject ()
   2.281 +		{
   2.282 +			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
   2.283 +			req.AddUnitTypeRequirement(unitType1, 2);
   2.284 +			return req;
   2.285 +		}
   2.286 +
   2.287 +		public override RequiresNoMoreThanNOfUnitTypeRequirement GetSameObject ()
   2.288 +		{
   2.289 +			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
   2.290 +			req.AddUnitTypeRequirement(unitType1, 2);
   2.291 +			return req;
   2.292 +		}
   2.293 +
   2.294 +		public override RequiresNoMoreThanNOfUnitTypeRequirement GetDifferentObject ()
   2.295 +		{
   2.296 +			RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
   2.297 +			DummyWarFoundryFactory factory = new DummyWarFoundryFactory();
   2.298 +			GameSystem gameSystem = new GameSystem("system", "system", factory);
   2.299 +			Race race = new Race("race", "race", gameSystem, factory);
   2.300 +			req.AddUnitTypeRequirement(new UnitType("id2", "Type 2", race), 2);
   2.301 +			return req;
   2.302  		}
   2.303  	}
   2.304  }