Skip to content

Commit

Permalink
Remove EnumHelper from Runtime and fix some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
geoperez committed Mar 12, 2018
1 parent 61c25a5 commit 1773066
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 53 deletions.
119 changes: 84 additions & 35 deletions src/Unosquare.Swan.Lite/Components/EnumHelper.cs
Expand Up @@ -3,30 +3,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Abstractions;

/// <summary>
/// Provide Enumerations helpers with internal cache
/// </summary>
public class EnumHelper : CacheRepository<Type, Tuple<string, object>>
public class EnumHelper
: SingletonBase<CacheRepository<Type, Tuple<string, object>>>
{
/// <summary>
/// Gets all the names and enumerators from a specific Enum type
/// </summary>
/// <typeparam name="T">The type of the attribute to be retrieved</typeparam>
/// <returns>A tuple of enumarator names and their value stored for the specified type</returns>
public Tuple<string, object>[] Retrieve<T>()
public static Tuple<string, object>[] Retrieve<T>()
where T : struct, IConvertible
{
return Retrieve(typeof(T), () =>
return Instance.Retrieve(typeof(T), () =>
{
var list = new List<Tuple<string, object>>();
var values = Enum.GetValues(typeof(T)).Cast<object>();
foreach (var item in values)
{
list.Add(new Tuple<string, object>(Enum.GetName(typeof(T), item), item));
}
return list;
return values.Select(item => new Tuple<string, object>(Enum.GetName(typeof(T), item), item)).ToList();
});
}

Expand All @@ -39,12 +36,12 @@ public class EnumHelper : CacheRepository<Type, Tuple<string, object>>
/// A collection of Type/Tuple pairs
/// that represents items with the enum item value
/// </returns>
public Tuple<int, string>[] GetItemsWithValue<T>(bool humanize = true)
where T : struct, IConvertible
public static Tuple<int, string>[] GetItemsWithValue<T>(bool humanize = true)
where T : struct, IConvertible
{
return Retrieve<T>()
.Select(x => new Tuple<int, string>((int)x.Item2,humanize ? x.Item1.Humanize() : x.Item1))
.ToArray();
.Select(x => new Tuple<int, string>((int) x.Item2, humanize ? x.Item1.Humanize() : x.Item1))
.ToArray();
}

/// <summary>
Expand All @@ -56,60 +53,112 @@ public class EnumHelper : CacheRepository<Type, Tuple<string, object>>
/// <returns>
/// A list of values in the flag
/// </returns>
public List<int> GetFlagValues<TEnum>(int value, bool ignoreZero = false)
public static List<int> GetFlagValues<TEnum>(int value, bool ignoreZero = false)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.Select(x => (int)x.Item2)
.When(() => ignoreZero, q => q.Where(f => f!= 0))
.Where(x => (x & value) == x)
.ToList();
.Select(x => (int) x.Item2)
.When(() => ignoreZero, q => q.Where(f => f != 0))
.Where(x => (x & value) == x)
.ToList();
}

/// <summary>
/// Gets the flag values.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <returns>A list of values in the flag</returns>
public List<long> GetFlagValues<TEnum>(long value)
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <returns>
/// A list of values in the flag
/// </returns>
public static List<long> GetFlagValues<TEnum>(long value, bool ignoreZero = false)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.Select(x => (long)x.Item2)
.Where(x => (x & value) == x)
.ToList();
.Select(x => (long) x.Item2)
.When(() => ignoreZero, q => q.Where(f => f != 0))
.Where(x => (x & value) == x)
.ToList();
}

/// <summary>
/// Gets the flag values.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <returns>A list of values in the flag</returns>
public List<byte> GetFlagValues<TEnum>(byte value)
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <returns>
/// A list of values in the flag
/// </returns>
public static List<byte> GetFlagValues<TEnum>(byte value, bool ignoreZero = false)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.Select(x => (byte)x.Item2)
.Where(x => (x & value) == x)
.ToList();
.Select(x => (byte) x.Item2)
.When(() => ignoreZero, q => q.Where(f => f != 0))
.Where(x => (x & value) == x)
.ToList();
}

