1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 22:55:42 +00:00

Add tests for reflection extensions

This commit is contained in:
Oliver Booth 2022-04-29 11:17:02 +01:00
parent dc3de3816e
commit e7bdb20b19
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
2 changed files with 137 additions and 0 deletions

View File

@ -0,0 +1,78 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Reflection;
namespace X10D.Tests.Reflection;
[TestClass]
public class MemberInfoTests
{
[TestMethod]
public void HasCustomAttribute_ShouldBeTrue_GivenCLSCompliantAttributeOnUnsignedTypes()
{
Assert.IsTrue(typeof(sbyte).HasCustomAttribute(typeof(CLSCompliantAttribute))); // okay, sbyte is signed. I know.
Assert.IsTrue(typeof(ushort).HasCustomAttribute(typeof(CLSCompliantAttribute)));
Assert.IsTrue(typeof(uint).HasCustomAttribute(typeof(CLSCompliantAttribute)));
Assert.IsTrue(typeof(ulong).HasCustomAttribute(typeof(CLSCompliantAttribute)));
}
[TestMethod]
public void HasCustomAttribute_ShouldBeTrue_GivenCLSCompliantAttributeOnUnsignedTypes_Generic()
{
Assert.IsTrue(typeof(sbyte).HasCustomAttribute<CLSCompliantAttribute>()); // okay, sbyte is signed. I know.
Assert.IsTrue(typeof(ushort).HasCustomAttribute<CLSCompliantAttribute>());
Assert.IsTrue(typeof(uint).HasCustomAttribute<CLSCompliantAttribute>());
Assert.IsTrue(typeof(ulong).HasCustomAttribute<CLSCompliantAttribute>());
}
[TestMethod]
public void HasCustomAttribute_ShouldThrow_GivenNull()
{
Type? type = null;
Assert.ThrowsException<ArgumentNullException>(() => type!.HasCustomAttribute<CLSCompliantAttribute>());
Assert.ThrowsException<ArgumentNullException>(() => type!.HasCustomAttribute(typeof(CLSCompliantAttribute)));
Assert.ThrowsException<ArgumentNullException>(() => typeof(object).HasCustomAttribute(null!));
}
[TestMethod]
public void HasCustomAttribute_ShouldThrow_GivenNonAttribute()
{
Assert.ThrowsException<ArgumentException>(() => typeof(object).HasCustomAttribute(typeof(object)));
}
[TestMethod]
public void SelectFromCustomAttribute_ShouldBeFalse_GivenCLSCompliantAttributeOnUnsignedTypes()
{
Assert.IsFalse(typeof(sbyte).SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant));
Assert.IsFalse(typeof(ushort).SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant));
Assert.IsFalse(typeof(uint).SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant));
Assert.IsFalse(typeof(ulong).SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant));
}
[TestMethod]
public void SelectFromCustomAttribute_ShouldBeTrue_GivenCLSCompliantAttributeOnSignedTypes()
{
Assert.IsTrue(typeof(byte).SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant, true));
Assert.IsTrue(typeof(short).SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant, true));
Assert.IsTrue(typeof(int).SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant, true));
Assert.IsTrue(typeof(long).SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant, true));
}
[TestMethod]
public void SelectFromCustomAttribute_ShouldThrow_GivenNull()
{
Type? type = null;
Assert.ThrowsException<ArgumentNullException>(() =>
(type!.SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant)));
Assert.ThrowsException<ArgumentNullException>(() =>
(type!.SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant, true)));
Assert.ThrowsException<ArgumentNullException>(() =>
{
Func<CLSCompliantAttribute, bool>? selector = null;
typeof(int).SelectFromCustomAttribute(selector!);
});
}
}

View File

@ -0,0 +1,59 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using X10D.Reflection;
namespace X10D.Tests.Reflection;
[TestClass]
public class TypeTests
{
[TestMethod]
public void Inherits_ShouldBeTrue_GivenStringInheritsObject()
{
Assert.IsTrue(typeof(string).Inherits(typeof(object)));
Assert.IsTrue(typeof(string).Inherits<object>());
}
[TestMethod]
public void Inherits_ShouldBeFalse_GivenObjectInheritsString()
{
Assert.IsFalse(typeof(object).Inherits(typeof(string)));
Assert.IsFalse(typeof(object).Inherits<string>());
}
[TestMethod]
public void Inherits_ShouldThrow_GivenValueType()
{
Assert.ThrowsException<ArgumentException>(() => typeof(int).Inherits(typeof(object)));
Assert.ThrowsException<ArgumentException>(() => typeof(object).Inherits(typeof(int)));
}
[TestMethod]
public void Inherits_ShouldThrow_GivenNull()
{
Assert.ThrowsException<ArgumentNullException>(() => typeof(object).Inherits(null!));
Assert.ThrowsException<ArgumentNullException>(() => ((Type?)null)!.Inherits(typeof(object)));
}
[TestMethod]
public void Implements_ShouldBeTrue_GivenInt32ImplementsIComparable()
{
Assert.IsTrue(typeof(int).Implements<IComparable>());
Assert.IsTrue(typeof(int).Implements<IComparable<int>>());
Assert.IsTrue(typeof(int).Implements(typeof(IComparable)));
Assert.IsTrue(typeof(int).Implements(typeof(IComparable<int>)));
}
[TestMethod]
public void Implements_ShouldThrow_GivenNull()
{
Assert.ThrowsException<ArgumentNullException>(() => typeof(object).Implements(null!));
Assert.ThrowsException<ArgumentNullException>(() => ((Type?)null)!.Implements(typeof(object)));
}
[TestMethod]
public void Implements_ShouldThrow_GivenNonInterface()
{
Assert.ThrowsException<ArgumentException>(() => typeof(string).Implements<object>());
Assert.ThrowsException<ArgumentException>(() => typeof(string).Implements(typeof(object)));
}
}