|
| 1 | +using System.Globalization; |
| 2 | +using System.Reflection; |
| 3 | +using Bunit.Rendering; |
| 4 | +#if NET6_0_OR_GREATER |
| 5 | +using ParameterViewDictionary = System.Collections.Generic.Dictionary<string, object?>; |
| 6 | +#else |
| 7 | +using ParameterViewDictionary = System.Collections.Generic.Dictionary<string, object>; |
| 8 | +#endif |
| 9 | + |
| 10 | +namespace Bunit.TestDoubles; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// This is an internal service that is used to update components with route parameters. |
| 14 | +/// It is not meant to be used outside bUnit internal classes. |
| 15 | +/// </summary> |
| 16 | +public sealed class ComponentRouteParameterService |
| 17 | +{ |
| 18 | + private readonly ComponentRegistry componentRegistry; |
| 19 | + |
| 20 | + /// <summary> |
| 21 | + /// Initializes a new instance of the <see cref="ComponentRouteParameterService"/> class. |
| 22 | + /// </summary> |
| 23 | + public ComponentRouteParameterService(ComponentRegistry componentRegistry) |
| 24 | + { |
| 25 | + this.componentRegistry = componentRegistry; |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Triggers the components to update their parameters based on the route parameters. |
| 30 | + /// </summary> |
| 31 | + public void UpdateComponentsWithRouteParameters(Uri uri) |
| 32 | + { |
| 33 | + _ = uri ?? throw new ArgumentNullException(nameof(uri)); |
| 34 | + |
| 35 | + var relativeUri = uri.PathAndQuery; |
| 36 | + |
| 37 | + foreach (var instance in componentRegistry.Components) |
| 38 | + { |
| 39 | + var routeAttributes = GetRouteAttributesFromComponent(instance); |
| 40 | + |
| 41 | + if (routeAttributes.Length == 0) |
| 42 | + { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + foreach (var template in routeAttributes.Select(r => r.Template)) |
| 47 | + { |
| 48 | + var parameters = GetParametersFromTemplateAndUri(template, relativeUri, instance); |
| 49 | + if (parameters.Count > 0) |
| 50 | + { |
| 51 | + instance.SetParametersAsync(ParameterView.FromDictionary(parameters)); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static RouteAttribute[] GetRouteAttributesFromComponent(IComponent instance) => |
| 58 | + instance.GetType() |
| 59 | + .GetCustomAttributes(typeof(RouteAttribute), true) |
| 60 | + .Cast<RouteAttribute>() |
| 61 | + .ToArray(); |
| 62 | + |
| 63 | + private static ParameterViewDictionary GetParametersFromTemplateAndUri(string template, string relativeUri, IComponent instance) |
| 64 | + { |
| 65 | + var templateSegments = template.Trim('/').Split("/"); |
| 66 | + var uriSegments = relativeUri.Trim('/').Split("/"); |
| 67 | + |
| 68 | + if (templateSegments.Length > uriSegments.Length) |
| 69 | + { |
| 70 | + return []; |
| 71 | + } |
| 72 | + |
| 73 | + var parameters = new ParameterViewDictionary(); |
| 74 | + |
| 75 | + for (var i = 0; i < templateSegments.Length; i++) |
| 76 | + { |
| 77 | + var templateSegment = templateSegments[i]; |
| 78 | + if (templateSegment.StartsWith('{') && templateSegment.EndsWith('}')) |
| 79 | + { |
| 80 | + var parameterName = GetParameterName(templateSegment); |
| 81 | + var property = GetParameterProperty(instance, parameterName); |
| 82 | + |
| 83 | + if (property is null) |
| 84 | + { |
| 85 | + continue; |
| 86 | + } |
| 87 | + |
| 88 | + var isCatchAllParameter = templateSegment[1] == '*'; |
| 89 | + parameters[property.Name] = isCatchAllParameter |
| 90 | + ? string.Join("/", uriSegments.Skip(i)) |
| 91 | + : GetValue(uriSegments[i], property); |
| 92 | + } |
| 93 | + else if (templateSegment != uriSegments[i]) |
| 94 | + { |
| 95 | + return []; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return parameters; |
| 100 | + } |
| 101 | + |
| 102 | + private static string GetParameterName(string templateSegment) => |
| 103 | + templateSegment |
| 104 | + .Trim('{', '}', '*') |
| 105 | + .Replace("?", string.Empty, StringComparison.OrdinalIgnoreCase) |
| 106 | + .Split(':')[0]; |
| 107 | + |
| 108 | + private static PropertyInfo? GetParameterProperty(object instance, string propertyName) |
| 109 | + { |
| 110 | + var propertyInfos = instance.GetType() |
| 111 | + .GetProperties(BindingFlags.Public | BindingFlags.Instance); |
| 112 | + |
| 113 | + return Array.Find(propertyInfos, prop => prop.GetCustomAttributes(typeof(ParameterAttribute), true).Any() && |
| 114 | + string.Equals(prop.Name, propertyName, StringComparison.OrdinalIgnoreCase)); |
| 115 | + } |
| 116 | + |
| 117 | + private static object GetValue(string value, PropertyInfo property) |
| 118 | + { |
| 119 | + var underlyingType = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; |
| 120 | + return Convert.ChangeType(value, underlyingType, CultureInfo.InvariantCulture); |
| 121 | + } |
| 122 | +} |
0 commit comments