view API/Objects/Requirement/RequirementHandlerTests.cs @ 211:bc782a0d47bf

Re #379: Fix validation of requirements to check for unit * Finish off validation tests for "N units for M Units" requirement
author IBBoard <dev@ibboard.co.uk>
date Sat, 25 Feb 2012 17:02:38 +0000
parents 0d40d80226b8
children e173c5512067
line wrap: on
line source

// This file (RequirementHandlerTests.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 IBBoard.WarFoundry.API.Objects;
using NUnit.Framework.SyntaxHelpers;
using IBBoard.WarFoundry.API.Objects.Requirement.Mock;
using System.Collections.Generic;

namespace IBBoard.WarFoundry.API.Objects.Requirement
{
	[TestFixture()]
	public class RequirementHandlerTests
	{
		[Test()]
		public void TestArmyWithNoRequirementsValidates()
		{
			MockRace race = new MockRace();
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.ValidateArmy(army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestArmyWithFailingRequirementFails()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new FailingRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.ValidateArmy(army), Is.EqualTo(Validation.Failed));
		}

		[Test()]
		public void TestArmyWithPassingRequirementPasses()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new PassingRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.ValidateArmy(army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestArmyWithNotApplicableRequirementPasses()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new NotApplicableRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.ValidateArmy(army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestArmyWithMixedRequirementsWithFailFails()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new PassingRequirement());
			mockUnitType.AddRequirement(new FailingRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.ValidateArmy(army), Is.EqualTo(Validation.Failed));
		}

		[Test()]
		public void TestArmyWithMixedRequirementsWithFailInOtherOrderFails()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new FailingRequirement());
			mockUnitType.AddRequirement(new PassingRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.ValidateArmy(army), Is.EqualTo(Validation.Failed));
		}

		[Test()]
		public void TestArmyWithMixedRequirementsWithNoFailsPasses()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new PassingRequirement());
			mockUnitType.AddRequirement(new NotApplicableRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.ValidateArmy(army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestCanAddToArmyWithNoRequirementsValidates()
		{
			MockRace race = new MockRace();
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.AllowsAdding(new MockUnitType(), army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestCanAddToArmyWithFailingRequirementFails()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new FailingRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.AllowsAdding(mockUnitType, army), Is.EqualTo(Validation.Failed));
		}

		[Test()]
		public void TestCanAddToArmyWithPassingRequirementPasses()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new PassingRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.AllowsAdding(mockUnitType, army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestCanAddToArmyWithNotApplicableRequirementPasses()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new NotApplicableRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.AllowsAdding(mockUnitType, army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestCanAddToArmyWithMixedRequirementsWithFailFails()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new PassingRequirement());
			mockUnitType.AddRequirement(new FailingRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.AllowsAdding(mockUnitType, army), Is.EqualTo(Validation.Failed));
		}

		[Test()]
		public void TestCanAddToArmyWithMixedRequirementsWithFailInOtherOrderFails()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new FailingRequirement());
			mockUnitType.AddRequirement(new PassingRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.AllowsAdding(mockUnitType, army), Is.EqualTo(Validation.Failed));
		}

		[Test()]
		public void TestCanAddToArmyWithMixedRequirementsWithNoFailsPasses()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new PassingRequirement());
			mockUnitType.AddRequirement(new NotApplicableRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			Assert.That(RequirementHandler.AllowsAdding(mockUnitType, army), Is.EqualTo(Validation.Passed));
		}

		[Test()]
		public void TestArmyWithMixedRequirementsGetsExpectedFailureMessageCount()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new PassingRequirement());
			mockUnitType.AddRequirement(new NotApplicableRequirement());
			mockUnitType.AddRequirement(new FailingRequirement());
			mockUnitType.AddRequirement(new FailingRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			ICollection<string> failureMessages;
			RequirementHandler.ValidateArmy(army, out failureMessages);
			Assert.That(failureMessages, Has.Count(2));
		}

		[Test()]
		public void TestAddingToArmyWithMixedRequirementsGetsExpectedFailureMessageCount()
		{
			MockRace race = new MockRace();
			MockUnitType mockUnitType = new MockUnitType();
			mockUnitType.AddRequirement(new PassingRequirement());
			mockUnitType.AddRequirement(new NotApplicableRequirement());
			mockUnitType.AddRequirement(new FailingRequirement());
			mockUnitType.AddRequirement(new FailingRequirement());
			race.AddUnitType(mockUnitType);
			Army army = new Army(race, "test", 1000);
			ICollection<string> failureMessages;
			RequirementHandler.AllowsAdding(mockUnitType, army, out failureMessages);
			Assert.That(failureMessages, Has.Count(2));
		}
	}
}