Mercurial > repos > IBBoard.WarFoundry.API.Tests
diff API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs @ 210:649759343da5
Re #379: Fix validation of requirements to check for unit
* Tidy up tests to reduce replication and fix copy-and-paste inconsistencies
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 25 Feb 2012 16:36:20 +0000 |
parents | fc9a90d29ae0 |
children | bc782a0d47bf |
line wrap: on
line diff
--- a/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs Wed Feb 22 21:00:01 2012 +0000 +++ b/API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs Sat Feb 25 16:36:20 2012 +0000 @@ -29,43 +29,64 @@ } [Test()] - public void TestAddingUnitWithNoUnitsAndOneUnitTypeRequiredIsAllowed() + public void TestAddingUnitWithNoUnitsAndOneUnitTypeRequiredFails() { Army army = new Army(mockRace, "Test", 1000); Unit unit = CreateUnitOfType(unitType1, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed)); + Assert_That__FailsAdding(req, unit, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1)."); } [Test()] - public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredIsNotAllowed() + public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequiredFails() + { + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1)."); + } + + [Test()] + public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredPasses() { Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType2, army); Unit unit = CreateUnitOfType(unitType1, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed)); + Assert_That__PassesAdding(req, unit, army); } [Test()] - public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequiredIsAllowed() - { - Army army = new Army(mockRace, "Test", 1000); - UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); - req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); - } - - [Test()] - public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredIsNotAllowed() + public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredPasses() { Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType2, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); + Assert_That__PassesAdding(req, unitType1, army); + } + + [Test()] + public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredPassesWhenAddingRequiredUnit() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + Unit unit = CreateUnitOfType(unitType2, army); + UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert_That__PassesAdding(req, unit, army); + } + + [Test()] + public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredPassesWhenAddingRequiredUnitType() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert_That__PassesAdding(req, unitType2, army); } [Test()] @@ -75,64 +96,85 @@ AddUnitOfTypeToArmy(unitType2, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2, 1); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); + Assert_That__PassesAdding(req, unitType1, army); } [Test()] public void TestAddingUnitTypeSetsLimitForFailure() { Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType1, army); AddUnitOfTypeToArmy(unitType2, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2, 1); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); + Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 1 for 2)."); } [Test()] - public void TestAddingUnitTriggersFailure() + public void TestAddingUnitTriggersPass() { Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType1, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2, 1); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); + Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1)."); AddUnitOfTypeToArmy(unitType2, army); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); + Assert_That__PassesAdding(req, unitType1, army); } [Test()] public void TestAddingUnitTypeDefaultsToOne() { Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType1, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); + Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1)."); AddUnitOfTypeToArmy(unitType2, army); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); + Assert_That__PassesAdding(req, unitType1, army); } [Test] - public void TestRequirementNotApplicableWhenLimitedUnitNotIncluded() + public void TestRequirementNotApplicableWhenCheckingAddForNonLimitedUnits() + { + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert_That__AddingNotApplicable(req, unitType3, army); + Assert_That__AddingNotApplicable(req, CreateUnitOfType(unitType3, army), army); + Assert_That__AddingNotApplicable(req, unitType2, army); + Assert_That__AddingNotApplicable(req, CreateUnitOfType(unitType2, army), army); + } + + [Test] + public void TestRequirementNotApplicableAfterAddingLimitedUnit() { Army army = new Army(mockRace, "Test", 1000); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unitType3, army), Is.EqualTo(Validation.NotApplicable)); - Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.NotApplicable)); + Assert_That__AddingNotApplicable(req, unitType3, army); + AddUnitOfTypeToArmy(unitType1, army); + Assert_That__AddingNotApplicable(req, unitType3, army); } [Test] - public void TestRequirementBecomesInvalidWhenProhibitedUnitsChange() + public void TestRequirementNotApplicableBecomesPassAfterAddingLimitedUnit() { Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType1, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); - Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed)); - AddUnitOfTypeToArmy(unitType2, army); - Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed)); + Assert_That__AddingNotApplicable(req, unitType2, army); + AddUnitOfTypeToArmy(unitType1, army); + Assert_That__PassesAdding(req, unitType2, army); + } + + [Test] + public void TestRequirementNotApplicableBecomesFailAfterAddingLimitedUnit() + { + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2, 2, 1); + Assert_That__AddingNotApplicable(req, unitType2, army); + AddUnitOfTypeToArmy(unitType1, army); + Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 2 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 1 for 1)."); } [Test()] @@ -142,11 +184,60 @@ AddUnitOfTypeToArmy(unitType1, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Passed)); + Assert_That__PassesAdding(req, unitType2, army); + AddUnitOfTypeToArmy(unitType1, army); + Assert_That__FailsAdding(req, unitType2, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 1 for 2)."); + } + + [Test] + public void TestRequirementFailsWhenLimitedUnitsChange() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + AddUnitOfTypeToArmy(unitType2, army); + UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert_That__PassesAdding(req, unitType2, army); + AddUnitOfTypeToArmy(unitType1, army); + Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed)); } [Test] - public void TestValidationMessageIsBlankForPass() + public void TestValidationNotApplicableWithNoLimitedUnit() + { + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable)); + AddUnitOfTypeToArmy(unitType3, army); + Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable)); + AddUnitOfTypeToArmy(unitType2, army); + Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable)); + } + + [Test()] + public void TestValidationWithNoUnitsAndOneUnitTypeRequiredFails() + { + Army army = new Army(mockRace, "Test", 1000); + UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + AddUnitOfTypeToArmy(unitType1, army); + Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed)); + } + + [Test()] + public void TestValidationWithOneUnitAndOneUnitTypeRequiredPasses() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + AddUnitOfTypeToArmy(unitType2, army); + UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed)); + } + + [Test] + public void TestValidationMessageIsBlankForNotApplicable() { Army army = new Army(mockRace, "Test", 1000); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); @@ -155,14 +246,13 @@ } [Test] - public void TestValidationMessageIsCorrectForFailWithOneUnitProhibited() + public void TestValidationMessageIsCorrectForFailWithOneUnitRequired() { Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType1, army); - AddUnitOfTypeToArmy(unitType2, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); - Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 0 × " + unitType2.Name + " for every " + unitType1.Name + " (have 1).")); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).")); } [Test] @@ -170,16 +260,16 @@ { Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType1, army); - AddUnitOfTypeToArmy(unitType2, army); - AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType1, army); + AddUnitOfTypeToArmy(unitType1, army); AddUnitOfTypeToArmy(unitType2, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2, 2); - Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 2 × " + unitType2.Name + " for every " + unitType1.Name + " (have 3).")); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 2 × " + unitType1.Name + " (have 1 for 3).")); } [Test] - public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibited() + public void TestValidationMessageIsCorrectForFailWithTwoUnitsRequiredAndOneOfOneUnit() { Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType1, army); @@ -191,7 +281,7 @@ } [Test] - public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibitedAndOtherUnitInArmy() + public void TestValidationMessageIsCorrectForFailWithTwoUnitsRequiredAndOneOfOtherUnitInArmy() { Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType1, army); @@ -203,35 +293,14 @@ } [Test] - public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibitedAndBothUnitsInArmy() + public void TestValidationMessageIsCorrectForFailWithTwoRequiredUnitsAndNeitherInArmy() { Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType1, army); - AddUnitOfTypeToArmy(unitType2, army); - AddUnitOfTypeToArmy(unitType3, army); - UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); - req.AddUnitTypeRequirement(unitType2); - req.AddUnitTypeRequirement(unitType3); - Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 0 × " + unitType2.Name + " for every " + unitType1.Name + " (have 1); 0 × " + unitType3.Name + " for every " + unitType1.Name + " (have 1).")); - } - - [Test] - public void TestAllowsAddMessageIsBlankForPass() - { - Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType1, army); - UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); - Assert.That(req.GetAllowsAddingMessage(unitType2, army), Is.Empty); - } - - [Test] - public void TestAllowsAddMessageIsCorrectForFailWithOneUnitProhibited() - { - Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType1, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); - Assert.That(req.GetAllowsAddingMessage(unitType2, army), Is.EqualTo("Army must contain: 0 × " + unitType2.Name + " for every " + unitType1.Name + " (would have 1).")); + req.AddUnitTypeRequirement(unitType3); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1); 1 × " + unitType3.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).")); } [Test] @@ -239,15 +308,15 @@ { Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType1, army); - AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType1, army); AddUnitOfTypeToArmy(unitType2, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2, 2); - Assert.That(req.GetAllowsAddingMessage(unitType2, army), Is.EqualTo("Army must contain: 2 × " + unitType2.Name + " for every " + unitType1.Name + " (would have 3).")); + Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 2 × " + unitType1.Name + " (would have 1 for 3).")); } [Test] - public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsProhibited() + public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsRequired() { Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType1, army); @@ -258,27 +327,61 @@ } [Test] - public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsProhibitedAndOneUnitInArmy() + public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsRequiredAndOneUnitInArmy() { Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType1, army); AddUnitOfTypeToArmy(unitType2, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); req.AddUnitTypeRequirement(unitType3); - Assert.That(req.GetAllowsAddingMessage(unitType3, army), Is.EqualTo("Army must contain: 0 × " + unitType2.Name + " for every " + unitType1.Name + " (would have 1); 0 × " + unitType3.Name + " for every " + unitType1.Name + " (would have 1).")); + Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 1 × " + unitType3.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1).")); } [Test] - public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsProhibitedAndOtherUnitInArmy() + public void TestAllowsAddMessageIsCorrectForFailWithTwoUnitsRequiredAndOtherUnitInArmy() { Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType1, army); AddUnitOfTypeToArmy(unitType3, army); UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); req.AddUnitTypeRequirement(unitType3); - Assert.That(req.GetAllowsAddingMessage(unitType2, army), Is.EqualTo("Army must contain: 0 × " + unitType2.Name + " for every " + unitType1.Name + " (would have 1); 0 × " + unitType3.Name + " for every " + unitType1.Name + " (would have 1).")); + Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1).")); + } + + private static void Assert_That__PassesAdding(UnitRequiresNUnitsForMUnitsRequirement req, UnitType unitType, Army army) + { + Assert.That(req.AllowsAdding(unitType, army), Is.EqualTo(Validation.Passed)); + Assert.That(req.GetAllowsAddingMessage(unitType, army), Is.Empty); + } + + private static void Assert_That__PassesAdding(UnitRequiresNUnitsForMUnitsRequirement req, Unit unit, Army army) + { + Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed)); + Assert.That(req.GetAllowsAddingMessage(unit, army), Is.Empty); + } + + private static void Assert_That__AddingNotApplicable(UnitRequiresNUnitsForMUnitsRequirement req, UnitType unitType, Army army) + { + Assert.That(req.AllowsAdding(unitType, army), Is.EqualTo(Validation.NotApplicable)); + Assert.That(req.GetAllowsAddingMessage(unitType, army), Is.Empty); + } + + private static void Assert_That__AddingNotApplicable(UnitRequiresNUnitsForMUnitsRequirement req, Unit unit, Army army) + { + Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.NotApplicable)); + Assert.That(req.GetAllowsAddingMessage(unit, army), Is.Empty); + } + + private static void Assert_That__FailsAdding(UnitRequiresNUnitsForMUnitsRequirement req, UnitType unitType, Army army, string message) + { + Assert.That(req.AllowsAdding(unitType, army), Is.EqualTo(Validation.Failed)); + Assert.That(req.GetAllowsAddingMessage(unitType, army), Is.EqualTo(message)); + } + + private static void Assert_That__FailsAdding(UnitRequiresNUnitsForMUnitsRequirement req, Unit unit, Army army, string message) + { + Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed)); + Assert.That(req.GetAllowsAddingMessage(unit, army), Is.EqualTo(message)); } private static void AddUnitOfTypeToArmy(UnitType unitType, Army army)