Skip to content

Commit be74c1d

Browse files
cabaumanglennawatson
authored andcommitted
Housekeeping: add resolver tests (#1901)
* Fix: WrappingFullLogger constructor update * Add dependency resolver test
1 parent eb53eee commit be74c1d

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

src/ReactiveUI.Tests/DependencyResolverTests.cs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
// See the LICENSE file in the project root for full license information.
55

66
using System;
7-
7+
using System.Collections.Generic;
8+
using System.Linq;
89
using ReactiveUI;
910
using ReactiveUI.Tests;
1011
using Splat;
@@ -185,9 +186,87 @@ public void SingleInstanceViewsWithContractShouldResolveCorrectly()
185186
}
186187
}
187188

189+
[Fact]
190+
public void AllDefaultServicesShouldBeRegistered()
191+
{
192+
using (_resolver.WithResolver())
193+
{
194+
foreach (var shouldRegistered in GetServicesThatShouldBeRegistered())
195+
{
196+
IEnumerable<object> resolvedServices = _resolver.GetServices(shouldRegistered.Key);
197+
Assert.Equal(shouldRegistered.Value.Count, resolvedServices.Count());
198+
foreach (Type implementationType in shouldRegistered.Value)
199+
{
200+
var isRegistered = resolvedServices.Any(rs => rs.GetType() == implementationType);
201+
Assert.Equal(true, isRegistered);
202+
}
203+
}
204+
}
205+
}
206+
188207
public void Dispose()
189208
{
190209
_resolver?.Dispose();
191210
}
211+
212+
private static Dictionary<Type, List<Type>> GetServicesThatShouldBeRegistered()
213+
{
214+
Dictionary<Type, List<Type>> serviceTypeToImplementationTypes = new Dictionary<Type, List<Type>>();
215+
216+
new Registrations().Register((factory, serviceType) =>
217+
{
218+
if (serviceTypeToImplementationTypes.TryGetValue(serviceType, out List<Type> implementationTypes) == false)
219+
{
220+
implementationTypes = new List<Type>();
221+
serviceTypeToImplementationTypes.Add(serviceType, implementationTypes);
222+
}
223+
224+
implementationTypes.Add(factory().GetType());
225+
});
226+
227+
new PlatformRegistrations().Register((factory, serviceType) =>
228+
{
229+
if (serviceTypeToImplementationTypes.TryGetValue(serviceType, out List<Type> implementationTypes) == false)
230+
{
231+
implementationTypes = new List<Type>();
232+
serviceTypeToImplementationTypes.Add(serviceType, implementationTypes);
233+
}
234+
235+
implementationTypes.Add(factory().GetType());
236+
});
237+
238+
var typeNames = new[]
239+
{
240+
"ReactiveUI.XamForms.Registrations, ReactiveUI.XamForms",
241+
"ReactiveUI.Winforms.Registrations, ReactiveUI.Winforms",
242+
"ReactiveUI.Wpf.Registrations, ReactiveUI.Wpf"
243+
};
244+
245+
typeNames.ForEach(typeName => GetRegistrationsForPlatform(typeName, serviceTypeToImplementationTypes));
246+
247+
return serviceTypeToImplementationTypes;
248+
}
249+
250+
private static void GetRegistrationsForPlatform(string typeName, Dictionary<Type, List<Type>> serviceTypeToImplementationTypes)
251+
{
252+
Type platformRegistrationsType = Type.GetType(typeName);
253+
if (platformRegistrationsType != null)
254+
{
255+
var platformRegistrations = Activator.CreateInstance(platformRegistrationsType);
256+
System.Reflection.MethodInfo register = platformRegistrationsType.GetMethod("Register");
257+
var registerParameter = new Action<Func<object>, Type>((factory, serviceType) =>
258+
{
259+
if (serviceTypeToImplementationTypes.TryGetValue(serviceType, out List<Type> implementationTypes) == false)
260+
{
261+
implementationTypes = new List<Type>();
262+
serviceTypeToImplementationTypes.Add(serviceType, implementationTypes);
263+
}
264+
265+
implementationTypes.Add(factory().GetType());
266+
});
267+
268+
register.Invoke(platformRegistrations, new object[] { registerParameter });
269+
}
270+
}
192271
}
193272
}

0 commit comments

Comments
 (0)