Skip to content

Commit

Permalink
Fix expressions in factories using typeof() as a tag
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Dec 16, 2024
1 parent b92c1c9 commit 2af8553
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 22 deletions.
21 changes: 1 addition & 20 deletions src/Pure.DI.Core/Core/Code/FactoryRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,26 +244,7 @@ private bool TryInitialize(
&& node is { Expression: IdentifierNameSyntax identifierName, Name.Identifier.Text: nameof(IContext.Tag) }
&& identifierName.Identifier.Text == factory.Source.Context.Identifier.Text)
{
var token = SyntaxFactory.ParseToken(variable.Injection.Tag.ValueToString());
if (token.IsKind(SyntaxKind.NumericLiteralToken))
{
return SyntaxFactory.LiteralExpression(SyntaxKind.NumericLiteralExpression, token);
}

if (token.IsKind(SyntaxKind.CharacterLiteralToken))
{
return SyntaxFactory.LiteralExpression(SyntaxKind.CharacterLiteralExpression, token);
}

if (token.IsKind(SyntaxKind.StringLiteralToken))
{
return SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, token);
}

if (token.IsKind(SyntaxKind.NullKeyword))
{
return SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, token);
}
return SyntaxFactory.ParseExpression(variable.Injection.Tag.ValueToString());
}

return base.VisitMemberAccessExpression(node);
Expand Down
94 changes: 92 additions & 2 deletions tests/Pure.DI.IntegrationTests/FactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,8 @@ public class Program
{
public static void Main()
{
var composition = new Composition();
var service = composition.Service;
var composition = new Composition();
var service = composition.Service;
}
}
}
Expand All @@ -663,6 +663,96 @@ public static void Main()
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe(["Created"], result);
}

[Theory]
[InlineData("123", "ctx.Tag", "123")]
[InlineData("\"123\"", "ctx.Tag", "123")]
[InlineData("'1'", "((char)ctx.Tag) + 1", "50")]
[InlineData("123", "(object)ctx.Tag", "123")]
[InlineData("123", "int.Parse(ctx.Tag.ToString() ?? \"\")", "123")]
[InlineData("123", "dependency.GetInt((int)ctx.Tag)", "123")]
[InlineData("123", "((int)ctx.Tag) * 2", "246")]
[InlineData("123", "dependency.GetInt((int)ctx.Tag) * 2", "246")]
[InlineData("typeof(int)", "ctx.Tag", "System.Int32")]
public async Task ShouldSupportFactoryWhenUsingParentTag(string tag, string tagExpression, string output)
{
// Given

// When
var result = await """
using System;
using Pure.DI;
namespace Sample
{
interface IDependency
{
int GetInt(int val);
}
class Dependency: IDependency
{
public int GetInt(int val) => val;
}
interface IService
{
IDependency Dep { get; }
}
class Service: IService
{
public Service(IDependency dep)
{
Dep = dep;
Console.WriteLine("Created");
}
public IDependency Dep { get; }
}
internal partial class Composition
{
private partial T OnDependencyInjection<T>(in T value, object? tag, Lifetime lifetime)
{
return value;
}
}
static class Setup
{
private static void SetupComposition()
{
// OnDependencyInjection = On
DI.Setup("Composition")
.Bind<IDependency>(#tag#).To(ctx => new Dependency())
.Bind<IService>(#tag#).To(ctx => {
ctx.Inject<IDependency>(ctx.Tag, out var dependency);
Console.WriteLine(#tagExpression#);
return new Service(dependency);
})
.Root<IService>("Service", #tag#);
}
}
public class Program
{
public static void Main()
{
var composition = new Composition();
var service = composition.Service;
}
}
}
"""
.Replace("#tagExpression#", tagExpression)
.Replace("#tag#", tag)
.RunAsync(new Options(LanguageVersion.CSharp9));

// Then
result.Success.ShouldBeTrue(result);
result.StdOut.ShouldBe([output, "Created"], result);
}

[Fact]
public async Task ShouldSupportFactoryWithInjectInFunc()
Expand Down

0 comments on commit 2af8553

Please sign in to comment.