diff API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs @ 211:bc782a0d47bf

Re #379: Fix validation of requirements to check for unit * Finish off validation tests for "N units for M Units" requirement
author IBBoard <dev@ibboard.co.uk>
date Sat, 25 Feb 2012 17:02:38 +0000
parents 649759343da5
children 325943cb1db0
line wrap: on
line diff
--- a/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs	Sat Feb 25 16:36:20 2012 +0000
+++ b/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs	Sat Feb 25 17:02:38 2012 +0000
@@ -122,6 +122,18 @@
 		}
 
 		[Test()]
+		public void TestAddingLimitedUnitTriggersFailure()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType1, army);
+			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__PassesAdding(req, unitType2, army);
+			AddUnitOfTypeToArmy(unitType1, army);
+			Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 1 for 2).");
+		}
+
+		[Test()]
 		public void TestAddingUnitTypeDefaultsToOne()
 		{
 			Army army = new Army(mockRace, "Test", 1000);
@@ -177,8 +189,8 @@
 			Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 2 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 1 for 1).");
 		}
 
-		[Test()]
-		public void TestAddingLimitedUnitTriggersFailure()
+		[Test]
+		public void TestRequirementFailsWhenLimitedUnitsChange()
 		{
 			Army army = new Army(mockRace, "Test", 1000);
 			AddUnitOfTypeToArmy(unitType1, army);
@@ -190,162 +202,146 @@
 		}
 
 		[Test]
-		public void TestRequirementFailsWhenLimitedUnitsChange()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType1, army);
-			AddUnitOfTypeToArmy(unitType2, army);
-			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
-			req.AddUnitTypeRequirement(unitType2);
-			Assert_That__PassesAdding(req, unitType2, army);
-			AddUnitOfTypeToArmy(unitType1, army);
-			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
-		}
-
-		[Test]
 		public void TestValidationNotApplicableWithNoLimitedUnit()
 		{
 			Army army = new Army(mockRace, "Test", 1000);
 			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
 			req.AddUnitTypeRequirement(unitType2);
-			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
+			Assert_That__NotApplicable(req, army);
 			AddUnitOfTypeToArmy(unitType3, army);
-			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
+			Assert_That__NotApplicable(req, army);
 			AddUnitOfTypeToArmy(unitType2, army);
-			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
+			Assert_That__NotApplicable(req, army);
+		}
+
+		[Test()]
+		public void TestValidatingWithNoUnitsAndOneUnitTypeRequiredFails()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType1, army);
+			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
+		}
+
+		[Test()]
+		public void TestValidatingUnitWithOneUnitAndOneUnitTypeRequiredPasses()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitOfTypeToArmy(unitType1, army);
+			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2);
+			Assert_That__ValidationPasses(req, army);
 		}
 
 		[Test()]
-		public void TestValidationWithNoUnitsAndOneUnitTypeRequiredFails()
+		public void TestValidatingUnitTypeSetsLimit()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType2, army);
+			AddUnitOfTypeToArmy(unitType1, army);
+			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2, 1);
+			Assert_That__ValidationPasses(req, army);
+		}
+
+		[Test()]
+		public void TestValidatingUnitTypeSetsLimitForFailure()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			AddUnitOfTypeToArmy(unitType1, army);
+			AddUnitOfTypeToArmy(unitType1, army);
+			AddUnitOfTypeToArmy(unitType2, army);
+			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2, 1);
+			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
+		}
+
+		[Test()]
+		public void TestAddingUnitTriggersValidationPass()
 		{
 			Army army = new Army(mockRace, "Test", 1000);
 			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
-			req.AddUnitTypeRequirement(unitType2);
+			req.AddUnitTypeRequirement(unitType2, 1);
 			AddUnitOfTypeToArmy(unitType1, army);
-			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
+			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
+			AddUnitOfTypeToArmy(unitType2, army);
+			Assert_That__ValidationPasses(req, army);
 		}
 
 		[Test()]
-		public void TestValidationWithOneUnitAndOneUnitTypeRequiredPasses()
+		public void TestAddingLimitedUnitTriggersValidationFailure()
 		{
 			Army army = new Army(mockRace, "Test", 1000);
 			AddUnitOfTypeToArmy(unitType1, army);
 			AddUnitOfTypeToArmy(unitType2, army);
 			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
 			req.AddUnitTypeRequirement(unitType2);
-			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
+			Assert_That__ValidationPasses(req, army);
+			AddUnitOfTypeToArmy(unitType1, army);
+			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
+		}
+
+		[Test()]
+		public void TestValidatingUnitTypeDefaultsToOne()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2);
+			AddUnitOfTypeToArmy(unitType1, army);
+			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
+			AddUnitOfTypeToArmy(unitType2, army);
+			Assert_That__ValidationPasses(req, army);
 		}
 
 		[Test]
-		public void TestValidationMessageIsBlankForNotApplicable()
+		public void TestValidationRequirementNotApplicableWhenCheckingForNonLimitedUnits()
 		{
 			Army army = new Army(mockRace, "Test", 1000);
 			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
 			req.AddUnitTypeRequirement(unitType2);
-			Assert.That(req.GetValidationMessage(army), Is.Empty);
-		}
-
-		[Test]
-		public void TestValidationMessageIsCorrectForFailWithOneUnitRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType1, army);
-			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
-			req.AddUnitTypeRequirement(unitType2);
-			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1)."));
+			AddUnitOfTypeToArmy(unitType3, army);
+			Assert_That__NotApplicable(req, army);
+			AddUnitOfTypeToArmy(unitType2, army);
+			Assert_That__NotApplicable(req, army);
 		}
 
 		[Test]
