|
1 | 1 | using System.Globalization;
|
2 | 2 | using System.Reflection;
|
3 | 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 |
4 | 9 |
|
5 | 10 | namespace Bunit.TestDoubles;
|
6 | 11 |
|
@@ -40,69 +45,58 @@ public void UpdateComponentsWithRouteParameters(Uri uri)
|
40 | 45 |
|
41 | 46 | foreach (var template in routeAttributes.Select(r => r.Template))
|
42 | 47 | {
|
43 |
| - var templateSegments = template.Trim('/').Split("/"); |
44 |
| - var uriSegments = relativeUri.Trim('/').Split("/"); |
45 |
| - |
46 |
| - if (templateSegments.Length > uriSegments.Length) |
| 48 | + var parameters = GetParametersFromTemplateAndUri(template, relativeUri, instance); |
| 49 | + if (parameters.Count > 0) |
47 | 50 | {
|
48 |
| - continue; |
| 51 | + instance.SetParametersAsync(ParameterView.FromDictionary(parameters)); |
49 | 52 | }
|
50 |
| -#if NET6_0_OR_GREATER |
51 |
| - var parameters = new Dictionary<string, object?>(); |
52 |
| -#else |
53 |
| - var parameters = new Dictionary<string, object>(); |
54 |
| -#endif |
| 53 | + } |
| 54 | + } |
| 55 | + } |
55 | 56 |
|
56 |
| - for (var i = 0; i < templateSegments.Length; i++) |
57 |
| - { |
58 |
| - var templateSegment = templateSegments[i]; |
59 |
| - if (templateSegment.StartsWith('{') && templateSegment.EndsWith('}')) |
60 |
| - { |
61 |
| - var parameterName = GetParameterName(templateSegment); |
62 |
| - var property = GetParameterProperty(instance, parameterName); |
63 |
| - |
64 |
| - if (property is null) |
65 |
| - { |
66 |
| - continue; |
67 |
| - } |
68 |
| - |
69 |
| - var isCatchAllParameter = templateSegment[1] == '*'; |
70 |
| - if (!isCatchAllParameter) |
71 |
| - { |
72 |
| - parameters[property.Name] = GetValue(uriSegments[i], property); |
73 |
| - } |
74 |
| - else |
75 |
| - { |
76 |
| - parameters[parameterName] = string.Join("/", uriSegments.Skip(i)); |
77 |
| - } |
78 |
| - } |
79 |
| - else if (templateSegment != uriSegments[i]) |
80 |
| - { |
81 |
| - break; |
82 |
| - } |
83 |
| - } |
| 57 | + private static RouteAttribute[] GetRouteAttributesFromComponent(IComponent instance) => |
| 58 | + instance.GetType() |
| 59 | + .GetCustomAttributes(typeof(RouteAttribute), true) |
| 60 | + .Cast<RouteAttribute>() |
| 61 | + .ToArray(); |
84 | 62 |
|
85 |
| - if (parameters.Count == 0) |
| 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) |
86 | 84 | {
|
87 | 85 | continue;
|
88 | 86 | }
|
89 | 87 |
|
90 |
| - // Shall we await this? This should be synchronous in most cases |
91 |
| - // If not, very likely the user has overriden the SetParametersAsync method |
92 |
| - // And should use WaitForXXX methods to await the desired state |
93 |
| - instance.SetParametersAsync(ParameterView.FromDictionary(parameters)); |
| 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 []; |
94 | 96 | }
|
95 | 97 | }
|
96 |
| - } |
97 | 98 |
|
98 |
| - private static RouteAttribute[] GetRouteAttributesFromComponent(IComponent instance) |
99 |
| - { |
100 |
| - var routeAttributes = instance |
101 |
| - .GetType() |
102 |
| - .GetCustomAttributes(typeof(RouteAttribute), true) |
103 |
| - .Cast<RouteAttribute>() |
104 |
| - .ToArray(); |
105 |
| - return routeAttributes; |
| 99 | + return parameters; |
106 | 100 | }
|
107 | 101 |
|
108 | 102 | private static string GetParameterName(string templateSegment) =>
|
|
0 commit comments