|
| 1 | +namespace Pure.DI.IntegrationTests; |
| 2 | + |
| 3 | +using Core; |
| 4 | + |
| 5 | +public class SimplifiedBindingContractInferenceTests |
| 6 | +{ |
| 7 | + public interface IMetadataBaseContract { } |
| 8 | + public interface IMetadataDerivedContract : IMetadataBaseContract { } |
| 9 | + public sealed class MetadataRouter : IMetadataBaseContract { } |
| 10 | + public sealed class MetadataRepository : IMetadataDerivedContract { } |
| 11 | + |
| 12 | + [Fact] |
| 13 | + public async Task ShouldInferOnlyDirectContractsForTypesFromReferencedAssembly() |
| 14 | + { |
| 15 | + // When |
| 16 | + var result = await """ |
| 17 | + using System; |
| 18 | + using Pure.DI; |
| 19 | + using IBaseContract = Pure.DI.IntegrationTests.SimplifiedBindingContractInferenceTests.IMetadataBaseContract; |
| 20 | + using Router = Pure.DI.IntegrationTests.SimplifiedBindingContractInferenceTests.MetadataRouter; |
| 21 | + using Repository = Pure.DI.IntegrationTests.SimplifiedBindingContractInferenceTests.MetadataRepository; |
| 22 | +
|
| 23 | + namespace Sample; |
| 24 | +
|
| 25 | + static class Setup |
| 26 | + { |
| 27 | + public static void Configure() => DI.Setup("BaseComposition", CompositionKind.Internal) |
| 28 | + .Singleton<Router, Repository>(); |
| 29 | + } |
| 30 | +
|
| 31 | + partial class Composition |
| 32 | + { |
| 33 | + void Setup() => DI.Setup() |
| 34 | + .DependsOn("BaseComposition") |
| 35 | + .Root<IBaseContract>("Root"); |
| 36 | + } |
| 37 | +
|
| 38 | + public class Program |
| 39 | + { |
| 40 | + public static void Main() => |
| 41 | + Console.WriteLine(new Composition().Root.GetType().Name); |
| 42 | + } |
| 43 | + """ |
| 44 | + .RunAsync(new Options( |
| 45 | + LanguageVersion.Preview, |
| 46 | + MetadataReferences: [MetadataReference.CreateFromFile(typeof(SimplifiedBindingContractInferenceTests).Assembly.Location)])); |
| 47 | + |
| 48 | + // Then |
| 49 | + result.Errors.ShouldBeEmpty(result); |
| 50 | + result.Warnings.ShouldBeEmpty(result); |
| 51 | + result.StdOut.ShouldBe(["MetadataRouter"], result); |
| 52 | + } |
| 53 | + |
| 54 | + public static TheoryData<string> Bindings => new() |
| 55 | + { |
| 56 | + { |
| 57 | + """ |
| 58 | + .Bind().To<FirstService>() |
| 59 | + .Bind().To<SecondService>() |
| 60 | + .Bind().To<FirstConcreteService>() |
| 61 | + .Bind().To<SecondConcreteService>() |
| 62 | + """ |
| 63 | + }, |
| 64 | + { |
| 65 | + """ |
| 66 | + .PerBlock<FirstService>() |
| 67 | + .PerBlock<SecondService>() |
| 68 | + .PerBlock<FirstConcreteService>() |
| 69 | + .PerBlock<SecondConcreteService>() |
| 70 | + """ |
| 71 | + }, |
| 72 | + { |
| 73 | + """ |
| 74 | + .PerBlock<FirstService, SecondService, FirstConcreteService, SecondConcreteService>() |
| 75 | + """ |
| 76 | + }, |
| 77 | + { |
| 78 | + """ |
| 79 | + .PerBlock<Dependency, FirstService>(dependency => new FirstService(dependency)) |
| 80 | + .PerBlock<Dependency, SecondService>(dependency => new SecondService(dependency)) |
| 81 | + .PerBlock<Dependency, FirstConcreteService>(dependency => new FirstConcreteService(dependency)) |
| 82 | + .PerBlock<Dependency, SecondConcreteService>(dependency => new SecondConcreteService(dependency)) |
| 83 | + """ |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + [Theory] |
| 88 | + [MemberData(nameof(Bindings))] |
| 89 | + public async Task ShouldInferTheSameContractsForAllSimplifiedBindingForms(string bindings) |
| 90 | + { |
| 91 | + // When |
| 92 | + var result = await """ |
| 93 | + using System; |
| 94 | + using System.Collections; |
| 95 | + using System.Collections.Generic; |
| 96 | + using System.Threading.Tasks; |
| 97 | + using Pure.DI; |
| 98 | +
|
| 99 | + namespace Sample; |
| 100 | +
|
| 101 | + interface IInheritedFromDirectInterface { } |
| 102 | + interface IDirectAbstractContract : IInheritedFromDirectInterface { } |
| 103 | + interface IDirectConcreteContract { } |
| 104 | + interface ICustomSpecialContract { } |
| 105 | + interface IInheritedFromAbstractBase { } |
| 106 | + interface IInheritedFromConcreteBase { } |
| 107 | +
|
| 108 | + abstract class AbstractBase : IInheritedFromAbstractBase { } |
| 109 | + class ConcreteBase : IInheritedFromConcreteBase { } |
| 110 | + sealed class Dependency { } |
| 111 | +
|
| 112 | + sealed class FirstService : |
| 113 | + AbstractBase, |
| 114 | + IDirectAbstractContract, |
| 115 | + ICustomSpecialContract, |
| 116 | + IDisposable, |
| 117 | + IAsyncDisposable, |
| 118 | + IEnumerable<int> |
| 119 | + { |
| 120 | + public FirstService(Dependency dependency) { } |
| 121 | + public void Dispose() { } |
| 122 | + public ValueTask DisposeAsync() => ValueTask.CompletedTask; |
| 123 | + public IEnumerator<int> GetEnumerator() => ((IEnumerable<int>)Array.Empty<int>()).GetEnumerator(); |
| 124 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 125 | + } |
| 126 | +
|
| 127 | + sealed class SecondService : |
| 128 | + AbstractBase, |
| 129 | + IDirectAbstractContract, |
| 130 | + ICustomSpecialContract, |
| 131 | + IDisposable, |
| 132 | + IAsyncDisposable, |
| 133 | + IEnumerable<int> |
| 134 | + { |
| 135 | + public SecondService(Dependency dependency) { } |
| 136 | + public void Dispose() { } |
| 137 | + public ValueTask DisposeAsync() => ValueTask.CompletedTask; |
| 138 | + public IEnumerator<int> GetEnumerator() => ((IEnumerable<int>)Array.Empty<int>()).GetEnumerator(); |
| 139 | + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); |
| 140 | + } |
| 141 | +
|
| 142 | + sealed class FirstConcreteService : ConcreteBase, IDirectConcreteContract |
| 143 | + { |
| 144 | + public FirstConcreteService(Dependency dependency) { } |
| 145 | + } |
| 146 | +
|
| 147 | + sealed class SecondConcreteService : ConcreteBase, IDirectConcreteContract |
| 148 | + { |
| 149 | + public SecondConcreteService(Dependency dependency) { } |
| 150 | + } |
| 151 | +
|
| 152 | + partial class Composition |
| 153 | + { |
| 154 | + void Setup() => DI.Setup() |
| 155 | + .SpecialType<ICustomSpecialContract>() |
| 156 | + .Bind().To<Dependency>() |
| 157 | + #bindings# |
| 158 | + .Root<FirstService>("First") |
| 159 | + .Root<SecondService>("Second") |
| 160 | + .Root<FirstConcreteService>("FirstConcrete") |
| 161 | + .Root<SecondConcreteService>("SecondConcrete"); |
| 162 | + } |
| 163 | +
|
| 164 | + public class Program |
| 165 | + { |
| 166 | + public static void Main() |
| 167 | + { |
| 168 | + var composition = new Composition(); |
| 169 | + Console.WriteLine($"{composition.First.GetType().Name}," + |
| 170 | + $"{composition.Second.GetType().Name}," + |
| 171 | + $"{composition.FirstConcrete.GetType().Name}," + |
| 172 | + composition.SecondConcrete.GetType().Name); |
| 173 | + } |
| 174 | + } |
| 175 | + """ |
| 176 | + .Replace("#bindings#", bindings) |
| 177 | + .RunAsync(new Options(LanguageVersion.Preview)); |
| 178 | + |
| 179 | + // Then |
| 180 | + result.Errors.ShouldBeEmpty(result); |
| 181 | + result.StdOut.ShouldBe(["FirstService,SecondService,FirstConcreteService,SecondConcreteService"], result); |
| 182 | + |
| 183 | + var overriddenContracts = result.Warnings |
| 184 | + .Where(i => i.Id == LogId.WarningOverriddenBinding) |
| 185 | + .Select(i => i.Message) |
| 186 | + .ToArray(); |
| 187 | + |
| 188 | + overriddenContracts.Length.ShouldBe(3, result); |
| 189 | + overriddenContracts.Count(i => i.Contains("AbstractBase", StringComparison.Ordinal)).ShouldBe(1, result); |
| 190 | + overriddenContracts.Count(i => i.Contains("IDirectAbstractContract", StringComparison.Ordinal)).ShouldBe(1, result); |
| 191 | + overriddenContracts.Count(i => i.Contains("IDirectConcreteContract", StringComparison.Ordinal)).ShouldBe(1, result); |
| 192 | + |
| 193 | + foreach (var message in overriddenContracts) |
| 194 | + { |
| 195 | + message.ShouldNotContain("ConcreteBase"); |
| 196 | + message.ShouldNotContain("IInheritedFromAbstractBase"); |
| 197 | + message.ShouldNotContain("IInheritedFromConcreteBase"); |
| 198 | + message.ShouldNotContain("IInheritedFromDirectInterface"); |
| 199 | + message.ShouldNotContain("ICustomSpecialContract"); |
| 200 | + message.ShouldNotContain("IDisposable"); |
| 201 | + message.ShouldNotContain("IAsyncDisposable"); |
| 202 | + message.ShouldNotContain("IEnumerable"); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments