Sometimes enumerations shall be presented on the UI, that the user can choose one ore more values. And sometimes it would be easy if you had a short hand to execute a piece of code for every value of an enumeration.
Both you can do, with this helper class:
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace Any { /// <summary> /// Contains helper methods for enumeration types /// </summary> public static class Enumeration { #region Value extraction /// <summary> /// Finds all combinations for given enumeration values /// </summary> /// <typeparam name="T"> /// The enumeration type of the given enumeration values /// </typeparam> /// <param name="values"> /// The values of the enumeration, subject to find its values. /// All values must be cast able to type <c>int</c> in order to perform bitwise operations. /// </param> /// <returns> /// The found combinations /// </returns> static IEnumerable<T> FlagCombinations<T>(IEnumerable<T> values) where T : Enum { var combinations = new HashSet<int>(); var uniqueValues = values.Cast<int>() .Distinct() .Where(x => x > 0); foreach (var element in uniqueValues) { int groupCombination = element; foreach (var comparisonElement in uniqueValues .Where(x => x != element)) { var combination = comparisonElement | element; _ = combinations.Add(combination); groupCombination |= combination; _ = combinations.Add(groupCombination); } } return combinations .Cast<T>(); } /// <summary> /// Finds all flag combinations for given enumeration type /// </summary> /// <typeparam name="T"> /// The enumeration type of the given enumeration values /// </typeparam> /// <returns> /// The found combinations /// </returns> public static IEnumerable<T> FlagCombinations<T>() where T : Enum => FlagCombinations(Values<T>(false)); /// <summary> /// Finds all enumeration values for a given enumeration type /// </summary> /// <typeparam name="T"> /// The enumeration type, subject to find its values /// </typeparam> /// <param name="addMissingFlagCombinations"> /// If <c>true</c>, missing value combinations will be added, otherwise not. /// </param> /// <param name="allowDuplicates"> /// If <c>true</c>, value duplicates will remain (from double declaration), otherwise not /// </param> /// <returns> /// A collection of the found enumeration values /// </returns> public static IEnumerable<T> Values<T>(bool addMissingFlagCombinations, bool allowDuplicates) where T : Enum { var targetType = typeof(T); IEnumerable<T> ret; var elements = Enum.GetValues(targetType) .OfType<T>(); if (addMissingFlagCombinations) { ret = FlagCombinations(elements).Where(x => !elements.Any(y => x.Equals(y))).Concat(elements); } else { ret = elements; } return allowDuplicates ? ret : ret.Distinct(); } /// <summary> /// Finds all unique enumeration values for a given enumeration type /// </summary> /// <typeparam name="T"> /// The enumeration type, subject to find its values /// </typeparam> /// <param name="addMissingFlagCombinations"> /// If <c>true</c>, missing value combinations will be added, otherwise not. /// </param> /// <returns> /// A collection of the found enumeration values /// </returns> public static IEnumerable<T> Values<T>(bool addMissingFlagCombinations) where T : Enum => Values<T>(addMissingFlagCombinations, false); /// <summary> /// Finds all unique enumeration values without added flags combination for a given enumeration type /// </summary> /// <typeparam name="T"> /// The enumeration type, subject to find its values /// </typeparam> /// <returns> /// A collection of the found enumeration values /// </returns> public static IEnumerable<T> Values<T>() where T : Enum => Values<T>(false); #endregion #region Code execution /// <summary> /// Executes an action on every enumeration value of a given enumeration type /// </summary> /// <typeparam name="T"> /// The enumeration type, subject to perform actions for its values /// </typeparam> /// <param name="action"> /// The action to perform for each found enumeration value /// </param> /// <param name="allowDuplicates"> /// If <c>true</c>, the action can be performed multiple times in case a value is defined more then once, /// otherwise the action will performed exactly once each value /// </param> /// <param name="addMissingFlagCombinations"> /// If <c>true</c>, not yet existing flag combinations are added to the value list, otherwise not /// </param> public static void ForEach<T>(Action<T> action, bool allowDuplicates, bool addMissingFlagCombinations) where T : Enum { foreach (var element in Values<T>(addMissingFlagCombinations, allowDuplicates)) { action(element); } } /// <summary> /// Executes an action on every defined enumeration value of a given enumeration type /// </summary> /// <typeparam name="T"> /// The enumeration type, subject to perform actions for its values /// </typeparam> /// <param name="action"> /// The action to perform for each found enumeration value /// </param> /// <param name="allowDuplicates"> /// If <c>true</c>, the action can be performed multiple times in case a value is defined more then once, /// otherwise the action will performed exactly once each value /// </param> public static void ForEach<T>(Action<T> action, bool allowDuplicates) where T : Enum => ForEach(action, allowDuplicates, false); /// <summary> /// Executes an action on every unique, defined enumeration value of a given enumeration type /// </summary> /// <typeparam name="T"> /// The enumeration type, subject to perform actions for its values /// </typeparam> /// <param name="action"> /// The action to perform for each found enumeration value /// </param> public static void ForEach<T>(Action<T> action) where T : Enum => ForEach(action, false); #endregion #region UI display /// <summary> /// Retrieves the UI display value for the values of a given enumeration type /// </summary> /// <typeparam name="T"> /// The enumeration type, subject to retrieve its value's UI display value /// </typeparam> /// <param name="elementConnector"> /// The concatenation string, used for duplicated values and built flags combinations /// </param> /// <param name="allowValueDuplicates"> /// If <c>true</c>, value duplicates will remain (from double declaration), otherwise not /// </param> /// <param name="addMissingFlagCombinations"> /// If <c>true</c>, missing value combinations will be added, otherwise not. /// </param> /// <param name="lastElementConnector"> /// The concatenation string for the last element, used for duplicated values and built flags combinations /// </param> /// <returns> /// A collection where <c>key</c> represents the enumeration value, while <c>value</c> represents the UI display value /// </returns> /// <remarks> /// Concatenations of display values are made left to right, in case you want to support right to left cultures, /// please change the code accordingly /// </remarks> public static IEnumerable<KeyValuePair<T, string>> UIDisplays<T>(string elementConnector, bool allowValueDuplicates, bool addMissingFlagCombinations, string lastElementConnector) where T : Enum { var targetType = typeof(T); var ret = new List<KeyValuePair<T, List<string>>>(); var definedElementNames = Enum.GetNames(targetType); // collect the elements defined as code foreach (var elementName in definedElementNames) { var fieldInfo = targetType.GetField(elementName, BindingFlags.Static | BindingFlags.Public); var enumValue = (T)Enum.Parse(targetType, elementName); var names = new List<string>(); if (!allowValueDuplicates) { var foundEntry = ret.FirstOrDefault(x => x.Key.Equals(enumValue)); if (foundEntry.Value == null) { ret.Add(new KeyValuePair<T, List<string>>(enumValue, names)); } names = foundEntry.Value ?? names; } else { ret.Add(new KeyValuePair<T, List<string>>(enumValue, names)); } var displayAttribute = (DisplayAttribute)fieldInfo.GetCustomAttribute(typeof(DisplayAttribute), true); names.Add(displayAttribute != null ? displayAttribute.GetName() : fieldInfo.Name); } // collect the flag combinations if (addMissingFlagCombinations) { IEnumerable<T> realElements = definedElementNames.Select(x => (T)Enum.Parse(targetType, x)); foreach (var element in FlagCombinations<T>() .Where(x => !ret.Any(y => y.Key.Equals(x)))) { IEnumerable<string> flaggedNames = new List<string>(); foreach (var realElement in realElements .Where(x => !x.Equals(default(T)) && element.HasFlag(x))) { foreach (var nameList in ret.Where(x => x.Key.Equals(realElement)).Select(x => x.Value)) { flaggedNames = flaggedNames.Concat(nameList); } } ret.Add(new KeyValuePair<T, List<string>>(element, flaggedNames.Distinct().ToList())); } } return ret .Select(x => { return new KeyValuePair<T, string>(x.Key, string.Join(lastElementConnector ?? elementConnector, new string[] { (x.Value.Count > 1 ? string.Join(elementConnector, x.Value.Take(x.Value.Count - 1)) : null), x.Value.Last() }.Where(y => y != null).ToList())); }); } /// <summary> /// Retrieves the UI display value for the values of a given enumeration type /// </summary> /// <typeparam name="T"> /// The enumeration type, subject to retrieve its value's UI display value /// </typeparam> /// <param name="elementConnector"> /// The concatenation string, used for duplicated values and built flags combinations /// </param> /// <param name="allowValueDuplicates"> /// If <c>true</c>, value duplicates will remain (from double declaration), otherwise not /// </param> /// <param name="addMissingFlagCombinations"> /// If <c>true</c>, missing value combinations will be added, otherwise not. /// </param> /// <returns> /// A collection where <c>key</c> represents the enumeration value, while <c>value</c> represents the UI display value /// </returns> /// <remarks> /// Concatenations of display values are made left to right, in case you want to support right to left cultures, /// please change the code accordingly /// </remarks> public static IEnumerable<KeyValuePair<T, string>> UIDisplays<T>(string elementConnector, bool allowValueDuplicates, bool addMissingFlagCombinations) where T : Enum => UIDisplays<T>(elementConnector, allowValueDuplicates, addMissingFlagCombinations, null); /// <summary> /// Retrieves the UI display value for the defined values of a given enumeration type /// </summary> /// <typeparam name="T"> /// The enumeration type, subject to retrieve its value's UI display value /// </typeparam> /// <param name="elementConnector"> /// The concatenation string, used for duplicated values and built flags combinations /// </param> /// <param name="allowValueDuplicates"> /// If <c>true</c>, value duplicates will remain (from double declaration), otherwise not /// </param> /// <returns> /// A collection where <c>key</c> represents the enumeration value, while <c>value</c> represents the UI display value /// </returns> /// <remarks> /// Concatenations of display values are made left to right, in case you want to support right to left cultures, /// please change the code accordingly /// </remarks> public static IEnumerable<KeyValuePair<T, string>> UIDisplays<T>(string elementConnector, bool allowValueDuplicates) where T : Enum => UIDisplays<T>(elementConnector, allowValueDuplicates, false, null); /// <summary> /// Retrieves the UI display value for the unique, defined values of a given enumeration type /// </summary> /// <typeparam name="T"> /// The enumeration type, subject to retrieve its value's UI display value /// </typeparam> /// <param name="elementConnector"> /// The concatenation string, used for duplicated values and built flags combinations /// </param> /// <returns> /// A collection where <c>key</c> represents the enumeration value, while <c>value</c> represents the UI display value /// </returns> /// <remarks> /// Concatenations of display values are made left to right, in case you want to support right to left cultures, /// please change the code accordingly /// </remarks> public static IDictionary<T, string> UIDisplays<T>(string elementConnector) where T : Enum => UIDisplays<T>(elementConnector, false) .ToDictionary(x => x.Key, x => x.Value); #endregion } }
Use the UIDisplays(...) method to retrieve a enumerations display values
Use the ForEach(...) method to execute any piece of code for the enumeration's values
The UI displays are retrieved either by the DisplayAttribute from the Component Model name space if available, and if not, the field name is taken instead. It automaticly builds combined display names for flags combination and if needed, for value duplicates. Whenever dealing with falg combination options (in case your enumeration acts as a bit mask), make sure the enumeration values are type of integer - otherwise falg combinations cannot be calculated properly and under circumstances, the code will raise runtime exceptions.
There are two extra methods which are needed anyway for the ForEach and UIDisplay, so they are basically a free extra:
Values(...) brings you all enumeration values
FlagCombinations() brings you all possible combinations according to the enumerations defined values
Each of the provided methods, except the FlagCombination has some overloads. It's late and therefore I left out examples, but I think, the documentation should explain what you have to expect, when calling the methods. At least, you can have an idea, when studying the test class:
using Microsoft.VisualStudio.TestTools.UnitTesting; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; using Testee = Any.Enumeration; namespace AnyTest { /// <summary> /// Tests the functionality of class <c>Any.Enumeration</c> /// </summary> [TestClass] public class Enumeration { enum TestEnum { Element1, Element2, Element3, Element4 = 8, Element5 = 16, Element6 = 32, Element5And6 = Element5 | Element6, Element4And5 = Element4 | Element5, ElementX = Element2 } enum FlagEnum { [Display(Name = "0")] NoFlag, [Display(Name = "1")] Flag1 = 1, [Display(Name = "2")] Flag2 = 2, [Display(Name = "3")] Flag3 = 4, [Display(Name = "D")] DuplicateFlag = Flag1 } #region Values [TestMethod] public void TestValuesCoded() { var list = Testee.Values<TestEnum>(); Assert.AreEqual(8, list.Count()); Assert.IsTrue(list.Contains(TestEnum.Element1)); Assert.IsTrue(list.Contains(TestEnum.Element2)); Assert.IsTrue(list.Contains(TestEnum.Element3)); Assert.IsTrue(list.Contains(TestEnum.Element4)); Assert.IsTrue(list.Contains(TestEnum.Element5)); Assert.IsTrue(list.Contains(TestEnum.Element6)); Assert.IsTrue(list.Contains(TestEnum.Element5And6)); Assert.IsTrue(list.Contains(TestEnum.Element4And5)); } [TestMethod] public void TestValuesWithFlagCombinations() { var list = Testee.Values<FlagEnum>(true); Assert.AreEqual(8, list.Count()); Assert.IsTrue(list.Contains(FlagEnum.NoFlag)); Assert.IsTrue(list.Contains(FlagEnum.Flag1)); Assert.IsTrue(list.Contains(FlagEnum.Flag2)); Assert.IsTrue(list.Contains(FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag2)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag2 | FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag2 | FlagEnum.Flag3)); } [TestMethod] public void TestValuesCodedWithDuplicates() { var list = Testee.Values<TestEnum>(false, true); Assert.AreEqual(9, list.Count()); Assert.IsTrue(list.Contains(TestEnum.Element1)); Assert.AreEqual(2, list.Count(x => x == TestEnum.Element2)); Assert.IsTrue(list.Contains(TestEnum.Element3)); Assert.IsTrue(list.Contains(TestEnum.Element4)); Assert.IsTrue(list.Contains(TestEnum.Element5)); Assert.IsTrue(list.Contains(TestEnum.Element6)); Assert.IsTrue(list.Contains(TestEnum.Element5And6)); Assert.IsTrue(list.Contains(TestEnum.Element4And5)); } [TestMethod] public void TestValuesCodedWithDuplicatesAndFlagCombinations() { var list = Testee.Values<FlagEnum>(true, true); Assert.AreEqual(9, list.Count()); Assert.IsTrue(list.Contains(FlagEnum.NoFlag)); Assert.AreEqual(2, list.Count(x => x == FlagEnum.Flag1)); Assert.IsTrue(list.Contains(FlagEnum.Flag2)); Assert.IsTrue(list.Contains(FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag2)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag2 | FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag2 | FlagEnum.Flag3)); } #endregion [TestMethod] public void FlagCombinations() { var list = Testee.FlagCombinations<FlagEnum>(); Assert.AreEqual(4, list.Count()); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag2)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag2 | FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag2 | FlagEnum.Flag3)); } #region For each [TestMethod] public void ForEach() { var list = new List<FlagEnum>(); Testee.ForEach<FlagEnum>(x => list.Add(x)); Assert.AreEqual(4, list.Count()); Assert.IsTrue(list.Contains(FlagEnum.NoFlag)); Assert.IsTrue(list.Contains(FlagEnum.Flag1)); Assert.IsTrue(list.Contains(FlagEnum.Flag2)); Assert.IsTrue(list.Contains(FlagEnum.Flag3)); } [TestMethod] public void ForEachWithDuplicates() { var list = new List<FlagEnum>(); Testee.ForEach<FlagEnum>(x => list.Add(x), true); Assert.AreEqual(5, list.Count()); Assert.IsTrue(list.Contains(FlagEnum.NoFlag)); Assert.IsTrue(list.Contains(FlagEnum.Flag1)); Assert.IsTrue(list.Contains(FlagEnum.Flag2)); Assert.IsTrue(list.Contains(FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.DuplicateFlag)); } [TestMethod] public void ForEachWithFlagCombinations() { var list = new List<FlagEnum>(); Testee.ForEach<FlagEnum>(x => list.Add(x), false, true); Assert.AreEqual(8, list.Count()); Assert.IsTrue(list.Contains(FlagEnum.NoFlag)); Assert.IsTrue(list.Contains(FlagEnum.Flag1)); Assert.IsTrue(list.Contains(FlagEnum.Flag2)); Assert.IsTrue(list.Contains(FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag2)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag2 | FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag2 | FlagEnum.Flag3)); } [TestMethod] public void ForEachWithDuplicatesAndFlagCombinations() { var list = new List<FlagEnum>(); Testee.ForEach<FlagEnum>(x => list.Add(x), true, true); Assert.AreEqual(9, list.Count()); Assert.IsTrue(list.Contains(FlagEnum.NoFlag)); Assert.IsTrue(list.Contains(FlagEnum.Flag1)); Assert.IsTrue(list.Contains(FlagEnum.Flag2)); Assert.IsTrue(list.Contains(FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag2)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag1 | FlagEnum.Flag2 | FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.Flag2 | FlagEnum.Flag3)); Assert.IsTrue(list.Contains(FlagEnum.DuplicateFlag)); } #endregion #region UI displays [TestMethod] public void UIDisplays() { var list = Testee.UIDisplays<FlagEnum>("+"); Assert.AreEqual(4, list.Count()); Assert.IsTrue(list[FlagEnum.NoFlag] == "0"); Assert.IsTrue(list[FlagEnum.Flag1] == "1+D" || list[FlagEnum.Flag1] == "D+1"); Assert.IsTrue(list[FlagEnum.Flag2] == "2"); Assert.IsTrue(list[FlagEnum.Flag3] == "3"); } [TestMethod] public void UIDisplaysAllowDuplicates() { var list = Testee.UIDisplays<FlagEnum>("+", true); Assert.AreEqual(5, list.Count()); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.NoFlag && x.Value == "0")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag1 && x.Value == "1")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag2 && x.Value == "2")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag3 && x.Value == "3")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.DuplicateFlag && x.Value == "D")); } [TestMethod] public void UIDisplaysFlagCombinations() { var list = Testee.UIDisplays<FlagEnum>("+", false, true); Assert.AreEqual(8, list.Count()); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.NoFlag && x.Value == "0")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag1 && (x.Value == "1+D" || x.Value == "D+1"))); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag2 && x.Value == "2")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag3 && x.Value == "3")); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag1 | FlagEnum.Flag2) && (x.Value == "1+D+2" || x.Value == "2+1+D" || x.Value == "D+1+2" || x.Value == "D+2+1" || x.Value == "1+D+2" || x.Value == "2+D+1"))); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag1 | FlagEnum.Flag3) && (x.Value == "1+3+D" || x.Value == "3+1+D" || x.Value == "D+3+1" || x.Value == "D+1+3" || x.Value == "3+D+1" || x.Value == "1+D+3"))); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag2 | FlagEnum.Flag3) && (x.Value == "3+2" || x.Value == "2+3"))); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag1 | FlagEnum.Flag2 | FlagEnum.Flag3) && (x.Value == "1+2+3+D" || x.Value == "1+3+2+D" || x.Value == "3+1+2+D" || x.Value == "3+2+1+D" || x.Value == "2+1+3+D" || x.Value == "2+3+1+D" || x.Value == "D+2+3+1" || x.Value == "D+3+2+1" || x.Value == "D+1+2+3" || x.Value == "D+2+1+3" || x.Value == "D+1+3+2" || x.Value == "D+3+1+2" || x.Value == "2+D+3+1" || x.Value == "3+D+2+1" || x.Value == "1+D+2+3" || x.Value == "2+D+1+3" || x.Value == "1+D+3+2" || x.Value == "3+D+1+2" || x.Value == "3+2+D+1" || x.Value == "2+3+D+1" || x.Value == "3+1+D+3" || x.Value == "1+2+D+3" || x.Value == "3+1+D+2" || x.Value == "1+3+D+2"))); } [TestMethod] public void UIDisplaysFlagCombinationsAndAllowDuplicates() { var list = Testee.UIDisplays<FlagEnum>("+", true, true); Assert.AreEqual(9, list.Count()); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.NoFlag && x.Value == "0")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag1 && x.Value == "1")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag2 && x.Value == "2")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag3 && x.Value == "3")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.DuplicateFlag && x.Value == "D")); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag1 | FlagEnum.Flag2) && (x.Value == "1+D+2" || x.Value == "2+1+D" || x.Value == "D+1+2" || x.Value == "D+2+1" || x.Value == "1+D+2" || x.Value == "2+D+1"))); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag1 | FlagEnum.Flag3) && (x.Value == "1+3+D" || x.Value == "3+1+D" || x.Value == "D+3+1" || x.Value == "D+1+3" || x.Value == "3+D+1" || x.Value == "1+D+3"))); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag2 | FlagEnum.Flag3) && (x.Value == "3+2" || x.Value == "2+3"))); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag1 | FlagEnum.Flag2 | FlagEnum.Flag3) && (x.Value == "1+2+3+D" || x.Value == "1+3+2+D" || x.Value == "3+1+2+D" || x.Value == "3+2+1+D" || x.Value == "2+1+3+D" || x.Value == "2+3+1+D" || x.Value == "D+2+3+1" || x.Value == "D+3+2+1" || x.Value == "D+1+2+3" || x.Value == "D+2+1+3" || x.Value == "D+1+3+2" || x.Value == "D+3+1+2" || x.Value == "2+D+3+1" || x.Value == "3+D+2+1" || x.Value == "1+D+2+3" || x.Value == "2+D+1+3" || x.Value == "1+D+3+2" || x.Value == "3+D+1+2" || x.Value == "3+2+D+1" || x.Value == "2+3+D+1" || x.Value == "3+1+D+3" || x.Value == "1+2+D+3" || x.Value == "3+1+D+2" || x.Value == "1+3+D+2"))); } [TestMethod] public void UIDisplaysAlternativeEndConcatenation() { var list = Testee.UIDisplays<FlagEnum>("+", true, true, "-"); Assert.AreEqual(9, list.Count()); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.NoFlag && x.Value == "0")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag1 && x.Value == "1")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag2 && x.Value == "2")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.Flag3 && x.Value == "3")); Assert.IsTrue(list.Any(x => x.Key == FlagEnum.DuplicateFlag && x.Value == "D")); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag1 | FlagEnum.Flag2) && (x.Value == "1+D-2" || x.Value == "2+1-D" || x.Value == "D+1-2" || x.Value == "D+2-1" || x.Value == "1+D-2" || x.Value == "2+D-1"))); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag1 | FlagEnum.Flag3) && (x.Value == "1+3-D" || x.Value == "3+1-D" || x.Value == "D+3-1" || x.Value == "D+1-3" || x.Value == "3+D-1" || x.Value == "1+D-3"))); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag2 | FlagEnum.Flag3) && (x.Value == "3-2" || x.Value == "2-3"))); Assert.IsTrue(list.Any(x => x.Key == (FlagEnum.Flag1 | FlagEnum.Flag2 | FlagEnum.Flag3) && (x.Value == "1+2+3-D" || x.Value == "1+3+2-D" || x.Value == "3+1+2-D" || x.Value == "3+2+1-D" || x.Value == "2+1+3-D" || x.Value == "2+3+1-D" || x.Value == "D+2+3-1" || x.Value == "D+3+2-1" || x.Value == "D+1+2-3" || x.Value == "D+2+1-3" || x.Value == "D+1+3-2" || x.Value == "D+3+1-2" || x.Value == "2+D+3-1" || x.Value == "3+D+2-1" || x.Value == "1+D+2-3" || x.Value == "2+D+1-3" || x.Value == "1+D+3-2" || x.Value == "3+D+1-2" || x.Value == "3+2+D-1" || x.Value == "2+3+D-1" || x.Value == "3+1+D-3" || x.Value == "1+2+D-3" || x.Value == "3+1+D-2" || x.Value == "1+3+D-2"))); } #endregion } }
As always - hope you can use it as I can :)