PageRenderTime 60ms CodeModel.GetById 33ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Utilities/Tests/EnumHelperTests.cs

#
C# | 287 lines | 202 code | 33 blank | 52 comment | 1 complexity | 61cfbf581be6f2a7705497f85d13ca89 MD5 | raw file
  1using System;
  2using System.ComponentModel;
  3using Delta.Utilities.Helpers;
  4using Delta.Utilities.Math;
  5using NUnit.Framework;
  6
  7namespace Delta.Utilities.Tests
  8{
  9	/// <summary>
 10	/// Test class for the EnumHelper.
 11	/// </summary>
 12	public class EnumHelperTests
 13	{
 14		#region Helpers
 15		/// <summary>
 16		/// Test enum
 17		/// </summary>
 18		private enum TestEnum
 19		{
 20			[Description("Some entry")]
 21			SomeEntry,
 22
 23			[Description("Another one")]
 24			AnotherOne,
 25
 26			[Description("Hey stop it now")]
 27			HeyStopItNow,
 28
 29			[Description("Okay okay")]
 30			OkayOkay,
 31		}
 32		#endregion
 33
 34		#region Helpers
 35		/// <summary>
 36		/// Test combined enum
 37		/// </summary>
 38		[Flags]
 39		private enum TestCombinedEnum
 40		{
 41			Zero = 0,
 42
 43			NumberOne = 1,
 44
 45			NumberTwo = 2,
 46
 47			NumberFour = 4,
 48		}
 49		#endregion
 50
 51		#region GetNames (Static)
 52		/// <summary>
 53		/// Get names
 54		/// </summary>
 55		[Test]
 56		public static void GetNames()
 57		{
 58			string[] names = EnumHelper.GetNames(typeof(StringSplitOptions));
 59			Assert.Equal(names.Length, 2);
 60			Assert.Equal(names[0], "None");
 61			Assert.Equal(names[1], "RemoveEmptyEntries");
 62
 63			string[] genericNames = EnumHelper.GetNames<StringSplitOptions>();
 64			Assert.Equal(genericNames.Length, 2);
 65			Assert.Equal(genericNames[0], "None");
 66			Assert.Equal(genericNames[1], "RemoveEmptyEntries");
 67		}
 68		#endregion
 69
 70		#region TestEnumStuff (LongRunning)
 71		/// <summary>
 72		/// Test enum stuff. Note: Too slow for a dynamic unit test.
 73		/// </summary>
 74		[Test]
 75		[NUnit.Framework.Category("LongRunning")]
 76		public static void TestEnumStuff()
 77		{
 78			// First get size of TestEnum (with type of enum)
 79			Assert.Equal(EnumHelper.GetCount<TestEnum>(), 4);
 80
 81			// Now try with some enum member (should give the same result)
 82			Assert.Equal(EnumHelper.GetCount(TestEnum.SomeEntry), 4);
 83
 84			// Test with extension
 85			Assert.Equal(TestEnum.SomeEntry.GetCount(), 4);
 86			Assert.Equal(TestEnum.SomeEntry.GetCount(), 4);
 87
 88			// Try to get enum from name
 89			Assert.Equal(TestEnum.OkayOkay, EnumHelper.ToEnum<TestEnum>("OkayOkay"));
 90
 91			// Get description of an enum members
 92			Assert.Equal(TestEnum.OkayOkay.GetEnumDescription(),
 93				"Okay okay");
 94			Assert.Equal(TestEnum.OkayOkay.Description(), "Okay okay");
 95
 96			// Test enum enumerator stuff
 97			string allEnumNames = EnumHelper.GetEnumerator(typeof(TestEnum)).Write();
 98			Assert.Equal("SomeEntry, AnotherOne, HeyStopItNow, OkayOkay",
 99				allEnumNames);
100
101			// Test same with description values
102			string allEnumDescNames = "";
103			foreach (TestEnum enumValue in
104				EnumHelper.GetEnumerator(typeof(TestEnum)))
105			{
106				allEnumDescNames +=
107					(allEnumDescNames.Length == 0
108					 	? ""
109					 	: ", ") +
110					enumValue.GetEnumDescription();
111			}
112			Assert.Equal(
113				"Some entry, Another one, Hey stop it now, Okay okay",
114				allEnumDescNames);
115
116			// Same check with GetAllEnumDescriptions
117			Assert.Equal(
118				"Some entry, Another one, Hey stop it now, Okay okay",
119				EnumHelper.GetAllEnumDescriptions(typeof(TestEnum)).Write());
120		}
121		#endregion
122
123		#region TestNextAndPreviousEnum (LongRunning)
124		/// <summary>
125		/// Test next and previous enum. Note: Too slow for a dynamic unit test.
126		/// </summary>
127		[Test]
128		[NUnit.Framework.Category("LongRunning")]
129		public static void TestNextAndPreviousEnum()
130		{
131			TestEnum someEnum = TestEnum.SomeEntry;
132			Assert.Equal(TestEnum.SomeEntry, someEnum);
133
134			// Check next entry
135			someEnum = (TestEnum)someEnum.SelectNextEnum();
136			Assert.Equal(TestEnum.AnotherOne, someEnum);
137			Assert.Equal(TestEnum.AnotherOne, TestEnum.SomeEntry.Next());
138
139			// Go back 2 entries, this will select the last enum entry
140			someEnum = (TestEnum)someEnum.SelectPreviousEnum();
141			someEnum = (TestEnum)someEnum.SelectPreviousEnum();
142			Assert.Equal(TestEnum.OkayOkay, someEnum);
143			Assert.Equal(TestEnum.AnotherOne, TestEnum.HeyStopItNow.Previous());
144		}
145		#endregion
146
147		#region GetEnumFromName (LongRunning)
148		/// <summary>
149		/// Get enum from name
150		/// </summary>
151		[Test]
152		[NUnit.Framework.Category("LongRunning")]
153		public void GetEnumFromName()
154		{
155			Assert.Equal(TestEnum.OkayOkay,
156			  EnumHelper.ToEnum<TestEnum>("OkayOkay"));
157			Assert.Equal(TestCombinedEnum.NumberFour,
158			  EnumHelper.ToEnum<TestCombinedEnum>("NumberFour"));
159		}
160		#endregion
161
162		#region GetValues (LongRunning)
163		/// <summary>
164		/// Get values
165		/// </summary>
166		[Test]
167		[NUnit.Framework.Category("LongRunning")]
168		public static void GetValues()
169		{
170			object[] values = typeof(StringSplitOptions).GetValues();
171			Assert.Equal(values.Length, 2);
172			Assert.Equal(values[0], StringSplitOptions.None);
173			Assert.Equal(values[1], StringSplitOptions.RemoveEmptyEntries);
174
175			StringSplitOptions[] genericValues =
176				EnumHelper.GetValues<StringSplitOptions>();
177			Assert.Equal(genericValues.Length, 2);
178			Assert.Equal(genericValues[0], StringSplitOptions.None);
179			Assert.Equal(genericValues[1], StringSplitOptions.RemoveEmptyEntries);
180		}
181		#endregion
182
183		#region GetCount
184		/// <summary>
185		/// Get count
186		/// </summary>
187		[Test]
188		public void GetCount()
189		{
190			Assert.Equal(7, EnumHelper.GetCount<DayOfWeek>());
191		}
192		#endregion
193
194		#region NextValue
195		/// <summary>
196		/// Next value
197		/// </summary>
198		[Test]
199		public void NextValue()
200		{
201			Assert.Equal(DayOfWeek.Monday, DayOfWeek.Sunday.NextValue());
202		}
203		#endregion
204
205		#region ConvertToNumber
206		/// <summary>
207		/// Convert to number
208		/// </summary>
209		[Test]
210		public void ConvertToNumber()
211		{
212			Assert.Equal(0,
213			  EnumHelper.ToInt(TestCombinedEnum.Zero));
214			Assert.Equal(2,
215				EnumHelper.ToInt(TestCombinedEnum.NumberTwo));
216
217			Assert.Equal(4,
218				EnumHelper.ToInt(TestCombinedEnum.NumberFour));
219			Assert.Equal(6, EnumHelper.ToInt(
220				TestCombinedEnum.NumberTwo | TestCombinedEnum.NumberFour));
221			Assert.Equal(TestCombinedEnum.NumberFour,
222				EnumHelper.ToEnum<TestCombinedEnum>("4"));
223			
224			Assert.Equal(TestCombinedEnum.NumberTwo | TestCombinedEnum.NumberFour,
225				EnumHelper.ToEnum<TestCombinedEnum>("6"));
226		}
227		#endregion
228
229		#region ConvertToNumberWithCombinedEnum
230		/// <summary>
231		/// Convert to number
232		/// </summary>
233		[Test]
234		public void ConvertToNumberWithCombinedEnum()
235		{
236			// Reproduce test with standard methods
237			Assert.Equal(4, (int)"NumberFour".ToEnum<TestCombinedEnum>());
238			Assert.Equal(6,
239				(int)"NumberTwo, NumberFour".ToEnum<TestCombinedEnum>());
240			Assert.Equal(
241				(int)(TestCombinedEnum.NumberTwo | TestCombinedEnum.NumberFour),
242				(int)"6".ToEnum<TestCombinedEnum>());
243			Assert.Equal("NumberTwo, NumberFour",
244				(TestCombinedEnum.NumberTwo |
245				 TestCombinedEnum.NumberFour).ToString());
246			Assert.Equal(TestCombinedEnum.Zero, "bla".ToEnum<TestCombinedEnum>());
247			Assert.Equal("Zero", TestCombinedEnum.Zero.ToString());
248
249			Assert.Equal(0,
250				(int)(TestCombinedEnum.Zero.ToString().
251							ToEnum<TestCombinedEnum>()));
252			Assert.Equal(
253				(int)(TestCombinedEnum.NumberTwo |
254							TestCombinedEnum.NumberFour),
255				(int)(TestCombinedEnum.NumberTwo |
256							TestCombinedEnum.NumberFour.ToString().
257								ToEnum<TestCombinedEnum>()));
258
259			// Finally write down a combined enum.
260			TestCombinedEnum combinedValue =
261				TestCombinedEnum.NumberTwo |
262				TestCombinedEnum.NumberFour;
263			Assert.Equal("NumberTwo, NumberFour", combinedValue.ToString());
264			Assert.Equal(combinedValue.ToStringArray(), new string[]
265				{
266					"NumberTwo", "NumberFour"
267				});
268			// This can be useful to build a combined enum name as a filename!
269			Assert.Equal(combinedValue.ToStringArray().Write("_"),
270				"NumberTwo_NumberFour");
271			Assert.Equal(combinedValue,
272				combinedValue.ToStringArray().ToEnum<TestCombinedEnum>());
273		}
274		#endregion
275
276		#region ToBlablabla
277		/// <summary>
278		/// To string
279		/// </summary>
280		[Test]
281		public void ToBlablabla()
282		{
283			Assert.Equal("5 + i * 2", new Complex(5.0f, 2.0f).ToString());
284		}
285		#endregion
286	}
287}