-		public void TestValidationMessageIsCorrectForFailWithTwoOfOneUnitLimit()
+		public void TestValidationNotApplicableBecomesPassAfterAddingLimitedUnit()
 		{
 			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType1, army);
-			AddUnitOfTypeToArmy(unitType1, army);
-			AddUnitOfTypeToArmy(unitType1, army);
+			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2);
 			AddUnitOfTypeToArmy(unitType2, army);
-			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
-			req.AddUnitTypeRequirement(unitType2, 2);
-			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 2 × " + unitType1.Name + " (have 1 for 3)."));
+			Assert_That__NotApplicable(req, army);
+			AddUnitOfTypeToArmy(unitType1, army);
+			Assert_That__ValidationPasses(req, army);
 		}
 
 		[Test]
-		public void TestValidationMessageIsCorrectForFailWithTwoUnitsRequiredAndOneOfOneUnit()
+		public void TestValidationRequirementNotApplicableBecomesFailAfterAddingLimitedUnit()
+		{
+			Army army = new Army(mockRace, "Test", 1000);
+			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
+			req.AddUnitTypeRequirement(unitType2, 2, 1);
+			AddUnitOfTypeToArmy(unitType2, army);
+			Assert_That__NotApplicable(req, army);
+			AddUnitOfTypeToArmy(unitType1, army);
+			Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 1).");
+		}
+
+		[Test]
+		public void TestValidationRequirementFailsWhenLimitedUnitsChange()
 		{
 			Army army = new Army(mockRace, "Test", 1000);
 			AddUnitOfTypeToArmy(unitType1, army);
 			AddUnitOfTypeToArmy(unitType2, army);
 			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
 			req.AddUnitTypeRequirement(unitType2);
-			req.AddUnitTypeRequirement(unitType3);
-			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType3.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1)."));
-		}
-
-		[Test]
-		public void TestValidationMessageIsCorrectForFailWithTwoUnitsRequiredAndOneOfOtherUnitInArmy()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType1, army);
-			AddUnitOfTypeToArmy(unitType3, army);
-			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
-			req.AddUnitTypeRequirement(unitType2);
-			req.AddUnitTypeRequirement(unitType3);
-			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1)."));
-		}
-
-		[Test]
-		public void TestValidationMessageIsCorrectForFailWithTwoRequiredUnitsAndNeitherInArmy()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType1, army);
-			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
-			req.AddUnitTypeRequirement(unitType2);
-			req.AddUnitTypeRequirement(unitType3);
-			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1); 1 × " + unitType3.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1)."));
-		}
-
-		[Test]
-		public void TestAllowsAddMessageIsCorrectForFailWithTwoOfOneUnitLimit()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType1, army);
+			Assert_That__ValidationPasses(req, army);
 			AddUnitOfTypeToArmy(unitType1, army);
-			AddUnitOfTypeToArmy(unitType2, army);
-			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
-			req.AddUnitTypeRequirement(unitType2, 2);
-			Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 2 × " + unitType1.Name + " (would have 1 for 3)."));
-		}
-
-		[Test]
-		public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsRequired()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType1, army);
-			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
-			req.AddUnitTypeRequirement(unitType2);
-			req.AddUnitTypeRequirement(unitType3);
-			Assert.That(req.GetAllowsAddingMessage(unitType2, army), Is.EqualTo("Army must contain: 1 × " + unitType3.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1)."));
-		}
-
-		[Test]
-		public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsRequiredAndOneUnitInArmy()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType2, army);
-			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
-			req.AddUnitTypeRequirement(unitType2);
-			req.AddUnitTypeRequirement(unitType3);
-			Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 1 × " + unitType3.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1)."));
-		}
-
-		[Test]
-		public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsRequiredAndOtherUnitInArmy()
-		{
-			Army army = new Army(mockRace, "Test", 1000);
-			AddUnitOfTypeToArmy(unitType3, army);
-			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
-			req.AddUnitTypeRequirement(unitType2);
-			req.AddUnitTypeRequirement(unitType3);
-			Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1)."));
+			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
 		}
 
 		private static void Assert_That__PassesAdding(UnitRequiresNUnitsForMUnitsRequirement req, UnitType unitType, Army army)
@@ -384,6 +380,24 @@
 			Assert.That(req.GetAllowsAddingMessage(unit, army), Is.EqualTo(message));
 		}
 
+		private static void Assert_That__ValidationPasses(UnitRequiresNUnitsForMUnitsRequirement req, Army army)
+		{
+			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
+			Assert.That(req.GetValidationMessage(army), Is.Empty);
+		}
+
+		private static void Assert_That__NotApplicable(UnitRequiresNUnitsForMUnitsRequirement req, Army army)
+		{
+			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
+			Assert.That(req.GetValidationMessage(army), Is.Empty);
+		}
+
+		private static void Assert_That__ValidationFails(UnitRequiresNUnitsForMUnitsRequirement req, Army army, string message)
+		{
+			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
+			Assert.That(req.GetValidationMessage(army), Is.EqualTo(message));
+		}
+
 		private static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
 		{
 			army.AddUnit(CreateUnitOfType(unitType, army));