changeset 138:11faab6c712a

Re #140: Add equality methods * Add equality method tests for RequiresAtLeastNUnitsRequirement
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Apr 2011 19:14:34 +0000
parents 081b48413f6d
children 3e4864e206ea
files API/Objects/Requirement/RequiresAtLeastNUnitsRequirementTest.cs
diffstat 1 files changed, 223 insertions(+), 151 deletions(-) [+]
line wrap: on
line diff
--- a/API/Objects/Requirement/RequiresAtLeastNUnitsRequirementTest.cs	Tue Apr 26 15:03:46 2011 +0000
+++ b/API/Objects/Requirement/RequiresAtLeastNUnitsRequirementTest.cs	Tue Apr 26 19:14:34 2011 +0000
@@ -1,179 +1,251 @@
-// This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 IBBoard
+// This file (UnitRequiresAtLeastNUnitsRequirement.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;
+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 RequiresAtLeastNUnitsRequirementTest
-	{
-		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);
-		}
+	public class RequiresAtLeastNUnitsRequirementTest : AbstractEqualityTest<RequiresAtLeastNUnitsRequirement>
+	{
+		//FIXME We shouldn't duplicate these tests, but it is broken at the moment
+		[Test]
+		public void TestEquality()
+		{
+			Assert.AreEqual(GetObject(), GetSameObject());
+			Assert.AreEqual(GetSameObject(), GetObject());
+		}
+
+		[Test]
+		public void TestInequality()
+		{
+			Assert.AreNotEqual(GetObject(), GetDifferentObject());
+			Assert.AreNotEqual(GetSameObject(), GetDifferentObject());
+			Assert.AreNotEqual(GetDifferentObject(), GetObject());
+			Assert.AreNotEqual(GetDifferentObject(), GetSameObject());
+		}
+
+		[Test]
+		public void TestReflexiveEquality()
+		{
+			Assert.AreEqual(GetObject(), GetObject());
+			Assert.AreEqual(GetSameObject(), GetSameObject());
+			Assert.AreEqual(GetDifferentObject(), GetDifferentObject());
+		}
+
+		[Test]
+		public void TestOtherInequality()
+		{
+			MethodInfo[] methodInfo = GetType().GetMethods();
+			RequiresAtLeastNUnitsRequirement obj = GetObject();
+
+			foreach (MethodInfo method in methodInfo)
+			{
+				if (method.Name.StartsWith("GetOtherDifferent"))
+				{
+					RequiresAtLeastNUnitsRequirement otherObj = (RequiresAtLeastNUnitsRequirement)method.Invoke(this, new object[0]);
+					Assert.AreNotEqual(obj, otherObj, "Objects equal for "+method.Name);
+					Assert.AreNotEqual(otherObj, obj, "Objects equal for "+method.Name);
+				}
+			}
+		}
+
+
+
+
+		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 TestAddingUnrelatedUnitWithNoUnitsAndOneUnitTypeRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			Unit unit = CreateUnitOfType(unitType1, army);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2);
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			Unit unit = CreateUnitOfType(unitType1, army);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2);
 			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.NotApplicable));
