Skip to content

Commit

Permalink
IOC hints needs to contain full type name.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobris committed Jan 29, 2024
1 parent 344fb1f commit 3d16a3e
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 4 deletions.
7 changes: 5 additions & 2 deletions BTDB.SourceGenerator.Test/GeneratorTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ protected static Task VerifySourceGenerator(string sourceCode)
// Assert the driver doesn't recompute the output
var result = runResult2.GetRunResult().Results.Single();
var allOutputs = result.TrackedOutputSteps.SelectMany(outputStep => outputStep.Value)
.SelectMany(output => output.Outputs);
Assert.Collection(allOutputs, output => Assert.Equal(IncrementalStepRunReason.Cached, output.Reason));
.SelectMany(output => output.Outputs).ToList();
if (allOutputs.Count != 0)
{
Assert.Collection(allOutputs, output => Assert.Equal(IncrementalStepRunReason.Cached, output.Reason));
}

return Verifier.Verify(runResult);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//HintName: TestNamespace.Logger.g.cs
//HintName: TestNamespace.Logger[int].g.cs
// <auto-generated/>
#pragma warning disable 612,618
using System;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//HintName: TestNamespace.Logger[int].g.cs
// <auto-generated/>
#pragma warning disable 612,618
using System;
using System.Runtime.CompilerServices;

namespace TestNamespace;

static file class LoggerRegistration
{
[ModuleInitializer]
internal static unsafe void Register4BTDB()
{
global::BTDB.IOC.IContainer.RegisterFactory(typeof(global::TestNamespace.Logger<int>), (container, ctx) =>
{
var f0 = container.CreateFactory(ctx, typeof(int), "a");
if (f0 == null) throw new global::System.ArgumentException("Cannot resolve int a parameter of TestNamespace.Logger<int>");
return (container2, ctx2) =>
{
var res = new global::TestNamespace.Logger<int>((int)(f0(container2, ctx2)));
return res;
};
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//HintName: TestNamespace.Logger[string].g.cs
// <auto-generated/>
#pragma warning disable 612,618
using System;
using System.Runtime.CompilerServices;

namespace TestNamespace;

static file class LoggerRegistration
{
[ModuleInitializer]
internal static unsafe void Register4BTDB()
{
global::BTDB.IOC.IContainer.RegisterFactory(typeof(global::TestNamespace.Logger<string>), (container, ctx) =>
{
var f0 = container.CreateFactory(ctx, typeof(string), "a");
if (f0 == null) throw new global::System.ArgumentException("Cannot resolve string a parameter of TestNamespace.Logger<string>");
return (container2, ctx2) =>
{
var res = new global::TestNamespace.Logger<string>(Unsafe.As<string>(f0(container2, ctx2)));
return res;
};
});
}
}
22 changes: 22 additions & 0 deletions BTDB.SourceGenerator.Test/IOCTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,28 @@ public Logger(T a)
");
}

[Fact]
public Task VerifyGenerateForCouldBeUsedForGenericClassesWithMultipleVariants()
{
// language=cs
return VerifySourceGenerator(@"
namespace TestNamespace;
public interface ILogger
{
}
[BTDB.GenerateFor(typeof(Logger<int>))]
[BTDB.GenerateFor(typeof(Logger<string>))]
public class Logger<T>: ILogger
{
public Logger(T a)
{
}
}
");
}

[Fact]
public Task VerifyGenerateForCouldBeUsedForChoosingDifferentConstructor()
{
Expand Down
2 changes: 1 addition & 1 deletion BTDB.SourceGenerator/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ internal static unsafe void Register4BTDB()
""";

context.AddSource(
$"{(generationInfo.Namespace == null ? "" : generationInfo.Namespace + ".") + generationInfo.Name}.g.cs",
$"{generationInfo.FullName.Replace("global::","").Replace("<","[").Replace(">","]")}.g.cs",
SourceText.From(code, Encoding.UTF8));
}
}
Expand Down

0 comments on commit 3d16a3e

Please sign in to comment.