Skip to content

fix(#241): TypeLocator bug -- add tests #377

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2018
Merged
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
24 changes: 18 additions & 6 deletions src/JsonApiDotNetCore/Graph/TypeLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ private static Type[] GetAssemblyTypes(Assembly assembly)
return types;
}


/// <summary>
/// Get all implementations of <see cref="IIdentifiable"/>. in the assembly
/// </summary>
Expand Down Expand Up @@ -80,15 +79,28 @@ public static List<ResourceDescriptor> GetIdentifableTypes(Assembly assembly)
/// </example>
public static (Type implementation, Type registrationInterface) GetGenericInterfaceImplementation(Assembly assembly, Type openGenericInterfaceType, params Type[] genericInterfaceArguments)
{
if(assembly == null) throw new ArgumentNullException(nameof(assembly));
if(openGenericInterfaceType == null) throw new ArgumentNullException(nameof(openGenericInterfaceType));
if(genericInterfaceArguments == null) throw new ArgumentNullException(nameof(genericInterfaceArguments));
if(genericInterfaceArguments.Length == 0) throw new ArgumentException("No arguments supplied for the generic interface.", nameof(genericInterfaceArguments));
if(openGenericInterfaceType.IsGenericType == false) throw new ArgumentException("Requested type is not a generic type.", nameof(openGenericInterfaceType));

foreach (var type in assembly.GetTypes())
{
var interfaces = type.GetInterfaces();
foreach (var interfaceType in interfaces)
if (interfaceType.GetTypeInfo().IsGenericType && interfaceType.GetGenericTypeDefinition() == openGenericInterfaceType)
return (
type,
interfaceType.MakeGenericType(genericInterfaceArguments)
);
{
if (interfaceType.IsGenericType)
{
var genericTypeDefinition = interfaceType.GetGenericTypeDefinition();
if(genericTypeDefinition == openGenericInterfaceType.GetGenericTypeDefinition()) {
return (
type,
genericTypeDefinition.MakeGenericType(genericInterfaceArguments)
);
}
}
}
}

return (null, null);
Expand Down
99 changes: 99 additions & 0 deletions test/UnitTests/Graph/TypeLocator_Tests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using System;
using JsonApiDotNetCore.Graph;
using JsonApiDotNetCore.Models;
using Xunit;

namespace UnitTests.Internal
{
public class TypeLocator_Tests
{
[Fact]
public void GetGenericInterfaceImplementation_Gets_Implementation()
{
// arrange
var assembly = GetType().Assembly;
var openGeneric = typeof(IGenericInterface<>);
var genericArg = typeof(int);

var expectedImplementation = typeof(Implementation);
var expectedInterface = typeof(IGenericInterface<int>);

// act
var result = TypeLocator.GetGenericInterfaceImplementation(
assembly,
openGeneric,
genericArg
);

// assert
Assert.NotNull(result);
Assert.Equal(expectedImplementation, result.implementation);
Assert.Equal(expectedInterface, result.registrationInterface);
}

[Fact]
public void GetDerivedGenericTypes_Gets_Implementation()
{
// arrange
var assembly = GetType().Assembly;
var openGeneric = typeof(BaseType<>);
var genericArg = typeof(int);

var expectedImplementation = typeof(DerivedType);

// act
var results = TypeLocator.GetDerivedGenericTypes(
assembly,
openGeneric,
genericArg
);

// assert
Assert.NotNull(results);
var result = Assert.Single(results);
Assert.Equal(expectedImplementation, result);
}

[Fact]
public void GetIdType_Correctly_Identifies_JsonApiResource()
{
// arrange
var type = typeof(Model);
var exextedIdType = typeof(int);

// act
var result = TypeLocator.GetIdType(type);

// assert
Assert.NotNull(result);
Assert.True(result.isJsonApiResource);
Assert.Equal(exextedIdType, result.idType);
}

[Fact]
public void GetIdType_Correctly_Identifies_NonJsonApiResource()
{
// arrange
var type = typeof(DerivedType);
Type exextedIdType = null;

// act
var result = TypeLocator.GetIdType(type);

// assert
Assert.NotNull(result);
Assert.False(result.isJsonApiResource);
Assert.Equal(exextedIdType, result.idType);
}
}


public interface IGenericInterface<T> { }
public class Implementation : IGenericInterface<int> { }


public class BaseType<T> { }
public class DerivedType : BaseType<int> { }

public class Model : Identifiable { }
}