Skip to content

Commit

Permalink
Add extra tests for ReflectionExtension
Browse files Browse the repository at this point in the history
And remove dead code.
  • Loading branch information
nover committed Oct 22, 2017
1 parent 99dd993 commit 9ed379a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
10 changes: 2 additions & 8 deletions src/Core/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal static MethodInfo FindMethodRecursively(this Type prop, string name, pa
{
if (prop == null) return null;

// GetMethod searches base types...
var method = prop.GetMethod(name, typeArgs);
if (method != null) return method;

Expand All @@ -50,16 +51,9 @@ internal static MethodInfo FindMethodRecursively(this Type prop, string name, pa
method = iface.FindMethodRecursively(name, typeArgs);
if (method != null) return method;
}
// base types
var baseType = prop.GetTypeInfo().BaseType;
if (baseType != null)
{
method = baseType.FindMethodRecursively(name, typeArgs);
if (method != null) return method;
}

// TODO better bailout exception?
throw new ArgumentException($"Unable to locate method with name: {name}");
throw new ArgumentException($"Unable to locate method with name: {name}", nameof(name));
}

/// <summary>
Expand Down
55 changes: 53 additions & 2 deletions test/unit/Core/ReflectionExtensionTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Linq;
using System;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using RapidCore.Reflection;
Expand All @@ -23,11 +24,17 @@ public void ReflectionExtension_resolves_prop_names_correctly()
Assert.Equal("MemberNoCustomName", propNoCustomName.GetPropSerializedName());
var propWithCustomName = dtoProps.Single(p => p.Name == "MemberWithCustomName");
Assert.Equal("CallMeThis", propWithCustomName.GetPropSerializedName());
}

[Fact]
public void ReflectionExtension_resolves_prop_names_correctly_handles_null_prop()
{
PropertyInfo prop = null;
Assert.Null(prop.GetPropSerializedName());
}

[Fact]
public void ReflectionExtension_can_find_methods_from_base_types_and_interfaces()
public void ReflectionExtension_can_find_methods_from_interfaces()
{
var dto = new ContactListHubSpotEntity<ContactHubSpotEntity>();

Expand All @@ -38,6 +45,37 @@ public void ReflectionExtension_can_find_methods_from_base_types_and_interfaces(
Assert.NotNull(method);
}

[Fact]
public void ReflectionExtension_can_find_methods_from_base_classes()
{
// all classes inherits from Object and Object has ToString() method!
var instance = new Inheritor();
var method = instance.GetType().FindMethodRecursively("ToString");

Assert.NotNull(method);
}

[Fact]
public void ReflectionExtension_throws_when_unable_to_find_method()
{
// all classes inherits from Object and Object has ToString() method!
var instance = new Inheritor();
var exception = Record.Exception(() => instance.GetType().FindMethodRecursively("IfThisIsHereStuffIsWhack"));

Assert.NotNull(exception);
var argumentException = Assert.IsType<ArgumentException>(exception);

Assert.Equal("name", argumentException.ParamName);
}


[Fact]
public void ReflectionExtension_can_find_methods_from_base_types_and_interfaces_handles_null()
{
Type type = null;
Assert.Null(type.FindMethodRecursively("Add"));
}

[Fact]
public void ReflectionExtension_can_determine_if_prop_has_ignore_data_member()
{
Expand All @@ -51,6 +89,13 @@ public void ReflectionExtension_can_determine_if_prop_has_ignore_data_member()
}

[Fact]
public void ReflectionExtension_can_determine_if_prop_has_ignore_data_member_handles_null()
{
PropertyInfo prop = null;
Assert.False(prop.HasIgnoreDataMemberAttribute());
}

[Fact]
public void ReflectionExtension_can_determine_if_stuff_is_complex()
{
string nuller = null;
Expand Down Expand Up @@ -78,5 +123,11 @@ private class ClassWithDataMembers
[IgnoreDataMember]
public string IgnoreMePlease { get; set; }
}


private class Inheritor
{

}
}
}

0 comments on commit 9ed379a

Please sign in to comment.