view API/Objects/Requirement/RequiresNUnitsForMUnitsRequirementTest.cs @ 195:8e95ccb8e9d5

Re #350: Add requirement to allow N of unit for specific other units * Add tests for multiple failures
author IBBoard <dev@ibboard.co.uk>
date Sat, 03 Dec 2011 20:09:25 +0000
parents f25cb069f700
children aa921248ffbf
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));
			Assert.That(req.GetAllowsAddingMessage(unit.UnitType, army), Is.Empty);
		}

		[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));
			Assert.That(req.GetAllowsAddingMessage(unit.UnitType, army), Is.Empty);
		}

		[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));
			Assert.That(req.GetAllowsAddingMessage(unit.UnitType, army), Is.EqualTo("Army must contain: 1 "+unitType2.Name+" for every 1 "+unitType1.Name + " (would have 0 for 1)."));
		}

		[Test()]
		public void TestAddingOneUnitWithTwoDifferentUnitTypesRequiredAndNoUnitOfOneTypeFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			req.AddUnitTypeRequirement(unitType3);
			Unit unit = CreateUnitOfType(unitType1, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetAllowsAddingMessage(unit.UnitType, army), Is.EqualTo("Army must contain: 1 " + unitType3.Name + " for every 1 " + unitType1.Name + " (would have 0 for 1)."));
		}

		[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));
			Assert.That(req.GetAllowsAddingMessage(unit.UnitType, army), Is.EqualTo("Army must contain: 1 " + unitType2.Name + " for every 1 " + unitType1.Name + " (would have 1 for 2)."));
		}

		[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));
			Assert.That(req.GetAllowsAddingMessage(unit.UnitType, army), Is.Empty);
		}

		[Test()]
		public void TestAddingTwoUnitsWithTwoAllowedAndOneAllowedForDifferentTypesFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType3, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1, 2);
			req.AddUnitTypeRequirement(unitType3, 1, 1);
			Unit unit = CreateUnitOfType(unitType1, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetAllowsAddingMessage(unit.UnitType, army), Is.EqualTo("Army must contain: 1 " + unitType3.Name + " for every 1 " + unitType1.Name + " (would have 1 for 2)."));
		}

		[Test()]
		public void TestAddingSecondUnitsWithOneAllowedForTwoTypesFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType3, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1, 1);
			req.AddUnitTypeRequirement(unitType3, 1, 1);
			Unit unit = CreateUnitOfType(unitType1, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetAllowsAddingMessage(unit.UnitType, army), Is.EqualTo("Army must contain: 1 " + unitType2.Name + " for every 1 " + unitType1.Name + " (would have 1 for 2); 1 " + unitType3.Name + " for every 1 " + unitType1.Name + " (would have 1 for 2)."));
		}

		[Test()]
		public void TestAddingSecondUnitsWithOneAllowedForTwoTypesFails_ShorthandMethod()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType3, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2, unitType3);
			Unit unit = CreateUnitOfType(unitType1, army);
			Assert.That(req.AllowsAdding(unit, army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetAllowsAddingMessage(unit.UnitType, army), Is.EqualTo("Army must contain: 1 " + unitType2.Name + " for every 1 " + unitType1.Name + " (would have 1 for 2); 1 " + unitType3.Name + " for every 1 " + unitType1.Name + " (would have 1 for 2)."));
		}

		[Test()]
		public void TestAddingThreeUnitsWithTwoOfUnitTypeAllowedForOneUnitTypeRequiredAndOneUnitOfTypeFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType1, 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.Failed));
			Assert.That(req.GetAllowsAddingMessage(unit.UnitType, army), Is.EqualTo("Army must contain: 1 " + unitType2.Name + " for every 2 " + unitType1.Name + " (would have 1 for 3)."));
		}

		[Test()]
		public void TestValidatingArmyWithNoUnitsAndOneUnitTypeRequiredIsNA()
		{
			Army army = new Army(mockRace, "Test", 1000);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2);
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.NotApplicable));
			Assert.That(req.GetValidationMessage(army), Is.Empty);
		}

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

		[Test()]
		public void TestValidatingArmyWithOneUnitTypeRequiredAndNoUnitOfTypeFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2);
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 " + unitType2.Name + " for every 1 " + unitType1.Name + " (have 0 for 1)."));
		}

		[Test()]
		public void TestValidatingArmyWithTwoDifferentUnitTypesRequiredAndNoUnitOfOneTypeFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			req.AddUnitTypeRequirement(unitType3);
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 " + unitType3.Name + " for every 1 " + unitType1.Name + " (have 0 for 1)."));
		}

		[Test()]
		public void TestValidatingArmyWithTwoUnitsAndOneUnitTypeRequiredAndOneUnitOfTypeFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2);
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 " + unitType2.Name + " for every 1 " + unitType1.Name + " (have 1 for 2)."));
		}

		[Test()]
		public void TestValidatingArmyWithTwoUnitsAndTwoOfUnitTypeAllowedForOneUnitTypeRequiredAndOneUnitOfTypePasses()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1, 2);
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Passed));
			Assert.That(req.GetValidationMessage(army), Is.Empty);
		}

		[Test()]
		public void TestValidatingArmyWithTwoUnitsWithTwoAllowedAndOneAllowedForDifferentTypesFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType3, army);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1, 2);
			req.AddUnitTypeRequirement(unitType3, 1, 1);
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 " + unitType3.Name + " for every 1 " + unitType1.Name + " (have 1 for 2)."));
		}

		[Test()]
		public void TestValidatingArmyWithTwoUnitsWithOneAllowedForTwoTypesFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType3, army);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1, 1);
			req.AddUnitTypeRequirement(unitType3, 1, 1);
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 " + unitType2.Name + " for every 1 " + unitType1.Name + " (have 1 for 2); 1 " + unitType3.Name + " for every 1 " + unitType1.Name + " (have 1 for 2)."));
		}

		[Test()]
		public void TestValidatingArmyWithTwoUnitsWithOneAllowedForTwoTypesFails_ShorthandMethod()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType3, army);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1, unitType2, unitType3);
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 " + unitType2.Name + " for every 1 " + unitType1.Name + " (have 1 for 2); 1 " + unitType3.Name + " for every 1 " + unitType1.Name + " (have 1 for 2)."));
		}

		[Test()]
		public void TestValidatingArmyWithThreeUnitsAndTwoOfUnitTypeAllowedForOneUnitTypeRequiredAndOneUnitOfTypeFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			AddUnitOfTypeToArmy(unitType2, army);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType1, army);
			AddUnitOfTypeToArmy(unitType1, army);
			RequiresNUnitsForMUnitsRequirement req = new RequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2, 1, 2);
			Assert.That(req.ValidatesArmy(army), Is.EqualTo(Validation.Failed));
			Assert.That(req.GetValidationMessage(army), Is.EqualTo("Army must contain: 1 " + unitType2.Name + " for every 2 " + unitType1.Name + " (have 1 for 3)."));
		}

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