From ec4c9d3b431510cd9cb9f214a9c6eccc3ff0e0bc Mon Sep 17 00:00:00 2001 From: BEN ABT Date: Mon, 29 Apr 2024 15:54:49 +0200 Subject: [PATCH 1/2] add instance tests --- .../StrongOf.AspNetCore.csproj | 4 ++ .../StrongOf.FluentValidation.csproj | 4 ++ src/StrongOf.Json/StrongOf.Json.csproj | 4 ++ .../Factories/StrongOfInstanceFactory.cs | 19 ++++++-- src/StrongOf/StrongOf.csproj | 4 ++ .../Factories/StrongOfInstanceFactoryTests.cs | 46 +++++++++++++++++++ 6 files changed, 77 insertions(+), 4 deletions(-) create mode 100644 tests/StrongOf.UnitTests/Factories/StrongOfInstanceFactoryTests.cs diff --git a/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj b/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj index a90692f..167d22a 100644 --- a/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj +++ b/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj @@ -1,5 +1,9 @@  + + $(GlobalAssemblyNamePrefix).AspNetCore + + all diff --git a/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj b/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj index 4eb8ca7..a4dc403 100644 --- a/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj +++ b/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj @@ -1,5 +1,9 @@  + + $(GlobalAssemblyNamePrefix).FluentValidation + + all diff --git a/src/StrongOf.Json/StrongOf.Json.csproj b/src/StrongOf.Json/StrongOf.Json.csproj index 065027c..83b5ea0 100644 --- a/src/StrongOf.Json/StrongOf.Json.csproj +++ b/src/StrongOf.Json/StrongOf.Json.csproj @@ -1,5 +1,9 @@  + + $(GlobalAssemblyNamePrefix).Json + + all diff --git a/src/StrongOf/Factories/StrongOfInstanceFactory.cs b/src/StrongOf/Factories/StrongOfInstanceFactory.cs index 3d63933..7234bed 100644 --- a/src/StrongOf/Factories/StrongOfInstanceFactory.cs +++ b/src/StrongOf/Factories/StrongOfInstanceFactory.cs @@ -6,15 +6,25 @@ namespace StrongOf.Factories; /// /// A factory class for creating Lambda Expressions. /// -internal static class StrongOfInstanceFactory +public static class StrongOfInstanceFactory { /// - /// Creates a LambdaExpression with one parameter. + /// Creates a lambda expression representing the instantiation of a constructor with one parameter. /// - /// The type of the object to be created. + /// The type of object to be constructed. /// The type of the parameter for the constructor. - /// A LambdaExpression that represents the creation of an object of type TStrong with a constructor parameter of type TTarget. + /// A lambda expression representing the constructor invocation. + /// Thrown when no constructor is found for the specified type with the given parameter type. + /// + /// The following example demonstrates how to use the CreateWithOneParameterExpression method. + /// + /// var expression = LambdaExpressionHelper.CreateWithOneParameterExpression<TestClass, int>(); + /// Func<int, TestClass> func = (Func<int, TestClass>)expression.Compile(); + /// TestClass instance = func.Invoke(42); + /// + /// public static LambdaExpression CreateWithOneParameterExpression() + where TStrong : class, IStrongOf { Type ctorParameterType = typeof(TTarget); ParameterExpression ctorParameter = Expression.Parameter(ctorParameterType); @@ -36,6 +46,7 @@ public static LambdaExpression CreateWithOneParameterExpressionThe type of the parameter for the constructor. /// A Func delegate that represents the creation of an object of type TStrong with a constructor parameter of type TTarget. public static Func CreateWithOneParameterDelegate() + where TStrong : class, IStrongOf { return (Func)CreateWithOneParameterExpression().Compile(); } diff --git a/src/StrongOf/StrongOf.csproj b/src/StrongOf/StrongOf.csproj index be03d13..7188ac2 100644 --- a/src/StrongOf/StrongOf.csproj +++ b/src/StrongOf/StrongOf.csproj @@ -1,5 +1,9 @@  + + $(GlobalAssemblyNamePrefix) + + all diff --git a/tests/StrongOf.UnitTests/Factories/StrongOfInstanceFactoryTests.cs b/tests/StrongOf.UnitTests/Factories/StrongOfInstanceFactoryTests.cs new file mode 100644 index 0000000..1d82372 --- /dev/null +++ b/tests/StrongOf.UnitTests/Factories/StrongOfInstanceFactoryTests.cs @@ -0,0 +1,46 @@ +using System.Linq.Expressions; +using System.Reflection; +using StrongOf.Factories; +using Xunit; + +namespace StrongOf.UnitTests.Factories; + +public class StrongOfInstanceFactoryTests +{ + private sealed class TestInt32Of(int Value) : StrongInt32(Value) { } + + [Fact] + public void CreateWithOneParameterExpression_SuccessfullyCreatesLambdaExpression() + { + // Arrange + Type expectedStrong = typeof(TestInt32Of); + Type expectedType = typeof(int); + ConstructorInfo? expectedCtor = expectedStrong.GetConstructor([expectedType]); + + ArgumentNullException.ThrowIfNull(expectedCtor); + + // Act + LambdaExpression lambdaExpression = StrongOfInstanceFactory.CreateWithOneParameterExpression(); + + // Assert + Assert.NotNull(lambdaExpression); + + // Assert type + Assert.IsAssignableFrom(lambdaExpression); + Assert.IsAssignableFrom>>(lambdaExpression); + + // Assert instance + Func func = (Func)lambdaExpression.Compile(); + TestInt32Of newInstance = func.Invoke(10); + + Assert.NotNull(newInstance); + Assert.IsType(newInstance); + + // Assert params + Expression? ctorParameter = ((NewExpression)lambdaExpression.Body).Arguments.First(); + Assert.Equal(expectedType, ctorParameter.Type); + + ConstructorInfo? ctor = ((NewExpression)lambdaExpression.Body).Constructor; + Assert.Equal(expectedCtor, ctor); + } +} From 980d19009a1c6749a37f2b13669494a2c5e8e4c0 Mon Sep 17 00:00:00 2001 From: BEN ABT Date: Mon, 29 Apr 2024 15:58:27 +0200 Subject: [PATCH 2/2] fix package name --- src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj | 1 + src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj | 1 + src/StrongOf.Json/StrongOf.Json.csproj | 1 + src/StrongOf/StrongOf.csproj | 1 + 4 files changed, 4 insertions(+) diff --git a/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj b/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj index 167d22a..ba10afc 100644 --- a/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj +++ b/src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj @@ -12,6 +12,7 @@ + StrongOf.AspNetCore true readme.md LICENSE.txt diff --git a/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj b/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj index a4dc403..8e8d250 100644 --- a/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj +++ b/src/StrongOf.FluentValidation/StrongOf.FluentValidation.csproj @@ -12,6 +12,7 @@ + StrongOf.FluentValidation true readme.md LICENSE.txt diff --git a/src/StrongOf.Json/StrongOf.Json.csproj b/src/StrongOf.Json/StrongOf.Json.csproj index 83b5ea0..0a724f5 100644 --- a/src/StrongOf.Json/StrongOf.Json.csproj +++ b/src/StrongOf.Json/StrongOf.Json.csproj @@ -12,6 +12,7 @@ + StrongOf.Json true readme.md LICENSE.txt diff --git a/src/StrongOf/StrongOf.csproj b/src/StrongOf/StrongOf.csproj index 7188ac2..95f7bff 100644 --- a/src/StrongOf/StrongOf.csproj +++ b/src/StrongOf/StrongOf.csproj @@ -12,6 +12,7 @@ + StrongOf true readme.md LICENSE.txt