view API/Objects/Requirement/UnitRequiresNParentModelsForMUnitsRequirementTests.cs @ 231:5e03b68dd214

Re #410: Create "N units per M models in parent unit" requirement * Add required usage of context objects * Add tests for new requirement * Add some extras to test unit type setup that the new requirement needs
author IBBoard <dev@ibboard.co.uk>
date Fri, 27 Jul 2012 20:33:40 +0100
parents
children fdebdeb52a40
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;

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);
			Unit firstChild = AddUnitOfTypeToArmy(unitType1, army);
			parent.AddContainedUnit(firstChild);
			UnitRequiresNParentModelsForMUnitsRequirement req = new UnitRequiresNParentModelsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType3);
			Assert_That__FailsAdding(req, unitType1, parent, army, "Army can only contain 1 × " + unitType1.Name + " as a sub-unit of each " + unitType3.Name + ", would have 2");
		}

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