view API/Factories/Requirement/UnitRequiresAtLeastNUnitsRequirementFactoryTest.cs @ 155:3725fd449c77

Re #353: Handle unloaded units being referenced outside factory * Add mock factory and use it in tests All tests now pass (including tests that started failing because of unit loading order - triggered by requirement changes)
author IBBoard <dev@ibboard.co.uk>
date Mon, 04 Jul 2011 20:03:47 +0000
parents bf2880cf7bc5
children 17c5030485dc
line wrap: on
line source

// This file (UnitRequiresAtLeastNUnitsRequirementFactoryTest.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 UnitRequiresAtLeastNUnitsRequirementFactoryTest
	{
		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()
		{
			UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory();
			Assert.That(factory.CreateRequirement(unitType2, typeID1, raceFactory), Is.Not.Null);
		}

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

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

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

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

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

		[Test()]
		public void TestCreatesCorrectRequirementForTwoIDsWithOtherNumber()
		{
			UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			UnitRequiresAtLeastNUnitsRequirement expectedReq = new UnitRequiresAtLeastNUnitsRequirement(unitType);
			expectedReq.AddUnitTypeRequirement(unitType1);
			expectedReq.AddUnitTypeRequirement(unitType2, 3);
			UnitRequiresAtLeastNUnitsRequirement 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 at least N units' requirement")]
		public void TestExceptionsForInvalidID()
		{
			UnitRequiresAtLeastNUnitsRequirementFactory factory = new UnitRequiresAtLeastNUnitsRequirementFactory();
			UnitType unitType = new UnitType("testUnit", "Test Unit", race);
			factory.CreateRequirement(unitType, "fibble", raceFactory);
		}

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