view API/Objects/Requirement/AbstractUnitRequirementTest.cs @ 212:325943cb1db0

Re #379: Fix validation of requirements to check for unit * Create common base class for requirement testing with helper methods * Rework unit adding tests in UnitRequiresNoMoreThanNOfUnitTypeRequirementTest to new structure
author IBBoard <dev@ibboard.co.uk>
date Sun, 26 Feb 2012 15:20:48 +0000
parents
children 1674a499168e
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);
		}

		protected static void Assert_That__PassesAdding(AbstractUnitRequirement<OBJECT_TYPE> req, Unit unit, Army 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);
		}

		protected static void Assert_That__AddingNotApplicable(AbstractUnitRequirement<OBJECT_TYPE> req, Unit unit, Army 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));
		}

		protected static void Assert_That__FailsAdding(AbstractUnitRequirement<OBJECT_TYPE> 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));
		}

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