view API/Objects/Requirement/UnitRequiresAtLeastNUnitsRequirementTest.cs @ 217:9bf34e88da89

Re #379: Fix validation of requirements to check for unit * Add equality tests to try to resolve UnitType tests for limits as requirements * Ensure consistency with extra layer in hierarchy
author IBBoard <dev@ibboard.co.uk>
date Mon, 05 Mar 2012 20:34:45 +0000
parents 1674a499168e
children c40cb2048fc3
line wrap: on
line source

// This file (UnitRequiresAtLeastNUnitsRequirementTest.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.Requirement
{
	[TestFixture()]
	public class UnitRequiresAtLeastNUnitsRequirementTest : AbstractUnitTypeUnitRequirementTest<UnitType, UnitRequiresAtLeastNUnitsRequirement>
	{
		[Test()]
		public void TestAddingUnitWithNoUnitsAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " (would have 0).");
		}

		[Test()]
		public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__PassesAdding(req, unitType1, army);
		}

		[Test()]
		public void TestAddingUnitTypeWithNoUnitsAndTwoUnitTypesRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			req.AddUnitTypeRequirement(unitType3);
			Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " (would have 0); 1 × " + unitType3.Name + " (would have 0).");
		}

		[Test()]
		public void TestAddingUnitTypeWithOneUnitAndTwoUnitTypesRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			req.AddUnitTypeRequirement(unitType3);
			Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType3.Name + " (would have 0).");
		}

		[Test()]
		public void TestAddingUnitTypeWithOtherOneUnitAndTwoUnitTypesRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType3, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			req.AddUnitTypeRequirement(unitType3);
			Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " (would have 0).");
		}

		[Test()]
		public void TestAddingUnitTypeWithBothUnitsAndTwoUnitTypesRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType3, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			req.AddUnitTypeRequirement(unitType3);
			Assert_That__PassesAdding(req, unitType1, army);
		}

		[Test()]
		public void TestAddingUnitTypeWithOneUnitAndTwoOfOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 2);
			Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 2 × " + unitType2.Name + " (would have 1).");
		}

		[Test()]
		public void TestAddingUnrelatedUnitTypeValidatesAsNotApplicable()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 2);
			Assert_That__AddingNotApplicable(req, unitType3, army);
		}

		[Test()]
		public void TestAddingUnrelatedUnitTypeValidatesAsNotApplicableWhenRequirementsNotMet()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 2);
			Assert_That__AddingNotApplicable(req, unitType3, army);
		}

		[Test()]
		public void TestAddingUnitTypeWithTwoUnitsAndTwoOfOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 2);
			Assert_That__PassesAdding(req, unitType1, army);
		}

		[Test()]
		public void TestAddingUnitTypeRequirementWithNoNumberDefaultsToHavingOne()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__PassesAdding(req, unitType1, army);
		}

		[Test()]
		public void TestAddingUnitTypeMakesRequirementPass()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 2);
			Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 2 × " + unitType2.Name + " (would have 1).");
			AddUnitOfTypeToArmy(unitType2, army);
			Assert_That__PassesAdding(req, unitType1, army);
		}

		[Test()]
		public void TestValidatingWithNoUnitsAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " (have 0).");
		}

		[Test()]
		public void TestValidatingWithOneUnitAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__ValidationPasses(req, army);
		}

		[Test()]
		public void TestValidatingWithNoUnitsAndTwoUnitTypesRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			req.AddUnitTypeRequirement(unitType3);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " (have 0); 1 × " + unitType3.Name + " (have 0).");
		}

		[Test()]
		public void TestValidatingWithOneUnitAndTwoUnitTypesRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			req.AddUnitTypeRequirement(unitType3);
			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType3.Name + " (have 0).");
		}

		[Test()]
		public void TestValidatingWithOtherOneUnitAndTwoUnitTypesRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType3, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			req.AddUnitTypeRequirement(unitType3);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__ValidationFails(req, army, "Army must contain: 1 × " + unitType2.Name + " (have 0).");
		}

		[Test()]
		public void TestValidatingWithBothUnitsAndTwoUnitTypesRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType3, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			req.AddUnitTypeRequirement(unitType3);
			Assert_That__ValidationPasses(req, army);
		}

		[Test()]
		public void TestValidatingWithOneUnitAndTwoOfOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 2);
			AddUnitOfTypeToArmy(unitType1, army);
			Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " (have 1).");
		}

		[Test()]
		public void TestValidatingWithUnrelatedUnitTypeValidatesAsNotApplicable()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType3, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 2);
			Assert_That__NotApplicable(req, army);
		}

		[Test()]
		public void TestValidatingWithTwoUnitsAndTwoOfOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 2);
			Assert_That__ValidationPasses(req, army);
		}

		[Test()]
		public void TestValidatingRequirementWithNoNumberDefaultsToHavingOne()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__ValidationPasses(req, army);
		}

		[Test()]
		public void TestAddingUnitTypeMakesRequirementPassValidating()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType2, army);
			UnitRequiresAtLeastNUnitsRequirement req = new UnitRequiresAtLeastNUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 2);
			Assert_That__ValidationFails(req, army, "Army must contain: 2 × " + unitType2.Name + " (have 1).");
			AddUnitOfTypeToArmy(unitType2, army);
			Assert_That__ValidationPasses(req, army);
		}

		protected override UnitRequiresAtLeastNUnitsRequirement CreateRequirement(UnitType requirementOn)
		{
			return new UnitRequiresAtLeastNUnitsRequirement(requirementOn);
		}
	}
}