-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f895f30
commit 7f4a437
Showing
11 changed files
with
242 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
namespace Pure.DI.IntegrationTests; | ||
|
||
public class TypeAttributeTests | ||
{ | ||
[Fact] | ||
public async Task ShouldSupportTypeAttribute() | ||
{ | ||
// Given | ||
|
||
// When | ||
var result = await """ | ||
using System; | ||
using Pure.DI; | ||
namespace Sample | ||
{ | ||
interface IDependency { } | ||
class AbcDependency : IDependency { } | ||
class XyzDependency : IDependency { } | ||
interface IService | ||
{ | ||
IDependency Dependency1 { get; } | ||
IDependency Dependency2 { get; } | ||
} | ||
class Service : IService | ||
{ | ||
public Service( | ||
[Type(typeof(AbcDependency))] IDependency dependency1, | ||
[Type(typeof(XyzDependency))] IDependency dependency2) | ||
{ | ||
Dependency1 = dependency1; | ||
Dependency2 = dependency2; | ||
} | ||
public IDependency Dependency1 { get; } | ||
public IDependency Dependency2 { get; } | ||
} | ||
partial class Composition | ||
{ | ||
private static void SetupComposition() | ||
{ | ||
DI.Setup(nameof(Composition)) | ||
.Bind<IService>().To<Service>() | ||
// Composition root | ||
.Root<IService>("Root"); | ||
} | ||
} | ||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
var composition = new Composition(); | ||
var service = composition.Root; | ||
Console.WriteLine(service.Dependency1); | ||
Console.WriteLine(service.Dependency2); | ||
} | ||
} | ||
} | ||
""".RunAsync(); | ||
|
||
// Then | ||
result.Success.ShouldBeTrue(result); | ||
result.StdOut.ShouldBe(["Sample.AbcDependency", "Sample.XyzDependency"], result); | ||
} | ||
|
||
#if ROSLYN4_8_OR_GREATER | ||
[Fact] | ||
public async Task ShouldSupportGenericTypeAttribute() | ||
{ | ||
// Given | ||
|
||
// When | ||
var result = await """ | ||
using System; | ||
using Pure.DI; | ||
namespace Sample | ||
{ | ||
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)] | ||
internal class TypeAttribute<T> : Attribute | ||
{ | ||
} | ||
interface IDependency { } | ||
class AbcDependency : IDependency { } | ||
class XyzDependency : IDependency { } | ||
interface IService | ||
{ | ||
IDependency Dependency1 { get; } | ||
IDependency Dependency2 { get; } | ||
} | ||
class Service : IService | ||
{ | ||
public Service( | ||
[Type<AbcDependency>] IDependency dependency1, | ||
[Type<XyzDependency>] IDependency dependency2) | ||
{ | ||
Dependency1 = dependency1; | ||
Dependency2 = dependency2; | ||
} | ||
public IDependency Dependency1 { get; } | ||
public IDependency Dependency2 { get; } | ||
} | ||
partial class Composition | ||
{ | ||
private static void SetupComposition() | ||
{ | ||
DI.Setup(nameof(Composition)) | ||
.TypeAttribute<TypeAttribute<TT>>() | ||
.Bind<IService>().To<Service>() | ||
// Composition root | ||
.Root<IService>("Root"); | ||
} | ||
} | ||
public class Program | ||
{ | ||
public static void Main() | ||
{ | ||
var composition = new Composition(); | ||
var service = composition.Root; | ||
Console.WriteLine(service.Dependency1); | ||
Console.WriteLine(service.Dependency2); | ||
} | ||
} | ||
} | ||
""".RunAsync(new Options(LanguageVersion.CSharp11)); | ||
|
||
// Then | ||
result.Success.ShouldBeTrue(result); | ||
result.StdOut.ShouldBe(["Sample.AbcDependency", "Sample.XyzDependency"], result); | ||
} | ||
#endif | ||
} |
Oops, something went wrong.