/// <summary>
/// Gets the flag names
/// </summary>
/// <typeparam name="TEnum">The type of the enum</typeparam>
/// <param name="value">the value</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A list of flag names
/// </returns>
public static List<string> GetFlagNames<TEnum>(int value, bool ignoreZero = false, bool humanize = true)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.When(() => ignoreZero, q => q.Where(f => (int) f.Item2 != 0))
.Where(x => ((int) x.Item2 & value) == (int) x.Item2)
.Select(x => humanize ? x.Item1.Humanize() : x.Item1)
.ToList();
}

/// <summary>
/// Gets the flag names.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>A list of flag names</returns>
public List<string> GetFlagNames<TEnum>(int value, bool humanize = false)
/// <returns>
/// A list of flag names
/// </returns>
public static List<string> GetFlagNames<TEnum>(long value, bool ignoreZero = false, bool humanize = true)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.When(() => ignoreZero, q => q.Where(f => (long) f.Item2 != 0))
.Where(x => ((long) x.Item2 & value) == (long) x.Item2)
.Select(x => humanize ? x.Item1.Humanize() : x.Item1)
.ToList();
}

/// <summary>
/// Gets the flag names.
/// </summary>
/// <typeparam name="TEnum">The type of the enum.</typeparam>
/// <param name="value">The value.</param>
/// <param name="ignoreZero">if set to <c>true</c> [ignore zero].</param>
/// <param name="humanize">if set to <c>true</c> [humanize].</param>
/// <returns>
/// A list of flag names
/// </returns>
public static List<string> GetFlagNames<TEnum>(byte value, bool ignoreZero = false, bool humanize = true)
where TEnum : struct, IConvertible
{
return Retrieve<TEnum>()
.Where(x => ((int)x.Item2 & value) == (int)x.Item2)
.Select(x => humanize ? x.Item1.Humanize() : x.Item1)
.ToList();
.When(() => ignoreZero, q => q.Where(f => (byte) f.Item2 != 0))
.Where(x => ((byte) x.Item2 & value) == (byte) x.Item2)
.Select(x => humanize ? x.Item1.Humanize() : x.Item1)
.ToList();
}

/// <summary>
Expand All @@ -120,14 +169,14 @@ public List<string> GetFlagNames<TEnum>(int value, bool humanize = false)
/// <returns>
/// A collection of Type/Tuple pairs that represents items with the enum item value
/// </returns>
public Tuple<int, string>[] GetItemsWithIndex<T>(bool humanize = true)
public static Tuple<int, string>[] GetItemsWithIndex<T>(bool humanize = true)
where T : struct, IConvertible
{
var i = 0;

return Retrieve<T>()
.Select(x => new Tuple<int, string>(i++, humanize ? x.Item1.Humanize() : x.Item1))
.ToArray();
.ToArray();
}
}
}
10 changes: 1 addition & 9 deletions src/Unosquare.Swan.Lite/Runtime.cs
Expand Up @@ -183,15 +183,7 @@ public static bool IsTheOnlyInstance
/// The attribute cache.
/// </value>
public static AttributeCache AttributeCache => _attributeCache.Value;

/// <summary>
/// Gets the enum helper.
/// </summary>
/// <value>
/// The attribute cache.
/// </value>
public static EnumHelper EnumHelper => _enumHelper.Value;


