view API/Objects/Requirement/RequiresNUnitsForMUnitsRequirementTest.cs @ 188:0762766bd3f2

Re #350: Add requirement to allow N of unit for specific other units * Remove static access to mock race - it broke tests * Add first tests for new requirement
author IBBoard <dev@ibboard.co.uk>
date Tue, 29 Nov 2011 20:56:34 +0000
parents
children eea440e5891b
line wrap: on
line source

// This file (UnitRequiresNUnitsForMUnitsRequirement.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;
using System.Reflection;
using IBBoard.NUnit;
using IBBoard.WarFoundry.API.Factories;

namespace IBBoard.WarFoundry.API.Objects.Requirement
{
	[TestFixture()]
	public class RequiresNUnitsForMUnitsRequirementTest : AbstractEqualityTest<RequiresNUnitsForMUnitsRequirement>
	{
		private MockRace mockRace;
		private UnitType unitType1;
		private UnitType unitType2;
		private UnitType unitType3;

		[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);
			unitType3 = new MockUnitType("type3", "Unit Type 3");
			mockRace.AddUnitType(unitType3);
		}

		[Test()]
		public void TestAddingUnrelatedUnitWithNoUnitsAndOneUnitTypeRequiredIsNA()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2);
			Unit unit = CreateUnitOfType(unitType3, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.NotApplicable));
		}

		[Test()]
		public void TestAddingOneUnitWithOneUnitTypeRequiredAndOneUnitOfTypePasses()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2);
			Unit unit = CreateUnitOfType(unitType1, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestAddingOneUnitWithOneUnitTypeRequiredAndNoUnitOfTypeFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2);
			Unit unit = CreateUnitOfType(unitType1, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
		}

		[Test()]
		public void TestAddingTwoUnitsWithOneUnitTypeRequiredAndOneUnitOfTypeFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2);
			Unit unit = CreateUnitOfType(unitType1, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
		}

		[Test()]
		public void TestAddingTwoUnitsWithTwoOfUnitTypeAllowedForOneUnitTypeRequiredAndOneUnitOfTypePasses()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1, 2);
			Unit unit = CreateUnitOfType(unitType1, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Passed));
		}

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

		public override RequiresNUnitsForMUnitsRequirement GetObject()
		{
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType3);
			req.AddUnitTypeRequirement(unitType1, 2, 3);
			return req;
		}

		public override RequiresNUnitsForMUnitsRequirement GetSameObject()
		{
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType3);
			req.AddUnitTypeRequirement(unitType1, 2, 3);
			return req;
		}

		public override RequiresNUnitsForMUnitsRequirement GetDifferentObject()
		{
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType3);
			DummyWarFoundryFactory factory = new DummyWarFoundryFactory();
			GameSystem gameSystem = new GameSystem("system", "system", factory);
			Race race = new Race("race", "race", gameSystem, factory);
			req.AddUnitTypeRequirement(new UnitType("id2", "Type 2", race), 2, 3);
			return req;
		}
	}
}