|
| 1 | +namespace Atc.Test; |
| 2 | + |
| 3 | +internal static class FrozenParameterInjector |
| 4 | +{ |
| 5 | + private static readonly MethodInfo? InjectMethod = typeof(FixtureRegistrar).GetMethod( |
| 6 | + nameof(FixtureRegistrar.Inject), |
| 7 | + BindingFlags.Public | BindingFlags.Static); |
| 8 | + |
| 9 | + /// <summary> |
| 10 | + /// Builds an injector delegate that performs positional frozen injections and (optionally) exact-type promotions. |
| 11 | + /// </summary> |
| 12 | + /// <param name="parameters">Ordered method parameters.</param> |
| 13 | + /// <param name="enableExactTypePromotion">When true, a later frozen parameter whose index exceeds supplied row length |
| 14 | + /// will re-use an earlier supplied value only if the earlier parameter type is exactly the same (no interface/base widening).</param> |
| 15 | + /// <returns>An injector delegate accepting (suppliedRowValues, fixture) that performs injections/promotions.</returns> |
| 16 | + internal static Action<object?[], IFixture> Build( |
| 17 | + ParameterInfo[] parameters, |
| 18 | + bool enableExactTypePromotion) |
| 19 | + { |
| 20 | + var frozenParameters = parameters |
| 21 | + .Select((p, i) => new FrozenDescriptor(i, p, p.GetCustomAttribute<FrozenAttribute>() != null)) |
| 22 | + .Where(x => x.IsFrozen) |
| 23 | + .ToArray(); |
| 24 | + |
| 25 | + if (frozenParameters.Length == 0) |
| 26 | + { |
| 27 | + return static (_, _) => { }; |
| 28 | + } |
| 29 | + |
| 30 | + // Pre-compute mapping from type to earliest supplied parameter index for fast exact promotion lookup. |
| 31 | + var earliestIndexByType = parameters |
| 32 | + .Select((p, i) => (p.ParameterType, Index: i)) |
| 33 | + .GroupBy(x => x.ParameterType) |
| 34 | + .ToDictionary(g => g.Key, g => g.Min(x => x.Index)); |
| 35 | + |
| 36 | + return (suppliedData, fixture) => |
| 37 | + { |
| 38 | + // Phase 1: direct positional injection where row already supplies the frozen parameter value. |
| 39 | + foreach (var frozen in frozenParameters) |
| 40 | + { |
| 41 | + if (suppliedData.Length > frozen.Index) |
| 42 | + { |
| 43 | + InjectGeneric(fixture, frozen.Parameter.ParameterType, suppliedData[frozen.Index]); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (!enableExactTypePromotion) |
| 48 | + { |
| 49 | + return; // Class data does not promote. |
| 50 | + } |
| 51 | + |
| 52 | + // Phase 2: exact-type promotion only (no interface/base assignability) to avoid cross-interface bleed. |
| 53 | + foreach (var frozen in frozenParameters) |
| 54 | + { |
| 55 | + if (suppliedData.Length > frozen.Index) |
| 56 | + { |
| 57 | + continue; // Already handled by direct positional reuse. |
| 58 | + } |
| 59 | + |
| 60 | + if (!earliestIndexByType.TryGetValue(frozen.Parameter.ParameterType, out var earliestIndex)) |
| 61 | + { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + if (earliestIndex >= suppliedData.Length) |
| 66 | + { |
| 67 | + continue; // Earliest instance not actually supplied in this row. |
| 68 | + } |
| 69 | + |
| 70 | + // Reuse supplied instance at earliest index for this exact type. |
| 71 | + var instance = suppliedData[earliestIndex]; |
| 72 | + InjectGeneric(fixture, frozen.Parameter.ParameterType, instance); |
| 73 | + } |
| 74 | + }; |
| 75 | + } |
| 76 | + |
| 77 | + private static void InjectGeneric( |
| 78 | + IFixture fixture, |
| 79 | + Type type, |
| 80 | + object? instance) |
| 81 | + => InjectMethod? |
| 82 | + .MakeGenericMethod(type) |
| 83 | + .Invoke(null, [fixture, instance]); |
| 84 | + |
| 85 | + private readonly struct FrozenDescriptor( |
| 86 | + int index, |
| 87 | + ParameterInfo parameter, |
| 88 | + bool isFrozen) |
| 89 | + { |
| 90 | + public int Index { get; } = index; |
| 91 | + |
| 92 | + public ParameterInfo Parameter { get; } = parameter; |
| 93 | + |
| 94 | + public bool IsFrozen { get; } = isFrozen; |
| 95 | + } |
| 96 | +} |
0 commit comments