|
| 1 | +namespace Pure.DI.IntegrationTests; |
| 2 | + |
| 3 | +using Core; |
| 4 | + |
| 5 | +public class MultipleImplementationsTests |
| 6 | +{ |
| 7 | + [Fact] |
| 8 | + public async Task ShouldWarnWhenMultipleImplementationsHaveNoTag() |
| 9 | + { |
| 10 | + var result = await """ |
| 11 | + using System; |
| 12 | + using Pure.DI; |
| 13 | +
|
| 14 | + namespace Sample; |
| 15 | +
|
| 16 | + interface IService { } |
| 17 | + sealed class Service1 : IService { } |
| 18 | + sealed class Service2 : IService { } |
| 19 | +
|
| 20 | + partial class Composition |
| 21 | + { |
| 22 | + void Setup() => DI.Setup() |
| 23 | + .Bind<IService>().To<Service1, Service2>() |
| 24 | + .Root<IService>("Service"); |
| 25 | + } |
| 26 | +
|
| 27 | + public class Program |
| 28 | + { |
| 29 | + public static void Main() => Console.WriteLine(new Composition().Service.GetType().Name); |
| 30 | + } |
| 31 | + """.RunAsync(new Options(LanguageVersion.CSharp12)); |
| 32 | + |
| 33 | + result.Errors.ShouldBeEmpty(result); |
| 34 | + result.StdOut.ShouldBe(["Service2"], result); |
| 35 | + result.Warnings.Count.ShouldBe(2, result); |
| 36 | + result.Warnings.Count(i => i.Id == LogId.WarningOverriddenBinding && i.Locations.FirstOrDefault().GetSource() == "Service2").ShouldBe(1, result); |
| 37 | + result.Warnings.Count(i => i.Id == LogId.WarningBindingNotUsed && i.Locations.FirstOrDefault().GetSource() == "Service1").ShouldBe(1, result); |
| 38 | + } |
| 39 | + |
| 40 | + [Fact] |
| 41 | + public async Task ShouldWarnWhenMultipleImplementationsShareAnOrdinaryTag() |
| 42 | + { |
| 43 | + var result = await """ |
| 44 | + using System; |
| 45 | + using Pure.DI; |
| 46 | +
|
| 47 | + namespace Sample; |
| 48 | +
|
| 49 | + interface IService { } |
| 50 | + sealed class Service1 : IService { } |
| 51 | + sealed class Service2 : IService { } |
| 52 | +
|
| 53 | + partial class Composition |
| 54 | + { |
| 55 | + void Setup() => DI.Setup() |
| 56 | + .Bind<IService>("main").To<Service1, Service2>() |
| 57 | + .Root<IService>("Service", "main"); |
| 58 | + } |
| 59 | +
|
| 60 | + public class Program |
| 61 | + { |
| 62 | + public static void Main() => Console.WriteLine(new Composition().Service.GetType().Name); |
| 63 | + } |
| 64 | + """.RunAsync(new Options(LanguageVersion.CSharp12)); |
| 65 | + |
| 66 | + result.Errors.ShouldBeEmpty(result); |
| 67 | + result.StdOut.ShouldBe(["Service2"], result); |
| 68 | + result.Warnings.Count.ShouldBe(2, result); |
| 69 | + result.Warnings.Count(i => i.Id == LogId.WarningOverriddenBinding && i.Locations.FirstOrDefault().GetSource() == "Service2").ShouldBe(1, result); |
| 70 | + result.Warnings.Count(i => i.Id == LogId.WarningBindingNotUsed && i.Locations.FirstOrDefault().GetSource() == "Service1").ShouldBe(1, result); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public async Task ShouldSupportSixtyFourImplementationTypes() |
| 75 | + { |
| 76 | + var implementations = string.Join(", ", Enumerable.Repeat("Service", 64)); |
| 77 | + var source = """ |
| 78 | + using System; |
| 79 | + using Pure.DI; |
| 80 | +
|
| 81 | + namespace Sample; |
| 82 | +
|
| 83 | + interface IService { } |
| 84 | + sealed class Service : IService { } |
| 85 | +
|
| 86 | + partial class Composition |
| 87 | + { |
| 88 | + void Setup() => DI.Setup() |
| 89 | + .Bind<IService>(Tag.Unique).To<IMPLEMENTATIONS>() |
| 90 | + .Root<IService[]>("Services"); |
| 91 | + } |
| 92 | +
|
| 93 | + public class Program |
| 94 | + { |
| 95 | + public static void Main() => Console.WriteLine(new Composition().Services.Length); |
| 96 | + } |
| 97 | + """.Replace("IMPLEMENTATIONS", implementations); |
| 98 | + |
| 99 | + var result = await source.RunAsync(new Options(LanguageVersion.CSharp12)); |
| 100 | + |
| 101 | + result.Success.ShouldBeTrue(result); |
| 102 | + result.StdOut.ShouldBe(["64"], result); |
| 103 | + } |
| 104 | + |
| 105 | + [Fact] |
| 106 | + public async Task ShouldBindMultipleImplementationsWithLifetime() |
| 107 | + { |
| 108 | + var result = await """ |
| 109 | + using System; |
| 110 | + using System.Linq; |
| 111 | + using Pure.DI; |
| 112 | +
|
| 113 | + namespace Sample; |
| 114 | +
|
| 115 | + interface IService { } |
| 116 | + sealed class Service1 : IService { } |
| 117 | + sealed class Service2 : IService { } |
| 118 | + sealed class Service3 : IService { } |
| 119 | +
|
| 120 | + partial class Composition |
| 121 | + { |
| 122 | + void Setup() => DI.Setup() |
| 123 | + .Bind<IService>(Tag.Unique).As(Lifetime.Singleton) |
| 124 | + .To<Service1, Service2, Service3>() |
| 125 | + .Root<IService[]>("Services"); |
| 126 | + } |
| 127 | +
|
| 128 | + public class Program |
| 129 | + { |
| 130 | + public static void Main() |
| 131 | + { |
| 132 | + var composition = new Composition(); |
| 133 | + var services = composition.Services; |
| 134 | + Console.WriteLine(string.Join(",", services.Select(service => service.GetType().Name))); |
| 135 | + Console.WriteLine(services.SequenceEqual(composition.Services)); |
| 136 | + } |
| 137 | + } |
| 138 | + """.RunAsync(new Options(LanguageVersion.CSharp12)); |
| 139 | + |
| 140 | + result.Success.ShouldBeTrue(result); |
| 141 | + result.StdOut.ShouldBe(["Service1,Service2,Service3", "True"], result); |
| 142 | + } |
| 143 | +} |
0 commit comments