Click here to Skip to main content
11,713,565 members (81,605 online)
Click here to Skip to main content
Articles » Languages » C# » Enumerations » Downloads
Add your own
alternative version

Enum Generitized

, 12 May 2007 CPOL 25.7K 143 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 TestGenericEnumClass
	{

		/// <summary>
		/// Test multiple assignmesnt to same ABC
		/// </summary>
		[Test(Category="Assign value",Description="Test multiple assignments to same ABC.")]
		public void AssignMultipleValuesToABC()
		{
            TestGenericEnumClassABC a1 = new TestGenericEnumClassABC();

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

		/// <summary>
		/// Declare abc and xyz TestGenericEnums.
		/// Makes sure can do different enums from abstrct GenericEnum.
		/// Confirms return correct value.
		/// </summary>
		[Test(Category="Assign value",Description="Sets ABC and XYZ, makes sure values are different")]
		public void SetABCAndXYZValues()
		{
			TestGenericEnumClassABC abc = new TestGenericEnumClassABC();
			abc.Value = TestGenericEnumClassABC.A;
			Assert.IsTrue(abc.Value == TestGenericEnumClassABC.A,"The value should equal A");
			TestGenericEnumClassXYZ xyz = new TestGenericEnumClassXYZ();
			xyz.Value = TestGenericEnumClassXYZ.X;
			Assert.IsTrue(abc.Value == TestGenericEnumClassABC.A,"The value should equal A");
			Assert.IsTrue(xyz.Value == TestGenericEnumClassXYZ.X,"The value should equal X");
			Assert.IsTrue(abc.Value!=xyz.Value,"The values should be different");
		}
        /// <summary>
        /// Declare abc and xyz TestGenericEnums.
        /// Makes sure can do different enums from abstrct GenericEnum.
        /// Confirms return correct value.
        /// </summary>
        [Test(Category = "Assign value", Description = "For with value not set, try to assign null to Value.")]
        [ExpectedException(typeof(NoNullAllowedException))]
        public void SetNullValueToUnset()
        {
            TestGenericStringEnumClassABC abc = new TestGenericStringEnumClassABC();
            abc.Value = null;
            
        }

        ///// <summary>
        ///// Makes sure can assign Null and Not applicable
        ///// </summary>
        //[Test(Category="Assign value",Description="Check ability to assign Null and NotApplicable")]
        //public void AssignABCNullAndNotApplicableValue()
        //{
        //    TestGenericEnumClassABC abc = new TestGenericEnumClassABC();
        //    abc.Value = TestGenericEnumClassABC.GenericNull ;
        //    Assert.IsTrue(abc.Value == TestGenericEnumABC.GenericNull ,"The value should equal GenericNull");
        //    Console.WriteLine("Value should be Generic null value = " + abc.Value.ToString("G"));
        //    abc.Value = TestGenericEnumClassABC.GenericNotApplicable ;
        //    Assert.IsTrue(abc.Value == TestGenericEnumABC.GenericNotApplicable ,"The value should equal Not Applicable");
        //    Console.WriteLine("Value should be Generic not applicable value = " + abc.Value.ToString("G"));
			
        //}
		
		/// <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()
		{
			TestGenericEnumClassABC abc = new TestGenericEnumClassABC();
			abc.Value = DateTime.Now ;
			Assert.IsTrue(abc.Value == DateTime.Now,"The value should equal now");
			
		}

        /// <summary>
        /// Value not set Exception
        /// </summary>
        [Test(Category = "Assign Value", Description = "Tried to access value when not set.")]
        [ExpectedException(typeof(ApplicationException))]
        public void AccessValueNotAssigned()
        {
            TestGenericEnumClassABC sEnum = new TestGenericEnumClassABC();
            Console.WriteLine("Test value is " + sEnum.Value.ToString());
            //Assert.IsTrue(sEnum.Value == DateTime.Now, "The value should equal now");

        }

        /// <summary>
        /// Check if value set
        /// </summary>
        [Test(Category = "Assign Value", Description = "Check if value set.")]
        public void AccessValueCheckAssigned()
        {
            TestGenericEnumClassABC sEnum = new TestGenericEnumClassABC();
            Assert.IsTrue(sEnum.IsValueSet == false, "The value should not be set.");
            sEnum.Value = TestGenericEnumClassABC.C;
            Assert.IsTrue(sEnum.IsValueSet == true, "The value should be set.");


        }

        /// <summary>
        /// Create multiple generic types
        /// </summary>
        [Test(Category = "Declaration", Description = "Create multiple generic instances")]
         public void DeclareMultipleGenerics()
        {
            TestGenericEnumClassABC gEnumABC = new TestGenericEnumClassABC();
            Assert.IsTrue(gEnumABC.IsValueSet == false, "The value should not be set.");
            TestGenericEnumClassXYZ gEnumXYZ = new TestGenericEnumClassXYZ();
            gEnumXYZ.Value = TestGenericEnumClassXYZ.Y;
            Assert.IsTrue(gEnumXYZ.IsValueSet == true, "The value should be set.");
            TestGenericStringEnumClassABC sEnumABC = new TestGenericStringEnumClassABC();
            Assert.IsTrue(sEnumABC.IsValueSet == false, "The value should not be set.");

        }


        /// <summary>
        /// Set value at instantiation
        /// </summary>
        [Test(Category = "Assign Value", Description = "Instantiate with a value.")]
        public void CreateWithValue()
        {
            TestGenericEnumClassABC sEnum = new TestGenericEnumClassABC(TestGenericEnumClassABC.A);
            Console.WriteLine("Test value is " + sEnum.Value.ToString());
            Assert.IsTrue(sEnum.Value == TestGenericEnumClassABC.A, "The value should equal A");

        }

        /// <summary>
        /// Create where set constants value at initialization
        /// </summary>
        [Test(Category = "Declaration", Description = "Set constant values at initialization")]
        public void DeclareInitializeConstants()
        {
            TestGenericStringEnumClassInit gEnumABC = new TestGenericStringEnumClassInit();
            Assert.IsTrue(gEnumABC.IsValueSet == false, "The value should not be set.");
            Assert.IsTrue(TestGenericStringEnumClassInit.A == "A", "The value should be 'A'");

        }


        #region Test Passing to function
        /// <summary>
        /// Declare in function parameter
        /// </summary>
        [Test(Category = "Assign Value", Description = "Pass to parameter on function.")]
        public void PassToFunction()
        {
            TestGenericEnumClassABC sEnum = new TestGenericEnumClassABC(TestGenericEnumClassABC.A);
            Console.WriteLine("Test value is " + sEnum.Value.ToString());
            Assert.IsTrue(TestParam(sEnum) == true, "The value should equal A");
            //Assert.IsFalse(TestParam("A") == true, "The value should equal A"); //won't compile, good
            sEnum.Value = TestGenericEnumClassABC.B;
            Assert.IsTrue(TestParam(sEnum) == false, "The value should equal A");


        }
        
        /// <summary>
        /// Returns true if inGen = TestGenericEnumClassABC.A
        /// </summary>
        /// <param name="inGen"></param>
        /// <returns></returns>
        private bool TestParam(TestGenericEnumClassABC inGen)
        {
            if (inGen.Value == TestGenericEnumClassABC.A)
            {
                return true;
            }
            return false;
        }
        #endregion Passing to Function



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

		/// <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 EnumWithDuplicateDeclaration()
		{
			TestGenericEnumClassDuplicate sEnum = new TestGenericEnumClassDuplicate();
			sEnum.Value = DateTime.Now;
			Assert.IsTrue(sEnum.Value == DateTime.Now,"The value should equal now");
			
		}

        /// <summary>
        /// Makes sure receive exception when have a value that's null
        /// </summary>
        [Test(Category = "Improper Declaration", Description = "Developer created a value with null.")]
        [ExpectedException(typeof(NotSupportedException))]
        public void EnumWithNullDeclaration()
        {
            TestGenericStringEnumClassWithNull gEnum = new TestGenericStringEnumClassWithNull();
            gEnum.Value = TestGenericStringEnumClassWithNull.A;
            Assert.IsTrue(gEnum.Value == TestGenericStringEnumClassWithNull.A, "The value should be 'A'");

        }


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

		
		/// <summary>
		/// Get array of constant names from enumeration
		/// </summary>
		[Test(Category="Get",Description="Get array of Names.")]
		public void GetNamesForABC()
		{
			TestGenericEnumClassABC abc = new TestGenericEnumClassABC();
			//abc.Value = TestGenericEnumClassABC.DateTimeNull ;
			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()
		{
			TestGenericEnumClassABC abc = new TestGenericEnumClassABC();
			//abc.Value = TestGenericEnumClassABC.DateTimeNull ;
			object[,] 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].ToString());
			}
			
		}

		/// <summary>
		/// Get DataTable of Name and Values
		/// </summary>
		[Test(Category="Get",Description="Get Name and Values in DataTable.")]
		public void GetNamedValuesDataTableFromClassABC()
		{
			TestGenericEnumClassABC abc = new TestGenericEnumClassABC();
			//abc.Value = TestGenericEnumClassABC.DateTimeNull ;
			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()
		{
			TestGenericEnumClassABC abc = new TestGenericEnumClassABC();
			abc.Value = TestGenericEnumClassABC.A ;
			Console.WriteLine("Name of value " + TestGenericEnumClassABC.A.ToString("G") + " = " + 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()
		{
			TestGenericEnumClassABC abc = new TestGenericEnumClassABC();
			
			Console.WriteLine("value " + TestGenericEnumClassABC.B.ToString("G")
								+ " has name " + abc.GetNameForValue(TestGenericEnumClassABC.B));
			//Console.WriteLine("value " + TestGenericEnumClassABC.DateTimeNull 
			//	+ " has name " + abc.GetNameForValue(TestGenericEnumClassABC.DateTimeNull));
			
			Assert.AreEqual(abc.GetNameForValue(TestGenericEnumClassABC.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)

Share

About the Author

Tim Schwallie
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.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150819.1 | Last Updated 12 May 2007
Article Copyright 2007 by Tim Schwallie
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid