view API/Savers/IWarFoundryFileSaverTests.cs @ 95:db6db25921fb

Re #324: Add saving of Race and System data to files * Add tests for the zip entry name
author IBBoard <dev@ibboard.co.uk>
date Sat, 05 Mar 2011 15:53:29 +0000
parents 10da200c7799
children 45b80fed5e2a
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;

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

		protected abstract IWarFoundryFileSaver GetSaver();

		protected abstract string GetEntryName(WarFoundryLoadedObject obj);

		protected abstract string GetFileExtensionForType(WarFoundryLoadedObject obj);
	}
}