From d9b2dde8ced1c264e7a9febc8c0d0d187ee2701b Mon Sep 17 00:00:00 2001 From: Rory Primrose Date: Thu, 28 May 2020 22:20:48 +1000 Subject: [PATCH] Updated TypeCreatorBase to include logging of type mapping (#111) Closes #100 --- .../ExpressionExtensionTests.cs | 4 +- .../ModelBuilder.UnitTests.csproj | 4 +- ModelBuilder.UnitTests/ModelTests.cs | 15 +++- ModelBuilder.UnitTests/Models/IItem.cs | 7 ++ ModelBuilder.UnitTests/Models/Item.cs | 10 +++ .../Models/ParameterModel.cs | 12 +++ .../Models/PropertyModel.cs | 7 ++ .../Models/ReadOnlyParent.cs | 2 +- .../Scenarios/TypeMappingTests.cs | 74 +++++++++++++++++++ ModelBuilder/ModelBuilder.csproj | 2 +- ModelBuilder/TypeCreators/TypeCreatorBase.cs | 5 ++ 11 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 ModelBuilder.UnitTests/Models/IItem.cs create mode 100644 ModelBuilder.UnitTests/Models/Item.cs create mode 100644 ModelBuilder.UnitTests/Models/ParameterModel.cs create mode 100644 ModelBuilder.UnitTests/Models/PropertyModel.cs create mode 100644 ModelBuilder.UnitTests/Scenarios/TypeMappingTests.cs diff --git a/ModelBuilder.UnitTests/ExpressionExtensionTests.cs b/ModelBuilder.UnitTests/ExpressionExtensionTests.cs index 2fdf1b1..0a54c0b 100644 --- a/ModelBuilder.UnitTests/ExpressionExtensionTests.cs +++ b/ModelBuilder.UnitTests/ExpressionExtensionTests.cs @@ -67,14 +67,14 @@ public void GetPropertyThrowsExceptionWithNullExpression() action.Should().Throw(); } - private PropertyInfo Wrapper(Expression> expression) + private static PropertyInfo Wrapper(Expression> expression) { return expression.GetProperty(); } private class StaticGetter { - public static string Value { get { return Guid.NewGuid().ToString(); } } + public static string Value => Guid.NewGuid().ToString(); } } } \ No newline at end of file diff --git a/ModelBuilder.UnitTests/ModelBuilder.UnitTests.csproj b/ModelBuilder.UnitTests/ModelBuilder.UnitTests.csproj index f734fa2..f9184cc 100644 --- a/ModelBuilder.UnitTests/ModelBuilder.UnitTests.csproj +++ b/ModelBuilder.UnitTests/ModelBuilder.UnitTests.csproj @@ -12,11 +12,11 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/ModelBuilder.UnitTests/ModelTests.cs b/ModelBuilder.UnitTests/ModelTests.cs index 2189790..8f0c7b8 100644 --- a/ModelBuilder.UnitTests/ModelTests.cs +++ b/ModelBuilder.UnitTests/ModelTests.cs @@ -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() { @@ -75,7 +83,12 @@ public void IgnoringUsesConfigurationToCreateInstance() [Fact] public void MappingUsesConfigurationToCreateInstance() { - var actual = Model.Mapping().Create()!; + var configuration = Model.Mapping(); + var executeStrategy = configuration.UsingExecuteStrategy>(); + + var actual = executeStrategy.Create()!; + + _output.WriteLine(executeStrategy.Log.Output); actual.Should().BeOfType(); actual.FirstName.Should().NotBeNullOrWhiteSpace(); diff --git a/ModelBuilder.UnitTests/Models/IItem.cs b/ModelBuilder.UnitTests/Models/IItem.cs new file mode 100644 index 0000000..29af482 --- /dev/null +++ b/ModelBuilder.UnitTests/Models/IItem.cs @@ -0,0 +1,7 @@ +namespace ModelBuilder.UnitTests.Models +{ + public interface IItem + { + void DoSomething(); + } +} \ No newline at end of file diff --git a/ModelBuilder.UnitTests/Models/Item.cs b/ModelBuilder.UnitTests/Models/Item.cs new file mode 100644 index 0000000..de5a41d --- /dev/null +++ b/ModelBuilder.UnitTests/Models/Item.cs @@ -0,0 +1,10 @@ +namespace ModelBuilder.UnitTests.Models +{ + public class Item : IItem + { + public void DoSomething() + { + throw new System.NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/ModelBuilder.UnitTests/Models/ParameterModel.cs b/ModelBuilder.UnitTests/Models/ParameterModel.cs new file mode 100644 index 0000000..b757f2f --- /dev/null +++ b/ModelBuilder.UnitTests/Models/ParameterModel.cs @@ -0,0 +1,12 @@ +namespace ModelBuilder.UnitTests.Models +{ + public class ParameterModel + { + public ParameterModel(T value) + { + Value = value; + } + + public T Value { get; } + } +} \ No newline at end of file diff --git a/ModelBuilder.UnitTests/Models/PropertyModel.cs b/ModelBuilder.UnitTests/Models/PropertyModel.cs new file mode 100644 index 0000000..93cd3e0 --- /dev/null +++ b/ModelBuilder.UnitTests/Models/PropertyModel.cs @@ -0,0 +1,7 @@ +namespace ModelBuilder.UnitTests.Models +{ + public class PropertyModel where T : notnull + { + public T Value { get; set; } = default!; + } +} \ No newline at end of file diff --git a/ModelBuilder.UnitTests/Models/ReadOnlyParent.cs b/ModelBuilder.UnitTests/Models/ReadOnlyParent.cs index f196607..fd30cf6 100644 --- a/ModelBuilder.UnitTests/Models/ReadOnlyParent.cs +++ b/ModelBuilder.UnitTests/Models/ReadOnlyParent.cs @@ -15,7 +15,7 @@ public ReadOnlyParent() PrivateValue = 0; } - private IEnumerable EmptySet() + private static IEnumerable EmptySet() { yield break; } diff --git a/ModelBuilder.UnitTests/Scenarios/TypeMappingTests.cs b/ModelBuilder.UnitTests/Scenarios/TypeMappingTests.cs new file mode 100644 index 0000000..b47ea82 --- /dev/null +++ b/ModelBuilder.UnitTests/Scenarios/TypeMappingTests.cs @@ -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().UsingExecuteStrategy>(); + + var actual = strategy.Create(); + + _output.WriteLine(strategy.Log.Output); + + actual.Should().BeOfType(); + } + + [Fact] + public void CanMapParameterType() + { + var strategy = Model.UsingExecuteStrategy>>(); + + var actual = strategy.Create()!; + + _output.WriteLine(strategy.Log.Output); + + actual.Value.Should().BeOfType(); + } + + [Fact] + public void CanMapPropertyType() + { + var strategy = Model.UsingExecuteStrategy>>(); + + var actual = strategy.Create()!; + + _output.WriteLine(strategy.Log.Output); + + actual.Value.Should().BeOfType(); + } + + [Fact] + public void CanMapRootType() + { + var strategy = Model.UsingExecuteStrategy>(); + + var actual = strategy.Create(); + + _output.WriteLine(strategy.Log.Output); + + actual.Should().BeOfType(); + } + + // ReSharper disable once ClassNeverInstantiated.Local + private class Private : IPrivate + { + } + } +} \ No newline at end of file diff --git a/ModelBuilder/ModelBuilder.csproj b/ModelBuilder/ModelBuilder.csproj index b2c91b5..95ae14c 100644 --- a/ModelBuilder/ModelBuilder.csproj +++ b/ModelBuilder/ModelBuilder.csproj @@ -49,7 +49,7 @@ - + all runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/ModelBuilder/TypeCreators/TypeCreatorBase.cs b/ModelBuilder/TypeCreators/TypeCreatorBase.cs index 98b26c4..f2e4109 100644 --- a/ModelBuilder/TypeCreators/TypeCreatorBase.cs +++ b/ModelBuilder/TypeCreators/TypeCreatorBase.cs @@ -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); }