|
4 | 4 | // See the LICENSE file in the project root for full license information.
|
5 | 5 |
|
6 | 6 | using System;
|
7 |
| - |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
8 | 9 | using ReactiveUI;
|
9 | 10 | using ReactiveUI.Tests;
|
10 | 11 | using Splat;
|
@@ -185,9 +186,87 @@ public void SingleInstanceViewsWithContractShouldResolveCorrectly()
|
185 | 186 | }
|
186 | 187 | }
|
187 | 188 |
|
| 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 | + |
188 | 207 | public void Dispose()
|
189 | 208 | {
|
190 | 209 | _resolver?.Dispose();
|
191 | 210 | }
|
| 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 | + } |
192 | 271 | }
|
193 | 272 | }
|
0 commit comments