var route = "dogs";
Routing.RegisterRoute(route, typeof(ShellItem));
- Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("dogs", typeof(ShellItem)));
+ Assert.Catch(typeof(ArgumentException), () => Routing.RegisterRoute("dogs", typeof(ContentPage)));
+ }
+
+ [Test]
+ public async Task SucceedWhenAddingDuplicateRouteOfSameType()
+ {
+ var route = "dogs";
+ Routing.RegisterRoute(route, typeof(ShellItem));
+ Routing.RegisterRoute(route, typeof(ShellItem));
}
[Test]
{
return IsImplicit(GetRoute(source));
}
-
- internal static bool CompareWithRegisteredRoutes(string compare) => s_routes.ContainsKey(compare);
-
+
internal static void Clear()
{
s_routes.Clear();
{
if (!String.IsNullOrWhiteSpace(route))
route = FormatRoute(route);
- ValidateRoute(route);
+ ValidateRoute(route, factory);
s_routes[route] = factory;
}
public static void RegisterRoute(string route, Type type)
{
- if(!String.IsNullOrWhiteSpace(route))
- route = FormatRoute(route);
-
- ValidateRoute(route);
-
- s_routes[route] = new TypeRouteFactory(type);
+ RegisterRoute(route, new TypeRouteFactory(type));
}
public static void SetRoute(Element obj, string value)
obj.SetValue(RouteProperty, value);
}
- static void ValidateRoute(string route)
+ static void ValidateRoute(string route, RouteFactory routeFactory)
{
if (string.IsNullOrWhiteSpace(route))
throw new ArgumentNullException(nameof(route), "Route cannot be an empty string");
+ routeFactory = routeFactory ?? throw new ArgumentNullException(nameof(routeFactory), "Route Factory cannot be null");
+
var uri = new Uri(route, UriKind.RelativeOrAbsolute);
var parts = uri.OriginalString.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
throw new ArgumentException($"Route contains invalid characters in \"{part}\"");
}
- if (CompareWithRegisteredRoutes(route))
+ RouteFactory existingRegistration = null;
+ if(s_routes.TryGetValue(route, out existingRegistration) && !existingRegistration.Equals(routeFactory))
throw new ArgumentException($"Duplicated Route: \"{route}\"");
}
{
return (Element)Activator.CreateInstance(_type);
}
+ public override bool Equals(object obj)
+ {
+ if ((obj is TypeRouteFactory typeRouteFactory))
+ return typeRouteFactory._type == _type;
+
+ return false;
+ }
+
+ public override int GetHashCode()
+ {
+ return _type.GetHashCode();
+ }
}
}
}
\ No newline at end of file