Skip to content

Commit

Permalink
Updated EnumerableTypeCreator to support more enumerable types and st…
Browse files Browse the repository at this point in the history
…rong type handling on ReadOnly types (#88)

Fixed bug in TypeCreatorBase which evaluated the wrong type on Create
Added performance fix on DefaultTypeResolver to not search for types when a List<object> or List<T> scenario could be supported

Closes #71
  • Loading branch information
roryprimrose authored May 2, 2020
1 parent c9dfb2e commit f2e13e7
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="nsubstitute" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
Expand Down
5 changes: 2 additions & 3 deletions ModelBuilder.UnitTests/DefaultTypeResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using FluentAssertions;
using ModelBuilder.UnitTests.Models;
Expand All @@ -25,9 +24,9 @@ public DefaultTypeResolverTests(ITestOutputHelper output)
[InlineData(typeof(ICollection), typeof(List<object>))]
[InlineData(typeof(IList), typeof(List<object>))]
[InlineData(typeof(IEnumerable<Person>), typeof(List<Person>))]
[InlineData(typeof(ICollection<Person>), typeof(Collection<Person>))]
[InlineData(typeof(ICollection<Person>), typeof(List<Person>))]
[InlineData(typeof(IList<Person>), typeof(List<Person>))]
[InlineData(typeof(IReadOnlyCollection<Person>), typeof(ReadOnlyCollection<Person>))]
[InlineData(typeof(IReadOnlyCollection<Person>), typeof(List<Person>))]
[InlineData(typeof(IDictionary<Guid, Person>), typeof(Dictionary<Guid, Person>))]
public void CanCreateEnumerableTypesFromInterfacesWithExpectedResolutionPriority(Type requestedType,
Type expectedType)
Expand Down
4 changes: 2 additions & 2 deletions ModelBuilder.UnitTests/ModelBuilder.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NodaTime" Version="2.4.7" />
<PackageReference Include="NodaTime" Version="2.4.8" />
<PackageReference Include="nsubstitute" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
Expand Down
8 changes: 8 additions & 0 deletions ModelBuilder.UnitTests/Models/IInterfaceCollection.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ModelBuilder.UnitTests.Models
{
using System.Collections.Generic;

public interface IInterfaceCollection<T> : IList<T>
{
}
}
6 changes: 6 additions & 0 deletions ModelBuilder.UnitTests/Models/MultipleGenericArguments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace ModelBuilder.UnitTests.Models
{
public class MultipleGenericArguments<TKey, TValue>
{
}
}
46 changes: 28 additions & 18 deletions ModelBuilder.UnitTests/Scenarios/ScenarioTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,34 @@ public void CanCreateCustomBuildConfigurationToCreateModels()
Guid.Parse(actual.Address.AddressLine1).Should().NotBeEmpty();
}

[Theory]
[InlineData(typeof(IEnumerable<string>))]
[InlineData(typeof(ICollection<string>))]
[InlineData(typeof(Collection<string>))]
[InlineData(typeof(IList<string>))]
[InlineData(typeof(List<string>))]
[InlineData(typeof(LinkedList<string>))]
[InlineData(typeof(InheritedGenericCollection))]
[InlineData(typeof(HashSet<string>))]
[InlineData(typeof(IList<KeyValuePair<string, Guid>>))]
[InlineData(typeof(List<KeyValuePair<string, Guid>>))]
[InlineData(typeof(List<MultipleGenericArguments<string, Guid>>))]
[InlineData(typeof(IList<MultipleGenericArguments<string, Guid>>))]
[InlineData(typeof(Collection<KeyValuePair<string, Guid>>))]
[InlineData(typeof(ICollection<KeyValuePair<string, Guid>>))]
[InlineData(typeof(IReadOnlyCollection<int>))]
[InlineData(typeof(IReadOnlyList<int>))]
[InlineData(typeof(IDictionary<string, Person>))]
[InlineData(typeof(Dictionary<string, Person>))]
public void CanCreateEnumerableTypes(Type type)
{
var actual = Model.Create(type);

actual.As<IEnumerable>().Should().NotBeEmpty();
}

[Fact]
public void CanCreateEnumerableTypes()
public void CanCreateInstanceWithEnumerablePropertyTypes()
{
var actual = Model.Create<EnumerableParent>();

Expand All @@ -81,22 +107,6 @@ public void CanCreateEnumerableTypes()
actual.List.Should().NotBeEmpty();
}

[Theory]
[InlineData(typeof(IEnumerable))]
[InlineData(typeof(ICollection))]
[InlineData(typeof(IList))]
[InlineData(typeof(IEnumerable<Person>))]
[InlineData(typeof(ICollection<Person>))]
[InlineData(typeof(IList<Person>))]
[InlineData(typeof(IReadOnlyCollection<Person>))]
[InlineData(typeof(IDictionary<Guid, Person>))]
public void CanCreateEnumerableTypesFromInterfaces(Type type)
{
var actual = Model.Create(type);

actual.As<IEnumerable>().Should().NotBeEmpty();
}

[Fact]
public void CanCreateInstanceWithoutProperties()
{
Expand All @@ -110,7 +120,7 @@ public void CanCreateReadOnlyCollection()
{
var actual = Model.Create<IReadOnlyCollection<Person>>();

actual.Should().BeOfType<ReadOnlyCollection<Person>>();
actual.Should().BeOfType<List<Person>>();
actual.Should().NotBeEmpty();
}

Expand Down
Loading

0 comments on commit f2e13e7

Please sign in to comment.