Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#

Enum Generitized

Rate me:
Please Sign up or sign in to vote.
3.28/5 (10 votes)
12 May 2007CPOL7 min read 35.1K   164   26  
Using Generics to make a different kind of enumeration: easy to comment, and supports many types.
using System;
using System.Data;
using Adapdev.UnitTest;
using Enums;


namespace TestEnum
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	[TestFixture]
	public class TestStringEnumClass
	{
		//public const string StringNull = ""; //we don't like nulls!
		//public const string StringNotApplicable = "NOTAPPLICABLE";

		
		/// <summary>
		/// Declares and sets values twice for TestStringEnumABC
		/// </summary>
		[Test(Category="Assign value",Description="Test multiple assignments to same ABC.")]
		public void AssignMultipleValuesToABC()
		{
            
			TestStringEnumClassABC abc = new TestStringEnumClassABC();
			abc.Value = TestStringEnumClassABC.A;
			Assert.IsTrue(abc.Value == "A","The value should equal 'A'");
			abc.Value = TestStringEnumClassABC.B;
			Assert.IsTrue(abc.Value == "B","The value should equal 'B'");
		}

		/// <summary>
		/// Test multiple ABC
		/// </summary>
		[Test(Category="Assign value",Description="Create two ABC's, assign different values.")]
		public void AssignMultipleABC()
		{
			TestStringEnumClassABC a1 = new TestStringEnumClassABC();
			TestStringEnumClassABC a2 = new TestStringEnumClassABC();
			a1.Value = TestStringEnumClassABC.A;
			a2.Value = TestStringEnumClassABC.B;
			Console.WriteLine("a1.value = " + a1.Value);
			Console.WriteLine("a2.value = " + a2.Value);
			Assert.IsTrue(a1.Value != a2.Value );
			
			
		}

		/// <summary>
		/// Declare abc and xyz TestStringEnums.
		/// Makes sure can do different enums from abstrct StringEnum.
		/// Confirms return correct value.
		/// </summary>
		[Test(Category="Assign value",Description="Sets ABC and XYZ, makes sure values are different")]
		public void AssignABCAndXYZValues()
		{
			TestStringEnumClassABC abc = new TestStringEnumClassABC();
			abc.Value = TestStringEnumClassABC.A;
			Assert.IsTrue(abc.Value == "A","The value should equal 'A'");
			TestStringEnumClassXYZ xyz = new TestStringEnumClassXYZ();
			xyz.Value = TestStringEnumClassXYZ.X;
			Assert.IsTrue(abc.Value == "A","The value should equal 'A'");
			Assert.IsTrue(xyz.Value == "X","The value should equal 'X'");
		}
		


		/// <summary>
		/// Makes sure receive exception when set to bad value.
		/// </summary>
		[Test(Category="Assign value",Description="Assigning a vad value to ABC, expect out of range exception")]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void AssignABCaBadValue()
		{
			TestStringEnumClassABC abc = new TestStringEnumClassABC();
			abc.Value = "N";
			Assert.IsTrue(abc.Value == "N","The value should equal 'N'");
			
		}


		/// <summary>
		/// Makes sure receive exception when have bad constant.
		/// </summary>
		[Test(Category="Improper Declaration",Description="Developer created an value with the wrong type.")]
		[ExpectedException(typeof(ArrayTypeMismatchException))]
		public void CreateEnumWithBadConstantTypeDeclaration()
		{
			TestStringEnumClassBad sEnum = new TestStringEnumClassBad();
			sEnum.Value = "N";
			Assert.IsTrue(sEnum.Value == "N","The value should equal 'N'");
			
		}

		/// <summary>
		/// Makes sure receive exception when have two constants the same.
		/// </summary>
		[Test(Category="Improper Declaration",Description="Developer created two values that are the equal.")]
		[ExpectedException(typeof(NotSupportedException))]
		public void CreateEnumWithDuplicateDeclaration()
		{
			TestStringEnumClassDuplicate sEnum = new TestStringEnumClassDuplicate();
			sEnum.Value = "N";
			Assert.IsTrue(sEnum.Value == "N","The value should equal 'N'");
			
		}
		

		/// <summary>
		/// Get array of values from enumeration
		/// </summary>
		[Test(Category="Get",Description="Get array of values.")]
		public void GetValuesForABC()
		{
			TestStringEnumClassABC abc = new TestStringEnumClassABC();
			//abc.Value = TestStringEnumClassABC.A ;
			string[] values = abc.GetValues();
			for (int i = 0;i<values.Length;i++)
			{
				Console.WriteLine("Value at position " + i.ToString() + " is " + values[i]);
			}
			
		}

		
		/// <summary>
		/// Get array of constant names from enumeration
		/// </summary>
		[Test(Category="Get",Description="Get array of Names.")]
		public void GetNamesForABC()
		{
			TestStringEnumClassABC abc = new TestStringEnumClassABC();
			//abc.Value = TestStringEnumClassABC.StringNull ;
			string[] names = abc.GetNames();
			for (int i = 0;i<names.Length;i++)
			{
				Console.WriteLine("Name at position " + i.ToString() + " is " + names[i]);
			}
			
		}

		/// <summary>
		/// Get two dimensional array 
		/// </summary>
		[Test(Category="Get",Description="Get name and values in array.")]
		public void GetNamedValuesFromClassABC()
		{
			TestStringEnumClassABC abc = new TestStringEnumClassABC();
			//abc.Value = TestStringEnumClassABC.StringNull ;
			string[,] namedValues = abc.GetNamedValues();
			for (int i = 0;i<namedValues.GetLength(1);i++)
			{
				Console.WriteLine("Name at position " + i.ToString() + " is " + namedValues[i,0]
							+ " value at position is " + namedValues[i,1]);
			}
			
		}

		/// <summary>
		/// Get DataTable of Name and Values
		/// </summary>
		[Test(Category="Get",Description="Get Name and Values in DataTable.")]
		public void GetNamedValuesDataTableFromClassABC()
		{
			TestStringEnumClassABC abc = new TestStringEnumClassABC();
			//abc.Value = TestStringEnumClassABC.StringNull ;
			DataTable dt = abc.GetNamedValuesTable();
			foreach (DataRow dr in dt.Rows)
			{
				Console.WriteLine(dr["Name"].ToString() + " has value " + dr["Value"].ToString());
			}
			
		}

		/// <summary>
		/// Check if Name returns value's name
		/// </summary>
		[Test(Category="Get",Description="Get Name for assigned Value.")]
		public void GetNamedOfAssignedValueFromClassABC()
		{
			TestStringEnumClassABC abc = new TestStringEnumClassABC();
			abc.Value = TestStringEnumClassABC.A ;
			Console.WriteLine("Name of value " + TestStringEnumClassABC.A + " = " + abc.Name);
			Assert.AreEqual(abc.Name,"A");
			
			
		}

		/// <summary>
		/// Check if Name of passed in value
		/// </summary>
		[Test(Category="Get",Description="Get Name for a value.")]
		public void GetNamedOfValueFromClassABC()
		{
			TestStringEnumClassABC abc = new TestStringEnumClassABC();
			
			Console.WriteLine("value " + TestStringEnumClassABC.B 
								+ " has name " + abc.GetNameForValue("B"));
			Console.WriteLine("value " + TestStringEnumClassABC.C 
				+ " has name " + abc.GetNameForValue(TestStringEnumClassABC.C));
			
			Assert.AreEqual(abc.GetNameForValue("B"),"B");
			
			
		}


	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
United States United States
A biography in this little spot...sure.
I've worked at GTE HawaiianTel. I've worked at Nuclear Plants. I've worked at Abbott Labs. I've consulted to Ameritech Cellular. I've consulted to Zurich North America. I've consulted to International Truck and Engine. Right now, I've consulted to Wachovia Securities to help with various projects. I've been to SHCDirect and now at Cision.

During this time, I've used all kinds of tools of the trade. Keeping it to the more familier tools, I've used VB3 to VB.NET, ASP to ASP/JAVASCRIPT/XML to ASP.NET. Currently, I'm developing with C# and ASP.NET. I built reports in Access, Excel, Crystal Reports, and Business Objects (including the Universes and ETLS). Been a DBA on SQL Server 4.2 to 2000 and a DBA for Oracle. I've built OLTP databases and DataMarts. Heck, I've even done Documentum. I've been lucky to have created software for the single user to thousands of concurrent users.

I consider myself fortunate to have met many different people and worked in many environments. It's through these experiences I've learned the most.

Comments and Discussions