view API/Objects/Requirement/RequiresAtLeastNUnitsRequirementTest.cs @ 205:d39682a98adf

Re #379: Fix validation of requirements to check for unit * Add unit tests for army object * Update tests that check the message
author IBBoard <dev@ibboard.co.uk>
date Sun, 29 Jan 2012 19:40:19 +0000
parents 0762766bd3f2
children c323770e61c2
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;
using System.Reflection;
using IBBoard.NUnit;
using IBBoard.WarFoundry.API.Factories;

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

		[TestFixtureSetUp()]
		public void SetupRace()
		{
			mockRace = new MockRace();
			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 TestAddingUnrelatedUnitWithNoUnitsAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			Unit unit = CreateUnitOfType(unitType1, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.NotApplicable));
		}

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

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

		[Test()]
		public void TestAddingUnrelatedUnitTypeWithNoUnitsAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2);
			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.NotApplicable));
		}

		[Test()]
		public void TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestAddingUnitTypeWithOneUnitAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestAddingUnitTypeWithNoUnitsAndTwoUnitTypesRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2, unitType3);
			Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Failed));
		}

		[Test()]
		public void TestAddingUnitTypeWithOneUnitAndTwoUnitTypesRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType3, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2, unitType3);
			Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestAddingUnitTypeWithBothUnitsAndTwoUnitTypesRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType3, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType2, unitType3);
			Assert.That(req.AllowsAdding(unitType2, army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestAddingOneOfUnitTypeWithTwoOfOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1, 2);
			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
		}

		[Test()]
		public void TestAddingOneUnitTypeWithOneUnitAndTwoOfOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1, 2);
			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestAddingUnitTypeRequirementWithNoNumberDefaultsToHavingOne()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1);
			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestValidityOfArmyWithNoUnitsAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
		}

		[Test()]
		public void TestValidityOfArmyWithOneUnitAndOneUnitTypeRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
		}

		[Test]
		public void TestAddingMinNumberUnitTypePassesRequirement()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1, 2);
			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Failed));
			AddUnitOfTypeToArmy(unitType1, army);
			Assert.That(req.AllowsAdding(unitType1, army), Is.EqualTo(Validation.Passed));
		}

		[Test]
		public void TestValidationMessageIsBlankForPass()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
			Assert.That(req.GetValidationMessage(army), Is.Empty);
		}

		[Test]
		public void TestValidationMessageIsCorrectForFailWithOneUnitRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType1.Name + " (have 0)."));
		}

		[Test]
		public void TestValidationMessageIsCorrectForFailWithTwoOfOneUnitRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1, 2);
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 2 × " + unitType1.Name + " (have 0)."));
		}

		[Test]
		public void TestValidationMessageIsCorrectForFailWithTwoUnitsRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType1.Name + " (have 0); 1 × " + unitType2.Name + " (have 0)."));
		}

		[Test]
		public void TestValidationMessageIsCorrectForFailWithTwoUnitsRequiredWithOneUnit()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " (have 0)."));
		}

		[Test]
		public void TestValidationMessageIsCorrectForFailWithTwoUnitsRequiredWithOtherUnit()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 × " + unitType1.Name + " (have 0)."));
		}

		[Test]
		public void TestAllowsAddingMessageIsBlankForPass()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement(unitType1);
			Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.Empty);
		}

		[Test]
		public void TestAllowsAddingMessageIsCorrectForFailWithTwoOfOneUnitRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1, 2);
			Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 2 × " + unitType1.Name + " (would have 1)."));
		}

		[Test]
		public void TestAllowsAddingMessageIsCorrectForFailWithTwoUnitsRequired()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " (would have 0)."));
		}

		[Test]
		public void TestAllowsAddingMessageIsCorrectForFailWithTwoUnitsRequiredOtherUnit()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert.That(req.GetAllowsAddingMessage(unitType2, army), Is.EqualTo("Army must contain: 1 × " + unitType1.Name + " (would have 0)."));
		}

		[Test]
		public void TestAllowsAddingMessageIsCorrectForFailWithTwoUnitsRequiredWithOneUnit()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert.That(req.GetAllowsAddingMessage(unitType1, army), Is.EqualTo("Army must contain: 1 × " + unitType2.Name + " (would have 0)."));
		}

		[Test]
		public void TestAllowsAddingMessageIsCorrectForFailWithTwoUnitsRequiredWithOtherUnit()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert.That(req.GetAllowsAddingMessage(unitType2, army), Is.EqualTo("Army must contain: 1 × " + unitType1.Name + " (would have 0)."));
		}

		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 RequiresAtLeastNUnitsRequirement GetObject ()
		{
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			req.AddUnitTypeRequirement(unitType1, 2);
			return req;
		}

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

		public override RequiresAtLeastNUnitsRequirement GetDifferentObject ()
		{
			RequiresAtLeastNUnitsRequirement req = new RequiresAtLeastNUnitsRequirement();
			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);
			return req;
		}
	}
}