Mercurial > repos > IBDev-IBBoard.WarFoundry.API.Tests
changeset 144:eced294e82f2
Re #345: Add failure message to requirements
* Add tests for "no more than" requirements
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 01 May 2011 15:38:01 +0000 |
parents | 6fdb76d48e04 |
children | 0d40d80226b8 |
files | API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirementTest.cs API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirementTest.cs |
diffstat | 2 files changed, 263 insertions(+), 130 deletions(-) [+] |
line wrap: on
line diff
--- a/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirementTest.cs Sun May 01 14:45:22 2011 +0000 +++ b/API/Objects/Requirement/RequiresNoMoreThanNOfUnitTypeRequirementTest.cs Sun May 01 15:38:01 2011 +0000 @@ -110,6 +110,69 @@ Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed)); } + [Test] + public void TestValidationMessageIsBlankForPass() + { + Army army = new Army(mockRace, "Test", 1000); + RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + Assert.That(req.GetValidationMessage(army), Is.Empty); + } + + [Test] + public void TestValidationMessageIsCorrectForFailWithOneUnitProhibited() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army cannot contain more than: 0 × " + unitType1.Name + " (have 1).")); + } + + [Test] + public void TestValidationMessageIsCorrectForFailWithTwoOfOneUnitLimit() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + AddUnitOfTypeToArmy(unitType1, army); + AddUnitOfTypeToArmy(unitType1, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(); + req.AddUnitTypeRequirement(unitType1, 2); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army cannot contain more than: 2 × " + unitType1.Name + " (have 3).")); + } + + [Test] + public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibited() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + AddUnitOfTypeToArmy(unitType2, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(); + req.AddUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army cannot contain more than: 0 × " + unitType1.Name + " (have 1); 0 × " + unitType2.Name + " (have 1).")); + } + + [Test] + public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibitedAndOneUnitInArmy() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(); + req.AddUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army cannot contain more than: 0 × " + unitType1.Name + " (have 1).")); + } + + [Test] + public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibitedAndOtherUnitInArmy() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType2, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new RequiresNoMoreThanNOfUnitTypeRequirement(); + req.AddUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army cannot contain more than: 0 × " + unitType2.Name + " (have 1).")); + } + private static void AddUnitOfTypeToArmy(UnitType unitType, Army army) { army.AddUnit(CreateUnitOfType(unitType, army));
--- a/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirementTest.cs Sun May 01 14:45:22 2011 +0000 +++ b/API/Objects/Requirement/UnitRequiresNoMoreThanNOfUnitTypeRequirementTest.cs Sun May 01 15:38:01 2011 +0000 @@ -1,157 +1,227 @@ -// This file (RequiresNoMoreThanNOfUnitTypeRequirementTest.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 IBBoard +// This file (RequiresNoMoreThanNOfUnitTypeRequirementTest.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 IBBoard // // 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. using System; -using NUnit.Framework; -using IBBoard.WarFoundry.API.Objects.Mock; +using NUnit.Framework; +using IBBoard.WarFoundry.API.Objects.Mock; using NUnit.Framework.SyntaxHelpers; namespace IBBoard.WarFoundry.API.Objects.Requirement { [TestFixture()] public class UnitRequiresNoMoreThanNOfUnitTypeRequirementTest - { - private MockRace mockRace; - private UnitType unitType1; - private UnitType unitType2; - private UnitType unitType3; - - [TestFixtureSetUp()] - public void SetupRace() - { - mockRace = new MockRace(); - unitType1 = new MockUnitType("type1", "Unit Type 1"); - mockRace.AddUnitType(unitType1); - unitType2 = new MockUnitType("type2", "Unit Type 2"); - mockRace.AddUnitType(unitType2); - unitType3 = new MockUnitType("type3", "Unit Type 3"); - mockRace.AddUnitType(unitType3); - } - - [Test()] - public void TestAddingUnitWithNoUnitsAndOneUnitTypeRequiredIsAllowed() - { - Army army = new Army(mockRace, "Test", 1000); - Unit unit = CreateUnitOfType(unitType1, army); - RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); - req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed)); - } - - [Test()] - public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredIsNotAllowed() - { - Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType2, army); - Unit unit = CreateUnitOfType(unitType1, army); - RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); - req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed)); - } - - [Test()] - public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequiredIsAllowed() - { - Army army = new Army(mockRace, "Test", 1000); - RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); - req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); - } - - [Test()] - public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredIsNotAllowed() - { - Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType2, army); - RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); - req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); - } - - [Test()] - public void TestAddingUnitTypeSetsLimit() - { - Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType2, army); - RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); - req.AddUnitTypeRequirement(unitType2, 1); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); - } - - [Test()] - public void TestAddingUnitTypeSetsLimitForFailure() - { - Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType2, army); - AddUnitOfTypeToArmy(unitType2, army); - RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); - req.AddUnitTypeRequirement(unitType2, 1); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); - } - - [Test()] - public void TestAddingUnitTriggersFailure() - { - Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType2, army); - RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); - req.AddUnitTypeRequirement(unitType2, 1); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); - AddUnitOfTypeToArmy(unitType2, army); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); - } - - [Test()] - public void TestAddingUnitTypeDefaultsToNoMoreThanZero() - { - Army army = new Army(mockRace, "Test", 1000); - RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); - req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); - AddUnitOfTypeToArmy(unitType2, army); - Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); + { + private MockRace mockRace; + private UnitType unitType1; + private UnitType unitType2; + private UnitType unitType3; + + [TestFixtureSetUp()] + public void SetupRace() + { + mockRace = new MockRace(); + unitType1 = new MockUnitType("type1", "Unit Type 1"); + mockRace.AddUnitType(unitType1); + unitType2 = new MockUnitType("type2", "Unit Type 2"); + mockRace.AddUnitType(unitType2); + unitType3 = new MockUnitType("type3", "Unit Type 3"); + mockRace.AddUnitType(unitType3); + } + + [Test()] + public void TestAddingUnitWithNoUnitsAndOneUnitTypeRequiredIsAllowed() + { + Army army = new Army(mockRace, "Test", 1000); + Unit unit = CreateUnitOfType(unitType1, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed)); + } + + [Test()] + public void TestAddingUnitWithOneUnitAndOneUnitTypeRequiredIsNotAllowed() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType2, army); + Unit unit = CreateUnitOfType(unitType1, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed)); + } + + [Test()] + public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequiredIsAllowed() + { + Army army = new Army(mockRace, "Test", 1000); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); + } + + [Test()] + public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredIsNotAllowed() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType2, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); + } + + [Test()] + public void TestAddingUnitTypeSetsLimit() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType2, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2, 1); + Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); + } + + [Test()] + public void TestAddingUnitTypeSetsLimitForFailure() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType2, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2, 1); + Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); + } + + [Test()] + public void TestAddingUnitTriggersFailure() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType2, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2, 1); + Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); + AddUnitOfTypeToArmy(unitType2, army); + Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); + } + + [Test()] + public void TestAddingUnitTypeDefaultsToNoMoreThanZero() + { + Army army = new Army(mockRace, "Test", 1000); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed)); + AddUnitOfTypeToArmy(unitType2, army); + Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed)); + } + + [Test] + public void TestRequirementNotApplicableWhenLimitedUnitNotIncluded() + { + Army army = new Army(mockRace, "Test", 1000); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.AllowsAdding(unitType3, army), Is.EqualTo(Validation.NotApplicable)); + Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.NotApplicable)); + } + + [Test] + public void TestRequirementBecomesInvalidWhenProhibitedUnitsChange() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(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)); + } + + [Test()] + public void TestAddingLimitedUnitTriggersFailure() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Failed)); + } + + [Test] + public void TestValidationMessageIsBlankForPass() + { + Army army = new Army(mockRace, "Test", 1000); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.GetValidationMessage(army), Is.Empty); + } + + [Test] + public void TestValidationMessageIsCorrectForFailWithOneUnitProhibited() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + AddUnitOfTypeToArmy(unitType2, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army contains " + unitType1.Name + " and so cannot contain more than: 0 × " + unitType2.Name + " (have 1).")); + } + + [Test] + public void TestValidationMessageIsCorrectForFailWithTwoOfOneUnitLimit() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType2, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2, 2); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army contains " + unitType1.Name + " and so cannot contain more than: 2 × " + unitType2.Name + " (have 3).")); + } + + [Test] + public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibited() + { + Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + AddUnitOfTypeToArmy(unitType2, army); + RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); + req.AddUnitTypeRequirement(unitType2); + req.AddUnitTypeRequirement(unitType3); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army contains " + unitType1.Name + " and so cannot contain more than: 0 × " + unitType2.Name + " (have 1).")); } [Test] - public void TestRequirementNotApplicableWhenLimitedUnitNotIncluded() + public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibitedAndOtherUnitInArmy() { Army army = new Army(mockRace, "Test", 1000); + AddUnitOfTypeToArmy(unitType1, army); + AddUnitOfTypeToArmy(unitType3, army); RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unitType3, army), Is.EqualTo(Validation.NotApplicable)); - Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.NotApplicable)); + req.AddUnitTypeRequirement(unitType3); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army contains " + unitType1.Name + " and so cannot contain more than: 0 × " + unitType3.Name + " (have 1).")); } [Test] - public void TestRequirementBecomesInvalidWhenProhibitedUnitsChange() + public void TestValidationMessageIsCorrectForFailWithTwoUnitsProhibitedAndBothUnitsInArmy() { Army army = new Army(mockRace, "Test", 1000); AddUnitOfTypeToArmy(unitType1, army); + AddUnitOfTypeToArmy(unitType2, army); + AddUnitOfTypeToArmy(unitType3, army); RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(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)); - } - - [Test()] - public void TestAddingLimitedUnitTriggersFailure() - { - Army army = new Army(mockRace, "Test", 1000); - AddUnitOfTypeToArmy(unitType1, army); - RequiresNoMoreThanNOfUnitTypeRequirement req = new UnitRequiresNoMoreThanNOfUnitTypeRequirement(unitType1); - req.AddUnitTypeRequirement(unitType2); - Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Failed)); - } - - private static void AddUnitOfTypeToArmy(UnitType unitType, Army army) - { - army.AddUnit(CreateUnitOfType(unitType, army)); - } - - private static Unit CreateUnitOfType(UnitType unitType, Army army) - { - return new Unit(unitType, army.GetCategory(unitType.MainCategory)); + req.AddUnitTypeRequirement(unitType3); + Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army contains " + unitType1.Name + " and so cannot contain more than: 0 × " + unitType2.Name + " (have 1); 0 × " + unitType3.Name + " (have 1).")); + } + + private static void AddUnitOfTypeToArmy(UnitType unitType, Army army) + { + army.AddUnit(CreateUnitOfType(unitType, army)); + } + + private static Unit CreateUnitOfType(UnitType unitType, Army army) + { + return new Unit(unitType, army.GetCategory(unitType.MainCategory)); } } }