view API/Objects/Requirements/UnitRequiresAtLeastNUnitsRequirementTest.cs @ 103:c6562eb62d04

Re #27: Define unit requirements * Refactor out common code * Simplify tests to try with one unit type required at first * Add test where requirement is satisfied
author IBBoard <dev@ibboard.co.uk>
date Sat, 26 Mar 2011 21:02:18 +0000
parents a3bc8174299f
children 782494137b58
line wrap: on
line source

// This file (UnitRequiresAtLeastNUnitsRequirement.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2011 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 IBBoard.WarFoundry.API.Objects.Mock;
using NUnit.Framework.SyntaxHelpers;

namespace IBBoard.WarFoundry.API.Objects.Requirements
{
	[TestFixture()]
	public class UnitRequiresAtLeastNUnitsRequirementTest
	{
		private MockRace mockRace;
		private UnitType unitType1;
		private UnitType unitType2;

		[TestFixtureSetUp()]
		public void SetupRace()
		{
			mockRace = MockRace.GetMockRace();
			unitType1 = new MockUnitType("type1", "Unit Type 1");
			mockRace.AddUnitType(unitType1);
			unitType2 = new MockUnitType("type2", "Unit Type 2");
			mockRace.AddUnitType(unitType2);
		}

		[Test()]
		public void TestWithNoUnitsAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit unit = CreateUnitOfType(unitType1, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType2);
			Assert.That(req.CanAddToArmy(unit, army), Is.False);
		}

		[Test()]
		public void TestWithOneUnitAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			Unit unit = CreateUnitOfType(unitType1, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType2);
			Assert.That(req.CanAddToArmy(unit, army), Is.True);
		}

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

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