view API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirementTest.cs @ 123:dd0ef8526a4e

Re #27: Unit Requirements * Test base "no more than" requirement validates armies correctly
author IBBoard <dev@ibboard.co.uk>
date Sat, 09 Apr 2011 10:50:29 +0000
parents aa37b31976e6
children 3e4864e206ea
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 RequiresNoMoreThanNOfUnitTypeRequirementTest
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 RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
37 Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
38 }
40 [Test()]
41 public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredIsNotAllowed()
42 {
43 Army army = new Army(mockRace, "Test", 1000);
44 AddUnitOfTypeToArmy(unitType2, army);
45 Unit unit = CreateUnitOfType(unitType1, army);
46 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
47 Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
48 }
50 [Test()]
51 public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequiredIsAllowed()
52 {
53 Army army = new Army(mockRace, "Test", 1000);
54 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
55 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
56 }
58 [Test()]
59 public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredIsNotAllowed()
60 {
61 Army army = new Army(mockRace, "Test", 1000);
62 AddUnitOfTypeToArmy(unitType2, army);
63 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType2);
64 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
65 }
67 [Test()]
68 public void TestAddingUnitTypeSetsLimit()
69 {
70 Army army = new Army(mockRace, "Test", 1000);
71 AddUnitOfTypeToArmy(unitType2, army);
72 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
73 req.AddUnitTypeRequirement(unitType2, 1);
74 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
75 }
77 [Test()]
78 public void TestAddingUnitTypeSetsLimitForFailure()
79 {
80 Army army = new Army(mockRace, "Test", 1000);
81 AddUnitOfTypeToArmy(unitType2, army);
82 AddUnitOfTypeToArmy(unitType2, army);
83 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
84 req.AddUnitTypeRequirement(unitType2, 1);
85 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
86 }
88 [Test()]
89 public void TestAddingUnitTypeDefaultsToNoMoreThanZero()
90 {
91 Army army = new Army(mockRace, "Test", 1000);
92 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
93 req.AddUnitTypeRequirement(unitType2);
94 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
95 AddUnitOfTypeToArmy(unitType2, army);
96 Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
97 }
99 [Test]
100 public void TestRequirementBecomesInvalidWhenProhibitedUnitsChange()
101 {
102 Army army = new Army(mockRace, "Test", 1000);
103 RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement();
104 req.AddUnitTypeRequirement(unitType2);
105 Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
106 AddUnitOfTypeToArmy(unitType2, army);
107 Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
108 }
110 private static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
111 {
112 army.AddUnit(CreateUnitOfType(unitType, army));
113 }
115 private static Unit CreateUnitOfType(UnitType unitType, Army army)
116 {
117 return new Unit(unitType, army.GetCategory(unitType.MainCategory));
118 }
119 }
120 }