view 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 source
1 // This file (UnitRequiresNUnitsForMUnitsRequirementTest.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 IBBoard
2 //
3 // 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.
4 using System;
5 using NUnit.Framework;
6 using IBBoard.WarFoundry.API.Objects.Mock;
7 using NUnit.Framework.SyntaxHelpers;
9 namespace IBBoard.WarFoundry.API.Objects.Requirement
10 {
11 [TestFixture()]
12 public class UnitRequiresNUnitsForMUnitsRequirementTests
13 {
14 private MockRace mockRace;
15 private UnitType unitType1;
16 private UnitType unitType2;
17 private UnitType unitType3;
19 [TestFixtureSetUp()]
20 public void SetupRace()
21 {
22 mockRace = new MockRace();
23 unitType1 = new MockUnitType("type1", "Unit Type 1");
24 mockRace.AddUnitType(unitType1);
25 unitType2 = new MockUnitType("type2", "Unit Type 2");
26 mockRace.AddUnitType(unitType2);
27 unitType3 = new MockUnitType("type3", "Unit Type 3");
28 mockRace.AddUnitType(unitType3);
29 }
31 [Test()]
32 public void TestAddingUnitWithNoUnitsAndOneUnitTypeRequiredFails()
33 {
34 Army army = new Army(mockRace, "Test", 1000);
35 Unit unit = CreateUnitOfType(unitType1, army);
36 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
37 req.AddUnitTypeRequirement(unitType2);
38 Assert_That__FailsAdding(req, unit, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1).");
39 }
41 [Test()]
42 public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequiredFails()
43 {
44 Army army = new Army(mockRace, "Test", 1000);
45 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
46 req.AddUnitTypeRequirement(unitType2);
47 Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1).");
48 }
50 [Test()]
51 public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredPasses()
52 {
53 Army army = new Army(mockRace, "Test", 1000);
54 AddUnitOfTypeToArmy(unitType2, army);
55 Unit unit = CreateUnitOfType(unitType1, army);
56 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
57 req.AddUnitTypeRequirement(unitType2);
58 Assert_That__PassesAdding(req, unit, army);
59 }
61 [Test()]
62 public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredPasses()
63 {
64 Army army = new Army(mockRace, "Test", 1000);
65 AddUnitOfTypeToArmy(unitType2, army);
66 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
67 req.AddUnitTypeRequirement(unitType2);
68 Assert_That__PassesAdding(req, unitType1, army);
69 }
71 [Test()]
72 public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredPassesWhenAddingRequiredUnit()
73 {
74 Army army = new Army(mockRace, "Test", 1000);
75 AddUnitOfTypeToArmy(unitType1, army);
76 Unit unit = CreateUnitOfType(unitType2, army);
77 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
78 req.AddUnitTypeRequirement(unitType2);
79 Assert_That__PassesAdding(req, unit, army);
80 }
82 [Test()]
83 public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredPassesWhenAddingRequiredUnitType()
84 {
85 Army army = new Army(mockRace, "Test", 1000);
86 AddUnitOfTypeToArmy(unitType1, army);
87 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
88 req.AddUnitTypeRequirement(unitType2);
89 Assert_That__PassesAdding(req, unitType2, army);
90 }
92 [Test()]
93 public void TestAddingUnitTypeSetsLimit()
94 {
95 Army army = new Army(mockRace, "Test", 1000);
96 AddUnitOfTypeToArmy(unitType2, army);
97 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
98 req.AddUnitTypeRequirement(unitType2, 1);
99 Assert_That__PassesAdding(req, unitType1, army);
100 }
102 [Test()]
103 public void TestAddingUnitTypeSetsLimitForFailure()
104 {
105 Army army = new Army(mockRace, "Test", 1000);
106 AddUnitOfTypeToArmy(unitType1, army);
107 AddUnitOfTypeToArmy(unitType2, army);
108 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
109 req.AddUnitTypeRequirement(unitType2, 1);
110 Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 1 for 2).");
111 }
113 [Test()]
114 public void TestAddingUnitTriggersPass()
115 {
116 Army army = new Army(mockRace, "Test", 1000);
117 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
118 req.AddUnitTypeRequirement(unitType2, 1);
119 Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1).");
120 AddUnitOfTypeToArmy(unitType2, army);
121 Assert_That__PassesAdding(req, unitType1, army);
122 }
124 [Test()]
125 public void TestAddingLimitedUnitTriggersFailure()
126 {
127 Army army = new Army(mockRace, "Test", 1000);
128 AddUnitOfTypeToArmy(unitType1, army);
129 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
130 req.AddUnitTypeRequirement(unitType2);
131 Assert_That__PassesAdding(req, unitType2, army);
132 AddUnitOfTypeToArmy(unitType1, army);
133 Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 1 for 2).");
134 }
136 [Test()]
137 public void TestAddingUnitTypeDefaultsToOne()
138 {
139 Army army = new Army(mockRace, "Test", 1000);
140 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
141 req.AddUnitTypeRequirement(unitType2);
142 Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1).");
143 AddUnitOfTypeToArmy(unitType2, army);
144 Assert_That__PassesAdding(req, unitType1, army);
145 }
147 [Test]
148 public void TestRequirementNotApplicableWhenCheckingAddForNonLimitedUnits()
149 {
150 Army army = new Army(mockRace, "Test", 1000);
151 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
152 req.AddUnitTypeRequirement(unitType2);
153 Assert_That__AddingNotApplicable(req, unitType3, army);
154 Assert_That__AddingNotApplicable(req, CreateUnitOfType(unitType3, army), army);
155 Assert_That__AddingNotApplicable(req, unitType2, army);
156 Assert_That__AddingNotApplicable(req, CreateUnitOfType(unitType2, army), army);
157 }
159 [Test]
160 public void TestRequirementNotApplicableAfterAddingLimitedUnit()
161 {
162 Army army = new Army(mockRace, "Test", 1000);
163 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
164 req.AddUnitTypeRequirement(unitType2);
165 Assert_That__AddingNotApplicable(req, unitType3, army);
166 AddUnitOfTypeToArmy(unitType1, army);
167 Assert_That__AddingNotApplicable(req, unitType3, army);
168 }
170 [Test]
171 public void TestRequirementNotApplicableBecomesPassAfterAddingLimitedUnit()
172 {
173 Army army = new Army(mockRace, "Test", 1000);
174 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
175 req.AddUnitTypeRequirement(unitType2);
176 Assert_That__AddingNotApplicable(req, unitType2, army);
177 AddUnitOfTypeToArmy(unitType1, army);
178 Assert_That__PassesAdding(req, unitType2, army);
179 }
181 [Test]
182 public void TestRequirementNotApplicableBecomesFailAfterAddingLimitedUnit()
183 {
184 Army army = new Army(mockRace, "Test", 1000);
185 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
186 req.AddUnitTypeRequirement(unitType2, 2, 1);
187 Assert_That__AddingNotApplicable(req, unitType2, army);
188 AddUnitOfTypeToArmy(unitType1, army);
189 Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 2 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 1 for 1).");
190 }
192 [Test]
193 public void TestRequirementFailsWhenLimitedUnitsChange()
194 {
195 Army army = new Army(mockRace, "Test", 1000);
196 AddUnitOfTypeToArmy(unitType1, army);
197 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
198 req.AddUnitTypeRequirement(unitType2);
199 Assert_That__PassesAdding(req, unitType2, army);
200 AddUnitOfTypeToArmy(unitType1, army);
201 Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 1 for 2).");
202 }
204 [Test]
205 public void TestValidationNotApplicableWithNoLimitedUnit()
206 {
207 Army army = new Army(mockRace, "Test", 1000);
208 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
209 req.AddUnitTypeRequirement(unitType2);
210 Assert_That__NotApplicable(req, army);
211 AddUnitOfTypeToArmy(unitType3, army);
212 Assert_That__NotApplicable(req, army);
213 AddUnitOfTypeToArmy(unitType2, army);
214 Assert_That__NotApplicable(req, army);
215 }
217 [Test()]
218 public void TestValidatingWithNoUnitsAndOneUnitTypeRequiredFails()
219 {
220 Army army = new Army(mockRace, "Test", 1000);
221 AddUnitOfTypeToArmy(unitType1, army);
222 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
223 req.AddUnitTypeRequirement(unitType2);
224 Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
225 }
227 [Test()]
228 public void TestValidatingUnitWithOneUnitAndOneUnitTypeRequiredPasses()
229 {
230 Army army = new Army(mockRace, "Test", 1000);
231 AddUnitOfTypeToArmy(unitType2, army);
232 AddUnitOfTypeToArmy(unitType1, army);
233 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
234 req.AddUnitTypeRequirement(unitType2);
235 Assert_That__ValidationPasses(req, army);
236 }
238 [Test()]
239 public void TestValidatingUnitTypeSetsLimit()
240 {
241 Army army = new Army(mockRace, "Test", 1000);
242 AddUnitOfTypeToArmy(unitType2, army);
243 AddUnitOfTypeToArmy(unitType1, army);
244 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
245 req.AddUnitTypeRequirement(unitType2, 1);
246 Assert_That__ValidationPasses(req, army);
247 }
249 [Test()]
250 public void TestValidatingUnitTypeSetsLimitForFailure()
251 {
252 Army army = new Army(mockRace, "Test", 1000);
253 AddUnitOfTypeToArmy(unitType1, army);
254 AddUnitOfTypeToArmy(unitType1, army);
255 AddUnitOfTypeToArmy(unitType2, army);
256 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
257 req.AddUnitTypeRequirement(unitType2, 1);
258 Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
259 }
261 [Test()]
262 public void TestAddingUnitTriggersValidationPass()
263 {
264 Army army = new Army(mockRace, "Test", 1000);
265 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
266 req.AddUnitTypeRequirement(unitType2, 1);
267 AddUnitOfTypeToArmy(unitType1, army);
268 Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
269 AddUnitOfTypeToArmy(unitType2, army);
270 Assert_That__ValidationPasses(req, army);
271 }
273 [Test()]
274 public void TestAddingLimitedUnitTriggersValidationFailure()
275 {
276 Army army = new Army(mockRace, "Test", 1000);
277 AddUnitOfTypeToArmy(unitType1, army);
278 AddUnitOfTypeToArmy(unitType2, army);
279 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
280 req.AddUnitTypeRequirement(unitType2);
281 Assert_That__ValidationPasses(req, army);
282 AddUnitOfTypeToArmy(unitType1, army);
283 Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
284 }
286 [Test()]
287 public void TestValidatingUnitTypeDefaultsToOne()
288 {
289 Army army = new Army(mockRace, "Test", 1000);
290 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
291 req.AddUnitTypeRequirement(unitType2);
292 AddUnitOfTypeToArmy(unitType1, army);
293 Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
294 AddUnitOfTypeToArmy(unitType2, army);
295 Assert_That__ValidationPasses(req, army);
296 }
298 [Test]
299 public void TestValidationRequirementNotApplicableWhenCheckingForNonLimitedUnits()
300 {
301 Army army = new Army(mockRace, "Test", 1000);
302 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
303 req.AddUnitTypeRequirement(unitType2);
304 AddUnitOfTypeToArmy(unitType3, army);
305 Assert_That__NotApplicable(req, army);
306 AddUnitOfTypeToArmy(unitType2, army);
307 Assert_That__NotApplicable(req, army);
308 }
310 [Test]
311 public void TestValidationNotApplicableBecomesPassAfterAddingLimitedUnit()
312 {
313 Army army = new Army(mockRace, "Test", 1000);
314 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
315 req.AddUnitTypeRequirement(unitType2);
316 AddUnitOfTypeToArmy(unitType2, army);
317 Assert_That__NotApplicable(req, army);
318 AddUnitOfTypeToArmy(unitType1, army);
319 Assert_That__ValidationPasses(req, army);
320 }
322 [Test]
323 public void TestValidationRequirementNotApplicableBecomesFailAfterAddingLimitedUnit()
324 {
325 Army army = new Army(mockRace, "Test", 1000);
326 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
327 req.AddUnitTypeRequirement(unitType2, 2, 1);
328 AddUnitOfTypeToArmy(unitType2, army);
329 Assert_That__NotApplicable(req, army);
330 AddUnitOfTypeToArmy(unitType1, army);
331 Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 1).");
332 }
334 [Test]
335 public void TestValidationRequirementFailsWhenLimitedUnitsChange()
336 {
337 Army army = new Army(mockRace, "Test", 1000);
338 AddUnitOfTypeToArmy(unitType1, army);
339 AddUnitOfTypeToArmy(unitType2, army);
340 UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
341 req.AddUnitTypeRequirement(unitType2);
342 Assert_That__ValidationPasses(req, army);
343 AddUnitOfTypeToArmy(unitType1, army);
344 Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
345 }
347 private static void Assert_That__PassesAdding(UnitRequiresNUnitsForMUnitsRequirement req, UnitType unitType, Army army)
348 {
349 Assert.That(req.AllowsAdding(unitType, army), Is.EqualTo(Validation.Passed));
350 Assert.That(req.GetAllowsAddingMessage(unitType, army), Is.Empty);
351 }
353 private static void Assert_That__PassesAdding(UnitRequiresNUnitsForMUnitsRequirement req, Unit unit, Army army)
354 {
355 Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
356 Assert.That(req.GetAllowsAddingMessage(unit, army), Is.Empty);
357 }
359 private static void Assert_That__AddingNotApplicable(UnitRequiresNUnitsForMUnitsRequirement req, UnitType unitType, Army army)
360 {
361 Assert.That(req.AllowsAdding(unitType, army), Is.EqualTo(Validation.NotApplicable));
362 Assert.That(req.GetAllowsAddingMessage(unitType, army), Is.Empty);
363 }
365 private static void Assert_That__AddingNotApplicable(UnitRequiresNUnitsForMUnitsRequirement req, Unit unit, Army army)
366 {
367 Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.NotApplicable));
368 Assert.That(req.GetAllowsAddingMessage(unit, army), Is.Empty);
369 }
371 private static void Assert_That__FailsAdding(UnitRequiresNUnitsForMUnitsRequirement req, UnitType unitType, Army army, string message)
372 {
373 Assert.That(req.AllowsAdding(unitType, army), Is.EqualTo(Validation.Failed));
374 Assert.That(req.GetAllowsAddingMessage(unitType, army), Is.EqualTo(message));
375 }
377 private static void Assert_That__FailsAdding(UnitRequiresNUnitsForMUnitsRequirement req, Unit unit, Army army, string message)
378 {
379 Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
380 Assert.That(req.GetAllowsAddingMessage(unit, army), Is.EqualTo(message));
381 }
383 private static void Assert_That__ValidationPasses(UnitRequiresNUnitsForMUnitsRequirement req, Army army)
384 {
385 Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
386 Assert.That(req.GetValidationMessage(army), Is.Empty);
387 }
389 private static void Assert_That__NotApplicable(UnitRequiresNUnitsForMUnitsRequirement req, Army army)
390 {
391 Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
392 Assert.That(req.GetValidationMessage(army), Is.Empty);
393 }
395 private static void Assert_That__ValidationFails(UnitRequiresNUnitsForMUnitsRequirement req, Army army, string message)
396 {
397 Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
398 Assert.That(req.GetValidationMessage(army), Is.EqualTo(message));
399 }
401 private static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
402 {
403 army.AddUnit(CreateUnitOfType(unitType, army));
404 }
406 private static Unit CreateUnitOfType(UnitType unitType, Army army)
407 {
408 return new Unit(unitType, army.GetCategory(unitType.MainCategory));
409 }
410 }
411 }