view API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs @ 233:99248d05ba9c

Re #410: Create "N units per M models in parent unit" requirement * Add "add unit" test helper method that sets a unit name * Add validation tests
author IBBoard <dev@ibboard.co.uk>
date Sun, 29 Jul 2012 15:29:43 +0100
parents fdebdeb52a40
children d122354eeebc
line wrap: on
line source

// This file (UnitRequiresNoMoreThanNUnitsPerMModelsInParentUnitRequirementTests.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2012 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 NUnit.Framework.SyntaxHelpers;
using IBBoard.WarFoundry.API.Objects.Requirement.Context;
using System.Collections.Generic;

namespace IBBoard.WarFoundry.API.Objects.Requirement
{
	[TestFixture()]
	public class UnitRequiresNParentModelsForMUnitsRequirementTests: AbstractUnitTypeUnitRequirementTest<UnitType, UnitRequiresNParentModelsForMUnitsRequirement>
	{
		[Test()]
		public void TestAddingUnitTypeWithNoParentAndOneUnitTypeRequiredIsAllowed()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__AddingNotApplicable(req, unitType1, army);
		}
		
		[Test()]
		public void TestAddingUnitTypeWithParentAndOneUnitTypeRequiredIsAllowed()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__PassesAdding(req, unitType1, parent, army);
		}
		
		[Test()]
		public void TestAddingUnitTypeWithWrongParentAndOneUnitTypeRequiredIsNotApplicable()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType3);
			Assert_That__AddingNotApplicable(req, unitType1, parent, army);
		}
		
		[Test()]
		public void TestAddingTooManyUnitTypesWithParentAndOneUnitTypeRequiredFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit parent = AddUnitOfTypeToArmy(unitType3, army);
			AddUnitOfTypeToArmy(unitType1, army, parent);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType3);
			Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 1 × " + unitType1.Name + " as sub-units of each " + unitType3.Name + ", would have 2");
		}
		
		[Test()]
		public void TestAddingTooManyUnitTypesWithParentAndFiveUnitTypeRequiredFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
			AddUnitsOfTypeToArmy(5, unitType1, army, parent);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 5 × " + unitType1.Name + " as sub-units of each " + unitType2.Name + ", would have 6");
		}
		
		[Test()]
		public void TestAddingTooManyUnitsAtThreshold()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
			AddUnitsOfTypeToArmy(4, unitType1, army, parent);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__PassesAdding(req, unitType1, parent, army);
			AddUnitOfTypeToArmy(unitType1, army, parent);
			Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 5 × " + unitType1.Name + " as sub-units of each " + unitType2.Name + ", would have 6");
		}

		[Test()]
		public void TestValidatingUnitTypeWithNoParentAndOneUnitTypeRequiredIsAllowed()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__NotApplicable(req, army);
		}
		
		[Test()]
		public void TestValidatingUnitTypeWithParentAndOneUnitTypeRequiredIsAllowed()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType1, army, parent);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__ValidationPasses(req, army);
		}
		
		[Test()]
		public void TestValidatingUnitTypeWithWrongParentAndOneUnitTypeRequiredIsNotApplicable()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit parent = AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType1, army, parent);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType3);
			Assert_That__NotApplicable(req, army);
		}
		
		[Test()]
		public void TestValidatingTooManyUnitTypesWithParentAndOneUnitTypeRequiredFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit parent = AddUnitOfTypeToArmy(unitType3, army, "TheParent");
			AddUnitsOfTypeToArmy(2, unitType1, army, parent);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType3);
			Assert_That__ValidationFails(req, army, "Army can only contain 1 × " + unitType1.Name + " for every 1 model in a parent unit of " + unitType3.Name + ": TheParent has 2 for 1.");
		}
		
		[Test()]
		public void TestValidatingTooManyUnitTypesWithParentAndFiveUnitTypeRequiredFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit parent = AddUnitOfTypeToArmy(unitType2, army, "TheParent");
			AddUnitsOfTypeToArmy(6, unitType1, army, parent);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__ValidationFails(req, army, "Army can only contain 1 × " + unitType1.Name + " for every 1 model in a parent unit of " + unitType2.Name + ": TheParent has 6 for 5.");
		}
		
		[Test()]
		public void TestValidatingTooManyUnitsAtThreshold()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit parent = AddUnitOfTypeToArmy(unitType2, army, "TheParent");
			AddUnitsOfTypeToArmy(5, unitType1, army, parent);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__ValidationPasses(req, army);
			AddUnitOfTypeToArmy(unitType1, army, parent);
			Assert_That__ValidationFails(req, army, "Army can only contain 1 × " + unitType1.Name + " for every 1 model in a parent unit of " + unitType2.Name + ": TheParent has 6 for 5.");
		}

		protected override UnitRequiresNParentModelsForMUnitsRequirement CreateRequirement(UnitType requirementOn)
		{
			return new UnitRequiresNParentModelsForMUnitsRequirement(requirementOn);
		}
		
		protected static void Assert_That__PassesAdding(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army)
		{
			AddingToParentContext ctx = new AddingToParentContext(parent);
			Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.Passed));
			Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.Empty);
			Unit unit = CreateUnitOfType(unitType, army);
			Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Passed));
			Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.Empty);
		}
		
		protected static void Assert_That__AddingNotApplicable(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army)
		{
			AddingToParentContext ctx = new AddingToParentContext(parent);
			Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.NotApplicable));
			Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.Empty);
			Unit unit = CreateUnitOfType(unitType, army);
			Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.NotApplicable));
			Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.Empty);
		}
		
		protected static void Assert_That__FailsAdding(AbstractUnitRequirement<UnitType> req, UnitType unitType, Unit parent, Army army, string message)
		{
			AddingToParentContext ctx = new AddingToParentContext(parent);
			Assert.That(req.AllowsAdding(unitType, army, ctx), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetAllowsAddingMessage(unitType, army, ctx), Is.EqualTo(message));
			Unit unit = CreateUnitOfType(unitType, army);
			Assert.That(req.AllowsAdding(unit, army, ctx), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetAllowsAddingMessage(unit, army, ctx), Is.EqualTo(message));
		}

		private Unit AddUnitOfTypeToArmy(UnitType unitType, Army army, Unit parent)
		{
			Unit child = AddUnitOfTypeToArmy(unitType, army);
			parent.AddContainedUnit(child);
			return child;
		}
		
		private ICollection<Unit> AddUnitsOfTypeToArmy(int count, UnitType unitType, Army army, Unit parent)
		{
			ICollection<Unit> units = new List<Unit>();

			for (int i = 0; i < count; i++)
			{
				units.Add(AddUnitOfTypeToArmy(unitType, army, parent));
			}

			return units;
		}
	}
}