mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-16 05:05:05 +00:00
Merge branch 'dev' of https://cs-gogs.icdpf.net/Common/Utils into dev
This commit is contained in:
@@ -7,6 +7,12 @@ namespace ICD.Common.Utils.Tests.Extensions
|
|||||||
{
|
{
|
||||||
[Test]
|
[Test]
|
||||||
public void GetPathTest()
|
public void GetPathTest()
|
||||||
|
{
|
||||||
|
Assert.Inconclusive();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void GetCreationTimeTest()
|
||||||
{
|
{
|
||||||
Assert.Inconclusive();
|
Assert.Inconclusive();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,61 +5,112 @@ using NUnit.Framework;
|
|||||||
|
|
||||||
namespace ICD.Common.Utils.Tests.Extensions
|
namespace ICD.Common.Utils.Tests.Extensions
|
||||||
{
|
{
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public sealed class TypeExtensionsTest
|
public sealed class TypeExtensionsTest
|
||||||
{
|
{
|
||||||
[Test]
|
[TestCase(typeof(byte), true)]
|
||||||
public void IsAssignableToTest()
|
[TestCase(typeof(decimal), true)]
|
||||||
{
|
[TestCase(typeof(double), true)]
|
||||||
Assert.IsTrue(typeof(string).IsAssignableTo(typeof(object)));
|
[TestCase(typeof(float), true)]
|
||||||
Assert.IsFalse(typeof(object).IsAssignableTo(typeof(string)));
|
[TestCase(typeof(int), true)]
|
||||||
}
|
[TestCase(typeof(long), true)]
|
||||||
|
[TestCase(typeof(sbyte), true)]
|
||||||
|
[TestCase(typeof(short), true)]
|
||||||
|
[TestCase(typeof(uint), true)]
|
||||||
|
[TestCase(typeof(ulong), true)]
|
||||||
|
[TestCase(typeof(ushort), true)]
|
||||||
|
[TestCase(typeof(string), false)]
|
||||||
|
public void IsNumericTest(Type value, bool expected)
|
||||||
|
{
|
||||||
|
Assert.AreEqual(expected, value.IsNumeric());
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[TestCase(typeof(byte), false)]
|
||||||
public void GetAllTypesTest()
|
[TestCase(typeof(decimal), true)]
|
||||||
{
|
[TestCase(typeof(double), true)]
|
||||||
Type[] allTypes = typeof(B).GetAllTypes().ToArray();
|
[TestCase(typeof(float), true)]
|
||||||
|
[TestCase(typeof(int), true)]
|
||||||
|
[TestCase(typeof(long), true)]
|
||||||
|
[TestCase(typeof(sbyte), true)]
|
||||||
|
[TestCase(typeof(short), true)]
|
||||||
|
[TestCase(typeof(uint), false)]
|
||||||
|
[TestCase(typeof(ulong), false)]
|
||||||
|
[TestCase(typeof(ushort), false)]
|
||||||
|
[TestCase(typeof(string), false)]
|
||||||
|
public void IsSignedNumericTest(Type value, bool expected)
|
||||||
|
{
|
||||||
|
Assert.AreEqual(expected, value.IsSignedNumeric());
|
||||||
|
}
|
||||||
|
|
||||||
Assert.AreEqual(6, allTypes.Length);
|
[TestCase(typeof(byte), false)]
|
||||||
|
[TestCase(typeof(decimal), true)]
|
||||||
|
[TestCase(typeof(double), true)]
|
||||||
|
[TestCase(typeof(float), true)]
|
||||||
|
[TestCase(typeof(int), false)]
|
||||||
|
[TestCase(typeof(long), false)]
|
||||||
|
[TestCase(typeof(sbyte), false)]
|
||||||
|
[TestCase(typeof(short), false)]
|
||||||
|
[TestCase(typeof(uint), false)]
|
||||||
|
[TestCase(typeof(ulong), false)]
|
||||||
|
[TestCase(typeof(ushort), false)]
|
||||||
|
[TestCase(typeof(string), false)]
|
||||||
|
public void IsDecimalNumericTest(Type value, bool expected)
|
||||||
|
{
|
||||||
|
Assert.AreEqual(expected, value.IsDecimalNumeric());
|
||||||
|
}
|
||||||
|
|
||||||
Assert.IsTrue(allTypes.Contains(typeof(E)));
|
[TestCase(typeof(string), typeof(object), true)]
|
||||||
Assert.IsTrue(allTypes.Contains(typeof(D)));
|
[TestCase(typeof(object), typeof(string), false)]
|
||||||
Assert.IsTrue(allTypes.Contains(typeof(C)));
|
public void IsAssignableToTest(Type a, Type b, bool expected)
|
||||||
Assert.IsTrue(allTypes.Contains(typeof(B)));
|
{
|
||||||
Assert.IsTrue(allTypes.Contains(typeof(A)));
|
Assert.AreEqual(expected, a.IsAssignableTo(b));
|
||||||
Assert.IsTrue(allTypes.Contains(typeof(object)));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void GetBaseTypesTest()
|
public void GetAllTypesTest()
|
||||||
{
|
{
|
||||||
Type[] baseTypes = typeof(B).GetBaseTypes().ToArray();
|
Type[] allTypes = typeof(B).GetAllTypes().ToArray();
|
||||||
|
|
||||||
Assert.AreEqual(2, baseTypes.Length);
|
Assert.AreEqual(6, allTypes.Length);
|
||||||
|
|
||||||
Assert.IsFalse(baseTypes.Contains(typeof(B)));
|
Assert.IsTrue(allTypes.Contains(typeof(E)));
|
||||||
Assert.IsTrue(baseTypes.Contains(typeof(A)));
|
Assert.IsTrue(allTypes.Contains(typeof(D)));
|
||||||
Assert.IsTrue(baseTypes.Contains(typeof(object)));
|
Assert.IsTrue(allTypes.Contains(typeof(C)));
|
||||||
}
|
Assert.IsTrue(allTypes.Contains(typeof(B)));
|
||||||
|
Assert.IsTrue(allTypes.Contains(typeof(A)));
|
||||||
|
Assert.IsTrue(allTypes.Contains(typeof(object)));
|
||||||
|
}
|
||||||
|
|
||||||
private interface C
|
[Test]
|
||||||
{
|
public void GetBaseTypesTest()
|
||||||
}
|
{
|
||||||
|
Type[] baseTypes = typeof(B).GetBaseTypes().ToArray();
|
||||||
|
|
||||||
private interface D
|
Assert.AreEqual(2, baseTypes.Length);
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private interface E : C, D
|
Assert.IsFalse(baseTypes.Contains(typeof(B)));
|
||||||
{
|
Assert.IsTrue(baseTypes.Contains(typeof(A)));
|
||||||
}
|
Assert.IsTrue(baseTypes.Contains(typeof(object)));
|
||||||
|
}
|
||||||
|
|
||||||
private class A
|
private interface C
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
private class B : A, E
|
private interface D
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
private interface E : C, D
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private class A
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private class B : A, E
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
public static string GetPath(this Assembly extends)
|
public static string GetPath(this Assembly extends)
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
throw new ArgumentNullException();
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
string path = extends
|
string path = extends
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
@@ -43,5 +43,19 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
|
|
||||||
return IcdFile.Exists(path) ? path : null;
|
return IcdFile.Exists(path) ? path : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the creation date of the given assembly.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static DateTime GetCreationTime(this Assembly extends)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentNullException("extends");
|
||||||
|
|
||||||
|
string path = extends.GetPath();
|
||||||
|
return path == null ? DateTime.MinValue : IcdFile.GetCreationTime(path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using ICD.Common.Utils.Collections;
|
||||||
#if SIMPLSHARP
|
#if SIMPLSHARP
|
||||||
using Crestron.SimplSharp.Reflection;
|
using Crestron.SimplSharp.Reflection;
|
||||||
#else
|
#else
|
||||||
@@ -10,6 +11,78 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
{
|
{
|
||||||
public static class TypeExtensions
|
public static class TypeExtensions
|
||||||
{
|
{
|
||||||
|
private static readonly IcdHashSet<Type> s_NumericTypes = new IcdHashSet<Type>
|
||||||
|
{
|
||||||
|
typeof(byte),
|
||||||
|
typeof(decimal),
|
||||||
|
typeof(double),
|
||||||
|
typeof(float),
|
||||||
|
typeof(int),
|
||||||
|
typeof(long),
|
||||||
|
typeof(sbyte),
|
||||||
|
typeof(short),
|
||||||
|
typeof(uint),
|
||||||
|
typeof(ulong),
|
||||||
|
typeof(ushort)
|
||||||
|
};
|
||||||
|
|
||||||
|
private static readonly IcdHashSet<Type> s_SignedNumericTypes = new IcdHashSet<Type>
|
||||||
|
{
|
||||||
|
typeof(decimal),
|
||||||
|
typeof(double),
|
||||||
|
typeof(float),
|
||||||
|
typeof(int),
|
||||||
|
typeof(long),
|
||||||
|
typeof(sbyte),
|
||||||
|
typeof(short),
|
||||||
|
};
|
||||||
|
|
||||||
|
private static readonly IcdHashSet<Type> s_DecimalNumericTypes = new IcdHashSet<Type>
|
||||||
|
{
|
||||||
|
typeof(decimal),
|
||||||
|
typeof(double),
|
||||||
|
typeof(float),
|
||||||
|
};
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if the given type is a numeric type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsNumeric(this Type extends)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentException("extends");
|
||||||
|
|
||||||
|
return s_NumericTypes.Contains(extends);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if the given type is a signed numeric type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsSignedNumeric(this Type extends)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentException("extends");
|
||||||
|
|
||||||
|
return s_SignedNumericTypes.Contains(extends);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns true if the given type is a non-integer numeric type.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="extends"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static bool IsDecimalNumeric(this Type extends)
|
||||||
|
{
|
||||||
|
if (extends == null)
|
||||||
|
throw new ArgumentException("extends");
|
||||||
|
|
||||||
|
return s_DecimalNumericTypes.Contains(extends);
|
||||||
|
}
|
||||||
|
|
||||||
public static Assembly GetAssembly(this Type extends)
|
public static Assembly GetAssembly(this Type extends)
|
||||||
{
|
{
|
||||||
if (extends == null)
|
if (extends == null)
|
||||||
@@ -68,7 +141,7 @@ namespace ICD.Common.Utils.Extensions
|
|||||||
{
|
{
|
||||||
extends = extends
|
extends = extends
|
||||||
#if !SIMPLSHARP
|
#if !SIMPLSHARP
|
||||||
.GetTypeInfo()
|
.GetTypeInfo()
|
||||||
#endif
|
#endif
|
||||||
.BaseType;
|
.BaseType;
|
||||||
|
|
||||||
|
|||||||
@@ -77,5 +77,11 @@ namespace ICD.Common.Utils.IO
|
|||||||
{
|
{
|
||||||
return new IcdFileStream(File.Open(path, mode));
|
return new IcdFileStream(File.Open(path, mode));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[PublicAPI]
|
||||||
|
public static DateTime GetCreationTime(string path)
|
||||||
|
{
|
||||||
|
return File.GetCreationTime(path);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user