view API/Factories/Requirement/UnitRequiresNUnitsForMUnitsRequirementFactoryTest.cs @ 194:f1f4df654a91

Re #350: Add requirement to allow N of unit for specific other units * Add tests for defining minimum amount as well as allowed amount (e.g. "2 of these allow 3 of those")
author IBBoard <dev@ibboard.co.uk>
date Sat, 03 Dec 2011 19:56:58 +0000
parents cf1c0a91eaa9
children 6a0a0e228e3c
line wrap: on
line source

// This file (UnitUnitRequiresNUnitsForMUnitsRequirementFactoryTest.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 IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Objects.Requirement;
using IBBoard.WarFoundry.API.Factories.Mock;

namespace IBBoard.WarFoundry.API.Factories.Requirement
{
	[TestFixture()]
	public class UnitUnitRequiresNUnitsForMUnitsRequirementFactoryTest
	{
		private static Race race;
		private static string typeID1 = "unit1";
		private static string typeID2 = "unit2";
		private static UnitType unitType1;
		private static UnitType unitType2;
		private static MockRaceFactory raceFactory = new MockRaceFactory();

		[TestFixtureSetUp()]
		public void Setup()
		{
			race = new MockRace();
			unitType1 = new UnitType(typeID1, "Unit 1", race);
			race.AddUnitType(unitType1);
			unitType2 = new UnitType(typeID2, "Unit 2", race);
			race.AddUnitType(unitType2);
		}

		[Test()]
		public void TestCreatesRequirement()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			Assert.That(factory.CreateRequirement(unitType2, typeID1, raceFactory), Is.Not.Null);
		}

		[Test()]
		public void TestCreatesCorrectRequirement()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
			expectedReq.AddUnitTypeRequirement(unitType1);
			IRequirement req = factory.CreateRequirement(unitType, typeID1, raceFactory);
			Assert.That(req, Is.EqualTo(expectedReq));
		}

		[Test()]
		public void TestCreatesCorrectRequirementForTwoIDs()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
			expectedReq.AddUnitTypeRequirement(unitType1);
			expectedReq.AddUnitTypeRequirement(unitType2);
			IRequirement req = factory.CreateRequirement(unitType, typeID1 + "|" + typeID2, raceFactory);
			Assert.That(req, Is.EqualTo(expectedReq));
		}

		[Test()]
		public void TestCreatesCorrectRequirementWithNumber()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
			expectedReq.AddUnitTypeRequirement(unitType1, 2);
			IRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2", raceFactory);
			Assert.That(req, Is.EqualTo(expectedReq));
		}

		[Test()]
		public void TestCreatesCorrectRequirementWithTwoNumbers()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
			expectedReq.AddUnitTypeRequirement(unitType1, 2, 3);
			IRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2;3", raceFactory);
			Assert.That(req, Is.EqualTo(expectedReq));
		}

		[Test()]
		public void TestCreatesCorrectRequirementForTwoIDsWithTwoNumbers()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
			expectedReq.AddUnitTypeRequirement(unitType1, 2);
			expectedReq.AddUnitTypeRequirement(unitType2, 3);
			IRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2|" + typeID2 + ":3", raceFactory);
			Assert.That(req, Is.EqualTo(expectedReq));
		}

		[Test()]
		public void TestCreatesCorrectRequirementForTwoIDsWithOneNumber()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
			expectedReq.AddUnitTypeRequirement(unitType1, 2);
			expectedReq.AddUnitTypeRequirement(unitType2);
			IRequirement req = factory.CreateRequirement(unitType, typeID1 + ":2|" + typeID2, raceFactory);
			Assert.That(req, Is.EqualTo(expectedReq));
		}

		[Test()]
		public void TestCreatesCorrectRequirementForTwoIDsWithOtherNumber()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			RequiresNUnitsForMUnitsRequirement expectedReq = new RequiresNUnitsForMUnitsRequirement(unitType);
			expectedReq.AddUnitTypeRequirement(unitType1);
			expectedReq.AddUnitTypeRequirement(unitType2, 3);
			IRequirement req = factory.CreateRequirement(unitType, typeID1 + "|" + typeID2 + ":3", raceFactory);
			Assert.That(req, Is.EqualTo(expectedReq));
		}

		[Test()]
		[ExpectedException(typeof(InvalidRequirementException), ExpectedMessage="Invalid unit type 'fibble' for 'Requires N units for M units' requirement")]
		public void TestExceptionsForInvalidID()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			factory.CreateRequirement(unitType, "fibble", raceFactory);
		}

		[Test()]
		[ExpectedException(typeof(InvalidRequirementException), ExpectedMessage="Invalid allowed amount 'a' for unit type 'unit1' for 'Requires N units for M units' requirement")]
		public void TestExceptionsForInvalidNumber()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			factory.CreateRequirement(unitType, typeID1+":a", raceFactory);
		}

		[Test()]
		[ExpectedException(typeof(InvalidRequirementException), ExpectedMessage="Invalid allowed amount 'a' for unit type 'unit1' for 'Requires N units for M units' requirement")]
		public void TestExceptionsForInvalidAllowedNumber()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			factory.CreateRequirement(unitType, typeID1 + ":3;a", raceFactory);
		}

		[Test()]
		[ExpectedException(typeof(InvalidRequirementException), ExpectedMessage="Invalid minimum amount 'a' for unit type 'unit1' for 'Requires N units for M units' requirement")]
		public void TestExceptionsForInvalidMinimumNumber()
		{
			UnitRequiresNUnitsForMUnitsRequirementFactory factory = new UnitRequiresNUnitsForMUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			factory.CreateRequirement(unitType, typeID1 + ":a;3", raceFactory);
		}
	}
}