-		}
-
-		[Test()]
-		public void TestAddingOneUnitWithOneUnitTypeRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			Unit unit = CreateUnitOfType(unitType1, army);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
-			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
-		}
-
-		[Test()]
-		public void TestAddingUnitWithOneUnitAndTwoOfUnitTypeRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
-			req.AddUnitTypeRequirement(unitType1, 2);
-			AddUnitOfTypeToArmy(unitType1, army);
-			Unit unit = CreateUnitOfType(unitType1, army);
-			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
-		}
-
-		[Test()]
-		public void TestAddingUnrelatedUnitTypeWithNoUnitsAndOneUnitTypeRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2);
-			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.NotApplicable));
-		}
-
-		[Test()]
-		public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
-			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
-		}
-
-		[Test()]
-		public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType1, army);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
-			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
-		}
-
-		[Test()]
-		public void TestAddingUnitTypeWithNoUnitsAndTwoUnitTypesRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2, unitType3);
-			Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Failed));
-		}
-
-		[Test()]
-		public void TestAddingUnitTypeWithOneUnitAndTwoUnitTypesRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType3, army);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2, unitType3);
-			Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Passed));
+		}
+
+		[Test()]
+		public void TestAddingOneUnitWithOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			Unit unit = CreateUnitOfType(unitType1, army);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
+			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
+		}
+
+		[Test()]
+		public void TestAddingUnitWithOneUnitAndTwoOfUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
+			req.AddUnitTypeRequirement(unitType1, 2);
+			AddUnitOfTypeToArmy(unitType1, army);
+			Unit unit = CreateUnitOfType(unitType1, army);
+			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
+		}
+
+		[Test()]
+		public void TestAddingUnrelatedUnitTypeWithNoUnitsAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2);
+			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.NotApplicable));
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
+			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType1, army);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
+			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithNoUnitsAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2, unitType3);
+			Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Failed));
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithOneUnitAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2, unitType3);
+			Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Passed));
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeWithBothUnitsAndTwoUnitTypesRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitOfTypeToArmy(unitType3, army);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2, unitType3);
+			Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Passed));
+		}
+
+		[Test()]
+		public void TestAddingOneOfUnitTypeWithTwoOfOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
+			req.AddUnitTypeRequirement(unitType1, 2);
+			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
+		}
+
+		[Test()]
+		public void TestAddingOneUnitTypeWithOneUnitAndTwoOfOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType1, army);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
+			req.AddUnitTypeRequirement(unitType1, 2);
+			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
+		}
+
+		[Test()]
+		public void TestAddingUnitTypeRequirementWithNoNumberDefaultsToHavingOne()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
+			req.AddUnitTypeRequirement(unitType1);
+			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
+		}
+
+		[Test()]
+		public void TestValidityOfArmyWithNoUnitsAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
+			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
+		}
+
+		[Test()]
+		public void TestValidityOfArmyWithOneUnitAndOneUnitTypeRequired()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType1, army);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
+			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
+		}
+
+		[Test]
+		public void TestAddingMinNumberUnitTypePassesRequirement()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
+			req.AddUnitTypeRequirement(unitType1, 2);
+			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
+			AddUnitOfTypeToArmy(unitType1, army);
+			Assert.That(req.AllowsAdding(unitType1, 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));
 		}
 
-		[Test()]
-		public void TestAddingUnitTypeWithBothUnitsAndTwoUnitTypesRequired()
+		public override RequiresAtLeastNUnitsRequirement GetObject ()
 		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType2, army);
-			AddUnitOfTypeToArmy(unitType3, army);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2, unitType3);
-			Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Passed));
-		}
-
-		[Test()]
-		public void TestAddingOneOfUnitTypeWithTwoOfOneUnitTypeRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
 			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
 			req.AddUnitTypeRequirement(unitType1, 2);
-			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
-		}
-
-		[Test()]
-		public void TestAddingOneUnitTypeWithOneUnitAndTwoOfOneUnitTypeRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType1, army);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
-			req.AddUnitTypeRequirement(unitType1, 2);
-			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
-		}
-
-		[Test()]
-		public void TestAddingUnitTypeRequirementWithNoNumberDefaultsToHavingOne()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
-			req.AddUnitTypeRequirement(unitType1);
-			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
+			return req;
 		}
 
-		[Test()]
-		public void TestValidityOfArmyWithNoUnitsAndOneUnitTypeRequired()
+		public override RequiresAtLeastNUnitsRequirement GetSameObject ()
 		{
-			Army army = new Army(mockRace, "Test", 1000);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
-			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
-		}
-
-		[Test()]
-		public void TestValidityOfArmyWithOneUnitAndOneUnitTypeRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType1, army);
-			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
-			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
+			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
+			req.AddUnitTypeRequirement(unitType1, 2);
+			return req;
 		}
 
-		[Test]
-		public void TestAddingMinNumberUnitTypePassesRequirement()
+		public override RequiresAtLeastNUnitsRequirement GetDifferentObject ()
 		{
-			Army army = new Army(mockRace, "Test", 1000);
 			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
-			req.AddUnitTypeRequirement(unitType1, 2);
-			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
-			AddUnitOfTypeToArmy(unitType1, army);
-			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
-		}
-
-		private static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
-		{
-			army.AddUnit(CreateUnitOfType(unitType, army));
+			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);
+			return req;
 		}
-
-		private static Unit CreateUnitOfType(UnitType unitType, Army army)
-		{
-			return new Unit(unitType, army.GetCategory(unitType.MainCategory));
-		}
 	}
 }