view API/Objects/Requirement/UnitRequiresNUnitsForMUnitsRequirementTests.cs @ 214:1674a499168e

Re #379: * Make all tests test both Unit and UnitType by modifying abstract class's helper methods * Re-implement UnitRequiresAtLeastNUnitsRequirementTest tests (adding and validation) * Remove unused tests from Requires... TODO: Re-implement equality testing
author IBBoard <dev@ibboard.co.uk>
date Sun, 26 Feb 2012 20:18:45 +0000
parents 325943cb1db0
children 9bf34e88da89
line wrap: on
line source

// This file (UnitRequiresNUnitsForMUnitsRequirementTest.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 UnitRequiresNUnitsForMUnitsRequirementTests : AbstractUnitRequirementTest<UnitType>
	{
		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 TestAddingUnitTypeWithNoUnitsAndOneUnitTypeRequiredFails()
		{
			Army army = new Army(mockRace, "Test", 1000);
			UnitRequiresNUnitsForMUnitsRequirement req = new UnitRequiresNUnitsForMUnitsRequirement(unitType1);
			req.AddUnitTypeRequirement(unitType2);
			Assert_That__FailsAdding(req, unitType1, army, "Army must contain: 1 × " + unitType2.Name + " for every 1 × " + unitType1.Name + " (would have 0 for 1).");
		}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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