view API/Objects/Requirement/AbstractUnitRequirementTest.cs @ 219:f951595143a9

Re #394: Make requirements (or factory) more closely match Rollcall methods * Add unit tests that match text * "N for M" factory already handled "2,X|Y" (format: "X;Y:2" - "X|Y" is equivalent to Rollcall's "1,X,1,Y")
author IBBoard <dev@ibboard.co.uk>
date Sat, 24 Mar 2012 16:46:06 +0000
parents 9bf34e88da89
children 5e03b68dd214
line wrap: on
line source

// This file (AbstractUnitRequirementTest.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.NUnit;

namespace IBBoard.WarFoundry.API.Objects.Requirement
{
	/// <summary>
	/// Base class of helper methods for checking unit requirements
	/// </summary>
	public abstract class AbstractUnitRequirementTest<OBJECT_TYPE, TEST_CLASS> : AbstractEqualityTest<TEST_CLASS> where OBJECT_TYPE : IWarFoundryObject where TEST_CLASS : AbstractUnitRequirement<OBJECT_TYPE>
	{
		protected static void Assert_That__PassesAdding(AbstractUnitRequirement<OBJECT_TYPE> req, UnitType unitType, Army army)
		{
			Assert.That(req.AllowsAdding(unitType, army), Is.EqualTo(Validation.Passed));
			Assert.That(req.GetAllowsAddingMessage(unitType, army), Is.Empty);
			Unit unit = CreateUnitOfType(unitType, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
			Assert.That(req.GetAllowsAddingMessage(unit, army), Is.Empty);
		}

		protected static void Assert_That__AddingNotApplicable(AbstractUnitRequirement<OBJECT_TYPE> req, UnitType unitType, Army army)
		{
			Assert.That(req.AllowsAdding(unitType, army), Is.EqualTo(Validation.NotApplicable));
			Assert.That(req.GetAllowsAddingMessage(unitType, army), Is.Empty);
			Unit unit = CreateUnitOfType(unitType, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.NotApplicable));
			Assert.That(req.GetAllowsAddingMessage(unit, army), Is.Empty);
		}

		protected static void Assert_That__FailsAdding(AbstractUnitRequirement<OBJECT_TYPE> 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));
			Unit unit = CreateUnitOfType(unitType, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetAllowsAddingMessage(unit, army), Is.EqualTo(message));
		}

		protected static void Assert_That__ValidationPasses(AbstractUnitRequirement<OBJECT_TYPE> req, Army army)
		{
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
			Assert.That(req.GetValidationMessage(army), Is.Empty);
		}

		protected static void Assert_That__NotApplicable(AbstractUnitRequirement<OBJECT_TYPE> req, Army army)
		{
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
			Assert.That(req.GetValidationMessage(army), Is.Empty);
		}

		protected static void Assert_That__ValidationFails(AbstractUnitRequirement<OBJECT_TYPE> req, Army army, string message)
		{
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetValidationMessage(army), Is.EqualTo(message));
		}

		protected static void AddUnitsOfTypeToArmy(int count, UnitType unitType, Army army)
		{
			for (int i = 0; i < count; i++)
			{
				AddUnitOfTypeToArmy(unitType, army);
			}
		}

		protected static void AddUnitOfTypeToArmy(UnitType unitType, Army army)
		{
			army.AddUnit(CreateUnitOfType(unitType, army));
		}

		protected static Unit CreateUnitOfType(UnitType unitType, Army army)
		{
			return new Unit(unitType, army.GetCategory(unitType.MainCategory));
		}
	}
}