From e7bdb20b19cd4d6b922c9c96cca8436d68d92c8f Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 29 Apr 2022 11:17:02 +0100 Subject: [PATCH] Add tests for reflection extensions --- X10D.Tests/src/Reflection/MemberInfoTests.cs | 78 ++++++++++++++++++++ X10D.Tests/src/Reflection/TypeTests.cs | 59 +++++++++++++++ 2 files changed, 137 insertions(+) create mode 100644 X10D.Tests/src/Reflection/MemberInfoTests.cs create mode 100644 X10D.Tests/src/Reflection/TypeTests.cs diff --git a/X10D.Tests/src/Reflection/MemberInfoTests.cs b/X10D.Tests/src/Reflection/MemberInfoTests.cs new file mode 100644 index 0000000..24d2675 --- /dev/null +++ b/X10D.Tests/src/Reflection/MemberInfoTests.cs @@ -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()); // okay, sbyte is signed. I know. + Assert.IsTrue(typeof(ushort).HasCustomAttribute()); + Assert.IsTrue(typeof(uint).HasCustomAttribute()); + Assert.IsTrue(typeof(ulong).HasCustomAttribute()); + } + + [TestMethod] + public void HasCustomAttribute_ShouldThrow_GivenNull() + { + Type? type = null; + Assert.ThrowsException(() => type!.HasCustomAttribute()); + Assert.ThrowsException(() => type!.HasCustomAttribute(typeof(CLSCompliantAttribute))); + + Assert.ThrowsException(() => typeof(object).HasCustomAttribute(null!)); + } + + [TestMethod] + public void HasCustomAttribute_ShouldThrow_GivenNonAttribute() + { + Assert.ThrowsException(() => 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(() => + (type!.SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant))); + + Assert.ThrowsException(() => + (type!.SelectFromCustomAttribute((CLSCompliantAttribute attribute) => attribute.IsCompliant, true))); + + Assert.ThrowsException(() => + { + Func? selector = null; + typeof(int).SelectFromCustomAttribute(selector!); + }); + } +} diff --git a/X10D.Tests/src/Reflection/TypeTests.cs b/X10D.Tests/src/Reflection/TypeTests.cs new file mode 100644 index 0000000..4324afb --- /dev/null +++ b/X10D.Tests/src/Reflection/TypeTests.cs @@ -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()); + } + + [TestMethod] + public void Inherits_ShouldBeFalse_GivenObjectInheritsString() + { + Assert.IsFalse(typeof(object).Inherits(typeof(string))); + Assert.IsFalse(typeof(object).Inherits()); + } + + [TestMethod] + public void Inherits_ShouldThrow_GivenValueType() + { + Assert.ThrowsException(() => typeof(int).Inherits(typeof(object))); + Assert.ThrowsException(() => typeof(object).Inherits(typeof(int))); + } + + [TestMethod] + public void Inherits_ShouldThrow_GivenNull() + { + Assert.ThrowsException(() => typeof(object).Inherits(null!)); + Assert.ThrowsException(() => ((Type?)null)!.Inherits(typeof(object))); + } + + [TestMethod] + public void Implements_ShouldBeTrue_GivenInt32ImplementsIComparable() + { + Assert.IsTrue(typeof(int).Implements()); + Assert.IsTrue(typeof(int).Implements>()); + Assert.IsTrue(typeof(int).Implements(typeof(IComparable))); + Assert.IsTrue(typeof(int).Implements(typeof(IComparable))); + } + + [TestMethod] + public void Implements_ShouldThrow_GivenNull() + { + Assert.ThrowsException(() => typeof(object).Implements(null!)); + Assert.ThrowsException(() => ((Type?)null)!.Implements(typeof(object))); + } + + [TestMethod] + public void Implements_ShouldThrow_GivenNonInterface() + { + Assert.ThrowsException(() => typeof(string).Implements()); + Assert.ThrowsException(() => typeof(string).Implements(typeof(object))); + } +}