Skip to content
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

add missing assembly prefix and instance tests #17

Merged
merged 2 commits into from
Apr 29, 2024
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
5 changes: 5 additions & 0 deletions src/StrongOf.AspNetCore/StrongOf.AspNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>$(GlobalAssemblyNamePrefix).AspNetCore</AssemblyName>
</PropertyGroup>

<ItemGroup Label="Analyzers">
<PackageReference Include="Meziantou.Analyzer">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -8,6 +12,7 @@
</ItemGroup>

<PropertyGroup Label="Package">
<PackageId>StrongOf.AspNetCore</PackageId>
<IsPackable>true</IsPackable>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>$(GlobalAssemblyNamePrefix).FluentValidation</AssemblyName>
</PropertyGroup>

<ItemGroup Label="Analyzers">
<PackageReference Include="Meziantou.Analyzer">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -8,6 +12,7 @@
</ItemGroup>

<PropertyGroup Label="Package">
<PackageId>StrongOf.FluentValidation</PackageId>
<IsPackable>true</IsPackable>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
Expand Down
5 changes: 5 additions & 0 deletions src/StrongOf.Json/StrongOf.Json.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>$(GlobalAssemblyNamePrefix).Json</AssemblyName>
</PropertyGroup>

<ItemGroup Label="Analyzers">
<PackageReference Include="Meziantou.Analyzer">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -8,6 +12,7 @@
</ItemGroup>

<PropertyGroup Label="Package">
<PackageId>StrongOf.Json</PackageId>
<IsPackable>true</IsPackable>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
Expand Down
19 changes: 15 additions & 4 deletions src/StrongOf/Factories/StrongOfInstanceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,25 @@ namespace StrongOf.Factories;
/// <summary>
/// A factory class for creating Lambda Expressions.
/// </summary>
internal static class StrongOfInstanceFactory
public static class StrongOfInstanceFactory
{
/// <summary>
/// Creates a LambdaExpression with one parameter.
/// Creates a lambda expression representing the instantiation of a constructor with one parameter.
/// </summary>
/// <typeparam name="TStrong">The type of the object to be created.</typeparam>
/// <typeparam name="TStrong">The type of object to be constructed.</typeparam>
/// <typeparam name="TTarget">The type of the parameter for the constructor.</typeparam>
/// <returns>A LambdaExpression that represents the creation of an object of type TStrong with a constructor parameter of type TTarget.</returns>
/// <returns>A lambda expression representing the constructor invocation.</returns>
/// <exception cref="InvalidOperationException">Thrown when no constructor is found for the specified type with the given parameter type.</exception>
/// <example>
/// The following example demonstrates how to use the CreateWithOneParameterExpression method.
/// <code>
/// var expression = LambdaExpressionHelper.CreateWithOneParameterExpression&lt;TestClass, int&gt;();
/// Func&lt;int, TestClass&gt; func = (Func&lt;int, TestClass&gt;)expression.Compile();
/// TestClass instance = func.Invoke(42);
/// </code>
/// </example>
public static LambdaExpression CreateWithOneParameterExpression<TStrong, TTarget>()
where TStrong : class, IStrongOf
{
Type ctorParameterType = typeof(TTarget);
ParameterExpression ctorParameter = Expression.Parameter(ctorParameterType);
Expand All @@ -36,6 +46,7 @@ public static LambdaExpression CreateWithOneParameterExpression<TStrong, TTarget
/// <typeparam name="TTarget">The type of the parameter for the constructor.</typeparam>
/// <returns>A Func delegate that represents the creation of an object of type TStrong with a constructor parameter of type TTarget.</returns>
public static Func<TTarget, TStrong> CreateWithOneParameterDelegate<TStrong, TTarget>()
where TStrong : class, IStrongOf
{
return (Func<TTarget, TStrong>)CreateWithOneParameterExpression<TStrong, TTarget>().Compile();
}
Expand Down
5 changes: 5 additions & 0 deletions src/StrongOf/StrongOf.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>$(GlobalAssemblyNamePrefix)</AssemblyName>
</PropertyGroup>

<ItemGroup Label="Analyzers">
<PackageReference Include="Meziantou.Analyzer">
<PrivateAssets>all</PrivateAssets>
Expand All @@ -8,6 +12,7 @@
</ItemGroup>

<PropertyGroup Label="Package">
<PackageId>StrongOf</PackageId>
<IsPackable>true</IsPackable>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
Expand Down
46 changes: 46 additions & 0 deletions tests/StrongOf.UnitTests/Factories/StrongOfInstanceFactoryTests.cs
Original file line number Diff line number Diff line change
@@ -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<TestInt32Of>(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<TestInt32Of, int>();

// Assert
Assert.NotNull(lambdaExpression);

// Assert type
Assert.IsAssignableFrom<LambdaExpression>(lambdaExpression);
Assert.IsAssignableFrom<Expression<Func<int, TestInt32Of>>>(lambdaExpression);

// Assert instance
Func<int, TestInt32Of> func = (Func<int, TestInt32Of>)lambdaExpression.Compile();
TestInt32Of newInstance = func.Invoke(10);

Assert.NotNull(newInstance);
Assert.IsType<TestInt32Of>(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);
}
}