view API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirementTest.cs @ 139:3e4864e206ea

Re #140: Equality methods * Add equality testing to RequiresNoMoreThanN requirement * Add extra different races for testing more of equality method
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Apr 2011 19:47:36 +0000
parents dd0ef8526a4e
children 2ae80631ec9c
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;
8 using System.Reflection;
9 using IBBoard.NUnit;
10 using IBBoard.WarFoundry.API.Factories;
12 namespace IBBoard.WarFoundry.API.Objects.Requirement
13 {
14 [TestFixture()]
15 public class RequiresNoMoreThanNOfUnitTypeRequirementTest : AbstractEqualityTest<RequiresNoMoreThanNOfUnitTypeRequirement>
16 {
17 //FIXME We shouldn't duplicate these tests, but it is broken at the moment
18 [Test]
19 public void TestEquality()
20 {
21 Assert.AreEqual(GetObject(), GetSameObject());
22 Assert.AreEqual(GetSameObject(), GetObject());
23 }
25 [Test]
26 public void TestInequality()
27 {
28 Assert.AreNotEqual(GetObject(), GetDifferentObject());
29 Assert.AreNotEqual(GetSameObject(), GetDifferentObject());
30 Assert.AreNotEqual(GetDifferentObject(), GetObject());
31 Assert.AreNotEqual(GetDifferentObject(), GetSameObject());
32 }
34 [Test]
35 public void TestReflexiveEquality()
36 {
37 Assert.AreEqual(GetObject(), GetObject());
38 Assert.AreEqual(GetSameObject(), GetSameObject());
39 Assert.AreEqual(GetDifferentObject(), GetDifferentObject());
40 }
42 [Test]
43 public void TestOtherInequality()
44 {
45 MethodInfo[] methodInfo = GetType().GetMethods();
46 RequiresNoMoreThanNOfUnitTypeRequirement obj = GetObject();
48 foreach (MethodInfo method in methodInfo)
49 {
50 if (method.Name.StartsWith("GetOtherDifferent"))
51 {
52 RequiresAtLeastNUnitsRequirement otherObj = (RequiresAtLeastNUnitsRequirement)method.Invoke(this, new object[0]);
53 Assert.AreNotEqual(obj, otherObj, "Objects equal for "+method.Name);
54 Assert.AreNotEqual(otherObj, obj, "Objects equal for "+method.Name);
55 }
56 }
57 }
62 private MockRace mockRace;
63 private UnitType unitType1;
64 private UnitType unitType2;
65 private UnitType unitType3;
67 [TestFixtureSetUp()]
68 public void SetupRace()
69 {
70 mockRace = new MockRace();
71 unitType1 = new MockUnitType("type1", "Unit Type 1");
72 mockRace.AddUnitType(unitType1);
73 unitType2 = new MockUnitType("type2", "Unit Type 2");
74 mockRace.AddUnitType(unitType2);
75 unitType3 = new MockUnitType("type3", "Unit Type 3");
76 mockRace.AddUnitType(unitType3);
77 }
79 [Test()]
80 public void TestAddingUnitWithNoUnitsAndOneUnitTypeRequiredIsAllowed()
81 {
82 Army army = new Army(mockRace, "Test", 1000);
83 Unit unit = CreateUnitOfType(unitType1, army);
84 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
85 Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
86 }
88 [Test()]
89 public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredIsNotAllowed()
90 {
91 Army army = new Army(mockRace, "Test", 1000);
92 AddUnitOfTypeToArmy(unitType2, army);
93 Unit unit = CreateUnitOfType(unitType1, army);
94 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
95 Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
96 }
98 [Test()]
99 public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequiredIsAllowed()
100 {
101 Army army = new Army(mockRace, "Test", 1000);
102 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
103 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
104 }
106 [Test()]
107 public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredIsNotAllowed()
108 {
109 Army army = new Army(mockRace, "Test", 1000);
110 AddUnitOfTypeToArmy(unitType2, army);
111 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
112 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
113 }
115 [Test()]
116 public void TestAddingUnitTypeSetsLimit()
117 {
118 Army army = new Army(mockRace, "Test", 1000);
119 AddUnitOfTypeToArmy(unitType2, army);
120 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
121 req.AddUnitTypeRequirement(unitType2, 1);
122 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
123 }
125 [Test()]
126 public void TestAddingUnitTypeSetsLimitForFailure()
127 {
128 Army army = new Army(mockRace, "Test", 1000);
129 AddUnitOfTypeToArmy(unitType2, army);
130 AddUnitOfTypeToArmy(unitType2, army);
131 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
132 req.AddUnitTypeRequirement(unitType2, 1);
133 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
134 }
136 [Test()]
137 public void TestAddingUnitTypeDefaultsToNoMoreThanZero()
138 {
139 Army army = new Army(mockRace, "Test", 1000);
140 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
141 req.AddUnitTypeRequirement(unitType2);
142 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
143 AddUnitOfTypeToArmy(unitType2, army);
144 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
145 }
147 [Test]
148 public void TestRequirementBecomesInvalidWhenProhibitedUnitsChange()
149 {
150 Army army = new Army(mockRace, "Test", 1000);
151 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
152 req.AddUnitTypeRequirement(unitType2);
153 Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
154 AddUnitOfTypeToArmy(unitType2, army);
155 Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
156 }
158 private static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
159 {
160 army.AddUnit(CreateUnitOfType(unitType, army));
161 }
163 private static Unit CreateUnitOfType(UnitType unitType, Army army)
164 {
165 return new Unit(unitType, army.GetCategory(unitType.MainCategory));
166 }
168 public override RequiresNoMoreThanNOfUnitTypeRequirement GetObject ()
169 {
170 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
171 req.AddUnitTypeRequirement(unitType1, 2);
172 return req;
173 }
175 public override RequiresNoMoreThanNOfUnitTypeRequirement GetSameObject ()
176 {
177 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
178 req.AddUnitTypeRequirement(unitType1, 2);
179 return req;
180 }
182 public override RequiresNoMoreThanNOfUnitTypeRequirement GetDifferentObject ()
183 {
184 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
185 DummyWarFoundryFactory factory = new DummyWarFoundryFactory();
186 GameSystem gameSystem = new GameSystem("system", "system", factory);
187 Race race = new Race("race", "race", gameSystem, factory);
188 req.AddUnitTypeRequirement(new UnitType("id2", "Type 2", race), 2);
189 return req;
190 }
191 }
192 }