mirror of
https://github.com/ICDSystems/ICD.Common.Utils.git
synced 2026-01-11 19:44:55 +00:00
Begin implementing ReflectionUtilsTest
This commit is contained in:
125
ICD.Common.Utils.Tests/ReflectionUtilsTest.cs
Normal file
125
ICD.Common.Utils.Tests/ReflectionUtilsTest.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
#if SIMPLSHARP
|
||||
using Crestron.SimplSharp.Reflection;
|
||||
#else
|
||||
using System.Reflection;
|
||||
#endif
|
||||
|
||||
namespace ICD.Common.Utils.Tests
|
||||
{
|
||||
[TestFixture]
|
||||
public sealed class ReflectionUtilsTest
|
||||
{
|
||||
private string TestProperty { get; set; }
|
||||
|
||||
private void TestMethod(string param1, int param2)
|
||||
{
|
||||
}
|
||||
|
||||
private class TestClass
|
||||
{
|
||||
public string Param1 { get; set; }
|
||||
public int Param2 { get; set; }
|
||||
|
||||
public TestClass(string param1, int param2)
|
||||
{
|
||||
Param1 = param1;
|
||||
Param2 = param2;
|
||||
}
|
||||
}
|
||||
|
||||
[TestCase("test", 10)]
|
||||
[TestCase(null, 0)]
|
||||
public void InstantiateTest(string param1, int param2)
|
||||
{
|
||||
TestClass result = ReflectionUtils.Instantiate(typeof(TestClass), param1, param2) as TestClass;
|
||||
|
||||
Assert.NotNull(result);
|
||||
Assert.AreEqual(param1, result.Param1);
|
||||
Assert.AreEqual(param2, result.Param2);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MatchesConstructorParametersTest()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => ReflectionUtils.MatchesConstructorParameters(null, new object[] { "test", 10 }));
|
||||
|
||||
ConstructorInfo constructor = typeof(TestClass).GetConstructor(new Type[] { typeof(string), typeof(int) });
|
||||
|
||||
Assert.IsTrue(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { "test", 10 }));
|
||||
Assert.IsTrue(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { null, 10 }));
|
||||
Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { "test", "test" }));
|
||||
Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { "test" }));
|
||||
Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { "test", "test", "test" }));
|
||||
Assert.IsFalse(ReflectionUtils.MatchesConstructorParameters(constructor, new object[] { 10, 10 }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MatchesMethodParametersTest()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => ReflectionUtils.MatchesMethodParameters(null, new object[] { "test", 10 }));
|
||||
|
||||
MethodInfo method = GetType().GetMethod("TestMethod",
|
||||
BindingFlags.NonPublic |
|
||||
BindingFlags.Instance);
|
||||
|
||||
Assert.IsTrue(ReflectionUtils.MatchesMethodParameters(method, new object[] { "test", 10 }));
|
||||
Assert.IsTrue(ReflectionUtils.MatchesMethodParameters(method, new object[] { null, 10 }));
|
||||
Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] { "test", "test" }));
|
||||
Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] { "test" }));
|
||||
Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] { "test", "test", "test" }));
|
||||
Assert.IsFalse(ReflectionUtils.MatchesMethodParameters(method, new object[] { 10, 10 }));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void MatchesPropertyParameterTest()
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => ReflectionUtils.MatchesPropertyParameter(null, 10));
|
||||
|
||||
PropertyInfo prop = GetType().GetProperty("TestProperty",
|
||||
BindingFlags.NonPublic |
|
||||
BindingFlags.Instance |
|
||||
BindingFlags.GetProperty);
|
||||
|
||||
Assert.IsTrue(ReflectionUtils.MatchesPropertyParameter(prop, "test"));
|
||||
Assert.IsTrue(ReflectionUtils.MatchesPropertyParameter(prop, null));
|
||||
Assert.IsFalse(ReflectionUtils.MatchesPropertyParameter(prop, 1));
|
||||
Assert.IsFalse(ReflectionUtils.MatchesPropertyParameter(prop, new string[0]));
|
||||
}
|
||||
|
||||
[TestCase(typeof(string), null)]
|
||||
[TestCase(typeof(int), 0)]
|
||||
public void GetDefaultValueTest(Type type, object expected)
|
||||
{
|
||||
Assert.AreEqual(expected, ReflectionUtils.GetDefaultValue(type));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CreateInstanceGenericTest()
|
||||
{
|
||||
List<string> output = ReflectionUtils.CreateInstance<List<string>>();
|
||||
Assert.NotNull(output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CreateInstanceTest()
|
||||
{
|
||||
List<string> output = ReflectionUtils.CreateInstance(typeof(List<string>)) as List<string>;
|
||||
Assert.NotNull(output);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCustomAttributesTest()
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void LoadAssemblyFromPath()
|
||||
{
|
||||
Assert.Inconclusive();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user