view API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirementTest.cs @ 144:eced294e82f2

Re #345: Add failure message to requirements * Add tests for "no more than" requirements
author IBBoard <dev@ibboard.co.uk>
date Sun, 01 May 2011 15:38:01 +0000
parents aa37b31976e6
children 8c519a44e699
line source
1 // This file (RequiresNoMoreThanNOfUnitTypeRequirementTest.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 UnitRequiresNoMoreThanNOfUnitTypeRequirementTest
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 TestAddingUnitWithNoUnitsAndOneUnitTypeRequiredIsAllowed()
33 {
34 Army army = new Army(mockRace, "Test", 1000);
35 Unit unit = CreateUnitOfType(unitType1, army);
36 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
37 req.AddUnitTypeRequirement(unitType2);
38 Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
39 }
41 [Test()]
42 public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredIsNotAllowed()
43 {
44 Army army = new Army(mockRace, "Test", 1000);
45 AddUnitOfTypeToArmy(unitType2, army);
46 Unit unit = CreateUnitOfType(unitType1, army);
47 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
48 req.AddUnitTypeRequirement(unitType2);
49 Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
50 }
52 [Test()]
53 public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequiredIsAllowed()
54 {
55 Army army = new Army(mockRace, "Test", 1000);
56 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
57 req.AddUnitTypeRequirement(unitType2);
58 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
59 }
61 [Test()]
62 public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredIsNotAllowed()
63 {
64 Army army = new Army(mockRace, "Test", 1000);
65 AddUnitOfTypeToArmy(unitType2, army);
66 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
67 req.AddUnitTypeRequirement(unitType2);
68 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
69 }
71 [Test()]
72 public void TestAddingUnitTypeSetsLimit()
73 {
74 Army army = new Army(mockRace, "Test", 1000);
75 AddUnitOfTypeToArmy(unitType2, army);
76 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
77 req.AddUnitTypeRequirement(unitType2, 1);
78 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
79 }
81 [Test()]
82 public void TestAddingUnitTypeSetsLimitForFailure()
83 {
84 Army army = new Army(mockRace, "Test", 1000);
85 AddUnitOfTypeToArmy(unitType2, army);
86 AddUnitOfTypeToArmy(unitType2, army);
87 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
88 req.AddUnitTypeRequirement(unitType2, 1);
89 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
90 }
92 [Test()]
93 public void TestAddingUnitTriggersFailure()
94 {
95 Army army = new Army(mockRace, "Test", 1000);
96 AddUnitOfTypeToArmy(unitType2, army);
97 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
98 req.AddUnitTypeRequirement(unitType2, 1);
99 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
100 AddUnitOfTypeToArmy(unitType2, army);
101 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
102 }
104 [Test()]
105 public void TestAddingUnitTypeDefaultsToNoMoreThanZero()
106 {
107 Army army = new Army(mockRace, "Test", 1000);
108 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
109 req.AddUnitTypeRequirement(unitType2);
110 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
111 AddUnitOfTypeToArmy(unitType2, army);
112 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
113 }
115 [Test]
116 public void TestRequirementNotApplicableWhenLimitedUnitNotIncluded()
117 {
118 Army army = new Army(mockRace, "Test", 1000);
119 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
120 req.AddUnitTypeRequirement(unitType2);
121 Assert.That(req.AllowsAdding(unitType3, army), Is.EqualTo(Validation.NotApplicable));
122 Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.NotApplicable));
123 }
125 [Test]
126 public void TestRequirementBecomesInvalidWhenProhibitedUnitsChange()
127 {
128 Army army = new Army(mockRace, "Test", 1000);
129 AddUnitOfTypeToArmy(unitType1, army);
130 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
131 req.AddUnitTypeRequirement(unitType2);
132 Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
133 AddUnitOfTypeToArmy(unitType2, army);
134 Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
135 }
137 [Test()]
138 public void TestAddingLimitedUnitTriggersFailure()
139 {
140 Army army = new Army(mockRace, "Test", 1000);
141 AddUnitOfTypeToArmy(unitType1, army);
142 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
143 req.AddUnitTypeRequirement(unitType2);
144 Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Failed));
145 }
147 [Test]
148 public void TestValidationMessageIsBlankForPass()
149 {
150 Army army = new Army(mockRace, "Test", 1000);
151 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
152 req.AddUnitTypeRequirement(unitType2);
153 Assert.That(req.GetValidationMessage(army), Is.Empty);
154 }
156 [Test]
157 public void TestValidationMessageIsCorrectForFailWithOneUnitProhibited()
158 {
159 Army army = new Army(mockRace, "Test", 1000);
160 AddUnitOfTypeToArmy(unitType1, army);
161 AddUnitOfTypeToArmy(unitType2, army);
162 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
163 req.AddUnitTypeRequirement(unitType2);
164 Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army contains " + unitType1.Name + " and so cannot contain more than: 0 × " + unitType2.Name + " (have 1)."));
165 }
167 [Test]
168 public void TestValidationMessageIsCorrectForFailWithTwoOfOneUnitLimit()
169 {
170 Army army = new Army(mockRace, "Test", 1000);
171 AddUnitOfTypeToArmy(unitType1, army);
172 AddUnitOfTypeToArmy(unitType2, army);
173 AddUnitOfTypeToArmy(unitType2, army);
174 AddUnitOfTypeToArmy(unitType2, army);
175 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
176 req.AddUnitTypeRequirement(unitType2, 2);
177 Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army contains " + unitType1.Name + " and so cannot contain more than: 2 × " + unitType2.Name + " (have 3)."));
178 }
180 [Test]
181 public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibited()
182 {
183 Army army = new Army(mockRace, "Test", 1000);
184 AddUnitOfTypeToArmy(unitType1, army);
185 AddUnitOfTypeToArmy(unitType2, army);
186 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
187 req.AddUnitTypeRequirement(unitType2);
188 req.AddUnitTypeRequirement(unitType3);
189 Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army contains " + unitType1.Name + " and so cannot contain more than: 0 × " + unitType2.Name + " (have 1)."));
190 }
192 [Test]
193 public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibitedAndOtherUnitInArmy()
194 {
195 Army army = new Army(mockRace, "Test", 1000);
196 AddUnitOfTypeToArmy(unitType1, army);
197 AddUnitOfTypeToArmy(unitType3, army);
198 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
199 req.AddUnitTypeRequirement(unitType2);
200 req.AddUnitTypeRequirement(unitType3);
201 Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army contains " + unitType1.Name + " and so cannot contain more than: 0 × " + unitType3.Name + " (have 1)."));
202 }
204 [Test]
205 public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibitedAndBothUnitsInArmy()
206 {
207 Army army = new Army(mockRace, "Test", 1000);
208 AddUnitOfTypeToArmy(unitType1, army);
209 AddUnitOfTypeToArmy(unitType2, army);
210 AddUnitOfTypeToArmy(unitType3, army);
211 RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1);
212 req.AddUnitTypeRequirement(unitType2);
213 req.AddUnitTypeRequirement(unitType3);
214 Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army contains " + unitType1.Name + " and so cannot contain more than: 0 × " + unitType2.Name + " (have 1); 0 × " + unitType3.Name + " (have 1)."));
215 }
217 private static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
218 {
219 army.AddUnit(CreateUnitOfType(unitType, army));
220 }
222 private static Unit CreateUnitOfType(UnitType unitType, Army army)
223 {
224 return new Unit(unitType, army.GetCategory(unitType.MainCategory));
225 }
226 }
227 }