changeset 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
files API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs
diffstat 1 files changed, 148 insertions(+), 134 deletions(-) [+]
line diff
     1.1 --- a/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs	Sat Feb 25 16:36:20 2012 +0000
     1.2 +++ b/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs	Sat Feb 25 17:02:38 2012 +0000
     1.3 @@ -122,6 +122,18 @@
     1.4  		}
     1.5  
     1.6  		[Test()]
     1.7 +		public void TestAddingLimitedUnitTriggersFailure()
     1.8 +		{
     1.9 +			Army army = new Army(mockRace, "Test", 1000);
    1.10 +			AddUnitOfTypeToArmy(unitType1, army);
    1.11 +			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
    1.12 +			req.AddUnitTypeRequirement(unitType2);
    1.13 +			Assert_That__PassesAdding(req, unitType2, army);
    1.14 +			AddUnitOfTypeToArmy(unitType1, army);
    1.15 +			Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 1 for 2).");
    1.16 +		}
    1.17 +
    1.18 +		[Test()]
    1.19  		public void TestAddingUnitTypeDefaultsToOne()
    1.20  		{
    1.21  			Army army = new Army(mockRace, "Test", 1000);
    1.22 @@ -177,8 +189,8 @@
    1.23  			Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 2 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 1 for 1).");
    1.24  		}
    1.25  
    1.26 -		[Test()]
    1.27 -		public void TestAddingLimitedUnitTriggersFailure()
    1.28 +		[Test]
    1.29 +		public void TestRequirementFailsWhenLimitedUnitsChange()
    1.30  		{
    1.31  			Army army = new Army(mockRace, "Test", 1000);
    1.32  			AddUnitOfTypeToArmy(unitType1, army);
    1.33 @@ -190,162 +202,146 @@
    1.34  		}
    1.35  
    1.36  		[Test]
    1.37 -		public void TestRequirementFailsWhenLimitedUnitsChange()
    1.38 +		public void TestValidationNotApplicableWithNoLimitedUnit()
    1.39 +		{
    1.40 +			Army army = new Army(mockRace, "Test", 1000);
    1.41 +			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
    1.42 +			req.AddUnitTypeRequirement(unitType2);
    1.43 +			Assert_That__NotApplicable(req, army);
    1.44 +			AddUnitOfTypeToArmy(unitType3, army);
    1.45 +			Assert_That__NotApplicable(req, army);
    1.46 +			AddUnitOfTypeToArmy(unitType2, army);
    1.47 +			Assert_That__NotApplicable(req, army);
    1.48 +		}
    1.49 +
    1.50 +		[Test()]
    1.51 +		public void TestValidatingWithNoUnitsAndOneUnitTypeRequiredFails()
    1.52 +		{
    1.53 +			Army army = new Army(mockRace, "Test", 1000);
    1.54 +			AddUnitOfTypeToArmy(unitType1, army);
    1.55 +			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
    1.56 +			req.AddUnitTypeRequirement(unitType2);
    1.57 +			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
    1.58 +		}
    1.59 +
    1.60 +		[Test()]
    1.61 +		public void TestValidatingUnitWithOneUnitAndOneUnitTypeRequiredPasses()
    1.62 +		{
    1.63 +			Army army = new Army(mockRace, "Test", 1000);
    1.64 +			AddUnitOfTypeToArmy(unitType2, army);
    1.65 +			AddUnitOfTypeToArmy(unitType1, army);
    1.66 +			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
    1.67 +			req.AddUnitTypeRequirement(unitType2);
    1.68 +			Assert_That__ValidationPasses(req, army);
    1.69 +		}
    1.70 +
    1.71 +		[Test()]
    1.72 +		public void TestValidatingUnitTypeSetsLimit()
    1.73 +		{
    1.74 +			Army army = new Army(mockRace, "Test", 1000);
    1.75 +			AddUnitOfTypeToArmy(unitType2, army);
    1.76 +			AddUnitOfTypeToArmy(unitType1, army);
    1.77 +			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
    1.78 +			req.AddUnitTypeRequirement(unitType2, 1);
    1.79 +			Assert_That__ValidationPasses(req, army);
    1.80 +		}
    1.81 +
    1.82 +		[Test()]
    1.83 +		public void TestValidatingUnitTypeSetsLimitForFailure()
    1.84 +		{
    1.85 +			Army army = new Army(mockRace, "Test", 1000);
    1.86 +			AddUnitOfTypeToArmy(unitType1, army);
    1.87 +			AddUnitOfTypeToArmy(unitType1, army);
    1.88 +			AddUnitOfTypeToArmy(unitType2, army);
    1.89 +			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
    1.90 +			req.AddUnitTypeRequirement(unitType2, 1);
    1.91 +			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
    1.92 +		}
    1.93 +
    1.94 +		[Test()]
    1.95 +		public void TestAddingUnitTriggersValidationPass()
    1.96 +		{
    1.97 +			Army army = new Army(mockRace, "Test", 1000);
    1.98 +			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
    1.99 +			req.AddUnitTypeRequirement(unitType2, 1);
   1.100 +			AddUnitOfTypeToArmy(unitType1, army);
   1.101 +			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
   1.102 +			AddUnitOfTypeToArmy(unitType2, army);
   1.103 +			Assert_That__ValidationPasses(req, army);
   1.104 +		}
   1.105 +
   1.106 +		[Test()]
   1.107 +		public void TestAddingLimitedUnitTriggersValidationFailure()
   1.108  		{
   1.109  			Army army = new Army(mockRace, "Test", 1000);
   1.110  			AddUnitOfTypeToArmy(unitType1, army);
   1.111  			AddUnitOfTypeToArmy(unitType2, army);
   1.112  			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.113  			req.AddUnitTypeRequirement(unitType2);
   1.114 -			Assert_That__PassesAdding(req, unitType2, army);
   1.115 +			Assert_That__ValidationPasses(req, army);
   1.116  			AddUnitOfTypeToArmy(unitType1, army);
   1.117 -			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
   1.118 -		}
   1.119 -
   1.120 -		[Test]
   1.121 -		public void TestValidationNotApplicableWithNoLimitedUnit()
   1.122 -		{
   1.123 -			Army army = new Army(mockRace, "Test", 1000);
   1.124 -			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.125 -			req.AddUnitTypeRequirement(unitType2);
   1.126 -			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
   1.127 -			AddUnitOfTypeToArmy(unitType3, army);
   1.128 -			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
   1.129 -			AddUnitOfTypeToArmy(unitType2, army);
   1.130 -			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
   1.131 +			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
   1.132  		}
   1.133  
   1.134  		[Test()]
   1.135 -		public void TestValidationWithNoUnitsAndOneUnitTypeRequiredFails()
   1.136 +		public void TestValidatingUnitTypeDefaultsToOne()
   1.137  		{
   1.138  			Army army = new Army(mockRace, "Test", 1000);
   1.139  			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.140  			req.AddUnitTypeRequirement(unitType2);
   1.141  			AddUnitOfTypeToArmy(unitType1, army);
   1.142 -			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
   1.143 +			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
   1.144 +			AddUnitOfTypeToArmy(unitType2, army);
   1.145 +			Assert_That__ValidationPasses(req, army);
   1.146  		}
   1.147  
   1.148 -		[Test()]
   1.149 -		public void TestValidationWithOneUnitAndOneUnitTypeRequiredPasses()
   1.150 +		[Test]
   1.151 +		public void TestValidationRequirementNotApplicableWhenCheckingForNonLimitedUnits()
   1.152 +		{
   1.153 +			Army army = new Army(mockRace, "Test", 1000);
   1.154 +			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.155 +			req.AddUnitTypeRequirement(unitType2);
   1.156 +			AddUnitOfTypeToArmy(unitType3, army);
   1.157 +			Assert_That__NotApplicable(req, army);
   1.158 +			AddUnitOfTypeToArmy(unitType2, army);
   1.159 +			Assert_That__NotApplicable(req, army);
   1.160 +		}
   1.161 +
   1.162 +		[Test]
   1.163 +		public void TestValidationNotApplicableBecomesPassAfterAddingLimitedUnit()
   1.164 +		{
   1.165 +			Army army = new Army(mockRace, "Test", 1000);
   1.166 +			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.167 +			req.AddUnitTypeRequirement(unitType2);
   1.168 +			AddUnitOfTypeToArmy(unitType2, army);
   1.169 +			Assert_That__NotApplicable(req, army);
   1.170 +			AddUnitOfTypeToArmy(unitType1, army);
   1.171 +			Assert_That__ValidationPasses(req, army);
   1.172 +		}
   1.173 +
   1.174 +		[Test]
   1.175 +		public void TestValidationRequirementNotApplicableBecomesFailAfterAddingLimitedUnit()
   1.176 +		{
   1.177 +			Army army = new Army(mockRace, "Test", 1000);
   1.178 +			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.179 +			req.AddUnitTypeRequirement(unitType2, 2, 1);
   1.180 +			AddUnitOfTypeToArmy(unitType2, army);
   1.181 +			Assert_That__NotApplicable(req, army);
   1.182 +			AddUnitOfTypeToArmy(unitType1, army);
   1.183 +			Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 1).");
   1.184 +		}
   1.185 +
   1.186 +		[Test]
   1.187 +		public void TestValidationRequirementFailsWhenLimitedUnitsChange()
   1.188  		{
   1.189  			Army army = new Army(mockRace, "Test", 1000);
   1.190  			AddUnitOfTypeToArmy(unitType1, army);
   1.191  			AddUnitOfTypeToArmy(unitType2, army);
   1.192  			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.193  			req.AddUnitTypeRequirement(unitType2);
   1.194 -			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
   1.195 -		}
   1.196 -
   1.197 -		[Test]
   1.198 -		public void TestValidationMessageIsBlankForNotApplicable()
   1.199 -		{
   1.200 -			Army army = new Army(mockRace, "Test", 1000);
   1.201 -			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.202 -			req.AddUnitTypeRequirement(unitType2);
   1.203 -			Assert.That(req.GetValidationMessage(army), Is.Empty);
   1.204 -		}
   1.205 -
   1.206 -		[Test]
   1.207 -		public void TestValidationMessageIsCorrectForFailWithOneUnitRequired()
   1.208 -		{
   1.209 -			Army army = new Army(mockRace, "Test", 1000);
   1.210 +			Assert_That__ValidationPasses(req, army);
   1.211  			AddUnitOfTypeToArmy(unitType1, army);
   1.212 -			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.213 -			req.AddUnitTypeRequirement(unitType2);
   1.214 -			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1)."));
   1.215 -		}
   1.216 -
   1.217 -		[Test]
   1.218 -		public void TestValidationMessageIsCorrectForFailWithTwoOfOneUnitLimit()
   1.219 -		{
   1.220 -			Army army = new Army(mockRace, "Test", 1000);
   1.221 -			AddUnitOfTypeToArmy(unitType1, army);
   1.222 -			AddUnitOfTypeToArmy(unitType1, army);
   1.223 -			AddUnitOfTypeToArmy(unitType1, army);
   1.224 -			AddUnitOfTypeToArmy(unitType2, army);
   1.225 -			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.226 -			req.AddUnitTypeRequirement(unitType2, 2);
   1.227 -			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 2 × " + unitType1.Name + " (have 1 for 3)."));
   1.228 -		}
   1.229 -
   1.230 -		[Test]
   1.231 -		public void TestValidationMessageIsCorrectForFailWithTwoUnitsRequiredAndOneOfOneUnit()
   1.232 -		{
   1.233 -			Army army = new Army(mockRace, "Test", 1000);
   1.234 -			AddUnitOfTypeToArmy(unitType1, army);
   1.235 -			AddUnitOfTypeToArmy(unitType2, army);
   1.236 -			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.237 -			req.AddUnitTypeRequirement(unitType2);
   1.238 -			req.AddUnitTypeRequirement(unitType3);
   1.239 -			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType3.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1)."));
   1.240 -		}
   1.241 -
   1.242 -		[Test]
   1.243 -		public void TestValidationMessageIsCorrectForFailWithTwoUnitsRequiredAndOneOfOtherUnitInArmy()
   1.244 -		{
   1.245 -			Army army = new Army(mockRace, "Test", 1000);
   1.246 -			AddUnitOfTypeToArmy(unitType1, army);
   1.247 -			AddUnitOfTypeToArmy(unitType3, army);
   1.248 -			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.249 -			req.AddUnitTypeRequirement(unitType2);
   1.250 -			req.AddUnitTypeRequirement(unitType3);
   1.251 -			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1)."));
   1.252 -		}
   1.253 -
   1.254 -		[Test]
   1.255 -		public void TestValidationMessageIsCorrectForFailWithTwoRequiredUnitsAndNeitherInArmy()
   1.256 -		{
   1.257 -			Army army = new Army(mockRace, "Test", 1000);
   1.258 -			AddUnitOfTypeToArmy(unitType1, army);
   1.259 -			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.260 -			req.AddUnitTypeRequirement(unitType2);
   1.261 -			req.AddUnitTypeRequirement(unitType3);
   1.262 -			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)."));
   1.263 -		}
   1.264 -
   1.265 -		[Test]
   1.266 -		public void TestAllowsAddMessageIsCorrectForFailWithTwoOfOneUnitLimit()
   1.267 -		{
   1.268 -			Army army = new Army(mockRace, "Test", 1000);
   1.269 -			AddUnitOfTypeToArmy(unitType1, army);
   1.270 -			AddUnitOfTypeToArmy(unitType1, army);
   1.271 -			AddUnitOfTypeToArmy(unitType2, army);
   1.272 -			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.273 -			req.AddUnitTypeRequirement(unitType2, 2);
   1.274 -			Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 2 × " + unitType1.Name + " (would have 1 for 3)."));
   1.275 -		}
   1.276 -
   1.277 -		[Test]
   1.278 -		public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsRequired()
   1.279 -		{
   1.280 -			Army army = new Army(mockRace, "Test", 1000);
   1.281 -			AddUnitOfTypeToArmy(unitType1, army);
   1.282 -			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.283 -			req.AddUnitTypeRequirement(unitType2);
   1.284 -			req.AddUnitTypeRequirement(unitType3);
   1.285 -			Assert.That(req.GetAllowsAddingMessage(unitType2, army), Is.EqualTo("Army must contain: 1 × " + unitType3.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1)."));
   1.286 -		}
   1.287 -
   1.288 -		[Test]
   1.289 -		public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsRequiredAndOneUnitInArmy()
   1.290 -		{
   1.291 -			Army army = new Army(mockRace, "Test", 1000);
   1.292 -			AddUnitOfTypeToArmy(unitType2, army);
   1.293 -			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.294 -			req.AddUnitTypeRequirement(unitType2);
   1.295 -			req.AddUnitTypeRequirement(unitType3);
   1.296 -			Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 1 × " + unitType3.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1)."));
   1.297 -		}
   1.298 -
   1.299 -		[Test]
   1.300 -		public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsRequiredAndOtherUnitInArmy()
   1.301 -		{
   1.302 -			Army army = new Army(mockRace, "Test", 1000);
   1.303 -			AddUnitOfTypeToArmy(unitType3, army);
   1.304 -			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
   1.305 -			req.AddUnitTypeRequirement(unitType2);
   1.306 -			req.AddUnitTypeRequirement(unitType3);
   1.307 -			Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1)."));
   1.308 +			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
   1.309  		}
   1.310  
   1.311  		private static void Assert_That__PassesAdding(UnitRequiresNUnitsForMUnitsRequirement req, UnitType unitType, Army army)
   1.312 @@ -384,6 +380,24 @@
   1.313  			Assert.That(req.GetAllowsAddingMessage(unit, army), Is.EqualTo(message));
   1.314  		}
   1.315  
   1.316 +		private static void Assert_That__ValidationPasses(UnitRequiresNUnitsForMUnitsRequirement req, Army army)
   1.317 +		{
   1.318 +			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
   1.319 +			Assert.That(req.GetValidationMessage(army), Is.Empty);
   1.320 +		}
   1.321 +
   1.322 +		private static void Assert_That__NotApplicable(UnitRequiresNUnitsForMUnitsRequirement req, Army army)
   1.323 +		{
   1.324 +			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
   1.325 +			Assert.That(req.GetValidationMessage(army), Is.Empty);
   1.326 +		}
   1.327 +
   1.328 +		private static void Assert_That__ValidationFails(UnitRequiresNUnitsForMUnitsRequirement req, Army army, string message)
   1.329 +		{
   1.330 +			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
   1.331 +			Assert.That(req.GetValidationMessage(army), Is.EqualTo(message));
   1.332 +		}
   1.333 +
   1.334  		private static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
   1.335  		{
   1.336  			army.AddUnit(CreateUnitOfType(unitType, army));