view API/Savers/IWarFoundryFileSaverTests.cs @ 210:649759343da5

Re #379: Fix validation of requirements to check for unit * Tidy up tests to reduce replication and fix copy-and-paste inconsistencies
author IBBoard <dev@ibboard.co.uk>
date Sat, 25 Feb 2012 16:36:20 +0000
parents 32b3e41bc8f0
children e173c5512067
line wrap: on
line source

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using IBBoard.WarFoundry.API.Objects;
using IBBoard.WarFoundry.API.Objects.Mock;
using ICSharpCode.SharpZipLib.Zip;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;
using ICSharpCode.SharpZipLib.Core;
using IBBoard.IO;
using IBBoard.WarFoundry.API.Factories;

namespace IBBoard.WarFoundry.API.Savers
{
	public abstract class IWarFoundryFileSaverTests
	{
		[Test()]
		public void TestSaverCreatesFile()
		{
			string tempFile = Path.GetTempFileName();
			
			try
			{
				bool saved = GetSaver().Save(tempFile, new MockGameSystem());
				Assert.That(saved);
				Assert.That(File.Exists(tempFile));
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesZipFile()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				GetSaver().Save(tempFile, new MockGameSystem());
				ZipFile file = new ZipFile(tempFile);
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesSingleEntryForOneObject()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				GetSaver().Save(tempFile, new MockGameSystem());
				ZipFile file = new ZipFile(tempFile);
				Assert.That(file.Count, Is.EqualTo(1));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesSingleEntryWithCorrectNameForOneObject()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem obj = new MockGameSystem();
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				IEnumerator enumerator = file.GetEnumerator();
				enumerator.MoveNext();
				ZipEntry current = (ZipEntry)enumerator.Current;
				Assert.That(current.Name, Is.EqualTo(GetEntryName(obj)));
				Assert.That(current.Name, Text.EndsWith(GetFileExtensionForType(obj)));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesSingleEntryWithCorrectContentForOneSystem()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem obj = new MockGameSystem();
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(obj));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetGameSystemContentBytes()));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesSingleEntryWithCorrectContentForOtherSystem()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				GameSystem obj = CreateOtherSystem();
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(obj));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetOtherGameSystemContentBytes()));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		private GameSystem CreateOtherSystem()
		{
			GameSystem system = new GameSystem("otherSystem", "Other System", new DummyWarFoundryFactory());
			system.AllowAllies = true;
			system.WarnOnError = false;
			system.SystemArmyDefaultSize = 2000;
			return system;
		}

		[Test()]
		public void TestSaverCreatesSingleEntryWithCorrectContentForOneArmy()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockArmy obj = new MockArmy();
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(obj));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetArmyContentBytes()));
				stream.Close();
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesTwoEntriesForOneSystemOneArmy()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				GetSaver().Save(tempFile, new MockGameSystem(), new MockArmy());
				ZipFile file = new ZipFile(tempFile);
				Assert.That(file.Count, Is.EqualTo(2));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesTwoEntriesWithCorrectContentForOneSystemOneArmy()
		{
			//This doesn't make much sense (system and army in one file) but at the time of writing then Race saving isn't implemented
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem system = new MockGameSystem();
				MockArmy army = new MockArmy();
				GetSaver().Save(tempFile, system, army);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(system));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetGameSystemContentBytes()));
				zipEntry = file.GetEntry(GetEntryName(army));
				stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetArmyContentBytes()));
				stream.Close();
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesTwoEntriesForTwoSystems()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				GetSaver().Save(tempFile, new MockGameSystem(), CreateOtherSystem());
				ZipFile file = new ZipFile(tempFile);
				Assert.That(file.Count, Is.EqualTo(2));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestSaverCreatesTwoEntriesWithCorrectContentForTwoSystems()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem system1 = new MockGameSystem();
				GameSystem system2 = CreateOtherSystem();
				GetSaver().Save(tempFile, system1, system2);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(system1));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetGameSystemContentBytes()));
				zipEntry = file.GetEntry(GetEntryName(system2));
				stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetOtherGameSystemContentBytes()));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}
		
		[Test()]
		public void TestFileExtensionsAreCorrectForType()
		{
			Assert.That(GetSaver().GetFileExtension(new MockArmy()), Is.EqualTo(GetArmyExtension()));
			Assert.That(GetSaver().GetFileExtension(new MockRace()), Is.EqualTo(GetRaceExtension()));
			Assert.That(GetSaver().GetFileExtension(new MockGameSystem()), Is.EqualTo(GetGameSystemExtension()));
		}

		[Test()]
		public void TestPointsValueAbbreviationsSaved()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem obj = new MockGameSystem();
				string plural = "ptii";
				string singular = "ptus";
				obj.SystemPtsAbbrevPlural = plural;
				obj.SystemPtsAbbrevSingle = singular;
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(obj));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetGameSystemContentWithPointsAbbrevsBytes(singular, plural)));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestPointsValueNamesSaved()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem obj = new MockGameSystem();
				string plural = "pointii";
				string singular = "pointus";
				obj.SystemPtsNamePlural = plural;
				obj.SystemPtsNameSingle = singular;
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(obj));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetGameSystemContentWithPointsNameBytes(singular, plural)));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestPointsValueAbbreviationsAndNamesSaved()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem obj = new MockGameSystem();
				string plural = "ptii";
				string singular = "ptus";
				string pluralName = "pointii";
				string singularName = "pointus";
				obj.SystemPtsAbbrevPlural = plural;
				obj.SystemPtsAbbrevSingle = singular;
				obj.SystemPtsNamePlural = pluralName;
				obj.SystemPtsNameSingle = singularName;
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(obj));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetGameSystemContentWithPointsAbbrevsAndNameBytes(singular, plural, singularName, pluralName)));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestGameSystemWithCategories()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem obj = new MockGameSystem();
				Category heroCat = new Category("cat1", "Heroes");
				heroCat.MaximumPercentage = 25;
				heroCat.MaximumPoints = 1000;
				obj.AddCategory(heroCat);
				Category warriorCat = new Category("cat2", "Warriors");
				warriorCat.MinimumPercentage = 35;
				warriorCat.MinimumPoints = 500;
				obj.AddCategory(warriorCat);
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(obj));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetGameSystemContentWithCategories()));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		[Test()]
		public void TestGameSystemWithStatLines()
		{
			string tempFile = Path.GetTempFileName();
			try
			{
				MockGameSystem obj = new MockGameSystem();
				//MockGameSystem already has a stat line, so add one more
				SystemStats systemStats = new SystemStats("stats");
				systemStats.AddStatSlot("M");
				systemStats.AddStatSlot("S");
				systemStats.AddStatSlot("T");
				systemStats.AddStatSlot("W");
				obj.AddSystemStats(systemStats);
				obj.StandardSystemStatsID = "stats";
				GetSaver().Save(tempFile, obj);
				ZipFile file = new ZipFile(tempFile);
				ZipEntry zipEntry = file.GetEntry(GetEntryName(obj));
				Stream stream = file.GetInputStream(zipEntry);
				Assert.That(StreamUtil.ToBytes(stream), Is.EqualTo(GetGameSystemContentWithStats()));
				file.Close();
			}
			finally
			{
				if (File.Exists(tempFile))
				{
					File.Delete(tempFile);
				}
			}
		}

		protected abstract IWarFoundryFileSaver GetSaver();

		protected abstract string GetEntryName(WarFoundryLoadedObject obj);

		protected abstract string GetFileExtensionForType(WarFoundryLoadedObject obj);
		
		protected abstract string GetArmyExtension();
		
		protected abstract string GetRaceExtension();
		
		protected abstract string GetGameSystemExtension();

		protected abstract byte[] GetGameSystemContentBytes();

		protected abstract byte[] GetOtherGameSystemContentBytes();
		
		protected abstract byte[] GetArmyContentBytes();

		protected abstract byte[] GetGameSystemContentWithPointsAbbrevsBytes(string singular, string plural);

		protected abstract byte[] GetGameSystemContentWithPointsNameBytes(string singular, string plural);

		protected abstract byte[] GetGameSystemContentWithPointsAbbrevsAndNameBytes(string singular, string plural, string singularName, string pluralName);

		protected abstract byte[] GetGameSystemContentWithCategories();

		protected abstract byte[] GetGameSystemContentWithStats();
	}
}