view API/Objects/Requirement/AbstractUnitRequirementTest.cs @ 214:1674a499168e

Re #379: * Make all tests test both Unit and UnitType by modifying abstract class's helper methods * Re-implement UnitRequiresAtLeastNUnitsRequirementTest tests (adding and validation) * Remove unused tests from Requires... TODO: Re-implement equality testing
author IBBoard <dev@ibboard.co.uk>
date Sun, 26 Feb 2012 20:18:45 +0000
parents 325943cb1db0
children 9bf34e88da89
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;

namespace IBBoard.WarFoundry.API.Objects.Requirement
{
	/// <summary>
	/// Base class of helper methods for checking unit requirements
	/// </summary>
	public abstract class AbstractUnitRequirementTest<OBJECT_TYPE> where OBJECT_TYPE : IWarFoundryObject
	{
		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 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));
		}
	}
}