Mercurial > repos > IBBoard.WarFoundry.API.Tests
changeset 221:c40cb2048fc3
Re #394:
* Fix UnitRequiresNUnitsForMUnitsRequirementTests unit test so it actually does test the other value
* Add tests to "requires at least N" requirement and factory to test alternatives (Rollcall's "-1,X|Y" notation)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 24 Mar 2012 20:34:42 +0000 |
parents | 0fa634c6391b |
children | 12ac487af0ba |
files | API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactoryTest.cs API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirementTest.cs API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs |
diffstat | 3 files changed, 285 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- a/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactoryTest.cs Sat Mar 24 19:32:47 2012 +0000 +++ b/API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactoryTest.cs Sat Mar 24 20:34:42 2012 +0000 @@ -17,8 +17,10 @@ private static Race race; private static string typeID1 = "unit1"; private static string typeID2 = "unit2"; + private static string typeID3 = "unit3"; private static UnitType unitType1; private static UnitType unitType2; + private static UnitType unitType3; private static MockRaceFactory raceFactory = new MockRaceFactory(); [TestFixtureSetUp()] @@ -29,6 +31,8 @@ race.AddUnitType(unitType1); unitType2 = new UnitType(typeID2, "Unit 2", race); race.AddUnitType(unitType2); + unitType3 = new UnitType(typeID3, "Unit 3", race); + race.AddUnitType(unitType3); } [Test()] @@ -118,12 +122,181 @@ } [Test()] - [ExpectedException(typeof(InvalidRequirementException), ExpectedMessage="Invalid amount 'a' for unit type 'unit1' for 'Requires at least N units' requirement")] + [ExpectedException(typeof(InvalidRequirementException), ExpectedMessage="Invalid amount 'a' for unit types 'unit1' for 'Requires at least N units' requirement")] public void TestExceptionsForInvalidNumber() { UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); UnitType unitType = new UnitType("testUnit", "Test Unit", race); - factory.CreateRequirement(unitType, typeID1+":a", raceFactory); + factory.CreateRequirement(unitType, typeID1 + ":a", raceFactory); + } + + [Test()] + public void TestCreatesCorrectTripleAdditiveRequirement() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(unitType1, unitType2, unitType3); + IRequirement req = factory.CreateRequirement(unitType, typeID1 + ";" + typeID2 + ";" + typeID3, raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + public void TestCreatesCorrectAdditiveRequirementForTwoIDs() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(unitType1, unitType2); + expectedReq.AddUnitTypeRequirement(unitType2); + IRequirement req = factory.CreateRequirement(unitType, typeID1 + ";" + typeID2 + "|" + typeID2, raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + public void TestCreatesCorrectAdditiveRequirementWithNumber() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(2, unitType1, unitType2); + IRequirement req = factory.CreateRequirement(unitType, typeID1 + ";" + typeID2 + ":2", raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + public void TestCreatesCorrectAdditiveRequirementForTwoIDsWithTwoNumbers() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(2, unitType1, unitType2); + expectedReq.AddUnitTypeRequirement(3, unitType2, unitType1); + IRequirement req = factory.CreateRequirement(unitType, typeID1 + ";" + typeID2 + ":2|" + typeID2 + ";" + typeID1 + ":3", raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + public void TestCreatesCorrectAdditiveRequirementForTwoIDsWithOneNumber() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(2, unitType1, unitType2); + expectedReq.AddUnitTypeRequirement(unitType2, unitType1); + IRequirement req = factory.CreateRequirement(unitType, typeID1 + ";" + typeID2 + ":2|" + typeID2 + ";" + typeID1, raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + public void TestCreatesCorrectAdditiveRequirementForTwoIDsWithOtherNumber() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(unitType1, unitType2); + expectedReq.AddUnitTypeRequirement(3, unitType2, unitType1); + IRequirement req = factory.CreateRequirement(unitType, typeID1 + ";" + typeID2 + "|" + typeID2 + ";" + typeID1 + ":3", raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + public void TestCreatesCorrectRequirementForOneIDAndOneAdditive() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(unitType1, unitType2); + expectedReq.AddUnitTypeRequirement(unitType2); + IRequirement req = factory.CreateRequirement(unitType, typeID1 + ";" + typeID2 + "|" + typeID2, raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + public void TestCreatesCorrectRequirementForOneIDAndOneAdditiveInOtherOrder() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(unitType2); + expectedReq.AddUnitTypeRequirement(unitType1, unitType2); + IRequirement req = factory.CreateRequirement(unitType, typeID2 + "|" + typeID1 + ";" + typeID2, raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + public void TestCreatesCorrectRequirementForOneIDWithNumberAndOneAdditive() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(unitType1, unitType2); + expectedReq.AddUnitTypeRequirement(unitType2, 3); + IRequirement req = factory.CreateRequirement(unitType, typeID1 + ";" + typeID2 + "|" + typeID2 + ":3", raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + public void TestCreatesCorrectRequirementForOneIDWithNumberAndOneAdditiveInOtherOrder() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(unitType2, 3); + expectedReq.AddUnitTypeRequirement(unitType1, unitType2); + IRequirement req = factory.CreateRequirement(unitType, typeID2 + ":3|" + typeID1 + ";" + typeID2, raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + public void TestCreatesCorrectRequirementForOneIDAndOneAdditiveWithNumber() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(3, unitType1, unitType2); + expectedReq.AddUnitTypeRequirement(unitType2); + IRequirement req = factory.CreateRequirement(unitType, typeID1 + ";" + typeID2 + ":3|" + typeID2, raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + public void TestCreatesCorrectRequirementForOneIDAndOneAdditiveWithNumberInOtherOrder() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType); + expectedReq.AddUnitTypeRequirement(unitType2); + expectedReq.AddUnitTypeRequirement(3, unitType1, unitType2); + IRequirement req = factory.CreateRequirement(unitType, typeID2 + "|" + typeID1 + ";" + typeID2 + ":3", raceFactory); + Assert.That(req, Is.EqualTo(expectedReq)); + } + + [Test()] + [ExpectedException(typeof(InvalidRequirementException), ExpectedMessage="Invalid unit type 'fibble' for 'Requires at least N units' requirement")] + public void TestExceptionsForAdditiveRequirementWithInvalidID() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + factory.CreateRequirement(unitType, "fibble;" + typeID1, raceFactory); + } + + [Test()] + [ExpectedException(typeof(InvalidRequirementException), ExpectedMessage="Invalid unit type 'fibble' for 'Requires at least N units' requirement")] + public void TestExceptionsForAdditiveRequirementWithOtherInvalidID() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + factory.CreateRequirement(unitType, typeID1 + ";fibble", raceFactory); + } + + [Test()] + [ExpectedException(typeof(InvalidRequirementException), ExpectedMessage="Invalid amount 'a' for unit types 'unit1;unit2' for 'Requires at least N units' requirement")] + public void TestExceptionsForAdditiveRequirementWithInvalidNumber() + { + UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory(); + UnitType unitType = new UnitType("testUnit", "Test Unit", race); + factory.CreateRequirement(unitType, typeID1 + ";" + typeID2 + ":a", raceFactory); } } }
--- a/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirementTest.cs Sat Mar 24 19:32:47 2012 +0000 +++ b/API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirementTest.cs Sat Mar 24 20:34:42 2012 +0000 @@ -33,6 +33,7 @@ [Test()] public void TestAddingUnitTypeWithNoUnitsAndTwoUnitTypesRequired() { + //Rollcall's "-1,X,-1,Y" requirement - unlimited once you have one of both Army army = new Army(mockRace, "Test", 1000); UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); @@ -43,6 +44,7 @@ [Test()] public void TestAddingUnitTypeWithOneUnitAndTwoUnitTypesRequired() { + //Rollcall's "-1,X,-1,Y" requirement - unlimited once you have one of both Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType2, army); UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1); @@ -54,6 +56,7 @@ [Test()] public void TestAddingUnitTypeWithOtherOneUnitAndTwoUnitTypesRequired() { + //Rollcall's "-1,X,-1,Y" requirement - unlimited once you have one of both Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType3, army); UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1); @@ -65,6 +68,7 @@ [Test()] public void TestAddingUnitTypeWithBothUnitsAndTwoUnitTypesRequired() { + //Rollcall's "-1,X,-1,Y" requirement - unlimited once you have one of both Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType2, army); AddUnitOfTypeToArmy(unitType3, army); @@ -136,6 +140,56 @@ Assert_That__PassesAdding(req, unitType1, army); } + [Test] + public void TestAddingRequirementWithOR() + { + //Rollcall's "-1,X|Y" requirement - unlimited for one of either + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2, unitType3); + Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " or " + unitType3.Name + " (would have 0)."); + AddUnitOfTypeToArmy(unitType2, army); + Assert_That__PassesAdding(req, unitType1, army); + } + + [Test] + public void TestAddingRequirementWithORWithOtherUnit() + { + //Rollcall's "-1,X|Y" requirement - unlimited for one of either + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2, unitType3); + Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " or " + unitType3.Name + " (would have 0)."); + AddUnitOfTypeToArmy(unitType3, army); + Assert_That__PassesAdding(req, unitType1, army); + } + + [Test] + public void TestAddingRequirementWithORWithLimit() + { + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(2, unitType2, unitType3); + Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 2 × " + unitType2.Name + " or " + unitType3.Name + " (would have 0)."); + AddUnitOfTypeToArmy(unitType2, army); + Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 2 × " + unitType2.Name + " or " + unitType3.Name + " (would have 1)."); + AddUnitOfTypeToArmy(unitType2, army); + Assert_That__PassesAdding(req, unitType1, army); + } + + [Test] + public void TestAddingRequirementWithORWithLimitWithOtherUnit() + { + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(2, unitType2, unitType3); + Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 2 × " + unitType2.Name + " or " + unitType3.Name + " (would have 0)."); + AddUnitOfTypeToArmy(unitType3, army); + Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 2 × " + unitType2.Name + " or " + unitType3.Name + " (would have 1)."); + AddUnitOfTypeToArmy(unitType3, army); + Assert_That__PassesAdding(req, unitType1, army); + } + [Test()] public void TestValidatingWithNoUnitsAndOneUnitTypeRequired() { @@ -264,6 +318,60 @@ Assert_That__ValidationPasses(req, army); } + [Test] + public void TestRequirementWithOR() + { + //Rollcall's "-1,X|Y" requirement - unlimited for one of either + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2, unitType3); + AddUnitOfTypeToArmy(unitType1, army); + Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " or " + unitType3.Name + " (have 0)."); + AddUnitOfTypeToArmy(unitType2, army); + Assert_That__ValidationPasses(req, army); + } + + [Test] + public void TestRequirementWithORWithOtherUnit() + { + //Rollcall's "-1,X|Y" requirement - unlimited for one of either + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2, unitType3); + AddUnitOfTypeToArmy(unitType1, army); + Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " or " + unitType3.Name + " (have 0)."); + AddUnitOfTypeToArmy(unitType3, army); + Assert_That__ValidationPasses(req, army); + } + + [Test] + public void TestRequirementWithORWithLimit() + { + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(2, unitType2, unitType3); + AddUnitOfTypeToArmy(unitType1, army); + Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " or " + unitType3.Name + " (have 0)."); + AddUnitOfTypeToArmy(unitType2, army); + Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " or " + unitType3.Name + " (have 1)."); + AddUnitOfTypeToArmy(unitType2, army); + Assert_That__ValidationPasses(req, army); + } + + [Test] + public void TestRequirementWithORWithLimitWithOtherUnit() + { + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(2, unitType2, unitType3); + AddUnitOfTypeToArmy(unitType1, army); + Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " or " + unitType3.Name + " (have 0)."); + AddUnitOfTypeToArmy(unitType3, army); + Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " or " + unitType3.Name + " (have 1)."); + AddUnitOfTypeToArmy(unitType3, army); + Assert_That__ValidationPasses(req, army); + } + protected override UnitRequiresAtLeastNUnitsRequirement CreateRequirement(UnitType requirementOn) { return new UnitRequiresAtLeastNUnitsRequirement(requirementOn);
--- a/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs Sat Mar 24 19:32:47 2012 +0000 +++ b/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs Sat Mar 24 20:34:42 2012 +0000 @@ -168,7 +168,7 @@ UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2, unitType3); Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " or " + unitType3.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1)."); - AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType3, army); Assert_That__PassesAdding(req, unitType1, army); } @@ -403,7 +403,7 @@ req.AddUnitTypeRequirement(unitType2, unitType3); AddUnitOfTypeToArmy(unitType1, army); Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " or " + unitType3.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1)."); - AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType3, army); Assert_That__ValidationPasses(req, army); }