|
|
|
@ -3,17 +3,11 @@ using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
using DryIoc;
|
|
|
|
|
using Moq;
|
|
|
|
|
using Moq.Language.Flow;
|
|
|
|
|
using NzbDrone.Common.Composition;
|
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
|
|
|
|
using NzbDrone.Test.Common.AutoMoq.Unity;
|
|
|
|
|
using Unity;
|
|
|
|
|
|
|
|
|
|
[assembly: InternalsVisibleTo("AutoMoq.Tests")]
|
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Test.Common.AutoMoq
|
|
|
|
|
{
|
|
|
|
@ -21,32 +15,18 @@ namespace NzbDrone.Test.Common.AutoMoq
|
|
|
|
|
public class AutoMoqer
|
|
|
|
|
{
|
|
|
|
|
public readonly MockBehavior DefaultBehavior = MockBehavior.Default;
|
|
|
|
|
public Type ResolveType;
|
|
|
|
|
private IUnityContainer _container;
|
|
|
|
|
private IContainer _container;
|
|
|
|
|
private IDictionary<Type, object> _registeredMocks;
|
|
|
|
|
|
|
|
|
|
public AutoMoqer()
|
|
|
|
|
{
|
|
|
|
|
SetupAutoMoqer(new UnityContainer());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AutoMoqer(MockBehavior defaultBehavior)
|
|
|
|
|
{
|
|
|
|
|
DefaultBehavior = defaultBehavior;
|
|
|
|
|
SetupAutoMoqer(new UnityContainer());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AutoMoqer(IUnityContainer container)
|
|
|
|
|
{
|
|
|
|
|
SetupAutoMoqer(container);
|
|
|
|
|
SetupAutoMoqer(CreateTestContainer(new Container()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual T Resolve<T>()
|
|
|
|
|
{
|
|
|
|
|
ResolveType = typeof(T);
|
|
|
|
|
var result = _container.Resolve<T>();
|
|
|
|
|
SetConstant(result);
|
|
|
|
|
ResolveType = null;
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -59,7 +39,6 @@ namespace NzbDrone.Test.Common.AutoMoq
|
|
|
|
|
public virtual Mock<T> GetMock<T>(MockBehavior behavior)
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
ResolveType = null;
|
|
|
|
|
var type = GetTheMockType<T>();
|
|
|
|
|
if (GetMockHasNotBeenCalledForThisType(type))
|
|
|
|
|
{
|
|
|
|
@ -78,87 +57,81 @@ namespace NzbDrone.Test.Common.AutoMoq
|
|
|
|
|
|
|
|
|
|
public virtual void SetMock(Type type, Mock mock)
|
|
|
|
|
{
|
|
|
|
|
if (_registeredMocks.ContainsKey(type) == false)
|
|
|
|
|
if (GetMockHasNotBeenCalledForThisType(type))
|
|
|
|
|
{
|
|
|
|
|
_registeredMocks.Add(type, mock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mock != null)
|
|
|
|
|
{
|
|
|
|
|
_container.RegisterInstance(type, mock.Object);
|
|
|
|
|
_container.RegisterInstance(type, mock.Object, ifAlreadyRegistered: IfAlreadyRegistered.Replace);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void SetConstant<T>(T instance)
|
|
|
|
|
{
|
|
|
|
|
_container.RegisterInstance(instance);
|
|
|
|
|
_container.RegisterInstance(instance, ifAlreadyRegistered: IfAlreadyRegistered.Replace);
|
|
|
|
|
SetMock(instance.GetType(), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ISetup<T> Setup<T>(Expression<Action<T>> expression)
|
|
|
|
|
where T : class
|
|
|
|
|
{
|
|
|
|
|
return GetMock<T>().Setup(expression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ISetup<T, TResult> Setup<T, TResult>(Expression<Func<T, TResult>> expression)
|
|
|
|
|
where T : class
|
|
|
|
|
private IContainer CreateTestContainer(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
return GetMock<T>().Setup(expression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Verify<T>(Expression<Action<T>> expression)
|
|
|
|
|
where T : class
|
|
|
|
|
var c = container.CreateChild(IfAlreadyRegistered.Replace,
|
|
|
|
|
container.Rules
|
|
|
|
|
.WithDynamicRegistration((serviceType, serviceKey) =>
|
|
|
|
|
{
|
|
|
|
|
GetMock<T>().Verify(expression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Verify<T>(Expression<Action<T>> expression, string failMessage)
|
|
|
|
|
where T : class
|
|
|
|
|
// ignore services with non-default key
|
|
|
|
|
if (serviceKey != null)
|
|
|
|
|
{
|
|
|
|
|
GetMock<T>().Verify(expression, failMessage);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Verify<T>(Expression<Action<T>> expression, Times times)
|
|
|
|
|
where T : class
|
|
|
|
|
if (serviceType == typeof(object))
|
|
|
|
|
{
|
|
|
|
|
GetMock<T>().Verify(expression, times);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Verify<T>(Expression<Action<T>> expression, Times times, string failMessage)
|
|
|
|
|
where T : class
|
|
|
|
|
if (serviceType.IsGenericType && serviceType.IsOpenGeneric())
|
|
|
|
|
{
|
|
|
|
|
GetMock<T>().Verify(expression, times, failMessage);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void VerifyAllMocks()
|
|
|
|
|
// get the Mock object for the abstract class or interface
|
|
|
|
|
if (serviceType.IsInterface || serviceType.IsAbstract)
|
|
|
|
|
{
|
|
|
|
|
foreach (var registeredMock in _registeredMocks)
|
|
|
|
|
{
|
|
|
|
|
var mock = registeredMock.Value as Mock;
|
|
|
|
|
if (mock != null)
|
|
|
|
|
var mockType = typeof(Mock<>).MakeGenericType(serviceType);
|
|
|
|
|
var mockFactory = new DelegateFactory(r =>
|
|
|
|
|
{
|
|
|
|
|
mock.VerifyAll();
|
|
|
|
|
}
|
|
|
|
|
var mock = (Mock)r.Resolve(mockType);
|
|
|
|
|
SetMock(serviceType, mock);
|
|
|
|
|
return mock.Object;
|
|
|
|
|
}, Reuse.Singleton);
|
|
|
|
|
|
|
|
|
|
return new[] { new DynamicRegistration(mockFactory, IfAlreadyRegistered.Keep) };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// concrete types
|
|
|
|
|
var concreteTypeFactory = serviceType.ToFactory(Reuse.Singleton, FactoryMethod.ConstructorWithResolvableArgumentsIncludingNonPublic);
|
|
|
|
|
|
|
|
|
|
return new[] { new DynamicRegistration(concreteTypeFactory) };
|
|
|
|
|
},
|
|
|
|
|
DynamicRegistrationFlags.Service | DynamicRegistrationFlags.AsFallback));
|
|
|
|
|
|
|
|
|
|
c.Register(typeof(Mock<>), Reuse.Singleton, FactoryMethod.DefaultConstructor());
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SetupAutoMoqer(IUnityContainer container)
|
|
|
|
|
private void SetupAutoMoqer(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
_container = container;
|
|
|
|
|
container.RegisterInstance(this);
|
|
|
|
|
|
|
|
|
|
RegisterPlatformLibrary(container);
|
|
|
|
|
|
|
|
|
|
_registeredMocks = new Dictionary<Type, object>();
|
|
|
|
|
AddTheAutoMockingContainerExtensionToTheContainer(container);
|
|
|
|
|
AssemblyLoader.RegisterSQLiteResolver();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AddTheAutoMockingContainerExtensionToTheContainer(IUnityContainer container)
|
|
|
|
|
{
|
|
|
|
|
container.AddNewExtension<AutoMockingContainerExtension>();
|
|
|
|
|
return;
|
|
|
|
|
LoadPlatformLibrary();
|
|
|
|
|
|
|
|
|
|
AssemblyLoader.RegisterSQLiteResolver();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Mock<T> TheRegisteredMockForThisType<T>(Type type)
|
|
|
|
@ -177,7 +150,7 @@ namespace NzbDrone.Test.Common.AutoMoq
|
|
|
|
|
|
|
|
|
|
private bool GetMockHasNotBeenCalledForThisType(Type type)
|
|
|
|
|
{
|
|
|
|
|
return _registeredMocks.ContainsKey(type) == false;
|
|
|
|
|
return !_registeredMocks.ContainsKey(type);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Type GetTheMockType<T>()
|
|
|
|
@ -186,7 +159,7 @@ namespace NzbDrone.Test.Common.AutoMoq
|
|
|
|
|
return typeof(T);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void RegisterPlatformLibrary(IUnityContainer container)
|
|
|
|
|
private void LoadPlatformLibrary()
|
|
|
|
|
{
|
|
|
|
|
var assemblyName = "Prowlarr.Windows";
|
|
|
|
|
|
|
|
|
|