Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions csharp/Platform.Reflection.Tests/DynamicExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using System;
using System.Collections.Generic;
using Xunit;

namespace Platform.Reflection.Tests
{
public class DynamicExtensionsTests
{
private class TestClass
{
public int IntProperty { get; set; }
public string StringProperty { get; set; } = "test";
public bool BoolProperty { get; set; }
}

[Fact]
public void HasPropertyExistingPropertyTest()
{
var obj = new TestClass();
Assert.True(obj.HasProperty("IntProperty"));
Assert.True(obj.HasProperty("StringProperty"));
Assert.True(obj.HasProperty("BoolProperty"));
}

[Fact]
public void HasPropertyNonExistingPropertyTest()
{
var obj = new TestClass();
Assert.False(obj.HasProperty("NonExistentProperty"));
Assert.False(obj.HasProperty("AnotherMissingProperty"));
}

[Fact]
public void HasPropertyCaseSensitiveTest()
{
var obj = new TestClass();
Assert.True(obj.HasProperty("IntProperty"));
Assert.False(obj.HasProperty("intproperty"));
Assert.False(obj.HasProperty("INTPROPERTY"));
Assert.False(obj.HasProperty("intProperty"));
}

[Fact]
public void HasPropertyWithDictionaryTest()
{
var dictionary = new Dictionary<string, object>
{
{ "Key1", "Value1" },
{ "Key2", 42 },
{ "Key3", true }
};

// The current implementation doesn't actually check if the object is a dictionary
// It checks if the type "is" IDictionary<string, object> which fails for Dictionary<string, object>
// So it will fall back to checking properties on the Dictionary type itself
object obj = dictionary;

// Dictionary<string, object> has properties like Keys, Values, Count, etc.
Assert.True(obj.HasProperty("Keys"));
Assert.True(obj.HasProperty("Values"));
Assert.True(obj.HasProperty("Count"));
Assert.False(obj.HasProperty("Key1")); // These are dictionary entries, not properties
}

[Fact]
public void HasPropertyWithEmptyDictionaryTest()
{
var dictionary = new Dictionary<string, object>();
object obj = dictionary;
// Same as above - it checks Dictionary type properties, not dictionary contents
Assert.True(obj.HasProperty("Count")); // Dictionary has Count property
Assert.False(obj.HasProperty("AnyKey")); // But not custom keys
}

[Fact]
public void HasPropertyWithNullPropertyNameTest()
{
var obj = new TestClass();
// The current implementation throws ArgumentNullException when propertyName is null
// because Type.GetProperty(null) throws
Assert.Throws<ArgumentNullException>(() => obj.HasProperty(null));
}

[Fact]
public void HasPropertyWithEmptyPropertyNameTest()
{
var obj = new TestClass();
Assert.False(obj.HasProperty(""));
Assert.False(obj.HasProperty(string.Empty));
}

[Fact]
public void HasPropertyWithSystemObjectTest()
{
var obj = new object();
// Object class has standard properties like GetType, ToString, etc.
Assert.False(obj.HasProperty("GetType")); // GetType is a method, not a property
Assert.False(obj.HasProperty("SomeProperty"));
}

[Fact]
public void HasPropertyWithBuiltInTypesTest()
{
var str = "test string";
object obj = str;
Assert.True(obj.HasProperty("Length")); // String has Length property
Assert.False(obj.HasProperty("Size"));

var array = new int[] { 1, 2, 3 };
obj = array;
Assert.True(obj.HasProperty("Length")); // Array has Length property
Assert.False(obj.HasProperty("Count"));
}

[Fact]
public void HasPropertyWithAnonymousObjectTest()
{
var obj = new { Name = "Test", Age = 25, IsActive = true };
Assert.True(obj.HasProperty("Name"));
Assert.True(obj.HasProperty("Age"));
Assert.True(obj.HasProperty("IsActive"));
Assert.False(obj.HasProperty("Height"));
}
}
}
103 changes: 103 additions & 0 deletions csharp/Platform.Reflection.Tests/FieldInfoExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Reflection;
using Xunit;

