comparison API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs @ 231:5e03b68dd214

Re #410: Create "N units per M models in parent unit" requirement * Add required usage of context objects * Add tests for new requirement * Add some extras to test unit type setup that the new requirement needs
author IBBoard <dev@ibboard.co.uk>
date Fri, 27 Jul 2012 20:33:40 +0100
parents
children fdebdeb52a40
comparison
equal deleted inserted replaced
230:d6883a522c70 231:5e03b68dd214
1 // This file (UnitRequiresNoMoreThanNUnitsPerMModelsInParentUnitRequirementTests.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2012 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 NUnit.Framework.SyntaxHelpers;
7 using IBBoard.WarFoundry.API.Objects.Requirement.Context;
8
9 namespace IBBoard.WarFoundry.API.Objects.Requirement
10 {
11 [TestFixture()]
12 public class UnitRequiresNParentModelsForMUnitsRequirementTests: AbstractUnitTypeUnitRequirementTest<UnitType, UnitRequiresNParentModelsForMUnitsRequirement>
13 {
14 [Test()]
15 public void TestAddingUnitTypeWithNoParentAndOneUnitTypeRequiredIsAllowed()
16 {
17 Army army = new Army(mockRace, "Test", 1000);
18 UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
19 req.AddUnitTypeRequirement(unitType2);
20 Assert_That__AddingNotApplicable(req, unitType1, army);
21 }
22
23 [Test()]
24 public void TestAddingUnitTypeWithParentAndOneUnitTypeRequiredIsAllowed()
25 {
26 Army army = new Army(mockRace, "Test", 1000);
27 Unit parent = AddUnitOfTypeToArmy(unitType2, army);
28 UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
29 req.AddUnitTypeRequirement(unitType2);
30 Assert_That__PassesAdding(req, unitType1, parent, army);
31 }
32
33 [Test()]
34 public void TestAddingUnitTypeWithWrongParentAndOneUnitTypeRequiredIsNotApplicable()
35 {
36 Army army = new Army(mockRace, "Test", 1000);
37 Unit parent = AddUnitOfTypeToArmy(unitType2, army);
38 UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
39 req.AddUnitTypeRequirement(unitType3);
40 Assert_That__AddingNotApplicable(req, unitType1, parent, army);
41 }
42
43 [Test()]
44 public void TestAddingTooManyUnitTypesWithParentAndOneUnitTypeRequiredFails()
45 {
46 Army army = new Army(mockRace, "Test", 1000);
47 Unit parent = AddUnitOfTypeToArmy(unitType3, army);
48 Unit firstChild = AddUnitOfTypeToArmy(unitType1, army);
49 parent.AddContainedUnit(firstChild);
50 UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
51 req.AddUnitTypeRequirement(unitType3);
52 Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 1 × " + unitType1.Name + " as a sub-unit of each " + unitType3.Name + ", would have 2");
53 }
54
55 protected override UnitRequiresNParentModelsForMUnitsRequirement CreateRequirement(UnitType requirementOn)
56 {
57 return new UnitRequiresNParentModelsForMUnitsRequirement(requirementOn);
58 }
59
60 protected static void Assert_That__PassesAdding(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army)
61 {
62 AddingToParentContext ctx = new AddingToParentContext(parent);
63 Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.Passed));
64 Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.Empty);
65 Unit unit = CreateUnitOfType(unitType, army);
66 Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Passed));
67 Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.Empty);
68 }
69
70 protected static void Assert_That__AddingNotApplicable(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army)
71 {
72 AddingToParentContext ctx = new AddingToParentContext(parent);
73 Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.NotApplicable));
74 Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.Empty);
75 Unit unit = CreateUnitOfType(unitType, army);
76 Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.NotApplicable));
77 Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.Empty);
78 }
79
80 protected static void Assert_That__FailsAdding(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army, string message)
81 {
82 AddingToParentContext ctx = new AddingToParentContext(parent);
83 Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.Failed));
84 Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.EqualTo(message));
85 Unit unit = CreateUnitOfType(unitType, army);
86 Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Failed));
87 Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.EqualTo(message));
88 }
89 }
90 }
91