Skip to content

Commit

Permalink
refactor: Extract methods
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Nov 3, 2024
1 parent b2137f1 commit 640d486
Showing 1 changed file with 46 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System.Globalization;
using System.Reflection;
using Bunit.Rendering;
#if NET6_0_OR_GREATER
using ParameterViewDictionary = System.Collections.Generic.Dictionary<string, object?>;
#else
using ParameterViewDictionary = System.Collections.Generic.Dictionary<string, object>;
#endif

namespace Bunit.TestDoubles;

Expand Down Expand Up @@ -40,69 +45,58 @@ public void UpdateComponentsWithRouteParameters(Uri uri)

foreach (var template in routeAttributes.Select(r => r.Template))
{
var templateSegments = template.Trim('/').Split("/");
var uriSegments = relativeUri.Trim('/').Split("/");

if (templateSegments.Length > uriSegments.Length)
var parameters = GetParametersFromTemplateAndUri(template, relativeUri, instance);
if (parameters.Count > 0)
{
continue;
instance.SetParametersAsync(ParameterView.FromDictionary(parameters));
}
#if NET6_0_OR_GREATER
var parameters = new Dictionary<string, object?>();
#else
var parameters = new Dictionary<string, object>();
#endif
}
}
}

for (var i = 0; i < templateSegments.Length; i++)
{
var templateSegment = templateSegments[i];
if (templateSegment.StartsWith('{') && templateSegment.EndsWith('}'))
{
var parameterName = GetParameterName(templateSegment);
var property = GetParameterProperty(instance, parameterName);

if (property is null)
{
continue;
}

var isCatchAllParameter = templateSegment[1] == '*';
if (!isCatchAllParameter)
{
parameters[property.Name] = GetValue(uriSegments[i], property);
}
else
{
parameters[parameterName] = string.Join("/", uriSegments.Skip(i));
}
}
else if (templateSegment != uriSegments[i])
{
break;
}
}
private static RouteAttribute[] GetRouteAttributesFromComponent(IComponent instance) =>
instance.GetType()
.GetCustomAttributes(typeof(RouteAttribute), true)
.Cast<RouteAttribute>()
.ToArray();

if (parameters.Count == 0)
private static ParameterViewDictionary GetParametersFromTemplateAndUri(string template, string relativeUri, IComponent instance)
{
var templateSegments = template.Trim('/').Split("/");
var uriSegments = relativeUri.Trim('/').Split("/");

if (templateSegments.Length > uriSegments.Length)
{
return [];
}

var parameters = new ParameterViewDictionary();

for (var i = 0; i < templateSegments.Length; i++)
{
var templateSegment = templateSegments[i];
if (templateSegment.StartsWith('{') && templateSegment.EndsWith('}'))
{
var parameterName = GetParameterName(templateSegment);
var property = GetParameterProperty(instance, parameterName);

if (property is null)
{
continue;
}

// Shall we await this? This should be synchronous in most cases
// If not, very likely the user has overriden the SetParametersAsync method
// And should use WaitForXXX methods to await the desired state
instance.SetParametersAsync(ParameterView.FromDictionary(parameters));
var isCatchAllParameter = templateSegment[1] == '*';
parameters[property.Name] = isCatchAllParameter
? string.Join("/", uriSegments.Skip(i))
: GetValue(uriSegments[i], property);
}
else if (templateSegment != uriSegments[i])
{
return [];
}
}
}

private static RouteAttribute[] GetRouteAttributesFromComponent(IComponent instance)
{
var routeAttributes = instance
.GetType()
.GetCustomAttributes(typeof(RouteAttribute), true)
.Cast<RouteAttribute>()
.ToArray();
return routeAttributes;
return parameters;
}

private static string GetParameterName(string templateSegment) =>
Expand Down

0 comments on commit 640d486

Please sign in to comment.