mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-02-12 11:15:12 +00:00
Extension methods for determining if a type is numeric
This commit is contained in:
@@ -5,61 +5,112 @@ using NUnit.Framework;
|
||||
|
||||
namespace ICD.Common.Utils.Tests.Extensions
|
||||
{
|
||||
[TestFixture]
|
||||
public sealed class TypeExtensionsTest
|
||||
{
|
||||
[Test]
|
||||
public void IsAssignableToTest()
|
||||
{
|
||||
Assert.IsTrue(typeof(string).IsAssignableTo(typeof(object)));
|
||||
Assert.IsFalse(typeof(object).IsAssignableTo(typeof(string)));
|
||||
}
|
||||
[TestFixture]
|
||||
public sealed class TypeExtensionsTest
|
||||
{
|
||||
[TestCase(typeof(byte), true)]
|
||||
[TestCase(typeof(decimal), true)]
|
||||
[TestCase(typeof(double), true)]
|
||||
[TestCase(typeof(float), true)]
|
||||
[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]
|
||||
public void GetAllTypesTest()
|
||||
{
|
||||
Type[] allTypes = typeof(B).GetAllTypes().ToArray();
|
||||
[TestCase(typeof(byte), false)]
|
||||
[TestCase(typeof(decimal), true)]
|
||||
[TestCase(typeof(double), true)]
|
||||
[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)));
|
||||
Assert.IsTrue(allTypes.Contains(typeof(D)));
|
||||
Assert.IsTrue(allTypes.Contains(typeof(C)));
|
||||
Assert.IsTrue(allTypes.Contains(typeof(B)));
|
||||
Assert.IsTrue(allTypes.Contains(typeof(A)));
|
||||
Assert.IsTrue(allTypes.Contains(typeof(object)));
|
||||
}
|
||||
[TestCase(typeof(string), typeof(object), true)]
|
||||
[TestCase(typeof(object), typeof(string), false)]
|
||||
public void IsAssignableToTest(Type a, Type b, bool expected)
|
||||
{
|
||||
Assert.AreEqual(expected, a.IsAssignableTo(b));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetBaseTypesTest()
|
||||
{
|
||||
Type[] baseTypes = typeof(B).GetBaseTypes().ToArray();
|
||||
[Test]
|
||||
public void GetAllTypesTest()
|
||||
{
|
||||
Type[] allTypes = typeof(B).GetAllTypes().ToArray();
|
||||
|
||||
Assert.AreEqual(2, baseTypes.Length);
|
||||
Assert.AreEqual(6, allTypes.Length);
|
||||
|
||||
Assert.IsFalse(baseTypes.Contains(typeof(B)));
|
||||
Assert.IsTrue(baseTypes.Contains(typeof(A)));
|
||||
Assert.IsTrue(baseTypes.Contains(typeof(object)));
|
||||
}
|
||||
Assert.IsTrue(allTypes.Contains(typeof(E)));
|
||||
Assert.IsTrue(allTypes.Contains(typeof(D)));
|
||||
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
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user