Skip to content

Commit

Permalink
Updated TypeCreatorBase to include logging of type mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
roryprimrose committed May 28, 2020
1 parent 517514c commit 747747f
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 7 deletions.
4 changes: 2 additions & 2 deletions ModelBuilder.UnitTests/ExpressionExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ public void GetPropertyThrowsExceptionWithNullExpression()
action.Should().Throw<ArgumentNullException>();
}

private PropertyInfo Wrapper<T>(Expression<Func<T, object>> expression)
private static PropertyInfo Wrapper<T>(Expression<Func<T, object>> expression)
{
return expression.GetProperty();
}

private class StaticGetter
{
public static string Value { get { return Guid.NewGuid().ToString(); } }
public static string Value => Guid.NewGuid().ToString();
}
}
}
4 changes: 2 additions & 2 deletions ModelBuilder.UnitTests/ModelBuilder.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

<ItemGroup>
<PackageReference Include="fluentassertions" Version="5.10.3" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NodaTime" Version="2.4.8" />
<PackageReference Include="NodaTime" Version="3.0.0" />
<PackageReference Include="nsubstitute" Version="4.2.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
15 changes: 14 additions & 1 deletion ModelBuilder.UnitTests/ModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,17 @@
using FluentAssertions;
using ModelBuilder.UnitTests.Models;
using Xunit;
using Xunit.Abstractions;

public class ModelTests
{
private readonly ITestOutputHelper _output;

public ModelTests(ITestOutputHelper output)
{
_output = output;
}

[Fact]
public void CreateReturnsInstance()
{
Expand Down Expand Up @@ -75,7 +83,12 @@ public void IgnoringUsesConfigurationToCreateInstance()
[Fact]
public void MappingUsesConfigurationToCreateInstance()
{
var actual = Model.Mapping<ITestItem, TestItem>().Create<ITestItem>()!;
var configuration = Model.Mapping<ITestItem, TestItem>();
var executeStrategy = configuration.UsingExecuteStrategy<DefaultExecuteStrategy<ITestItem>>();

var actual = executeStrategy.Create()!;

_output.WriteLine(executeStrategy.Log.Output);

actual.Should().BeOfType<TestItem>();
actual.FirstName.Should().NotBeNullOrWhiteSpace();
Expand Down
7 changes: 7 additions & 0 deletions ModelBuilder.UnitTests/Models/IItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ModelBuilder.UnitTests.Models
{
public interface IItem
{
void DoSomething();
}
}
10 changes: 10 additions & 0 deletions ModelBuilder.UnitTests/Models/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace ModelBuilder.UnitTests.Models
{
public class Item : IItem
{
public void DoSomething()
{
throw new System.NotImplementedException();
}
}
}
12 changes: 12 additions & 0 deletions ModelBuilder.UnitTests/Models/ParameterModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace ModelBuilder.UnitTests.Models
{
public class ParameterModel<T>
{
public ParameterModel(T value)
{
Value = value;
}

public T Value { get; }
}
}
7 changes: 7 additions & 0 deletions ModelBuilder.UnitTests/Models/PropertyModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace ModelBuilder.UnitTests.Models
{
public class PropertyModel<T> where T : notnull
{
public T Value { get; set; } = default!;
}
}
2 changes: 1 addition & 1 deletion ModelBuilder.UnitTests/Models/ReadOnlyParent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ReadOnlyParent()
PrivateValue = 0;
}

private IEnumerable<Person> EmptySet()
private static IEnumerable<Person> EmptySet()
{
yield break;
}
Expand Down
74 changes: 74 additions & 0 deletions ModelBuilder.UnitTests/Scenarios/TypeMappingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
namespace ModelBuilder.UnitTests.Scenarios
{
using FluentAssertions;
using ModelBuilder.UnitTests.Models;
using Xunit;
using Xunit.Abstractions;

public class TypeMappingTests
{
private readonly ITestOutputHelper _output;

public TypeMappingTests(ITestOutputHelper output)
{
_output = output;
}

private interface IPrivate
{
}

[Fact]
public void CanExplicitlyMapPrivateType()
{
var strategy = Model.Mapping<IPrivate, Private>().UsingExecuteStrategy<DefaultExecuteStrategy<IPrivate>>();

var actual = strategy.Create();

_output.WriteLine(strategy.Log.Output);

actual.Should().BeOfType<Private>();
}

[Fact]
public void CanMapParameterType()
{
var strategy = Model.UsingExecuteStrategy<DefaultExecuteStrategy<ParameterModel<IItem>>>();

var actual = strategy.Create()!;

_output.WriteLine(strategy.Log.Output);

actual.Value.Should().BeOfType<Item>();
}

[Fact]
public void CanMapPropertyType()
{
var strategy = Model.UsingExecuteStrategy<DefaultExecuteStrategy<PropertyModel<IItem>>>();

var actual = strategy.Create()!;

_output.WriteLine(strategy.Log.Output);

actual.Value.Should().BeOfType<Item>();
}

[Fact]
public void CanMapRootType()
{
var strategy = Model.UsingExecuteStrategy<DefaultExecuteStrategy<IItem>>();

var actual = strategy.Create();

_output.WriteLine(strategy.Log.Output);

actual.Should().BeOfType<Item>();
}

// ReSharper disable once ClassNeverInstantiated.Local
private class Private : IPrivate
{
}
}
}
2 changes: 1 addition & 1 deletion ModelBuilder/ModelBuilder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8" Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
5 changes: 5 additions & 0 deletions ModelBuilder/TypeCreators/TypeCreatorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ protected virtual bool CanPopulate(IBuildConfiguration configuration,
throw new NotSupportedException(message);
}

if (buildType != type)
{
executeStrategy.Log.MappedType(type, buildType);
}

return CreateInstance(executeStrategy, buildType, referenceName, args);
}

Expand Down

0 comments on commit 747747f

Please sign in to comment.