namespace Platform.Reflection.Tests
{
public class FieldInfoExtensionsTests
{
private static class TestClass
{
public static int StaticIntField = 42;
public static string StaticStringField = "test field";
public static bool StaticBoolField = true;
public static readonly DateTime StaticReadOnlyField = new DateTime(2023, 1, 1);
}

[Fact]
public void GetStaticValueIntFieldTest()
{
var fieldInfo = typeof(TestClass).GetField(nameof(TestClass.StaticIntField));
Assert.NotNull(fieldInfo);

var value = fieldInfo.GetStaticValue<int>();
Assert.Equal(42, value);
}

[Fact]
public void GetStaticValueStringFieldTest()
{
var fieldInfo = typeof(TestClass).GetField(nameof(TestClass.StaticStringField));
Assert.NotNull(fieldInfo);

var value = fieldInfo.GetStaticValue<string>();
Assert.Equal("test field", value);
}

[Fact]
public void GetStaticValueBoolFieldTest()
{
var fieldInfo = typeof(TestClass).GetField(nameof(TestClass.StaticBoolField));
Assert.NotNull(fieldInfo);

var value = fieldInfo.GetStaticValue<bool>();
Assert.True(value);
}

[Fact]
public void GetStaticValueObjectFieldTest()
{
var fieldInfo = typeof(TestClass).GetField(nameof(TestClass.StaticStringField));
Assert.NotNull(fieldInfo);

var value = fieldInfo.GetStaticValue<object>();
Assert.Equal("test field", value);
}

[Fact]
public void GetStaticValueReadOnlyFieldTest()
{
var fieldInfo = typeof(TestClass).GetField(nameof(TestClass.StaticReadOnlyField));
Assert.NotNull(fieldInfo);

var value = fieldInfo.GetStaticValue<DateTime>();
Assert.Equal(new DateTime(2023, 1, 1), value);
}

[Fact]
public void GetStaticValueWithFieldModificationTest()
{
var fieldInfo = typeof(TestClass).GetField(nameof(TestClass.StaticIntField));
Assert.NotNull(fieldInfo);

// Change the field value
TestClass.StaticIntField = 100;

var value = fieldInfo.GetStaticValue<int>();
Assert.Equal(100, value);

// Reset for other tests
TestClass.StaticIntField = 42;
}

[Fact]
public void GetStaticValueFromSystemTypeTest()
{
var fieldInfo = typeof(int).GetField("MaxValue");
Assert.NotNull(fieldInfo);

var value = fieldInfo.GetStaticValue<int>();
Assert.Equal(int.MaxValue, value);
}

[Fact]
public void GetStaticValueFromEnumTest()
{
var fieldInfo = typeof(DayOfWeek).GetField("Monday");
Assert.NotNull(fieldInfo);

var value = fieldInfo.GetStaticValue<DayOfWeek>();
Assert.Equal(DayOfWeek.Monday, value);
}
}
}
105 changes: 105 additions & 0 deletions csharp/Platform.Reflection.Tests/MethodInfoExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Reflection;
using Xunit;

namespace Platform.Reflection.Tests
{
public class MethodInfoExtensionsTests
{
private static class TestClass
{
public static void NoParametersMethod() { }
public static void OneParameterMethod(int parameter) { }
public static void TwoParametersMethod(int first, string second) { }
public static void ManyParametersMethod(int a, string b, bool c, double d) { }

public static int SimpleReturnMethod() => 42;
}

[Fact]
public void GetParameterTypesNoParametersTest()
{
var methodInfo = typeof(TestClass).GetMethod(nameof(TestClass.NoParametersMethod));
Assert.NotNull(methodInfo);

var parameterTypes = methodInfo.GetParameterTypes();
Assert.NotNull(parameterTypes);
Assert.Empty(parameterTypes);
}

[Fact]
public void GetParameterTypesOneParameterTest()
{
var methodInfo = typeof(TestClass).GetMethod(nameof(TestClass.OneParameterMethod));
Assert.NotNull(methodInfo);

var parameterTypes = methodInfo.GetParameterTypes();
Assert.NotNull(parameterTypes);
Assert.Single(parameterTypes);
Assert.Equal(typeof(int), parameterTypes[0]);
}

[Fact]
public void GetParameterTypesTwoParametersTest()
{
var methodInfo = typeof(TestClass).GetMethod(nameof(TestClass.TwoParametersMethod));
Assert.NotNull(methodInfo);

var parameterTypes = methodInfo.GetParameterTypes();
Assert.NotNull(parameterTypes);
Assert.Equal(2, parameterTypes.Length);
Assert.Equal(typeof(int), parameterTypes[0]);
Assert.Equal(typeof(string), parameterTypes[1]);
}

[Fact]
public void GetParameterTypesManyParametersTest()
{
var methodInfo = typeof(TestClass).GetMethod(nameof(TestClass.ManyParametersMethod));
Assert.NotNull(methodInfo);

var parameterTypes = methodInfo.GetParameterTypes();
Assert.NotNull(parameterTypes);
Assert.Equal(4, parameterTypes.Length);
Assert.Equal(typeof(int), parameterTypes[0]);
Assert.Equal(typeof(string), parameterTypes[1]);
Assert.Equal(typeof(bool), parameterTypes[2]);
Assert.Equal(typeof(double), parameterTypes[3]);
}

[Fact]
public void GetILBytesNotNullTest()
{
var methodInfo = typeof(TestClass).GetMethod(nameof(TestClass.SimpleReturnMethod));
Assert.NotNull(methodInfo);

var ilBytes = methodInfo.GetILBytes();
Assert.NotNull(ilBytes);
Assert.NotEmpty(ilBytes);
}

[Fact]
public void GetILBytesForLambdaTest()
{
var func = new Func<int>(() => 42);
var methodInfo = func.Method;

var ilBytes = methodInfo.GetILBytes();
Assert.NotNull(ilBytes);
Assert.NotEmpty(ilBytes);
}

[Fact]
public void GetParameterTypesForGenericMethodTest()
{
var method = typeof(Array).GetMethod("IndexOf", new[] { typeof(Array), typeof(object) });
Assert.NotNull(method);

var parameterTypes = method.GetParameterTypes();
Assert.NotNull(parameterTypes);
Assert.Equal(2, parameterTypes.Length);
Assert.Equal(typeof(Array), parameterTypes[0]);
Assert.Equal(typeof(object), parameterTypes[1]);
}
}
}
Loading
Loading