/// <summary>
/// Gets the object validator.
/// </summary>
Expand Down
27 changes: 19 additions & 8 deletions test/Unosquare.Swan.Test/EnumHelperTest.cs
Expand Up @@ -11,7 +11,7 @@ public class GetItemsWithIndex
[Test]
public void WithValidIndexEnum_ReturnsTuple()
{
var items = Runtime.EnumHelper.GetItemsWithIndex<MyEnum>();
var items = EnumHelper.GetItemsWithIndex<MyEnum>();

Assert.AreEqual("(0, One)", items[0].ToString());
Assert.AreEqual("(1, Two)", items[1].ToString());
Expand All @@ -25,7 +25,7 @@ public class GetItemsWithValue
[Test]
public void WithValidValueEnum_ReturnsTuple()
{
var items = Runtime.EnumHelper.GetItemsWithValue<MyEnum>();
var items = EnumHelper.GetItemsWithValue<MyEnum>();

Assert.AreEqual("(1, One)", items[0].ToString());
Assert.AreEqual("(2, Two)", items[1].ToString());
Expand All @@ -36,7 +36,7 @@ public void WithValidValueEnum_ReturnsTuple()
[TestFixture]
public class GetFlagValues
{
[TestCase(MyFlag.None, false, new[] {0})]
[TestCase(MyFlag.NoneOrZero, false, new[] {0})]
[TestCase(MyFlag.One, false, new[] {0, 1})]
[TestCase(MyFlag.Two, false, new[] {0, 2})]
[TestCase(MyFlag.All, false, new[] {0, 1, 2, 3})]
Expand All @@ -47,7 +47,7 @@ public class GetFlagValues
[TestCase(MyFlag.One | MyFlag.Two, true, new[] {1, 2, 3})]
public void WithFlag_ReturnsListofInt(MyFlag val, bool ignoreZero, int[] expected)
{
Assert.AreEqual(expected, Runtime.EnumHelper.GetFlagValues<MyFlag>((int) val, ignoreZero));
Assert.AreEqual(expected, EnumHelper.GetFlagValues<MyFlag>((int) val, ignoreZero));
}

[TestCase(MyFlag2.None, false, new[] {0})]
Expand All @@ -59,23 +59,34 @@ public void WithFlag_ReturnsListofInt(MyFlag val, bool ignoreZero, int[] expecte
[TestCase(MyFlag2.One | MyFlag2.Two, true, new[] {1, 2})]
public void WithFlag2_ReturnsListofInt(MyFlag2 val, bool ignoreZero, int[] expected)
{
Assert.AreEqual(expected, Runtime.EnumHelper.GetFlagValues<MyFlag2>((int) val, ignoreZero));
Assert.AreEqual(expected, EnumHelper.GetFlagValues<MyFlag2>((int) val, ignoreZero));
}

[Test]
public void WithInvalidType_ThrowsArgumentException()
{
Assert.Throws<ArgumentException>(() => Runtime.EnumHelper.GetFlagValues<int>(0));
Assert.Throws<ArgumentException>(() => EnumHelper.GetFlagValues<int>(0));
}
}

[TestFixture]
public class GetFlagNames
{
[TestCase(false, false, "NoneOrZero", "One")]
[TestCase(false, true, "None Or Zero", "One")]
[TestCase(true, false, "One", "Two")]
public void WithFlag_ReturnsListofStrings(bool ignoreZero, bool humanize, string zeroIndexValue, string oneIndexValue)
{
var names = EnumHelper.GetFlagNames<MyFlag>((int)MyFlag.All, ignoreZero, humanize);

Assert.AreEqual(zeroIndexValue, names[0]);
Assert.AreEqual(oneIndexValue, names[1]);
}

[Test]
public void Tes()
public void WithInvalidType_ThrowsArgumentException()
{
var names = Runtime.EnumHelper.GetFlagValues<MyFlag>((int)MyFlag.All);
Assert.Throws<ArgumentException>(() => EnumHelper.GetFlagNames<int>(0));
}
}
}
2 changes: 1 addition & 1 deletion test/Unosquare.Swan.Test/Mocks/JsonMock.cs
Expand Up @@ -138,7 +138,7 @@ public enum MyEnum
[Flags]
public enum MyFlag
{
None = 0,
NoneOrZero = 0,
One = 1,
Two = 2,
All = One | Two,
Expand Down

0 comments on commit 1773066

Please sign in to comment.