Skip to content

Commit 640d486

Browse files
committed
refactor: Extract methods
1 parent b2137f1 commit 640d486

File tree

1 file changed

+46
-52
lines changed

1 file changed

+46
-52
lines changed

src/bunit.web/TestDoubles/NavigationManager/ComponentRouteParameterService.cs

+46-52
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
using System.Globalization;
22
using System.Reflection;
33
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
49

510
namespace Bunit.TestDoubles;
611

@@ -40,69 +45,58 @@ public void UpdateComponentsWithRouteParameters(Uri uri)
4045

4146
foreach (var template in routeAttributes.Select(r => r.Template))
4247
{
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)
4750
{
48-
continue;
51+
instance.SetParametersAsync(ParameterView.FromDictionary(parameters));
4952
}
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+
}
5556

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();
8462

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)
8684
{
8785
continue;
8886
}
8987

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 [];
9496
}
9597
}
96-
}
9798

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;
106100
}
107101

108102
private static string GetParameterName(string templateSegment) =>

0 commit comments

Comments
 (0)