view Xml/XmlToolsTests.cs @ 12:298786adcddc

Fixes #25: Add method for parsing enums from XML elements * Add unit tests for XmlToolsTests to test enum parsing
author IBBoard <dev@ibboard.co.uk>
date Mon, 30 Nov 2009 20:47:22 +0000
parents
children f6fd30d3b817
line wrap: on
line source

// This file (AbstractLimitTest.cs) is a part of the IBBoard.Tests project and is copyright 2009 IBBoard
// 
//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.

using System;
using System.Xml;
using NUnit.Framework;

namespace IBBoard.Xml
{
	[TestFixture()]
	public class XmlToolsTests
	{
		private enum TestEnum
		{
			ALPHA
		}
		
		[Test()]
		public void TestParsingOfValidEnumString ()
		{
			XmlElement elem = GetElement("ALPHA");
			Assert.AreEqual(TestEnum.ALPHA, XmlTools.GetEnumValueFromAttribute<TestEnum>(elem, "enum"));
		}

		static XmlElement GetElement(string enumValue)
		{
			XmlDocument doc = new XmlDocument();
			XmlElement elem = doc.CreateElement ("sample");
			XmlAttribute attrib = doc.CreateAttribute ("enum");
			attrib.Value = enumValue;
			elem.Attributes.Append (attrib);
			return elem;
		}
		
		[Test()]
		public void TestParsingOfValidEnumStringWithDifferentCase ()
		{
			XmlElement elem = GetElement("ALPHA");
			Assert.AreEqual(TestEnum.ALPHA, XmlTools.GetEnumValueFromAttribute<TestEnum>(elem, "enum"));
		}
		
		[Test()]
		[ExpectedException(typeof(FormatException), ExpectedMessage="Attribute 'enum' of sample with ID  was not a valid TestEnum enum")]
		public void TestParsingValidEnumStringWithInvalidValueThrowsException ()
		{
			XmlElement elem = GetElement("Omega");
			XmlTools.GetEnumValueFromAttribute<TestEnum>(elem, "enum");
		}
		
		[Test()]
		[ExpectedException(typeof(FormatException), ExpectedMessage="Attribute 'enum' of sample with ID  was not a valid TestEnum enum")]
		public void TestCaseSensitiveParsingOfValidValueInInvalidCaseThrowsException ()
		{
			XmlElement elem = GetElement("Alpha");
			XmlTools.GetEnumValueFromAttribute<TestEnum>(elem, "enum", false);
		}
	}
}