view API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactoryTest.cs @ 193:cf1c0a91eaa9

Re #350: Add requirement to allow N of unit for specific other units * Add first unit tests for factory, based on existing factory unit tests
author IBBoard <dev@ibboard.co.uk>
date Sat, 03 Dec 2011 16:53:36 +0000
parents
children f1f4df654a91
line source
1 // This file (UnitUnitRequiresNUnitsForMUnitsRequirementFactoryTest.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 IBBoard.WarFoundry.API.Objects;
9 using IBBoard.WarFoundry.API.Objects.Requirement;
10 using IBBoard.WarFoundry.API.Factories.Mock;
12 namespace IBBoard.WarFoundry.API.Factories.Requirement
13 {
14 [TestFixture()]
15 public class UnitUnitRequiresNUnitsForMUnitsRequirementFactoryTest
16 {
17 private static Race race;
18 private static string typeID1 = "unit1";
19 private static string typeID2 = "unit2";
20 private static UnitType unitType1;
21 private static UnitType unitType2;
22 private static MockRaceFactory raceFactory = new MockRaceFactory();
24 [TestFixtureSetUp()]
25 public void Setup()
26 {
27 race = new MockRace();
28 unitType1 = new UnitType(typeID1, "Unit 1", race);
29 race.AddUnitType(unitType1);
30 unitType2 = new UnitType(typeID2, "Unit 2", race);
31 race.AddUnitType(unitType2);
32 }
34 [Test()]
35 public void TestCreatesRequirement()
36 {
37 UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
38 Assert.That(factory.CreateRequirement(unitType2, typeID1, raceFactory), Is.Not.Null);
39 }
41 [Test()]
42 public void TestCreatesCorrectRequirement()
43 {
44 UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
45 UnitType unitType = new UnitType("testUnit", "Test Unit", race);
46 RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
47 expectedReq.AddUnitTypeRequirement(unitType1);
48 IRequirement req = factory.CreateRequirement(unitType, typeID1, raceFactory);
49 Assert.That(req, Is.EqualTo(expectedReq));
50 }
52 [Test()]
53 public void TestCreatesCorrectRequirementForTwoIDs()
54 {
55 UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
56 UnitType unitType = new UnitType("testUnit", "Test Unit", race);
57 RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
58 expectedReq.AddUnitTypeRequirement(unitType1);
59 expectedReq.AddUnitTypeRequirement(unitType2);
60 IRequirement req = factory.CreateRequirement(unitType, typeID1 + "|" + typeID2, raceFactory);
61 Assert.That(req, Is.EqualTo(expectedReq));
62 }
64 [Test()]
65 public void TestCreatesCorrectRequirementWithNumber()
66 {
67 UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
68 UnitType unitType = new UnitType("testUnit", "Test Unit", race);
69 RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
70 expectedReq.AddUnitTypeRequirement(unitType1, 2);
71 IRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2", raceFactory);
72 Assert.That(req, Is.EqualTo(expectedReq));
73 }
75 [Test()]
76 public void TestCreatesCorrectRequirementForTwoIDsWithTwoNumbers()
77 {
78 UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
79 UnitType unitType = new UnitType("testUnit", "Test Unit", race);
80 RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
81 expectedReq.AddUnitTypeRequirement(unitType1, 2);
82 expectedReq.AddUnitTypeRequirement(unitType2, 3);
83 IRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2|" + typeID2 + ":3", raceFactory);
84 Assert.That(req, Is.EqualTo(expectedReq));
85 }
87 [Test()]
88 public void TestCreatesCorrectRequirementForTwoIDsWithOneNumber()
89 {
90 UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
91 UnitType unitType = new UnitType("testUnit", "Test Unit", race);
92 RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
93 expectedReq.AddUnitTypeRequirement(unitType1, 2);
94 expectedReq.AddUnitTypeRequirement(unitType2);
95 IRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2|" + typeID2, raceFactory);
96 Assert.That(req, Is.EqualTo(expectedReq));
97 }
99 [Test()]
100 public void TestCreatesCorrectRequirementForTwoIDsWithOtherNumber()
101 {
102 UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
103 UnitType unitType = new UnitType("testUnit", "Test Unit", race);
104 RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
105 expectedReq.AddUnitTypeRequirement(unitType1);
106 expectedReq.AddUnitTypeRequirement(unitType2, 3);
107 IRequirement req = factory.CreateRequirement(unitType, typeID1 + "|" + typeID2 + ":3", raceFactory);
108 Assert.That(req, Is.EqualTo(expectedReq));
109 }
111 [Test()]
112 [ExpectedException(typeof(InvalidRequirementException), ExpectedMessage="Invalid unit type 'fibble' for 'Requires N units for M units' requirement")]
113 public void TestExceptionsForInvalidID()
114 {
115 UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
116 UnitType unitType = new UnitType("testUnit", "Test Unit", race);
117 factory.CreateRequirement(unitType, "fibble", raceFactory);
118 }
120 [Test()]
121 [ExpectedException(typeof(InvalidRequirementException), ExpectedMessage="Invalid amount 'a' for unit type 'unit1' for 'Requires N units for M units' requirement")]
122 public void TestExceptionsForInvalidNumber()
123 {
124 UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
125 UnitType unitType = new UnitType("testUnit", "Test Unit", race);
126 factory.CreateRequirement(unitType, typeID1+":a", raceFactory);
127 }
128 }
129 }