view API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs @ 211:bc782a0d47bf

Re #379: Fix validation of requirements to check for unit * Finish off validation tests for "N units for M Units" requirement
author IBBoard <dev@ibboard.co.uk>
date Sat, 25 Feb 2012 17:02:38 +0000
parents 649759343da5
children 325943cb1db0
line wrap: on
line source

// This file (UnitRequiresNUnitsForMUnitsRequirementTest.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.SyntaxHelpers;

namespace IBBoard.WarFoundry.API.Objects.Requirement
{
	[TestFixture()]
	public class UnitRequiresNUnitsForMUnitsRequirementTests
	{
		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 TestAddingUnitWithNoUnitsAndOneUnitTypeRequiredFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit unit = CreateUnitOfType(unitType1, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			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 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__PassesAdding(req, unit, army);
		}

		[Test()]
		public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequiredPasses()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			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()]
		public void TestAddingUnitTypeSetsLimit()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1);
			Assert_That__PassesAdding(req, unitType1, army);
		}

		[Test()]
		public void TestAddingUnitTypeSetsLimitForFailure()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1);
			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 TestAddingUnitTriggersPass()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1);
			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__PassesAdding(req, unitType1, army);
		}

		[Test()]
		public void TestAddingLimitedUnitTriggersFailure()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			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 TestAddingUnitTypeDefaultsToOne()
		{
			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).");
			AddUnitOfTypeToArmy(unitType2, army);
			Assert_That__PassesAdding(req, unitType1, army);
		}

		[Test]
		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__AddingNotApplicable(req, unitType3, army);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__AddingNotApplicable(req, unitType3, army);
		}

		[Test]
		public void TestRequirementNotApplicableBecomesPassAfterAddingLimitedUnit()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			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]
		public void TestRequirementFailsWhenLimitedUnitsChange()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			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 TestValidationNotApplicableWithNoLimitedUnit()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__NotApplicable(req, army);
			AddUnitOfTypeToArmy(unitType3, army);
			Assert_That__NotApplicable(req, army);
			AddUnitOfTypeToArmy(unitType2, army);
			Assert_That__NotApplicable(req, army);
		}

		[Test()]
		public void TestValidatingWithNoUnitsAndOneUnitTypeRequiredFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
		}

		[Test()]
		public void TestValidatingUnitWithOneUnitAndOneUnitTypeRequiredPasses()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType1, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__ValidationPasses(req, army);
		}

		[Test()]
		public void TestValidatingUnitTypeSetsLimit()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType1, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1);
			Assert_That__ValidationPasses(req, army);
		}

		[Test()]
		public void TestValidatingUnitTypeSetsLimitForFailure()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1);
			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
		}

		[Test()]
		public void TestAddingUnitTriggersValidationPass()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
			AddUnitOfTypeToArmy(unitType2, army);
			Assert_That__ValidationPasses(req, army);
		}

		[Test()]
		public void TestAddingLimitedUnitTriggersValidationFailure()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__ValidationPasses(req, army);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
		}

		[Test()]
		public void TestValidatingUnitTypeDefaultsToOne()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 0 for 1).");
			AddUnitOfTypeToArmy(unitType2, army);
			Assert_That__ValidationPasses(req, army);
		}

		[Test]
		public void TestValidationRequirementNotApplicableWhenCheckingForNonLimitedUnits()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			AddUnitOfTypeToArmy(unitType3, army);
			Assert_That__NotApplicable(req, army);
			AddUnitOfTypeToArmy(unitType2, army);
			Assert_That__NotApplicable(req, army);
		}

		[Test]
		public void TestValidationNotApplicableBecomesPassAfterAddingLimitedUnit()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			AddUnitOfTypeToArmy(unitType2, army);
			Assert_That__NotApplicable(req, army);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__ValidationPasses(req, army);
		}

		[Test]
		public void TestValidationRequirementNotApplicableBecomesFailAfterAddingLimitedUnit()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 2, 1);
			AddUnitOfTypeToArmy(unitType2, army);
			Assert_That__NotApplicable(req, army);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 1).");
		}

		[Test]
		public void TestValidationRequirementFailsWhenLimitedUnitsChange()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__ValidationPasses(req, army);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (have 1 for 2).");
		}

		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 Assert_That__ValidationPasses(UnitRequiresNUnitsForMUnitsRequirement req, Army army)
		{
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
			Assert.That(req.GetValidationMessage(army), Is.Empty);
		}

		private static void Assert_That__NotApplicable(UnitRequiresNUnitsForMUnitsRequirement req, Army army)
		{
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
			Assert.That(req.GetValidationMessage(army), Is.Empty);
		}

		private static void Assert_That__ValidationFails(UnitRequiresNUnitsForMUnitsRequirement req, Army army, string message)
		{
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetValidationMessage(army), Is.EqualTo(message));
		}

		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));
		}
	}
}